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(const 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,
39 const void *data, int len)
42 sftp_pkt_ensure(pkt, pkt->length);
43 memcpy(pkt->data + pkt->length - len, data, len);
45 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
47 sftp_pkt_adddata(pkt, &byte, 1);
49 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
54 sftp_pkt_adddata(pkt, x, 4);
56 static struct sftp_packet *sftp_pkt_init(int pkt_type)
58 struct sftp_packet *pkt;
59 pkt = snew(struct sftp_packet);
64 sftp_pkt_adduint32(pkt, 0); /* length field will be filled in later */
65 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
69 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
71 sftp_pkt_adddata(pkt, &value, 1);
74 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
77 PUT_32BIT(x, value.hi);
78 PUT_32BIT(x + 4, value.lo);
79 sftp_pkt_adddata(pkt, x, 8);
81 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
83 sftp_pkt_adduint32(pkt, 0);
84 pkt->savedpos = pkt->length;
86 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, const char *data)
88 sftp_pkt_adddata(pkt, data, strlen(data));
89 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
91 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
92 const char *data, int len)
94 sftp_pkt_adddata(pkt, data, len);
95 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
97 static void sftp_pkt_addstring(struct sftp_packet *pkt, const char *data)
99 sftp_pkt_addstring_start(pkt);
100 sftp_pkt_addstring_str(pkt, data);
102 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
104 sftp_pkt_adduint32(pkt, attrs.flags);
105 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
106 sftp_pkt_adduint32(pkt, attrs.size.hi);
107 sftp_pkt_adduint32(pkt, attrs.size.lo);
109 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
110 sftp_pkt_adduint32(pkt, attrs.uid);
111 sftp_pkt_adduint32(pkt, attrs.gid);
113 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
114 sftp_pkt_adduint32(pkt, attrs.permissions);
116 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
117 sftp_pkt_adduint32(pkt, attrs.atime);
118 sftp_pkt_adduint32(pkt, attrs.mtime);
120 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
122 * We currently don't support sending any extended
128 /* ----------------------------------------------------------------------
129 * SFTP packet decode functions.
132 static int sftp_pkt_getbyte(struct sftp_packet *pkt, unsigned char *ret)
134 if (pkt->length - pkt->savedpos < 1)
136 *ret = (unsigned char) pkt->data[pkt->savedpos];
140 static int sftp_pkt_getuint32(struct sftp_packet *pkt, unsigned long *ret)
142 if (pkt->length - pkt->savedpos < 4)
144 *ret = GET_32BIT(pkt->data + pkt->savedpos);
148 static int sftp_pkt_getstring(struct sftp_packet *pkt,
149 char **p, int *length)
152 if (pkt->length - pkt->savedpos < 4)
154 *length = toint(GET_32BIT(pkt->data + pkt->savedpos));
156 if ((int)(pkt->length - pkt->savedpos) < *length || *length < 0) {
160 *p = pkt->data + pkt->savedpos;
161 pkt->savedpos += *length;
164 static int sftp_pkt_getattrs(struct sftp_packet *pkt, struct fxp_attrs *ret)
166 if (!sftp_pkt_getuint32(pkt, &ret->flags))
168 if (ret->flags & SSH_FILEXFER_ATTR_SIZE) {
169 unsigned long hi, lo;
170 if (!sftp_pkt_getuint32(pkt, &hi) ||
171 !sftp_pkt_getuint32(pkt, &lo))
173 ret->size = uint64_make(hi, lo);
175 if (ret->flags & SSH_FILEXFER_ATTR_UIDGID) {
176 if (!sftp_pkt_getuint32(pkt, &ret->uid) ||
177 !sftp_pkt_getuint32(pkt, &ret->gid))
180 if (ret->flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
181 if (!sftp_pkt_getuint32(pkt, &ret->permissions))
184 if (ret->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
185 if (!sftp_pkt_getuint32(pkt, &ret->atime) ||
186 !sftp_pkt_getuint32(pkt, &ret->mtime))
189 if (ret->flags & SSH_FILEXFER_ATTR_EXTENDED) {
191 if (!sftp_pkt_getuint32(pkt, &count))
197 * We should try to analyse these, if we ever find one
200 if (!sftp_pkt_getstring(pkt, &str, &len) ||
201 !sftp_pkt_getstring(pkt, &str, &len))
207 static void sftp_pkt_free(struct sftp_packet *pkt)
214 /* ----------------------------------------------------------------------
215 * Send and receive packet functions.
217 int sftp_send(struct sftp_packet *pkt)
220 PUT_32BIT(pkt->data, pkt->length - 4);
221 ret = sftp_senddata(pkt->data, pkt->length);
225 struct sftp_packet *sftp_recv(void)
227 struct sftp_packet *pkt;
231 if (!sftp_recvdata(x, 4))
234 pkt = snew(struct sftp_packet);
236 pkt->length = pkt->maxlen = GET_32BIT(x);
237 pkt->data = snewn(pkt->length, char);
239 if (!sftp_recvdata(pkt->data, pkt->length)) {
244 if (!sftp_pkt_getbyte(pkt, &uc)) {
254 /* ----------------------------------------------------------------------
255 * Request ID allocation and temporary dispatch routines.
258 #define REQUEST_ID_OFFSET 256
260 struct sftp_request {
266 static int sftp_reqcmp(void *av, void *bv)
268 struct sftp_request *a = (struct sftp_request *)av;
269 struct sftp_request *b = (struct sftp_request *)bv;
276 static int sftp_reqfind(void *av, void *bv)
278 unsigned *a = (unsigned *) av;
279 struct sftp_request *b = (struct sftp_request *)bv;
287 static tree234 *sftp_requests;
289 static struct sftp_request *sftp_alloc_request(void)
291 unsigned low, high, mid;
293 struct sftp_request *r;
295 if (sftp_requests == NULL)
296 sftp_requests = newtree234(sftp_reqcmp);
299 * First-fit allocation of request IDs: always pick the lowest
300 * unused one. To do this, binary-search using the counted
301 * B-tree to find the largest ID which is in a contiguous
302 * sequence from the beginning. (Precisely everything in that
303 * sequence must have ID equal to its tree index plus
304 * REQUEST_ID_OFFSET.)
306 tsize = count234(sftp_requests);
310 while (high - low > 1) {
311 mid = (high + low) / 2;
312 r = index234(sftp_requests, mid);
313 if (r->id == mid + REQUEST_ID_OFFSET)
314 low = mid; /* this one is fine */
316 high = mid; /* this one is past it */
319 * Now low points to either -1, or the tree index of the
320 * largest ID in the initial sequence.
323 unsigned i = low + 1 + REQUEST_ID_OFFSET;
324 assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
328 * So the request ID we need to create is
329 * low + 1 + REQUEST_ID_OFFSET.
331 r = snew(struct sftp_request);
332 r->id = low + 1 + REQUEST_ID_OFFSET;
335 add234(sftp_requests, r);
339 void sftp_cleanup_request(void)
341 if (sftp_requests != NULL) {
342 freetree234(sftp_requests);
343 sftp_requests = NULL;
347 void sftp_register(struct sftp_request *req)
352 struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
356 struct sftp_request *req;
359 fxp_internal_error("did not receive a valid SFTP packet\n");
363 if (!sftp_pkt_getuint32(pktin, &id)) {
364 fxp_internal_error("did not receive a valid SFTP packet\n");
368 req = find234(sftp_requests, &fid, sftp_reqfind);
370 if (!req || !req->registered) {
371 fxp_internal_error("request ID mismatch\n");
375 del234(sftp_requests, req);
380 /* ----------------------------------------------------------------------
381 * String handling routines.
384 static char *mkstr(char *s, int len)
386 char *p = snewn(len + 1, char);
392 /* ----------------------------------------------------------------------
397 * Deal with (and free) an FXP_STATUS packet. Return 1 if
398 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
399 * Also place the status into fxp_errtype.
401 static int fxp_got_status(struct sftp_packet *pktin)
403 static const char *const messages[] = {
404 /* SSH_FX_OK. The only time we will display a _message_ for this
405 * is if we were expecting something other than FXP_STATUS on
406 * success, so this is actually an error message! */
407 "unexpected OK response",
409 "no such file or directory",
415 "operation unsupported",
418 if (pktin->type != SSH_FXP_STATUS) {
419 fxp_error_message = "expected FXP_STATUS packet";
423 if (!sftp_pkt_getuint32(pktin, &ul)) {
424 fxp_error_message = "malformed FXP_STATUS packet";
428 if (fxp_errtype < 0 ||
429 fxp_errtype >= sizeof(messages) / sizeof(*messages))
430 fxp_error_message = "unknown error code";
432 fxp_error_message = messages[fxp_errtype];
436 if (fxp_errtype == SSH_FX_OK)
438 else if (fxp_errtype == SSH_FX_EOF)
444 static void fxp_internal_error(const char *msg)
446 fxp_error_message = msg;
450 const char *fxp_error(void)
452 return fxp_error_message;
455 int fxp_error_type(void)
461 * Perform exchange of init/version packets. Return 0 on failure.
465 struct sftp_packet *pktout, *pktin;
466 unsigned long remotever;
468 pktout = sftp_pkt_init(SSH_FXP_INIT);
469 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
474 fxp_internal_error("could not connect");
477 if (pktin->type != SSH_FXP_VERSION) {
478 fxp_internal_error("did not receive FXP_VERSION");
479 sftp_pkt_free(pktin);
482 if (!sftp_pkt_getuint32(pktin, &remotever)) {
483 fxp_internal_error("malformed FXP_VERSION packet");
484 sftp_pkt_free(pktin);
487 if (remotever > SFTP_PROTO_VERSION) {
489 ("remote protocol is more advanced than we support");
490 sftp_pkt_free(pktin);
494 * In principle, this packet might also contain extension-
495 * string pairs. We should work through them and look for any
496 * we recognise. In practice we don't currently do so because
497 * we know we don't recognise _any_.
499 sftp_pkt_free(pktin);
505 * Canonify a pathname.
507 struct sftp_request *fxp_realpath_send(const char *path)
509 struct sftp_request *req = sftp_alloc_request();
510 struct sftp_packet *pktout;
512 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
513 sftp_pkt_adduint32(pktout, req->id);
514 sftp_pkt_addstring_start(pktout);
515 sftp_pkt_addstring_str(pktout, path);
521 char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req)
525 if (pktin->type == SSH_FXP_NAME) {
530 if (!sftp_pkt_getuint32(pktin, &count) || count != 1) {
531 fxp_internal_error("REALPATH did not return name count of 1\n");
532 sftp_pkt_free(pktin);
535 if (!sftp_pkt_getstring(pktin, &path, &len)) {
536 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
537 sftp_pkt_free(pktin);
540 path = mkstr(path, len);
541 sftp_pkt_free(pktin);
544 fxp_got_status(pktin);
545 sftp_pkt_free(pktin);
553 struct sftp_request *fxp_open_send(const char *path, int type,
554 struct fxp_attrs *attrs)
556 struct sftp_request *req = sftp_alloc_request();
557 struct sftp_packet *pktout;
559 pktout = sftp_pkt_init(SSH_FXP_OPEN);
560 sftp_pkt_adduint32(pktout, req->id);
561 sftp_pkt_addstring(pktout, path);
562 sftp_pkt_adduint32(pktout, type);
564 sftp_pkt_addattrs(pktout, *attrs);
566 sftp_pkt_adduint32(pktout, 0); /* empty ATTRS structure */
572 struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin,
573 struct sftp_request *req)
577 if (pktin->type == SSH_FXP_HANDLE) {
579 struct fxp_handle *handle;
582 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
583 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
584 sftp_pkt_free(pktin);
587 handle = snew(struct fxp_handle);
588 handle->hstring = mkstr(hstring, len);
590 sftp_pkt_free(pktin);
593 fxp_got_status(pktin);
594 sftp_pkt_free(pktin);
602 struct sftp_request *fxp_opendir_send(const char *path)
604 struct sftp_request *req = sftp_alloc_request();
605 struct sftp_packet *pktout;
607 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
608 sftp_pkt_adduint32(pktout, req->id);
609 sftp_pkt_addstring(pktout, path);
615 struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin,
616 struct sftp_request *req)
619 if (pktin->type == SSH_FXP_HANDLE) {
621 struct fxp_handle *handle;
624 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
625 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
626 sftp_pkt_free(pktin);
629 handle = snew(struct fxp_handle);
630 handle->hstring = mkstr(hstring, len);
632 sftp_pkt_free(pktin);
635 fxp_got_status(pktin);
636 sftp_pkt_free(pktin);
644 struct sftp_request *fxp_close_send(struct fxp_handle *handle)
646 struct sftp_request *req = sftp_alloc_request();
647 struct sftp_packet *pktout;
649 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
650 sftp_pkt_adduint32(pktout, req->id);
651 sftp_pkt_addstring_start(pktout);
652 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
655 sfree(handle->hstring);
661 void fxp_close_recv(struct sftp_packet *pktin, struct sftp_request *req)
664 fxp_got_status(pktin);
665 sftp_pkt_free(pktin);
668 struct sftp_request *fxp_mkdir_send(const char *path)
670 struct sftp_request *req = sftp_alloc_request();
671 struct sftp_packet *pktout;
673 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
674 sftp_pkt_adduint32(pktout, req->id);
675 sftp_pkt_addstring(pktout, path);
676 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
682 int fxp_mkdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
686 id = fxp_got_status(pktin);
687 sftp_pkt_free(pktin);
694 struct sftp_request *fxp_rmdir_send(const char *path)
696 struct sftp_request *req = sftp_alloc_request();
697 struct sftp_packet *pktout;
699 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
700 sftp_pkt_adduint32(pktout, req->id);
701 sftp_pkt_addstring(pktout, path);
707 int fxp_rmdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
711 id = fxp_got_status(pktin);
712 sftp_pkt_free(pktin);
719 struct sftp_request *fxp_remove_send(const char *fname)
721 struct sftp_request *req = sftp_alloc_request();
722 struct sftp_packet *pktout;
724 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
725 sftp_pkt_adduint32(pktout, req->id);
726 sftp_pkt_addstring(pktout, fname);
732 int fxp_remove_recv(struct sftp_packet *pktin, struct sftp_request *req)
736 id = fxp_got_status(pktin);
737 sftp_pkt_free(pktin);
744 struct sftp_request *fxp_rename_send(const char *srcfname,
745 const char *dstfname)
747 struct sftp_request *req = sftp_alloc_request();
748 struct sftp_packet *pktout;
750 pktout = sftp_pkt_init(SSH_FXP_RENAME);
751 sftp_pkt_adduint32(pktout, req->id);
752 sftp_pkt_addstring(pktout, srcfname);
753 sftp_pkt_addstring(pktout, dstfname);
759 int fxp_rename_recv(struct sftp_packet *pktin, struct sftp_request *req)
763 id = fxp_got_status(pktin);
764 sftp_pkt_free(pktin);
772 * Retrieve the attributes of a file. We have fxp_stat which works
773 * on filenames, and fxp_fstat which works on open file handles.
775 struct sftp_request *fxp_stat_send(const char *fname)
777 struct sftp_request *req = sftp_alloc_request();
778 struct sftp_packet *pktout;
780 pktout = sftp_pkt_init(SSH_FXP_STAT);
781 sftp_pkt_adduint32(pktout, req->id);
782 sftp_pkt_addstring(pktout, fname);
788 int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req,
789 struct fxp_attrs *attrs)
792 if (pktin->type == SSH_FXP_ATTRS) {
793 if (!sftp_pkt_getattrs(pktin, attrs)) {
794 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
795 sftp_pkt_free(pktin);
798 sftp_pkt_free(pktin);
801 fxp_got_status(pktin);
802 sftp_pkt_free(pktin);
807 struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
809 struct sftp_request *req = sftp_alloc_request();
810 struct sftp_packet *pktout;
812 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
813 sftp_pkt_adduint32(pktout, req->id);
814 sftp_pkt_addstring_start(pktout);
815 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
821 int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req,
822 struct fxp_attrs *attrs)
825 if (pktin->type == SSH_FXP_ATTRS) {
826 if (!sftp_pkt_getattrs(pktin, attrs)) {
827 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
828 sftp_pkt_free(pktin);
831 sftp_pkt_free(pktin);
834 fxp_got_status(pktin);
835 sftp_pkt_free(pktin);
841 * Set the attributes of a file.
843 struct sftp_request *fxp_setstat_send(const char *fname,
844 struct fxp_attrs attrs)
846 struct sftp_request *req = sftp_alloc_request();
847 struct sftp_packet *pktout;
849 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
850 sftp_pkt_adduint32(pktout, req->id);
851 sftp_pkt_addstring(pktout, fname);
852 sftp_pkt_addattrs(pktout, attrs);
858 int fxp_setstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
862 id = fxp_got_status(pktin);
863 sftp_pkt_free(pktin);
870 struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
871 struct fxp_attrs attrs)
873 struct sftp_request *req = sftp_alloc_request();
874 struct sftp_packet *pktout;
876 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
877 sftp_pkt_adduint32(pktout, req->id);
878 sftp_pkt_addstring_start(pktout);
879 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
880 sftp_pkt_addattrs(pktout, attrs);
886 int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
890 id = fxp_got_status(pktin);
891 sftp_pkt_free(pktin);
899 * Read from a file. Returns the number of bytes read, or -1 on an
900 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
901 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
902 * error indicator. It might even depend on the SFTP server.)
904 struct sftp_request *fxp_read_send(struct fxp_handle *handle,
905 uint64 offset, int len)
907 struct sftp_request *req = sftp_alloc_request();
908 struct sftp_packet *pktout;
910 pktout = sftp_pkt_init(SSH_FXP_READ);
911 sftp_pkt_adduint32(pktout, req->id);
912 sftp_pkt_addstring_start(pktout);
913 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
914 sftp_pkt_adduint64(pktout, offset);
915 sftp_pkt_adduint32(pktout, len);
921 int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
922 char *buffer, int len)
925 if (pktin->type == SSH_FXP_DATA) {
929 if (!sftp_pkt_getstring(pktin, &str, &rlen)) {
930 fxp_internal_error("READ returned malformed SSH_FXP_DATA packet");
931 sftp_pkt_free(pktin);
935 if (rlen > len || rlen < 0) {
936 fxp_internal_error("READ returned more bytes than requested");
937 sftp_pkt_free(pktin);
941 memcpy(buffer, str, rlen);
942 sftp_pkt_free(pktin);
945 fxp_got_status(pktin);
946 sftp_pkt_free(pktin);
952 * Read from a directory.
954 struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
956 struct sftp_request *req = sftp_alloc_request();
957 struct sftp_packet *pktout;
959 pktout = sftp_pkt_init(SSH_FXP_READDIR);
960 sftp_pkt_adduint32(pktout, req->id);
961 sftp_pkt_addstring_start(pktout);
962 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
968 struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
969 struct sftp_request *req)
972 if (pktin->type == SSH_FXP_NAME) {
973 struct fxp_names *ret;
977 * Sanity-check the number of names. Minimum is obviously
978 * zero. Maximum is the remaining space in the packet
979 * divided by the very minimum length of a name, which is
980 * 12 bytes (4 for an empty filename, 4 for an empty
981 * longname, 4 for a set of attribute flags indicating that
982 * no other attributes are supplied).
984 if (!sftp_pkt_getuint32(pktin, &i) ||
985 i > (pktin->length-pktin->savedpos)/12) {
986 fxp_internal_error("malformed FXP_NAME packet");
987 sftp_pkt_free(pktin);
992 * Ensure the implicit multiplication in the snewn() call
993 * doesn't suffer integer overflow and cause us to malloc
996 if (i > INT_MAX / sizeof(struct fxp_name)) {
997 fxp_internal_error("unreasonably large FXP_NAME packet");
998 sftp_pkt_free(pktin);
1002 ret = snew(struct fxp_names);
1004 ret->names = snewn(ret->nnames, struct fxp_name);
1005 for (i = 0; i < (unsigned long)ret->nnames; i++) {
1008 if (!sftp_pkt_getstring(pktin, &str1, &len1) ||
1009 !sftp_pkt_getstring(pktin, &str2, &len2) ||
1010 !sftp_pkt_getattrs(pktin, &ret->names[i].attrs)) {
1011 fxp_internal_error("malformed FXP_NAME packet");
1013 sfree(ret->names[i].filename);
1014 sfree(ret->names[i].longname);
1021 ret->names[i].filename = mkstr(str1, len1);
1022 ret->names[i].longname = mkstr(str2, len2);
1024 sftp_pkt_free(pktin);
1027 fxp_got_status(pktin);
1028 sftp_pkt_free(pktin);
1034 * Write to a file. Returns 0 on error, 1 on OK.
1036 struct sftp_request *fxp_write_send(struct fxp_handle *handle,
1037 char *buffer, uint64 offset, int len)
1039 struct sftp_request *req = sftp_alloc_request();
1040 struct sftp_packet *pktout;
1042 pktout = sftp_pkt_init(SSH_FXP_WRITE);
1043 sftp_pkt_adduint32(pktout, req->id);
1044 sftp_pkt_addstring_start(pktout);
1045 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
1046 sftp_pkt_adduint64(pktout, offset);
1047 sftp_pkt_addstring_start(pktout);
1048 sftp_pkt_addstring_data(pktout, buffer, len);
1054 int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req)
1057 fxp_got_status(pktin);
1058 sftp_pkt_free(pktin);
1059 return fxp_errtype == SSH_FX_OK;
1063 * Free up an fxp_names structure.
1065 void fxp_free_names(struct fxp_names *names)
1069 for (i = 0; i < names->nnames; i++) {
1070 sfree(names->names[i].filename);
1071 sfree(names->names[i].longname);
1073 sfree(names->names);
1078 * Duplicate an fxp_name structure.
1080 struct fxp_name *fxp_dup_name(struct fxp_name *name)
1082 struct fxp_name *ret;
1083 ret = snew(struct fxp_name);
1084 ret->filename = dupstr(name->filename);
1085 ret->longname = dupstr(name->longname);
1086 ret->attrs = name->attrs; /* structure copy */
1091 * Free up an fxp_name structure.
1093 void fxp_free_name(struct fxp_name *name)
1095 sfree(name->filename);
1096 sfree(name->longname);
1101 * Store user data in an sftp_request structure.
1103 void *fxp_get_userdata(struct sftp_request *req)
1105 return req->userdata;
1108 void fxp_set_userdata(struct sftp_request *req, void *data)
1110 req->userdata = data;
1114 * A wrapper to go round fxp_read_* and fxp_write_*, which manages
1115 * the queueing of multiple read/write requests.
1120 int len, retlen, complete;
1122 struct req *next, *prev;
1126 uint64 offset, furthestdata, filesize;
1127 int req_totalsize, req_maxsize, eof, err;
1128 struct fxp_handle *fh;
1129 struct req *head, *tail;
1132 static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
1134 struct fxp_xfer *xfer = snew(struct fxp_xfer);
1137 xfer->offset = offset;
1138 xfer->head = xfer->tail = NULL;
1139 xfer->req_totalsize = 0;
1140 xfer->req_maxsize = 1048576;
1142 xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX);
1143 xfer->furthestdata = uint64_make(0, 0);
1148 int xfer_done(struct fxp_xfer *xfer)
1151 * We're finished if we've seen EOF _and_ there are no
1152 * outstanding requests.
1154 return (xfer->eof || xfer->err) && !xfer->head;
1157 void xfer_download_queue(struct fxp_xfer *xfer)
1159 while (xfer->req_totalsize < xfer->req_maxsize &&
1160 !xfer->eof && !xfer->err) {
1162 * Queue a new read request.
1165 struct sftp_request *req;
1167 rr = snew(struct req);
1168 rr->offset = xfer->offset;
1171 xfer->tail->next = rr;
1172 rr->prev = xfer->tail;
1181 rr->buffer = snewn(rr->len, char);
1182 sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
1183 fxp_set_userdata(req, rr);
1185 xfer->offset = uint64_add32(xfer->offset, rr->len);
1186 xfer->req_totalsize += rr->len;
1188 #ifdef DEBUG_DOWNLOAD
1189 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); }
1194 struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset)
1196 struct fxp_xfer *xfer = xfer_init(fh, offset);
1199 xfer_download_queue(xfer);
1205 * Returns INT_MIN to indicate that it didn't even get as far as
1206 * fxp_read_recv and hence has not freed pktin.
1208 int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1210 struct sftp_request *rreq;
1213 rreq = sftp_find_request(pktin);
1215 return INT_MIN; /* this packet doesn't even make sense */
1216 rr = (struct req *)fxp_get_userdata(rreq);
1218 fxp_internal_error("request ID is not part of the current download");
1219 return INT_MIN; /* this packet isn't ours */
1221 rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len);
1222 #ifdef DEBUG_DOWNLOAD
1223 printf("read request %p has returned [%d]\n", rr, rr->retlen);
1226 if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) {
1229 #ifdef DEBUG_DOWNLOAD
1230 printf("setting eof\n");
1232 } else if (rr->retlen < 0) {
1233 /* some error other than EOF; signal it back to caller */
1234 xfer_set_error(xfer);
1242 * Special case: if we have received fewer bytes than we
1243 * actually read, we should do something. For the moment I'll
1244 * just throw an ersatz FXP error to signal this; the SFTP
1245 * draft I've got says that it can't happen except on special
1246 * files, in which case seeking probably has very little
1247 * meaning and so queueing an additional read request to fill
1248 * up the gap sounds like the wrong answer. I'm not sure what I
1249 * should be doing here - if it _was_ a special file, I suspect
1250 * I simply shouldn't have been queueing multiple requests in
1251 * the first place...
1253 if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) {
1254 xfer->furthestdata = rr->offset;
1255 #ifdef DEBUG_DOWNLOAD
1257 uint64_decimal(xfer->furthestdata, buf);
1258 printf("setting furthestdata = %s\n", buf); }
1262 if (rr->retlen < rr->len) {
1263 uint64 filesize = uint64_add32(rr->offset,
1264 (rr->retlen < 0 ? 0 : rr->retlen));
1265 #ifdef DEBUG_DOWNLOAD
1267 uint64_decimal(filesize, buf);
1268 printf("short block! trying filesize = %s\n", buf); }
1270 if (uint64_compare(xfer->filesize, filesize) > 0) {
1271 xfer->filesize = filesize;
1272 #ifdef DEBUG_DOWNLOAD
1273 printf("actually changing filesize\n");
1278 if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) {
1279 fxp_error_message = "received a short buffer from FXP_READ, but not"
1282 xfer_set_error(xfer);
1289 void xfer_set_error(struct fxp_xfer *xfer)
1294 int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
1296 void *retbuf = NULL;
1300 * Discard anything at the head of the rr queue with complete <
1301 * 0; return the first thing with complete > 0.
1303 while (xfer->head && xfer->head->complete && !retbuf) {
1304 struct req *rr = xfer->head;
1306 if (rr->complete > 0) {
1307 retbuf = rr->buffer;
1308 retlen = rr->retlen;
1309 #ifdef DEBUG_DOWNLOAD
1310 printf("handing back data from read request %p\n", rr);
1313 #ifdef DEBUG_DOWNLOAD
1315 printf("skipping failed read request %p\n", rr);
1318 xfer->head = xfer->head->next;
1320 xfer->head->prev = NULL;
1323 xfer->req_totalsize -= rr->len;
1335 struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset)
1337 struct fxp_xfer *xfer = xfer_init(fh, offset);
1340 * We set `eof' to 1 because this will cause xfer_done() to
1341 * return true iff there are no outstanding requests. During an
1342 * upload, our caller will be responsible for working out
1343 * whether all the data has been sent, so all it needs to know
1344 * from us is whether the outstanding requests have been
1345 * handled once that's done.
1352 int xfer_upload_ready(struct fxp_xfer *xfer)
1354 if (sftp_sendbuffer() == 0)
1360 void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len)
1363 struct sftp_request *req;
1365 rr = snew(struct req);
1366 rr->offset = xfer->offset;
1369 xfer->tail->next = rr;
1370 rr->prev = xfer->tail;
1380 sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len));
1381 fxp_set_userdata(req, rr);
1383 xfer->offset = uint64_add32(xfer->offset, rr->len);
1384 xfer->req_totalsize += rr->len;
1387 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); }
1392 * Returns INT_MIN to indicate that it didn't even get as far as
1393 * fxp_write_recv and hence has not freed pktin.
1395 int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1397 struct sftp_request *rreq;
1398 struct req *rr, *prev, *next;
1401 rreq = sftp_find_request(pktin);
1403 return INT_MIN; /* this packet doesn't even make sense */
1404 rr = (struct req *)fxp_get_userdata(rreq);
1406 fxp_internal_error("request ID is not part of the current upload");
1407 return INT_MIN; /* this packet isn't ours */
1409 ret = fxp_write_recv(pktin, rreq);
1411 printf("write request %p has returned [%d]\n", rr, ret);
1415 * Remove this one from the queue.
1427 xfer->req_totalsize -= rr->len;
1436 void xfer_cleanup(struct fxp_xfer *xfer)
1439 while (xfer->head) {
1441 xfer->head = xfer->head->next;