2 * sftp.c: SFTP generic client code.
15 #define GET_32BIT(cp) \
16 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
17 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
18 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
19 ((unsigned long)(unsigned char)(cp)[3]))
21 #define PUT_32BIT(cp, value) { \
22 (cp)[0] = (unsigned char)((value) >> 24); \
23 (cp)[1] = (unsigned char)((value) >> 16); \
24 (cp)[2] = (unsigned char)((value) >> 8); \
25 (cp)[3] = (unsigned char)(value); }
34 static const char *fxp_error_message
;
35 static int fxp_errtype
;
37 static void fxp_internal_error(char *msg
);
39 /* ----------------------------------------------------------------------
40 * SFTP packet construction functions.
42 static void sftp_pkt_ensure(struct sftp_packet
*pkt
, int length
)
44 if (pkt
->maxlen
< length
) {
45 pkt
->maxlen
= length
+ 256;
46 pkt
->data
= sresize(pkt
->data
, pkt
->maxlen
, char);
49 static void sftp_pkt_adddata(struct sftp_packet
*pkt
, void *data
, int len
)
52 sftp_pkt_ensure(pkt
, pkt
->length
);
53 memcpy(pkt
->data
+ pkt
->length
- len
, data
, len
);
55 static void sftp_pkt_addbyte(struct sftp_packet
*pkt
, unsigned char byte
)
57 sftp_pkt_adddata(pkt
, &byte
, 1);
59 static struct sftp_packet
*sftp_pkt_init(int pkt_type
)
61 struct sftp_packet
*pkt
;
62 pkt
= snew(struct sftp_packet
);
67 sftp_pkt_addbyte(pkt
, (unsigned char) pkt_type
);
70 static void sftp_pkt_addbool(struct sftp_packet
*pkt
, unsigned char value
)
72 sftp_pkt_adddata(pkt
, &value
, 1);
74 static void sftp_pkt_adduint32(struct sftp_packet
*pkt
,
79 sftp_pkt_adddata(pkt
, x
, 4);
81 static void sftp_pkt_adduint64(struct sftp_packet
*pkt
, uint64 value
)
84 PUT_32BIT(x
, value
.hi
);
85 PUT_32BIT(x
+ 4, value
.lo
);
86 sftp_pkt_adddata(pkt
, x
, 8);
88 static void sftp_pkt_addstring_start(struct sftp_packet
*pkt
)
90 sftp_pkt_adduint32(pkt
, 0);
91 pkt
->savedpos
= pkt
->length
;
93 static void sftp_pkt_addstring_str(struct sftp_packet
*pkt
, char *data
)
95 sftp_pkt_adddata(pkt
, data
, strlen(data
));
96 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
98 static void sftp_pkt_addstring_data(struct sftp_packet
*pkt
,
101 sftp_pkt_adddata(pkt
, data
, len
);
102 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
104 static void sftp_pkt_addstring(struct sftp_packet
*pkt
, char *data
)
106 sftp_pkt_addstring_start(pkt
);
107 sftp_pkt_addstring_str(pkt
, data
);
109 static void sftp_pkt_addattrs(struct sftp_packet
*pkt
, struct fxp_attrs attrs
)
111 sftp_pkt_adduint32(pkt
, attrs
.flags
);
112 if (attrs
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
113 sftp_pkt_adduint32(pkt
, attrs
.size
.hi
);
114 sftp_pkt_adduint32(pkt
, attrs
.size
.lo
);
116 if (attrs
.flags
& SSH_FILEXFER_ATTR_UIDGID
) {
117 sftp_pkt_adduint32(pkt
, attrs
.uid
);
118 sftp_pkt_adduint32(pkt
, attrs
.gid
);
120 if (attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) {
121 sftp_pkt_adduint32(pkt
, attrs
.permissions
);
123 if (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
) {
124 sftp_pkt_adduint32(pkt
, attrs
.atime
);
125 sftp_pkt_adduint32(pkt
, attrs
.mtime
);
127 if (attrs
.flags
& SSH_FILEXFER_ATTR_EXTENDED
) {
129 * We currently don't support sending any extended
135 /* ----------------------------------------------------------------------
136 * SFTP packet decode functions.
139 static unsigned char sftp_pkt_getbyte(struct sftp_packet
*pkt
)
142 if (pkt
->length
- pkt
->savedpos
< 1)
143 return 0; /* arrgh, no way to decline (FIXME?) */
144 value
= (unsigned char) pkt
->data
[pkt
->savedpos
];
148 static unsigned long sftp_pkt_getuint32(struct sftp_packet
*pkt
)
151 if (pkt
->length
- pkt
->savedpos
< 4)
152 return 0; /* arrgh, no way to decline (FIXME?) */
153 value
= GET_32BIT(pkt
->data
+ pkt
->savedpos
);
157 static void sftp_pkt_getstring(struct sftp_packet
*pkt
,
158 char **p
, int *length
)
161 if (pkt
->length
- pkt
->savedpos
< 4)
163 *length
= GET_32BIT(pkt
->data
+ pkt
->savedpos
);
165 if (pkt
->length
- pkt
->savedpos
< *length
)
167 *p
= pkt
->data
+ pkt
->savedpos
;
168 pkt
->savedpos
+= *length
;
170 static struct fxp_attrs
sftp_pkt_getattrs(struct sftp_packet
*pkt
)
172 struct fxp_attrs ret
;
173 ret
.flags
= sftp_pkt_getuint32(pkt
);
174 if (ret
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
175 unsigned long hi
, lo
;
176 hi
= sftp_pkt_getuint32(pkt
);
177 lo
= sftp_pkt_getuint32(pkt
);
178 ret
.size
= uint64_make(hi
, lo
);
180 if (ret
.flags
& SSH_FILEXFER_ATTR_UIDGID
) {
181 ret
.uid
= sftp_pkt_getuint32(pkt
);
182 ret
.gid
= sftp_pkt_getuint32(pkt
);
184 if (ret
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) {
185 ret
.permissions
= sftp_pkt_getuint32(pkt
);
187 if (ret
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
) {
188 ret
.atime
= sftp_pkt_getuint32(pkt
);
189 ret
.mtime
= sftp_pkt_getuint32(pkt
);
191 if (ret
.flags
& SSH_FILEXFER_ATTR_EXTENDED
) {
193 count
= sftp_pkt_getuint32(pkt
);
198 * We should try to analyse these, if we ever find one
201 sftp_pkt_getstring(pkt
, &str
, &len
);
202 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
)
221 PUT_32BIT(x
, pkt
->length
);
222 ret
= (sftp_senddata(x
, 4) && sftp_senddata(pkt
->data
, pkt
->length
));
226 struct sftp_packet
*sftp_recv(void)
228 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 pkt
->type
= sftp_pkt_getbyte(pkt
);
249 /* ----------------------------------------------------------------------
250 * Request ID allocation and temporary dispatch routines.
253 #define REQUEST_ID_OFFSET 256
255 struct sftp_request
{
260 static int sftp_reqcmp(void *av
, void *bv
)
262 struct sftp_request
*a
= (struct sftp_request
*)av
;
263 struct sftp_request
*b
= (struct sftp_request
*)bv
;
270 static int sftp_reqfind(void *av
, void *bv
)
272 unsigned *a
= (unsigned *) av
;
273 struct sftp_request
*b
= (struct sftp_request
*)bv
;
281 static tree234
*sftp_requests
;
283 static struct sftp_request
*sftp_alloc_request(void)
285 const unsigned CHANNEL_NUMBER_OFFSET
= 256;
286 unsigned low
, high
, mid
;
288 struct sftp_request
*r
;
290 if (sftp_requests
== NULL
)
291 sftp_requests
= newtree234(sftp_reqcmp
);
294 * First-fit allocation of request IDs: always pick the lowest
295 * unused one. To do this, binary-search using the counted
296 * B-tree to find the largest ID which is in a contiguous
297 * sequence from the beginning. (Precisely everything in that
298 * sequence must have ID equal to its tree index plus
299 * SEQUENCE_NUMBER_OFFSET.)
301 tsize
= count234(sftp_requests
);
305 while (high
- low
> 1) {
306 mid
= (high
+ low
) / 2;
307 r
= index234(sftp_requests
, mid
);
308 if (r
->id
== mid
+ REQUEST_ID_OFFSET
)
309 low
= mid
; /* this one is fine */
311 high
= mid
; /* this one is past it */
314 * Now low points to either -1, or the tree index of the
315 * largest ID in the initial sequence.
318 unsigned i
= low
+ 1 + REQUEST_ID_OFFSET
;
319 assert(NULL
== find234(sftp_requests
, &i
, sftp_reqfind
));
323 * So the request ID we need to create is
324 * low + 1 + REQUEST_ID_OFFSET.
326 r
= snew(struct sftp_request
);
327 r
->id
= low
+ 1 + REQUEST_ID_OFFSET
;
329 add234(sftp_requests
, r
);
333 void sftp_register(struct sftp_request
*req
)
338 struct sftp_request
*sftp_find_request(struct sftp_packet
*pktin
)
341 struct sftp_request
*req
;
344 fxp_internal_error("did not receive a valid SFTP packet\n");
348 id
= sftp_pkt_getuint32(pktin
);
349 req
= find234(sftp_requests
, &id
, sftp_reqfind
);
351 if (!req
|| !req
->registered
) {
352 fxp_internal_error("request ID mismatch\n");
353 sftp_pkt_free(pktin
);
360 /* ----------------------------------------------------------------------
361 * String handling routines.
364 static char *mkstr(char *s
, int len
)
366 char *p
= snewn(len
+ 1, char);
372 /* ----------------------------------------------------------------------
377 * Deal with (and free) an FXP_STATUS packet. Return 1 if
378 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
379 * Also place the status into fxp_errtype.
381 static int fxp_got_status(struct sftp_packet
*pktin
)
383 static const char *const messages
[] = {
384 /* SSH_FX_OK. The only time we will display a _message_ for this
385 * is if we were expecting something other than FXP_STATUS on
386 * success, so this is actually an error message! */
387 "unexpected OK response",
389 "no such file or directory",
395 "operation unsupported",
398 if (pktin
->type
!= SSH_FXP_STATUS
) {
399 fxp_error_message
= "expected FXP_STATUS packet";
402 fxp_errtype
= sftp_pkt_getuint32(pktin
);
403 if (fxp_errtype
< 0 ||
404 fxp_errtype
>= sizeof(messages
) / sizeof(*messages
))
405 fxp_error_message
= "unknown error code";
407 fxp_error_message
= messages
[fxp_errtype
];
410 if (fxp_errtype
== SSH_FX_OK
)
412 else if (fxp_errtype
== SSH_FX_EOF
)
418 static void fxp_internal_error(char *msg
)
420 fxp_error_message
= msg
;
424 const char *fxp_error(void)
426 return fxp_error_message
;
429 int fxp_error_type(void)
435 * Perform exchange of init/version packets. Return 0 on failure.
439 struct sftp_packet
*pktout
, *pktin
;
442 pktout
= sftp_pkt_init(SSH_FXP_INIT
);
443 sftp_pkt_adduint32(pktout
, SFTP_PROTO_VERSION
);
448 fxp_internal_error("could not connect");
451 if (pktin
->type
!= SSH_FXP_VERSION
) {
452 fxp_internal_error("did not receive FXP_VERSION");
453 sftp_pkt_free(pktin
);
456 remotever
= sftp_pkt_getuint32(pktin
);
457 if (remotever
> SFTP_PROTO_VERSION
) {
459 ("remote protocol is more advanced than we support");
460 sftp_pkt_free(pktin
);
464 * In principle, this packet might also contain extension-
465 * string pairs. We should work through them and look for any
466 * we recognise. In practice we don't currently do so because
467 * we know we don't recognise _any_.
469 sftp_pkt_free(pktin
);
475 * Canonify a pathname.
477 struct sftp_request
*fxp_realpath_send(char *path
)
479 struct sftp_request
*req
= sftp_alloc_request();
480 struct sftp_packet
*pktout
;
482 pktout
= sftp_pkt_init(SSH_FXP_REALPATH
);
483 sftp_pkt_adduint32(pktout
, req
->id
);
484 sftp_pkt_addstring_start(pktout
);
485 sftp_pkt_addstring_str(pktout
, path
);
491 char *fxp_realpath_recv(struct sftp_packet
*pktin
)
493 if (pktin
->type
== SSH_FXP_NAME
) {
498 count
= sftp_pkt_getuint32(pktin
);
500 fxp_internal_error("REALPATH returned name count != 1\n");
501 sftp_pkt_free(pktin
);
504 sftp_pkt_getstring(pktin
, &path
, &len
);
506 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
507 sftp_pkt_free(pktin
);
510 path
= mkstr(path
, len
);
511 sftp_pkt_free(pktin
);
514 fxp_got_status(pktin
);
515 sftp_pkt_free(pktin
);
523 struct sftp_request
*fxp_open_send(char *path
, int type
)
525 struct sftp_request
*req
= sftp_alloc_request();
526 struct sftp_packet
*pktout
;
528 pktout
= sftp_pkt_init(SSH_FXP_OPEN
);
529 sftp_pkt_adduint32(pktout
, req
->id
);
530 sftp_pkt_addstring(pktout
, path
);
531 sftp_pkt_adduint32(pktout
, type
);
532 sftp_pkt_adduint32(pktout
, 0); /* (FIXME) empty ATTRS structure */
538 struct fxp_handle
*fxp_open_recv(struct sftp_packet
*pktin
)
540 if (pktin
->type
== SSH_FXP_HANDLE
) {
542 struct fxp_handle
*handle
;
545 sftp_pkt_getstring(pktin
, &hstring
, &len
);
547 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
548 sftp_pkt_free(pktin
);
551 handle
= snew(struct fxp_handle
);
552 handle
->hstring
= mkstr(hstring
, len
);
554 sftp_pkt_free(pktin
);
557 fxp_got_status(pktin
);
558 sftp_pkt_free(pktin
);
566 struct sftp_request
*fxp_opendir_send(char *path
)
568 struct sftp_request
*req
= sftp_alloc_request();
569 struct sftp_packet
*pktout
;
571 pktout
= sftp_pkt_init(SSH_FXP_OPENDIR
);
572 sftp_pkt_adduint32(pktout
, req
->id
);
573 sftp_pkt_addstring(pktout
, path
);
579 struct fxp_handle
*fxp_opendir_recv(struct sftp_packet
*pktin
)
581 if (pktin
->type
== SSH_FXP_HANDLE
) {
583 struct fxp_handle
*handle
;
586 sftp_pkt_getstring(pktin
, &hstring
, &len
);
588 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
589 sftp_pkt_free(pktin
);
592 handle
= snew(struct fxp_handle
);
593 handle
->hstring
= mkstr(hstring
, len
);
595 sftp_pkt_free(pktin
);
598 fxp_got_status(pktin
);
599 sftp_pkt_free(pktin
);
607 struct sftp_request
*fxp_close_send(struct fxp_handle
*handle
)
609 struct sftp_request
*req
= sftp_alloc_request();
610 struct sftp_packet
*pktout
;
612 pktout
= sftp_pkt_init(SSH_FXP_CLOSE
);
613 sftp_pkt_adduint32(pktout
, req
->id
);
614 sftp_pkt_addstring_start(pktout
);
615 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
618 sfree(handle
->hstring
);
624 void fxp_close_recv(struct sftp_packet
*pktin
)
626 fxp_got_status(pktin
);
627 sftp_pkt_free(pktin
);
630 struct sftp_request
*fxp_mkdir_send(char *path
)
632 struct sftp_request
*req
= sftp_alloc_request();
633 struct sftp_packet
*pktout
;
635 pktout
= sftp_pkt_init(SSH_FXP_MKDIR
);
636 sftp_pkt_adduint32(pktout
, req
->id
);
637 sftp_pkt_addstring(pktout
, path
);
638 sftp_pkt_adduint32(pktout
, 0); /* (FIXME) empty ATTRS structure */
644 int fxp_mkdir_recv(struct sftp_packet
*pktin
)
646 int id
= fxp_got_status(pktin
);
647 sftp_pkt_free(pktin
);
654 struct sftp_request
*fxp_rmdir_send(char *path
)
656 struct sftp_request
*req
= sftp_alloc_request();
657 struct sftp_packet
*pktout
;
659 pktout
= sftp_pkt_init(SSH_FXP_RMDIR
);
660 sftp_pkt_adduint32(pktout
, req
->id
);
661 sftp_pkt_addstring(pktout
, path
);
667 int fxp_rmdir_recv(struct sftp_packet
*pktin
)
669 int id
= fxp_got_status(pktin
);
670 sftp_pkt_free(pktin
);
677 struct sftp_request
*fxp_remove_send(char *fname
)
679 struct sftp_request
*req
= sftp_alloc_request();
680 struct sftp_packet
*pktout
;
682 pktout
= sftp_pkt_init(SSH_FXP_REMOVE
);
683 sftp_pkt_adduint32(pktout
, req
->id
);
684 sftp_pkt_addstring(pktout
, fname
);
690 int fxp_remove_recv(struct sftp_packet
*pktin
)
692 int id
= fxp_got_status(pktin
);
693 sftp_pkt_free(pktin
);
700 struct sftp_request
*fxp_rename_send(char *srcfname
, char *dstfname
)
702 struct sftp_request
*req
= sftp_alloc_request();
703 struct sftp_packet
*pktout
;
705 pktout
= sftp_pkt_init(SSH_FXP_RENAME
);
706 sftp_pkt_adduint32(pktout
, req
->id
);
707 sftp_pkt_addstring(pktout
, srcfname
);
708 sftp_pkt_addstring(pktout
, dstfname
);
714 int fxp_rename_recv(struct sftp_packet
*pktin
)
716 int id
= fxp_got_status(pktin
);
717 sftp_pkt_free(pktin
);
725 * Retrieve the attributes of a file. We have fxp_stat which works
726 * on filenames, and fxp_fstat which works on open file handles.
728 struct sftp_request
*fxp_stat_send(char *fname
)
730 struct sftp_request
*req
= sftp_alloc_request();
731 struct sftp_packet
*pktout
;
733 pktout
= sftp_pkt_init(SSH_FXP_STAT
);
734 sftp_pkt_adduint32(pktout
, req
->id
);
735 sftp_pkt_addstring(pktout
, fname
);
741 int fxp_stat_recv(struct sftp_packet
*pktin
, struct fxp_attrs
*attrs
)
743 if (pktin
->type
== SSH_FXP_ATTRS
) {
744 *attrs
= sftp_pkt_getattrs(pktin
);
745 sftp_pkt_free(pktin
);
748 fxp_got_status(pktin
);
749 sftp_pkt_free(pktin
);
754 struct sftp_request
*fxp_fstat_send(struct fxp_handle
*handle
)
756 struct sftp_request
*req
= sftp_alloc_request();
757 struct sftp_packet
*pktout
;
759 pktout
= sftp_pkt_init(SSH_FXP_FSTAT
);
760 sftp_pkt_adduint32(pktout
, req
->id
);
761 sftp_pkt_addstring_start(pktout
);
762 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
768 int fxp_fstat_recv(struct sftp_packet
*pktin
,
769 struct fxp_attrs
*attrs
)
771 if (pktin
->type
== SSH_FXP_ATTRS
) {
772 *attrs
= sftp_pkt_getattrs(pktin
);
773 sftp_pkt_free(pktin
);
776 fxp_got_status(pktin
);
777 sftp_pkt_free(pktin
);
783 * Set the attributes of a file.
785 struct sftp_request
*fxp_setstat_send(char *fname
, struct fxp_attrs attrs
)
787 struct sftp_request
*req
= sftp_alloc_request();
788 struct sftp_packet
*pktout
;
790 pktout
= sftp_pkt_init(SSH_FXP_SETSTAT
);
791 sftp_pkt_adduint32(pktout
, req
->id
);
792 sftp_pkt_addstring(pktout
, fname
);
793 sftp_pkt_addattrs(pktout
, attrs
);
799 int fxp_setstat_recv(struct sftp_packet
*pktin
)
801 int id
= fxp_got_status(pktin
);
802 sftp_pkt_free(pktin
);
809 struct sftp_request
*fxp_fsetstat_send(struct fxp_handle
*handle
,
810 struct fxp_attrs attrs
)
812 struct sftp_request
*req
= sftp_alloc_request();
813 struct sftp_packet
*pktout
;
815 pktout
= sftp_pkt_init(SSH_FXP_FSETSTAT
);
816 sftp_pkt_adduint32(pktout
, req
->id
);
817 sftp_pkt_addstring_start(pktout
);
818 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
819 sftp_pkt_addattrs(pktout
, attrs
);
825 int fxp_fsetstat_recv(struct sftp_packet
*pktin
)
827 int id
= fxp_got_status(pktin
);
828 sftp_pkt_free(pktin
);
836 * Read from a file. Returns the number of bytes read, or -1 on an
837 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
838 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
839 * error indicator. It might even depend on the SFTP server.)
841 struct sftp_request
*fxp_read_send(struct fxp_handle
*handle
,
842 uint64 offset
, int len
)
844 struct sftp_request
*req
= sftp_alloc_request();
845 struct sftp_packet
*pktout
;
847 pktout
= sftp_pkt_init(SSH_FXP_READ
);
848 sftp_pkt_adduint32(pktout
, req
->id
);
849 sftp_pkt_addstring_start(pktout
);
850 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
851 sftp_pkt_adduint64(pktout
, offset
);
852 sftp_pkt_adduint32(pktout
, len
);
858 int fxp_read_recv(struct sftp_packet
*pktin
, char *buffer
, int len
)
860 if (pktin
->type
== SSH_FXP_DATA
) {
864 sftp_pkt_getstring(pktin
, &str
, &rlen
);
866 if (rlen
> len
|| rlen
< 0) {
867 fxp_internal_error("READ returned more bytes than requested");
868 sftp_pkt_free(pktin
);
872 memcpy(buffer
, str
, rlen
);
873 sftp_pkt_free(pktin
);
876 fxp_got_status(pktin
);
877 sftp_pkt_free(pktin
);
883 * Read from a directory.
885 struct sftp_request
*fxp_readdir_send(struct fxp_handle
*handle
)
887 struct sftp_request
*req
= sftp_alloc_request();
888 struct sftp_packet
*pktout
;
890 pktout
= sftp_pkt_init(SSH_FXP_READDIR
);
891 sftp_pkt_adduint32(pktout
, req
->id
);
892 sftp_pkt_addstring_start(pktout
);
893 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
899 struct fxp_names
*fxp_readdir_recv(struct sftp_packet
*pktin
)
901 if (pktin
->type
== SSH_FXP_NAME
) {
902 struct fxp_names
*ret
;
904 ret
= snew(struct fxp_names
);
905 ret
->nnames
= sftp_pkt_getuint32(pktin
);
906 ret
->names
= snewn(ret
->nnames
, struct fxp_name
);
907 for (i
= 0; i
< ret
->nnames
; i
++) {
910 sftp_pkt_getstring(pktin
, &str
, &len
);
911 ret
->names
[i
].filename
= mkstr(str
, len
);
912 sftp_pkt_getstring(pktin
, &str
, &len
);
913 ret
->names
[i
].longname
= mkstr(str
, len
);
914 ret
->names
[i
].attrs
= sftp_pkt_getattrs(pktin
);
916 sftp_pkt_free(pktin
);
919 fxp_got_status(pktin
);
920 sftp_pkt_free(pktin
);
926 * Write to a file. Returns 0 on error, 1 on OK.
928 struct sftp_request
*fxp_write_send(struct fxp_handle
*handle
,
929 char *buffer
, uint64 offset
, int len
)
931 struct sftp_request
*req
= sftp_alloc_request();
932 struct sftp_packet
*pktout
;
934 pktout
= sftp_pkt_init(SSH_FXP_WRITE
);
935 sftp_pkt_adduint32(pktout
, req
->id
);
936 sftp_pkt_addstring_start(pktout
);
937 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
938 sftp_pkt_adduint64(pktout
, offset
);
939 sftp_pkt_addstring_start(pktout
);
940 sftp_pkt_addstring_data(pktout
, buffer
, len
);
946 int fxp_write_recv(struct sftp_packet
*pktin
)
948 fxp_got_status(pktin
);
949 sftp_pkt_free(pktin
);
950 return fxp_errtype
== SSH_FX_OK
;
954 * Free up an fxp_names structure.
956 void fxp_free_names(struct fxp_names
*names
)
960 for (i
= 0; i
< names
->nnames
; i
++) {
961 sfree(names
->names
[i
].filename
);
962 sfree(names
->names
[i
].longname
);
969 * Duplicate an fxp_name structure.
971 struct fxp_name
*fxp_dup_name(struct fxp_name
*name
)
973 struct fxp_name
*ret
;
974 ret
= snew(struct fxp_name
);
975 ret
->filename
= dupstr(name
->filename
);
976 ret
->longname
= dupstr(name
->longname
);
977 ret
->attrs
= name
->attrs
; /* structure copy */
982 * Free up an fxp_name structure.
984 void fxp_free_name(struct fxp_name
*name
)
986 sfree(name
->filename
);
987 sfree(name
->longname
);