2 * sftp.c: SFTP generic client code.
16 #define GET_32BIT(cp) \
17 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
18 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
19 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
20 ((unsigned long)(unsigned char)(cp)[3]))
22 #define PUT_32BIT(cp, value) { \
23 (cp)[0] = (unsigned char)((value) >> 24); \
24 (cp)[1] = (unsigned char)((value) >> 16); \
25 (cp)[2] = (unsigned char)((value) >> 8); \
26 (cp)[3] = (unsigned char)(value); }
35 static const char *fxp_error_message;
36 static int fxp_errtype;
38 static void fxp_internal_error(char *msg);
40 /* ----------------------------------------------------------------------
41 * SFTP packet construction functions.
43 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
45 if (pkt->maxlen < length) {
46 pkt->maxlen = length + 256;
47 pkt->data = sresize(pkt->data, pkt->maxlen, char);
50 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
53 sftp_pkt_ensure(pkt, pkt->length);
54 memcpy(pkt->data + pkt->length - len, data, len);
56 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
58 sftp_pkt_adddata(pkt, &byte, 1);
60 static struct sftp_packet *sftp_pkt_init(int pkt_type)
62 struct sftp_packet *pkt;
63 pkt = snew(struct sftp_packet);
68 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
72 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
74 sftp_pkt_adddata(pkt, &value, 1);
77 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
82 sftp_pkt_adddata(pkt, x, 4);
84 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
87 PUT_32BIT(x, value.hi);
88 PUT_32BIT(x + 4, value.lo);
89 sftp_pkt_adddata(pkt, x, 8);
91 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
93 sftp_pkt_adduint32(pkt, 0);
94 pkt->savedpos = pkt->length;
96 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
98 sftp_pkt_adddata(pkt, data, strlen(data));
99 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
101 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
104 sftp_pkt_adddata(pkt, data, len);
105 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
107 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
109 sftp_pkt_addstring_start(pkt);
110 sftp_pkt_addstring_str(pkt, data);
112 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
114 sftp_pkt_adduint32(pkt, attrs.flags);
115 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
116 sftp_pkt_adduint32(pkt, attrs.size.hi);
117 sftp_pkt_adduint32(pkt, attrs.size.lo);
119 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
120 sftp_pkt_adduint32(pkt, attrs.uid);
121 sftp_pkt_adduint32(pkt, attrs.gid);
123 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
124 sftp_pkt_adduint32(pkt, attrs.permissions);
126 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
127 sftp_pkt_adduint32(pkt, attrs.atime);
128 sftp_pkt_adduint32(pkt, attrs.mtime);
130 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
132 * We currently don't support sending any extended
138 /* ----------------------------------------------------------------------
139 * SFTP packet decode functions.
142 static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt)
145 if (pkt->length - pkt->savedpos < 1)
146 return 0; /* arrgh, no way to decline (FIXME?) */
147 value = (unsigned char) pkt->data[pkt->savedpos];
151 static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt)
154 if (pkt->length - pkt->savedpos < 4)
155 return 0; /* arrgh, no way to decline (FIXME?) */
156 value = GET_32BIT(pkt->data + pkt->savedpos);
160 static void sftp_pkt_getstring(struct sftp_packet *pkt,
161 char **p, int *length)
164 if (pkt->length - pkt->savedpos < 4)
166 *length = GET_32BIT(pkt->data + pkt->savedpos);
168 if (pkt->length - pkt->savedpos < *length)
170 *p = pkt->data + pkt->savedpos;
171 pkt->savedpos += *length;
173 static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt)
175 struct fxp_attrs ret;
176 ret.flags = sftp_pkt_getuint32(pkt);
177 if (ret.flags & SSH_FILEXFER_ATTR_SIZE) {
178 unsigned long hi, lo;
179 hi = sftp_pkt_getuint32(pkt);
180 lo = sftp_pkt_getuint32(pkt);
181 ret.size = uint64_make(hi, lo);
183 if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) {
184 ret.uid = sftp_pkt_getuint32(pkt);
185 ret.gid = sftp_pkt_getuint32(pkt);
187 if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
188 ret.permissions = sftp_pkt_getuint32(pkt);
190 if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
191 ret.atime = sftp_pkt_getuint32(pkt);
192 ret.mtime = sftp_pkt_getuint32(pkt);
194 if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) {
196 count = sftp_pkt_getuint32(pkt);
201 * We should try to analyse these, if we ever find one
204 sftp_pkt_getstring(pkt, &str, &len);
205 sftp_pkt_getstring(pkt, &str, &len);
210 static void sftp_pkt_free(struct sftp_packet *pkt)
217 /* ----------------------------------------------------------------------
218 * Send and receive packet functions.
220 int sftp_send(struct sftp_packet *pkt)
224 PUT_32BIT(x, pkt->length);
225 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
229 struct sftp_packet *sftp_recv(void)
231 struct sftp_packet *pkt;
234 if (!sftp_recvdata(x, 4))
237 pkt = snew(struct sftp_packet);
239 pkt->length = pkt->maxlen = GET_32BIT(x);
240 pkt->data = snewn(pkt->length, char);
242 if (!sftp_recvdata(pkt->data, pkt->length)) {
247 pkt->type = sftp_pkt_getbyte(pkt);
252 /* ----------------------------------------------------------------------
253 * Request ID allocation and temporary dispatch routines.
256 #define REQUEST_ID_OFFSET 256
258 struct sftp_request {
264 static int sftp_reqcmp(void *av, void *bv)
266 struct sftp_request *a = (struct sftp_request *)av;
267 struct sftp_request *b = (struct sftp_request *)bv;
274 static int sftp_reqfind(void *av, void *bv)
276 unsigned *a = (unsigned *) av;
277 struct sftp_request *b = (struct sftp_request *)bv;
285 static tree234 *sftp_requests;
287 static struct sftp_request *sftp_alloc_request(void)
289 unsigned low, high, mid;
291 struct sftp_request *r;
293 if (sftp_requests == NULL)
294 sftp_requests = newtree234(sftp_reqcmp);
297 * First-fit allocation of request IDs: always pick the lowest
298 * unused one. To do this, binary-search using the counted
299 * B-tree to find the largest ID which is in a contiguous
300 * sequence from the beginning. (Precisely everything in that
301 * sequence must have ID equal to its tree index plus
302 * REQUEST_ID_OFFSET.)
304 tsize = count234(sftp_requests);
308 while (high - low > 1) {
309 mid = (high + low) / 2;
310 r = index234(sftp_requests, mid);
311 if (r->id == mid + REQUEST_ID_OFFSET)
312 low = mid; /* this one is fine */
314 high = mid; /* this one is past it */
317 * Now low points to either -1, or the tree index of the
318 * largest ID in the initial sequence.
321 unsigned i = low + 1 + REQUEST_ID_OFFSET;
322 assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
326 * So the request ID we need to create is
327 * low + 1 + REQUEST_ID_OFFSET.
329 r = snew(struct sftp_request);
330 r->id = low + 1 + REQUEST_ID_OFFSET;
333 add234(sftp_requests, r);
337 void sftp_register(struct sftp_request *req)
342 struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
345 struct sftp_request *req;
348 fxp_internal_error("did not receive a valid SFTP packet\n");
352 id = sftp_pkt_getuint32(pktin);
353 req = find234(sftp_requests, &id, sftp_reqfind);
355 if (!req || !req->registered) {
356 fxp_internal_error("request ID mismatch\n");
357 sftp_pkt_free(pktin);
361 del234(sftp_requests, req);
366 /* ----------------------------------------------------------------------
367 * String handling routines.
370 static char *mkstr(char *s, int len)
372 char *p = snewn(len + 1, char);
378 /* ----------------------------------------------------------------------
383 * Deal with (and free) an FXP_STATUS packet. Return 1 if
384 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
385 * Also place the status into fxp_errtype.
387 static int fxp_got_status(struct sftp_packet *pktin)
389 static const char *const messages[] = {
390 /* SSH_FX_OK. The only time we will display a _message_ for this
391 * is if we were expecting something other than FXP_STATUS on
392 * success, so this is actually an error message! */
393 "unexpected OK response",
395 "no such file or directory",
401 "operation unsupported",
404 if (pktin->type != SSH_FXP_STATUS) {
405 fxp_error_message = "expected FXP_STATUS packet";
408 fxp_errtype = sftp_pkt_getuint32(pktin);
409 if (fxp_errtype < 0 ||
410 fxp_errtype >= sizeof(messages) / sizeof(*messages))
411 fxp_error_message = "unknown error code";
413 fxp_error_message = messages[fxp_errtype];
416 if (fxp_errtype == SSH_FX_OK)
418 else if (fxp_errtype == SSH_FX_EOF)
424 static void fxp_internal_error(char *msg)
426 fxp_error_message = msg;
430 const char *fxp_error(void)
432 return fxp_error_message;
435 int fxp_error_type(void)
441 * Perform exchange of init/version packets. Return 0 on failure.
445 struct sftp_packet *pktout, *pktin;
448 pktout = sftp_pkt_init(SSH_FXP_INIT);
449 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
454 fxp_internal_error("could not connect");
457 if (pktin->type != SSH_FXP_VERSION) {
458 fxp_internal_error("did not receive FXP_VERSION");
459 sftp_pkt_free(pktin);
462 remotever = sftp_pkt_getuint32(pktin);
463 if (remotever > SFTP_PROTO_VERSION) {
465 ("remote protocol is more advanced than we support");
466 sftp_pkt_free(pktin);
470 * In principle, this packet might also contain extension-
471 * string pairs. We should work through them and look for any
472 * we recognise. In practice we don't currently do so because
473 * we know we don't recognise _any_.
475 sftp_pkt_free(pktin);
481 * Canonify a pathname.
483 struct sftp_request *fxp_realpath_send(char *path)
485 struct sftp_request *req = sftp_alloc_request();
486 struct sftp_packet *pktout;
488 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
489 sftp_pkt_adduint32(pktout, req->id);
490 sftp_pkt_addstring_start(pktout);
491 sftp_pkt_addstring_str(pktout, path);
497 char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req)
501 if (pktin->type == SSH_FXP_NAME) {
506 count = sftp_pkt_getuint32(pktin);
508 fxp_internal_error("REALPATH returned name count != 1\n");
509 sftp_pkt_free(pktin);
512 sftp_pkt_getstring(pktin, &path, &len);
514 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
515 sftp_pkt_free(pktin);
518 path = mkstr(path, len);
519 sftp_pkt_free(pktin);
522 fxp_got_status(pktin);
523 sftp_pkt_free(pktin);
531 struct sftp_request *fxp_open_send(char *path, int type)
533 struct sftp_request *req = sftp_alloc_request();
534 struct sftp_packet *pktout;
536 pktout = sftp_pkt_init(SSH_FXP_OPEN);
537 sftp_pkt_adduint32(pktout, req->id);
538 sftp_pkt_addstring(pktout, path);
539 sftp_pkt_adduint32(pktout, type);
540 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
546 struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin,
547 struct sftp_request *req)
551 if (pktin->type == SSH_FXP_HANDLE) {
553 struct fxp_handle *handle;
556 sftp_pkt_getstring(pktin, &hstring, &len);
558 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
559 sftp_pkt_free(pktin);
562 handle = snew(struct fxp_handle);
563 handle->hstring = mkstr(hstring, len);
565 sftp_pkt_free(pktin);
568 fxp_got_status(pktin);
569 sftp_pkt_free(pktin);
577 struct sftp_request *fxp_opendir_send(char *path)
579 struct sftp_request *req = sftp_alloc_request();
580 struct sftp_packet *pktout;
582 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
583 sftp_pkt_adduint32(pktout, req->id);
584 sftp_pkt_addstring(pktout, path);
590 struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin,
591 struct sftp_request *req)
594 if (pktin->type == SSH_FXP_HANDLE) {
596 struct fxp_handle *handle;
599 sftp_pkt_getstring(pktin, &hstring, &len);
601 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
602 sftp_pkt_free(pktin);
605 handle = snew(struct fxp_handle);
606 handle->hstring = mkstr(hstring, len);
608 sftp_pkt_free(pktin);
611 fxp_got_status(pktin);
612 sftp_pkt_free(pktin);
620 struct sftp_request *fxp_close_send(struct fxp_handle *handle)
622 struct sftp_request *req = sftp_alloc_request();
623 struct sftp_packet *pktout;
625 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
626 sftp_pkt_adduint32(pktout, req->id);
627 sftp_pkt_addstring_start(pktout);
628 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
631 sfree(handle->hstring);
637 void fxp_close_recv(struct sftp_packet *pktin, struct sftp_request *req)
640 fxp_got_status(pktin);
641 sftp_pkt_free(pktin);
644 struct sftp_request *fxp_mkdir_send(char *path)
646 struct sftp_request *req = sftp_alloc_request();
647 struct sftp_packet *pktout;
649 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
650 sftp_pkt_adduint32(pktout, req->id);
651 sftp_pkt_addstring(pktout, path);
652 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
658 int fxp_mkdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
662 id = fxp_got_status(pktin);
663 sftp_pkt_free(pktin);
670 struct sftp_request *fxp_rmdir_send(char *path)
672 struct sftp_request *req = sftp_alloc_request();
673 struct sftp_packet *pktout;
675 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
676 sftp_pkt_adduint32(pktout, req->id);
677 sftp_pkt_addstring(pktout, path);
683 int fxp_rmdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
687 id = fxp_got_status(pktin);
688 sftp_pkt_free(pktin);
695 struct sftp_request *fxp_remove_send(char *fname)
697 struct sftp_request *req = sftp_alloc_request();
698 struct sftp_packet *pktout;
700 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
701 sftp_pkt_adduint32(pktout, req->id);
702 sftp_pkt_addstring(pktout, fname);
708 int fxp_remove_recv(struct sftp_packet *pktin, struct sftp_request *req)
712 id = fxp_got_status(pktin);
713 sftp_pkt_free(pktin);
720 struct sftp_request *fxp_rename_send(char *srcfname, char *dstfname)
722 struct sftp_request *req = sftp_alloc_request();
723 struct sftp_packet *pktout;
725 pktout = sftp_pkt_init(SSH_FXP_RENAME);
726 sftp_pkt_adduint32(pktout, req->id);
727 sftp_pkt_addstring(pktout, srcfname);
728 sftp_pkt_addstring(pktout, dstfname);
734 int fxp_rename_recv(struct sftp_packet *pktin, struct sftp_request *req)
738 id = fxp_got_status(pktin);
739 sftp_pkt_free(pktin);
747 * Retrieve the attributes of a file. We have fxp_stat which works
748 * on filenames, and fxp_fstat which works on open file handles.
750 struct sftp_request *fxp_stat_send(char *fname)
752 struct sftp_request *req = sftp_alloc_request();
753 struct sftp_packet *pktout;
755 pktout = sftp_pkt_init(SSH_FXP_STAT);
756 sftp_pkt_adduint32(pktout, req->id);
757 sftp_pkt_addstring(pktout, fname);
763 int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req,
764 struct fxp_attrs *attrs)
767 if (pktin->type == SSH_FXP_ATTRS) {
768 *attrs = sftp_pkt_getattrs(pktin);
769 sftp_pkt_free(pktin);
772 fxp_got_status(pktin);
773 sftp_pkt_free(pktin);
778 struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
780 struct sftp_request *req = sftp_alloc_request();
781 struct sftp_packet *pktout;
783 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
784 sftp_pkt_adduint32(pktout, req->id);
785 sftp_pkt_addstring_start(pktout);
786 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
792 int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req,
793 struct fxp_attrs *attrs)
796 if (pktin->type == SSH_FXP_ATTRS) {
797 *attrs = sftp_pkt_getattrs(pktin);
798 sftp_pkt_free(pktin);
801 fxp_got_status(pktin);
802 sftp_pkt_free(pktin);
808 * Set the attributes of a file.
810 struct sftp_request *fxp_setstat_send(char *fname, struct fxp_attrs attrs)
812 struct sftp_request *req = sftp_alloc_request();
813 struct sftp_packet *pktout;
815 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
816 sftp_pkt_adduint32(pktout, req->id);
817 sftp_pkt_addstring(pktout, fname);
818 sftp_pkt_addattrs(pktout, attrs);
824 int fxp_setstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
828 id = fxp_got_status(pktin);
829 sftp_pkt_free(pktin);
836 struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
837 struct fxp_attrs attrs)
839 struct sftp_request *req = sftp_alloc_request();
840 struct sftp_packet *pktout;
842 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
843 sftp_pkt_adduint32(pktout, req->id);
844 sftp_pkt_addstring_start(pktout);
845 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
846 sftp_pkt_addattrs(pktout, attrs);
852 int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
856 id = fxp_got_status(pktin);
857 sftp_pkt_free(pktin);
865 * Read from a file. Returns the number of bytes read, or -1 on an
866 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
867 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
868 * error indicator. It might even depend on the SFTP server.)
870 struct sftp_request *fxp_read_send(struct fxp_handle *handle,
871 uint64 offset, int len)
873 struct sftp_request *req = sftp_alloc_request();
874 struct sftp_packet *pktout;
876 pktout = sftp_pkt_init(SSH_FXP_READ);
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_adduint64(pktout, offset);
881 sftp_pkt_adduint32(pktout, len);
887 int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
888 char *buffer, int len)
891 if (pktin->type == SSH_FXP_DATA) {
895 sftp_pkt_getstring(pktin, &str, &rlen);
897 if (rlen > len || rlen < 0) {
898 fxp_internal_error("READ returned more bytes than requested");
899 sftp_pkt_free(pktin);
903 memcpy(buffer, str, rlen);
904 sftp_pkt_free(pktin);
907 fxp_got_status(pktin);
908 sftp_pkt_free(pktin);
914 * Read from a directory.
916 struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
918 struct sftp_request *req = sftp_alloc_request();
919 struct sftp_packet *pktout;
921 pktout = sftp_pkt_init(SSH_FXP_READDIR);
922 sftp_pkt_adduint32(pktout, req->id);
923 sftp_pkt_addstring_start(pktout);
924 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
930 struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
931 struct sftp_request *req)
934 if (pktin->type == SSH_FXP_NAME) {
935 struct fxp_names *ret;
937 ret = snew(struct fxp_names);
938 ret->nnames = sftp_pkt_getuint32(pktin);
939 ret->names = snewn(ret->nnames, struct fxp_name);
940 for (i = 0; i < ret->nnames; i++) {
943 sftp_pkt_getstring(pktin, &str, &len);
944 ret->names[i].filename = mkstr(str, len);
945 sftp_pkt_getstring(pktin, &str, &len);
946 ret->names[i].longname = mkstr(str, len);
947 ret->names[i].attrs = sftp_pkt_getattrs(pktin);
949 sftp_pkt_free(pktin);
952 fxp_got_status(pktin);
953 sftp_pkt_free(pktin);
959 * Write to a file. Returns 0 on error, 1 on OK.
961 struct sftp_request *fxp_write_send(struct fxp_handle *handle,
962 char *buffer, uint64 offset, int len)
964 struct sftp_request *req = sftp_alloc_request();
965 struct sftp_packet *pktout;
967 pktout = sftp_pkt_init(SSH_FXP_WRITE);
968 sftp_pkt_adduint32(pktout, req->id);
969 sftp_pkt_addstring_start(pktout);
970 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
971 sftp_pkt_adduint64(pktout, offset);
972 sftp_pkt_addstring_start(pktout);
973 sftp_pkt_addstring_data(pktout, buffer, len);
979 int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req)
982 fxp_got_status(pktin);
983 sftp_pkt_free(pktin);
984 return fxp_errtype == SSH_FX_OK;
988 * Free up an fxp_names structure.
990 void fxp_free_names(struct fxp_names *names)
994 for (i = 0; i < names->nnames; i++) {
995 sfree(names->names[i].filename);
996 sfree(names->names[i].longname);
1003 * Duplicate an fxp_name structure.
1005 struct fxp_name *fxp_dup_name(struct fxp_name *name)
1007 struct fxp_name *ret;
1008 ret = snew(struct fxp_name);
1009 ret->filename = dupstr(name->filename);
1010 ret->longname = dupstr(name->longname);
1011 ret->attrs = name->attrs; /* structure copy */
1016 * Free up an fxp_name structure.
1018 void fxp_free_name(struct fxp_name *name)
1020 sfree(name->filename);
1021 sfree(name->longname);
1026 * Store user data in an sftp_request structure.
1028 void *fxp_get_userdata(struct sftp_request *req)
1030 return req->userdata;
1033 void fxp_set_userdata(struct sftp_request *req, void *data)
1035 req->userdata = data;
1039 * A wrapper to go round fxp_read_* and fxp_write_*, which manages
1040 * the queueing of multiple read/write requests.
1045 int len, retlen, complete;
1047 struct req *next, *prev;
1051 uint64 offset, furthestdata, filesize;
1052 int nreqs, req_max, eof, err;
1053 struct fxp_handle *fh;
1054 struct req *head, *tail;
1057 static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
1059 struct fxp_xfer *xfer = snew(struct fxp_xfer);
1062 xfer->offset = offset;
1063 xfer->head = xfer->tail = NULL;
1065 xfer->req_max = 4; /* FIXME: set properly! */
1067 xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX);
1068 xfer->furthestdata = uint64_make(0, 0);
1073 int xfer_download_done(struct fxp_xfer *xfer)
1076 * We're finished if we've seen EOF _and_ there are no
1077 * outstanding requests.
1079 return (xfer->eof || xfer->err) && !xfer->head;
1082 void xfer_download_queue(struct fxp_xfer *xfer)
1084 while (xfer->nreqs < xfer->req_max && !xfer->eof) {
1086 * Queue a new read request.
1089 struct sftp_request *req;
1091 rr = snew(struct req);
1092 rr->offset = xfer->offset;
1095 xfer->tail->next = rr;
1096 rr->prev = xfer->tail;
1105 rr->buffer = snewn(rr->len, char);
1106 sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
1107 fxp_set_userdata(req, rr);
1109 xfer->offset = uint64_add32(xfer->offset, rr->len);
1112 #ifdef DEBUG_DOWNLOAD
1113 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); }
1118 struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset)
1120 struct fxp_xfer *xfer = xfer_init(fh, offset);
1123 xfer_download_queue(xfer);
1128 int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1130 struct sftp_request *rreq;
1133 rreq = sftp_find_request(pktin);
1134 rr = (struct req *)fxp_get_userdata(rreq);
1136 return 0; /* this packet isn't ours */
1137 rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len);
1138 #ifdef DEBUG_DOWNLOAD
1139 printf("read request %p has returned [%d]\n", rr, rr->retlen);
1142 if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) {
1145 #ifdef DEBUG_DOWNLOAD
1146 printf("setting eof\n");
1148 } else if (rr->retlen < 0) {
1149 /* some error other than EOF; signal it back to caller */
1156 * Special case: if we have received fewer bytes than we
1157 * actually read, we should do something. For the moment I'll
1158 * just throw an ersatz FXP error to signal this; the SFTP
1159 * draft I've got says that it can't happen except on special
1160 * files, in which case seeking probably has very little
1161 * meaning and so queueing an additional read request to fill
1162 * up the gap sounds like the wrong answer. I'm not sure what I
1163 * should be doing here - if it _was_ a special file, I suspect
1164 * I simply shouldn't have been queueing multiple requests in
1165 * the first place...
1167 if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) {
1168 xfer->furthestdata = rr->offset;
1169 #ifdef DEBUG_DOWNLOAD
1171 uint64_decimal(xfer->furthestdata, buf);
1172 printf("setting furthestdata = %s\n", buf); }
1176 if (rr->retlen < rr->len) {
1177 uint64 filesize = uint64_add32(rr->offset,
1178 (rr->retlen < 0 ? 0 : rr->retlen));
1179 #ifdef DEBUG_DOWNLOAD
1181 uint64_decimal(filesize, buf);
1182 printf("short block! trying filesize = %s\n", buf); }
1184 if (uint64_compare(xfer->filesize, filesize) > 0) {
1185 xfer->filesize = filesize;
1186 #ifdef DEBUG_DOWNLOAD
1187 printf("actually changing filesize\n");
1192 if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) {
1193 fxp_error_message = "received a short buffer from FXP_READ, but not"
1196 xfer_set_error(xfer);
1203 void xfer_set_error(struct fxp_xfer *xfer)
1208 int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
1210 void *retbuf = NULL;
1214 * Discard anything at the head of the rr queue with complete <
1215 * 0; return the first thing with complete > 0.
1217 while (xfer->head && xfer->head->complete && !retbuf) {
1218 struct req *rr = xfer->head;
1220 if (rr->complete > 0) {
1221 retbuf = rr->buffer;
1222 retlen = rr->retlen;
1223 #ifdef DEBUG_DOWNLOAD
1224 printf("handing back data from read request %p\n", rr);
1227 #ifdef DEBUG_DOWNLOAD
1229 printf("skipping failed read request %p\n", rr);
1232 xfer->head = xfer->head->next;
1234 xfer->head->prev = NULL;
1249 void xfer_cleanup(struct fxp_xfer *xfer)
1252 while (xfer->head) {
1254 xfer->head = xfer->head->next;