2 * sftp.c: SFTP generic client code.
18 unsigned length, maxlen;
23 static const char *fxp_error_message;
24 static int fxp_errtype;
26 static void fxp_internal_error(char *msg);
28 /* ----------------------------------------------------------------------
29 * SFTP packet construction functions.
31 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
33 if ((int)pkt->maxlen < length) {
34 pkt->maxlen = length + 256;
35 pkt->data = sresize(pkt->data, pkt->maxlen, char);
38 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
41 sftp_pkt_ensure(pkt, pkt->length);
42 memcpy(pkt->data + pkt->length - len, data, len);
44 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
46 sftp_pkt_adddata(pkt, &byte, 1);
48 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
53 sftp_pkt_adddata(pkt, x, 4);
55 static struct sftp_packet *sftp_pkt_init(int pkt_type)
57 struct sftp_packet *pkt;
58 pkt = snew(struct sftp_packet);
63 sftp_pkt_adduint32(pkt, 0); /* length field will be filled in later */
64 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
68 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
70 sftp_pkt_adddata(pkt, &value, 1);
73 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
76 PUT_32BIT(x, value.hi);
77 PUT_32BIT(x + 4, value.lo);
78 sftp_pkt_adddata(pkt, x, 8);
80 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
82 sftp_pkt_adduint32(pkt, 0);
83 pkt->savedpos = pkt->length;
85 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
87 sftp_pkt_adddata(pkt, data, strlen(data));
88 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
90 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
93 sftp_pkt_adddata(pkt, data, len);
94 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
96 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
98 sftp_pkt_addstring_start(pkt);
99 sftp_pkt_addstring_str(pkt, data);
101 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
103 sftp_pkt_adduint32(pkt, attrs.flags);
104 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
105 sftp_pkt_adduint32(pkt, attrs.size.hi);
106 sftp_pkt_adduint32(pkt, attrs.size.lo);
108 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
109 sftp_pkt_adduint32(pkt, attrs.uid);
110 sftp_pkt_adduint32(pkt, attrs.gid);
112 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
113 sftp_pkt_adduint32(pkt, attrs.permissions);
115 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
116 sftp_pkt_adduint32(pkt, attrs.atime);
117 sftp_pkt_adduint32(pkt, attrs.mtime);
119 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
121 * We currently don't support sending any extended
127 /* ----------------------------------------------------------------------
128 * SFTP packet decode functions.
131 static int sftp_pkt_getbyte(struct sftp_packet *pkt, unsigned char *ret)
133 if (pkt->length - pkt->savedpos < 1)
135 *ret = (unsigned char) pkt->data[pkt->savedpos];
139 static int sftp_pkt_getuint32(struct sftp_packet *pkt, unsigned long *ret)
141 if (pkt->length - pkt->savedpos < 4)
143 *ret = GET_32BIT(pkt->data + pkt->savedpos);
147 static int sftp_pkt_getstring(struct sftp_packet *pkt,
148 char **p, int *length)
151 if (pkt->length - pkt->savedpos < 4)
153 *length = toint(GET_32BIT(pkt->data + pkt->savedpos));
155 if ((int)(pkt->length - pkt->savedpos) < *length || *length < 0) {
159 *p = pkt->data + pkt->savedpos;
160 pkt->savedpos += *length;
163 static int sftp_pkt_getattrs(struct sftp_packet *pkt, struct fxp_attrs *ret)
165 if (!sftp_pkt_getuint32(pkt, &ret->flags))
167 if (ret->flags & SSH_FILEXFER_ATTR_SIZE) {
168 unsigned long hi, lo;
169 if (!sftp_pkt_getuint32(pkt, &hi) ||
170 !sftp_pkt_getuint32(pkt, &lo))
172 ret->size = uint64_make(hi, lo);
174 if (ret->flags & SSH_FILEXFER_ATTR_UIDGID) {
175 if (!sftp_pkt_getuint32(pkt, &ret->uid) ||
176 !sftp_pkt_getuint32(pkt, &ret->gid))
179 if (ret->flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
180 if (!sftp_pkt_getuint32(pkt, &ret->permissions))
183 if (ret->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
184 if (!sftp_pkt_getuint32(pkt, &ret->atime) ||
185 !sftp_pkt_getuint32(pkt, &ret->mtime))
188 if (ret->flags & SSH_FILEXFER_ATTR_EXTENDED) {
190 if (!sftp_pkt_getuint32(pkt, &count))
196 * We should try to analyse these, if we ever find one
199 if (!sftp_pkt_getstring(pkt, &str, &len) ||
200 !sftp_pkt_getstring(pkt, &str, &len))
206 static void sftp_pkt_free(struct sftp_packet *pkt)
213 /* ----------------------------------------------------------------------
214 * Send and receive packet functions.
216 int sftp_send(struct sftp_packet *pkt)
219 PUT_32BIT(pkt->data, pkt->length - 4);
220 ret = sftp_senddata(pkt->data, pkt->length);
224 struct sftp_packet *sftp_recv(void)
226 struct sftp_packet *pkt;
230 if (!sftp_recvdata(x, 4))
233 pkt = snew(struct sftp_packet);
235 pkt->length = pkt->maxlen = GET_32BIT(x);
236 pkt->data = snewn(pkt->length, char);
238 if (!sftp_recvdata(pkt->data, pkt->length)) {
243 if (!sftp_pkt_getbyte(pkt, &uc)) {
253 /* ----------------------------------------------------------------------
254 * Request ID allocation and temporary dispatch routines.
257 #define REQUEST_ID_OFFSET 256
259 struct sftp_request {
265 static int sftp_reqcmp(void *av, void *bv)
267 struct sftp_request *a = (struct sftp_request *)av;
268 struct sftp_request *b = (struct sftp_request *)bv;
275 static int sftp_reqfind(void *av, void *bv)
277 unsigned *a = (unsigned *) av;
278 struct sftp_request *b = (struct sftp_request *)bv;
286 static tree234 *sftp_requests;
288 static struct sftp_request *sftp_alloc_request(void)
290 unsigned low, high, mid;
292 struct sftp_request *r;
294 if (sftp_requests == NULL)
295 sftp_requests = newtree234(sftp_reqcmp);
298 * First-fit allocation of request IDs: always pick the lowest
299 * unused one. To do this, binary-search using the counted
300 * B-tree to find the largest ID which is in a contiguous
301 * sequence from the beginning. (Precisely everything in that
302 * sequence must have ID equal to its tree index plus
303 * REQUEST_ID_OFFSET.)
305 tsize = count234(sftp_requests);
309 while (high - low > 1) {
310 mid = (high + low) / 2;
311 r = index234(sftp_requests, mid);
312 if (r->id == mid + REQUEST_ID_OFFSET)
313 low = mid; /* this one is fine */
315 high = mid; /* this one is past it */
318 * Now low points to either -1, or the tree index of the
319 * largest ID in the initial sequence.
322 unsigned i = low + 1 + REQUEST_ID_OFFSET;
323 assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
327 * So the request ID we need to create is
328 * low + 1 + REQUEST_ID_OFFSET.
330 r = snew(struct sftp_request);
331 r->id = low + 1 + REQUEST_ID_OFFSET;
334 add234(sftp_requests, r);
338 void sftp_cleanup_request(void)
340 if (sftp_requests != NULL) {
341 freetree234(sftp_requests);
342 sftp_requests = NULL;
346 void sftp_register(struct sftp_request *req)
351 struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
354 struct sftp_request *req;
357 fxp_internal_error("did not receive a valid SFTP packet\n");
361 if (!sftp_pkt_getuint32(pktin, &id)) {
362 fxp_internal_error("did not receive a valid SFTP packet\n");
365 req = find234(sftp_requests, &id, sftp_reqfind);
367 if (!req || !req->registered) {
368 fxp_internal_error("request ID mismatch\n");
372 del234(sftp_requests, req);
377 /* ----------------------------------------------------------------------
378 * String handling routines.
381 static char *mkstr(char *s, int len)
383 char *p = snewn(len + 1, char);
389 /* ----------------------------------------------------------------------
394 * Deal with (and free) an FXP_STATUS packet. Return 1 if
395 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
396 * Also place the status into fxp_errtype.
398 static int fxp_got_status(struct sftp_packet *pktin)
400 static const char *const messages[] = {
401 /* SSH_FX_OK. The only time we will display a _message_ for this
402 * is if we were expecting something other than FXP_STATUS on
403 * success, so this is actually an error message! */
404 "unexpected OK response",
406 "no such file or directory",
412 "operation unsupported",
415 if (pktin->type != SSH_FXP_STATUS) {
416 fxp_error_message = "expected FXP_STATUS packet";
420 if (!sftp_pkt_getuint32(pktin, &ul)) {
421 fxp_error_message = "malformed FXP_STATUS packet";
425 if (fxp_errtype < 0 ||
426 fxp_errtype >= sizeof(messages) / sizeof(*messages))
427 fxp_error_message = "unknown error code";
429 fxp_error_message = messages[fxp_errtype];
433 if (fxp_errtype == SSH_FX_OK)
435 else if (fxp_errtype == SSH_FX_EOF)
441 static void fxp_internal_error(char *msg)
443 fxp_error_message = msg;
447 const char *fxp_error(void)
449 return fxp_error_message;
452 int fxp_error_type(void)
458 * Perform exchange of init/version packets. Return 0 on failure.
462 struct sftp_packet *pktout, *pktin;
463 unsigned long remotever;
465 pktout = sftp_pkt_init(SSH_FXP_INIT);
466 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
471 fxp_internal_error("could not connect");
474 if (pktin->type != SSH_FXP_VERSION) {
475 fxp_internal_error("did not receive FXP_VERSION");
476 sftp_pkt_free(pktin);
479 if (!sftp_pkt_getuint32(pktin, &remotever)) {
480 fxp_internal_error("malformed FXP_VERSION packet");
481 sftp_pkt_free(pktin);
484 if (remotever > SFTP_PROTO_VERSION) {
486 ("remote protocol is more advanced than we support");
487 sftp_pkt_free(pktin);
491 * In principle, this packet might also contain extension-
492 * string pairs. We should work through them and look for any
493 * we recognise. In practice we don't currently do so because
494 * we know we don't recognise _any_.
496 sftp_pkt_free(pktin);
502 * Canonify a pathname.
504 struct sftp_request *fxp_realpath_send(char *path)
506 struct sftp_request *req = sftp_alloc_request();
507 struct sftp_packet *pktout;
509 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
510 sftp_pkt_adduint32(pktout, req->id);
511 sftp_pkt_addstring_start(pktout);
512 sftp_pkt_addstring_str(pktout, path);
518 char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req)
522 if (pktin->type == SSH_FXP_NAME) {
527 if (!sftp_pkt_getuint32(pktin, &count) || count != 1) {
528 fxp_internal_error("REALPATH did not return name count of 1\n");
529 sftp_pkt_free(pktin);
532 if (!sftp_pkt_getstring(pktin, &path, &len)) {
533 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
534 sftp_pkt_free(pktin);
537 path = mkstr(path, len);
538 sftp_pkt_free(pktin);
541 fxp_got_status(pktin);
542 sftp_pkt_free(pktin);
550 struct sftp_request *fxp_open_send(char *path, int type,
551 struct fxp_attrs *attrs)
553 struct sftp_request *req = sftp_alloc_request();
554 struct sftp_packet *pktout;
556 pktout = sftp_pkt_init(SSH_FXP_OPEN);
557 sftp_pkt_adduint32(pktout, req->id);
558 sftp_pkt_addstring(pktout, path);
559 sftp_pkt_adduint32(pktout, type);
561 sftp_pkt_addattrs(pktout, *attrs);
563 sftp_pkt_adduint32(pktout, 0); /* empty ATTRS structure */
569 struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin,
570 struct sftp_request *req)
574 if (pktin->type == SSH_FXP_HANDLE) {
576 struct fxp_handle *handle;
579 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
580 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
581 sftp_pkt_free(pktin);
584 handle = snew(struct fxp_handle);
585 handle->hstring = mkstr(hstring, len);
587 sftp_pkt_free(pktin);
590 fxp_got_status(pktin);
591 sftp_pkt_free(pktin);
599 struct sftp_request *fxp_opendir_send(char *path)
601 struct sftp_request *req = sftp_alloc_request();
602 struct sftp_packet *pktout;
604 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
605 sftp_pkt_adduint32(pktout, req->id);
606 sftp_pkt_addstring(pktout, path);
612 struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin,
613 struct sftp_request *req)
616 if (pktin->type == SSH_FXP_HANDLE) {
618 struct fxp_handle *handle;
621 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
622 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
623 sftp_pkt_free(pktin);
626 handle = snew(struct fxp_handle);
627 handle->hstring = mkstr(hstring, len);
629 sftp_pkt_free(pktin);
632 fxp_got_status(pktin);
633 sftp_pkt_free(pktin);
641 struct sftp_request *fxp_close_send(struct fxp_handle *handle)
643 struct sftp_request *req = sftp_alloc_request();
644 struct sftp_packet *pktout;
646 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
647 sftp_pkt_adduint32(pktout, req->id);
648 sftp_pkt_addstring_start(pktout);
649 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
652 sfree(handle->hstring);
658 void fxp_close_recv(struct sftp_packet *pktin, struct sftp_request *req)
661 fxp_got_status(pktin);
662 sftp_pkt_free(pktin);
665 struct sftp_request *fxp_mkdir_send(char *path)
667 struct sftp_request *req = sftp_alloc_request();
668 struct sftp_packet *pktout;
670 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
671 sftp_pkt_adduint32(pktout, req->id);
672 sftp_pkt_addstring(pktout, path);
673 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
679 int fxp_mkdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
683 id = fxp_got_status(pktin);
684 sftp_pkt_free(pktin);
691 struct sftp_request *fxp_rmdir_send(char *path)
693 struct sftp_request *req = sftp_alloc_request();
694 struct sftp_packet *pktout;
696 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
697 sftp_pkt_adduint32(pktout, req->id);
698 sftp_pkt_addstring(pktout, path);
704 int fxp_rmdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
708 id = fxp_got_status(pktin);
709 sftp_pkt_free(pktin);
716 struct sftp_request *fxp_remove_send(char *fname)
718 struct sftp_request *req = sftp_alloc_request();
719 struct sftp_packet *pktout;
721 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
722 sftp_pkt_adduint32(pktout, req->id);
723 sftp_pkt_addstring(pktout, fname);
729 int fxp_remove_recv(struct sftp_packet *pktin, struct sftp_request *req)
733 id = fxp_got_status(pktin);
734 sftp_pkt_free(pktin);
741 struct sftp_request *fxp_rename_send(char *srcfname, char *dstfname)
743 struct sftp_request *req = sftp_alloc_request();
744 struct sftp_packet *pktout;
746 pktout = sftp_pkt_init(SSH_FXP_RENAME);
747 sftp_pkt_adduint32(pktout, req->id);
748 sftp_pkt_addstring(pktout, srcfname);
749 sftp_pkt_addstring(pktout, dstfname);
755 int fxp_rename_recv(struct sftp_packet *pktin, struct sftp_request *req)
759 id = fxp_got_status(pktin);
760 sftp_pkt_free(pktin);
768 * Retrieve the attributes of a file. We have fxp_stat which works
769 * on filenames, and fxp_fstat which works on open file handles.
771 struct sftp_request *fxp_stat_send(char *fname)
773 struct sftp_request *req = sftp_alloc_request();
774 struct sftp_packet *pktout;
776 pktout = sftp_pkt_init(SSH_FXP_STAT);
777 sftp_pkt_adduint32(pktout, req->id);
778 sftp_pkt_addstring(pktout, fname);
784 int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req,
785 struct fxp_attrs *attrs)
788 if (pktin->type == SSH_FXP_ATTRS) {
789 if (!sftp_pkt_getattrs(pktin, attrs)) {
790 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
791 sftp_pkt_free(pktin);
794 sftp_pkt_free(pktin);
797 fxp_got_status(pktin);
798 sftp_pkt_free(pktin);
803 struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
805 struct sftp_request *req = sftp_alloc_request();
806 struct sftp_packet *pktout;
808 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
809 sftp_pkt_adduint32(pktout, req->id);
810 sftp_pkt_addstring_start(pktout);
811 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
817 int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req,
818 struct fxp_attrs *attrs)
821 if (pktin->type == SSH_FXP_ATTRS) {
822 if (!sftp_pkt_getattrs(pktin, attrs)) {
823 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
824 sftp_pkt_free(pktin);
827 sftp_pkt_free(pktin);
830 fxp_got_status(pktin);
831 sftp_pkt_free(pktin);
837 * Set the attributes of a file.
839 struct sftp_request *fxp_setstat_send(char *fname, struct fxp_attrs attrs)
841 struct sftp_request *req = sftp_alloc_request();
842 struct sftp_packet *pktout;
844 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
845 sftp_pkt_adduint32(pktout, req->id);
846 sftp_pkt_addstring(pktout, fname);
847 sftp_pkt_addattrs(pktout, attrs);
853 int fxp_setstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
857 id = fxp_got_status(pktin);
858 sftp_pkt_free(pktin);
865 struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
866 struct fxp_attrs attrs)
868 struct sftp_request *req = sftp_alloc_request();
869 struct sftp_packet *pktout;
871 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
872 sftp_pkt_adduint32(pktout, req->id);
873 sftp_pkt_addstring_start(pktout);
874 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
875 sftp_pkt_addattrs(pktout, attrs);
881 int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
885 id = fxp_got_status(pktin);
886 sftp_pkt_free(pktin);
894 * Read from a file. Returns the number of bytes read, or -1 on an
895 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
896 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
897 * error indicator. It might even depend on the SFTP server.)
899 struct sftp_request *fxp_read_send(struct fxp_handle *handle,
900 uint64 offset, int len)
902 struct sftp_request *req = sftp_alloc_request();
903 struct sftp_packet *pktout;
905 pktout = sftp_pkt_init(SSH_FXP_READ);
906 sftp_pkt_adduint32(pktout, req->id);
907 sftp_pkt_addstring_start(pktout);
908 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
909 sftp_pkt_adduint64(pktout, offset);
910 sftp_pkt_adduint32(pktout, len);
916 int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
917 char *buffer, int len)
920 if (pktin->type == SSH_FXP_DATA) {
924 if (!sftp_pkt_getstring(pktin, &str, &rlen)) {
925 fxp_internal_error("READ returned malformed SSH_FXP_DATA packet");
926 sftp_pkt_free(pktin);
930 if (rlen > len || rlen < 0) {
931 fxp_internal_error("READ returned more bytes than requested");
932 sftp_pkt_free(pktin);
936 memcpy(buffer, str, rlen);
937 sftp_pkt_free(pktin);
940 fxp_got_status(pktin);
941 sftp_pkt_free(pktin);
947 * Read from a directory.
949 struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
951 struct sftp_request *req = sftp_alloc_request();
952 struct sftp_packet *pktout;
954 pktout = sftp_pkt_init(SSH_FXP_READDIR);
955 sftp_pkt_adduint32(pktout, req->id);
956 sftp_pkt_addstring_start(pktout);
957 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
963 struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
964 struct sftp_request *req)
967 if (pktin->type == SSH_FXP_NAME) {
968 struct fxp_names *ret;
972 * Sanity-check the number of names. Minimum is obviously
973 * zero. Maximum is the remaining space in the packet
974 * divided by the very minimum length of a name, which is
975 * 12 bytes (4 for an empty filename, 4 for an empty
976 * longname, 4 for a set of attribute flags indicating that
977 * no other attributes are supplied).
979 if (!sftp_pkt_getuint32(pktin, &i) ||
980 i > (pktin->length-pktin->savedpos)/12) {
981 fxp_internal_error("malformed FXP_NAME packet");
982 sftp_pkt_free(pktin);
987 * Ensure the implicit multiplication in the snewn() call
988 * doesn't suffer integer overflow and cause us to malloc
991 if (i > INT_MAX / sizeof(struct fxp_name)) {
992 fxp_internal_error("unreasonably large FXP_NAME packet");
993 sftp_pkt_free(pktin);
997 ret = snew(struct fxp_names);
999 ret->names = snewn(ret->nnames, struct fxp_name);
1000 for (i = 0; i < (unsigned long)ret->nnames; i++) {
1003 if (!sftp_pkt_getstring(pktin, &str1, &len1) ||
1004 !sftp_pkt_getstring(pktin, &str2, &len2) ||
1005 !sftp_pkt_getattrs(pktin, &ret->names[i].attrs)) {
1006 fxp_internal_error("malformed FXP_NAME packet");
1008 sfree(ret->names[i].filename);
1009 sfree(ret->names[i].longname);
1016 ret->names[i].filename = mkstr(str1, len1);
1017 ret->names[i].longname = mkstr(str2, len2);
1019 sftp_pkt_free(pktin);
1022 fxp_got_status(pktin);
1023 sftp_pkt_free(pktin);
1029 * Write to a file. Returns 0 on error, 1 on OK.
1031 struct sftp_request *fxp_write_send(struct fxp_handle *handle,
1032 char *buffer, uint64 offset, int len)
1034 struct sftp_request *req = sftp_alloc_request();
1035 struct sftp_packet *pktout;
1037 pktout = sftp_pkt_init(SSH_FXP_WRITE);
1038 sftp_pkt_adduint32(pktout, req->id);
1039 sftp_pkt_addstring_start(pktout);
1040 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
1041 sftp_pkt_adduint64(pktout, offset);
1042 sftp_pkt_addstring_start(pktout);
1043 sftp_pkt_addstring_data(pktout, buffer, len);
1049 int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req)
1052 fxp_got_status(pktin);
1053 sftp_pkt_free(pktin);
1054 return fxp_errtype == SSH_FX_OK;
1058 * Free up an fxp_names structure.
1060 void fxp_free_names(struct fxp_names *names)
1064 for (i = 0; i < names->nnames; i++) {
1065 sfree(names->names[i].filename);
1066 sfree(names->names[i].longname);
1068 sfree(names->names);
1073 * Duplicate an fxp_name structure.
1075 struct fxp_name *fxp_dup_name(struct fxp_name *name)
1077 struct fxp_name *ret;
1078 ret = snew(struct fxp_name);
1079 ret->filename = dupstr(name->filename);
1080 ret->longname = dupstr(name->longname);
1081 ret->attrs = name->attrs; /* structure copy */
1086 * Free up an fxp_name structure.
1088 void fxp_free_name(struct fxp_name *name)
1090 sfree(name->filename);
1091 sfree(name->longname);
1096 * Store user data in an sftp_request structure.
1098 void *fxp_get_userdata(struct sftp_request *req)
1100 return req->userdata;
1103 void fxp_set_userdata(struct sftp_request *req, void *data)
1105 req->userdata = data;
1109 * A wrapper to go round fxp_read_* and fxp_write_*, which manages
1110 * the queueing of multiple read/write requests.
1115 int len, retlen, complete;
1117 struct req *next, *prev;
1121 uint64 offset, furthestdata, filesize;
1122 int req_totalsize, req_maxsize, eof, err;
1123 struct fxp_handle *fh;
1124 struct req *head, *tail;
1127 static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
1129 struct fxp_xfer *xfer = snew(struct fxp_xfer);
1132 xfer->offset = offset;
1133 xfer->head = xfer->tail = NULL;
1134 xfer->req_totalsize = 0;
1135 xfer->req_maxsize = 1048576;
1137 xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX);
1138 xfer->furthestdata = uint64_make(0, 0);
1143 int xfer_done(struct fxp_xfer *xfer)
1146 * We're finished if we've seen EOF _and_ there are no
1147 * outstanding requests.
1149 return (xfer->eof || xfer->err) && !xfer->head;
1152 void xfer_download_queue(struct fxp_xfer *xfer)
1154 while (xfer->req_totalsize < xfer->req_maxsize &&
1155 !xfer->eof && !xfer->err) {
1157 * Queue a new read request.
1160 struct sftp_request *req;
1162 rr = snew(struct req);
1163 rr->offset = xfer->offset;
1166 xfer->tail->next = rr;
1167 rr->prev = xfer->tail;
1176 rr->buffer = snewn(rr->len, char);
1177 sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
1178 fxp_set_userdata(req, rr);
1180 xfer->offset = uint64_add32(xfer->offset, rr->len);
1181 xfer->req_totalsize += rr->len;
1183 #ifdef DEBUG_DOWNLOAD
1184 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); }
1189 struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset)
1191 struct fxp_xfer *xfer = xfer_init(fh, offset);
1194 xfer_download_queue(xfer);
1200 * Returns INT_MIN to indicate that it didn't even get as far as
1201 * fxp_read_recv and hence has not freed pktin.
1203 int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1205 struct sftp_request *rreq;
1208 rreq = sftp_find_request(pktin);
1210 return INT_MIN; /* this packet doesn't even make sense */
1211 rr = (struct req *)fxp_get_userdata(rreq);
1213 fxp_internal_error("request ID is not part of the current download");
1214 return INT_MIN; /* this packet isn't ours */
1216 rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len);
1217 #ifdef DEBUG_DOWNLOAD
1218 printf("read request %p has returned [%d]\n", rr, rr->retlen);
1221 if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) {
1224 #ifdef DEBUG_DOWNLOAD
1225 printf("setting eof\n");
1227 } else if (rr->retlen < 0) {
1228 /* some error other than EOF; signal it back to caller */
1229 xfer_set_error(xfer);
1237 * Special case: if we have received fewer bytes than we
1238 * actually read, we should do something. For the moment I'll
1239 * just throw an ersatz FXP error to signal this; the SFTP
1240 * draft I've got says that it can't happen except on special
1241 * files, in which case seeking probably has very little
1242 * meaning and so queueing an additional read request to fill
1243 * up the gap sounds like the wrong answer. I'm not sure what I
1244 * should be doing here - if it _was_ a special file, I suspect
1245 * I simply shouldn't have been queueing multiple requests in
1246 * the first place...
1248 if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) {
1249 xfer->furthestdata = rr->offset;
1250 #ifdef DEBUG_DOWNLOAD
1252 uint64_decimal(xfer->furthestdata, buf);
1253 printf("setting furthestdata = %s\n", buf); }
1257 if (rr->retlen < rr->len) {
1258 uint64 filesize = uint64_add32(rr->offset,
1259 (rr->retlen < 0 ? 0 : rr->retlen));
1260 #ifdef DEBUG_DOWNLOAD
1262 uint64_decimal(filesize, buf);
1263 printf("short block! trying filesize = %s\n", buf); }
1265 if (uint64_compare(xfer->filesize, filesize) > 0) {
1266 xfer->filesize = filesize;
1267 #ifdef DEBUG_DOWNLOAD
1268 printf("actually changing filesize\n");
1273 if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) {
1274 fxp_error_message = "received a short buffer from FXP_READ, but not"
1277 xfer_set_error(xfer);
1284 void xfer_set_error(struct fxp_xfer *xfer)
1289 int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
1291 void *retbuf = NULL;
1295 * Discard anything at the head of the rr queue with complete <
1296 * 0; return the first thing with complete > 0.
1298 while (xfer->head && xfer->head->complete && !retbuf) {
1299 struct req *rr = xfer->head;
1301 if (rr->complete > 0) {
1302 retbuf = rr->buffer;
1303 retlen = rr->retlen;
1304 #ifdef DEBUG_DOWNLOAD
1305 printf("handing back data from read request %p\n", rr);
1308 #ifdef DEBUG_DOWNLOAD
1310 printf("skipping failed read request %p\n", rr);
1313 xfer->head = xfer->head->next;
1315 xfer->head->prev = NULL;
1318 xfer->req_totalsize -= rr->len;
1330 struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset)
1332 struct fxp_xfer *xfer = xfer_init(fh, offset);
1335 * We set `eof' to 1 because this will cause xfer_done() to
1336 * return true iff there are no outstanding requests. During an
1337 * upload, our caller will be responsible for working out
1338 * whether all the data has been sent, so all it needs to know
1339 * from us is whether the outstanding requests have been
1340 * handled once that's done.
1347 int xfer_upload_ready(struct fxp_xfer *xfer)
1349 if (xfer->req_totalsize < xfer->req_maxsize)
1355 void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len)
1358 struct sftp_request *req;
1360 rr = snew(struct req);
1361 rr->offset = xfer->offset;
1364 xfer->tail->next = rr;
1365 rr->prev = xfer->tail;
1375 sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len));
1376 fxp_set_userdata(req, rr);
1378 xfer->offset = uint64_add32(xfer->offset, rr->len);
1379 xfer->req_totalsize += rr->len;
1382 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); }
1387 * Returns INT_MIN to indicate that it didn't even get as far as
1388 * fxp_write_recv and hence has not freed pktin.
1390 int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1392 struct sftp_request *rreq;
1393 struct req *rr, *prev, *next;
1396 rreq = sftp_find_request(pktin);
1398 return INT_MIN; /* this packet doesn't even make sense */
1399 rr = (struct req *)fxp_get_userdata(rreq);
1401 fxp_internal_error("request ID is not part of the current upload");
1402 return INT_MIN; /* this packet isn't ours */
1404 ret = fxp_write_recv(pktin, rreq);
1406 printf("write request %p has returned [%d]\n", rr, ret);
1410 * Remove this one from the queue.
1422 xfer->req_totalsize -= rr->len;
1431 void xfer_cleanup(struct fxp_xfer *xfer)
1434 while (xfer->head) {
1436 xfer->head = xfer->head->next;