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
);
71 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
73 sftp_pkt_adddata(pkt, &value, 1);
76 static void sftp_pkt_adduint32(struct sftp_packet
*pkt
,
81 sftp_pkt_adddata(pkt
, x
, 4);
83 static void sftp_pkt_adduint64(struct sftp_packet
*pkt
, uint64 value
)
86 PUT_32BIT(x
, value
.hi
);
87 PUT_32BIT(x
+ 4, value
.lo
);
88 sftp_pkt_adddata(pkt
, x
, 8);
90 static void sftp_pkt_addstring_start(struct sftp_packet
*pkt
)
92 sftp_pkt_adduint32(pkt
, 0);
93 pkt
->savedpos
= pkt
->length
;
95 static void sftp_pkt_addstring_str(struct sftp_packet
*pkt
, char *data
)
97 sftp_pkt_adddata(pkt
, data
, strlen(data
));
98 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
100 static void sftp_pkt_addstring_data(struct sftp_packet
*pkt
,
103 sftp_pkt_adddata(pkt
, data
, len
);
104 PUT_32BIT(pkt
->data
+ pkt
->savedpos
- 4, pkt
->length
- pkt
->savedpos
);
106 static void sftp_pkt_addstring(struct sftp_packet
*pkt
, char *data
)
108 sftp_pkt_addstring_start(pkt
);
109 sftp_pkt_addstring_str(pkt
, data
);
111 static void sftp_pkt_addattrs(struct sftp_packet
*pkt
, struct fxp_attrs attrs
)
113 sftp_pkt_adduint32(pkt
, attrs
.flags
);
114 if (attrs
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
115 sftp_pkt_adduint32(pkt
, attrs
.size
.hi
);
116 sftp_pkt_adduint32(pkt
, attrs
.size
.lo
);
118 if (attrs
.flags
& SSH_FILEXFER_ATTR_UIDGID
) {
119 sftp_pkt_adduint32(pkt
, attrs
.uid
);
120 sftp_pkt_adduint32(pkt
, attrs
.gid
);
122 if (attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) {
123 sftp_pkt_adduint32(pkt
, attrs
.permissions
);
125 if (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
) {
126 sftp_pkt_adduint32(pkt
, attrs
.atime
);
127 sftp_pkt_adduint32(pkt
, attrs
.mtime
);
129 if (attrs
.flags
& SSH_FILEXFER_ATTR_EXTENDED
) {
131 * We currently don't support sending any extended
137 /* ----------------------------------------------------------------------
138 * SFTP packet decode functions.
141 static unsigned char sftp_pkt_getbyte(struct sftp_packet
*pkt
)
144 if (pkt
->length
- pkt
->savedpos
< 1)
145 return 0; /* arrgh, no way to decline (FIXME?) */
146 value
= (unsigned char) pkt
->data
[pkt
->savedpos
];
150 static unsigned long sftp_pkt_getuint32(struct sftp_packet
*pkt
)
153 if (pkt
->length
- pkt
->savedpos
< 4)
154 return 0; /* arrgh, no way to decline (FIXME?) */
155 value
= GET_32BIT(pkt
->data
+ pkt
->savedpos
);
159 static void sftp_pkt_getstring(struct sftp_packet
*pkt
,
160 char **p
, int *length
)
163 if (pkt
->length
- pkt
->savedpos
< 4)
165 *length
= GET_32BIT(pkt
->data
+ pkt
->savedpos
);
167 if (pkt
->length
- pkt
->savedpos
< *length
)
169 *p
= pkt
->data
+ pkt
->savedpos
;
170 pkt
->savedpos
+= *length
;
172 static struct fxp_attrs
sftp_pkt_getattrs(struct sftp_packet
*pkt
)
174 struct fxp_attrs ret
;
175 ret
.flags
= sftp_pkt_getuint32(pkt
);
176 if (ret
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
177 unsigned long hi
, lo
;
178 hi
= sftp_pkt_getuint32(pkt
);
179 lo
= sftp_pkt_getuint32(pkt
);
180 ret
.size
= uint64_make(hi
, lo
);
182 if (ret
.flags
& SSH_FILEXFER_ATTR_UIDGID
) {
183 ret
.uid
= sftp_pkt_getuint32(pkt
);
184 ret
.gid
= sftp_pkt_getuint32(pkt
);
186 if (ret
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) {
187 ret
.permissions
= sftp_pkt_getuint32(pkt
);
189 if (ret
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
) {
190 ret
.atime
= sftp_pkt_getuint32(pkt
);
191 ret
.mtime
= sftp_pkt_getuint32(pkt
);
193 if (ret
.flags
& SSH_FILEXFER_ATTR_EXTENDED
) {
195 count
= sftp_pkt_getuint32(pkt
);
200 * We should try to analyse these, if we ever find one
203 sftp_pkt_getstring(pkt
, &str
, &len
);
204 sftp_pkt_getstring(pkt
, &str
, &len
);
209 static void sftp_pkt_free(struct sftp_packet
*pkt
)
216 /* ----------------------------------------------------------------------
217 * Send and receive packet functions.
219 int sftp_send(struct sftp_packet
*pkt
)
223 PUT_32BIT(x
, pkt
->length
);
224 ret
= (sftp_senddata(x
, 4) && sftp_senddata(pkt
->data
, pkt
->length
));
228 struct sftp_packet
*sftp_recv(void)
230 struct sftp_packet
*pkt
;
233 if (!sftp_recvdata(x
, 4))
236 pkt
= snew(struct sftp_packet
);
238 pkt
->length
= pkt
->maxlen
= GET_32BIT(x
);
239 pkt
->data
= snewn(pkt
->length
, char);
241 if (!sftp_recvdata(pkt
->data
, pkt
->length
)) {
246 pkt
->type
= sftp_pkt_getbyte(pkt
);
251 /* ----------------------------------------------------------------------
252 * Request ID allocation and temporary dispatch routines.
255 #define REQUEST_ID_OFFSET 256
257 struct sftp_request
{
262 static int sftp_reqcmp(void *av
, void *bv
)
264 struct sftp_request
*a
= (struct sftp_request
*)av
;
265 struct sftp_request
*b
= (struct sftp_request
*)bv
;
272 static int sftp_reqfind(void *av
, void *bv
)
274 unsigned *a
= (unsigned *) av
;
275 struct sftp_request
*b
= (struct sftp_request
*)bv
;
283 static tree234
*sftp_requests
;
285 static struct sftp_request
*sftp_alloc_request(void)
287 unsigned low
, high
, mid
;
289 struct sftp_request
*r
;
291 if (sftp_requests
== NULL
)
292 sftp_requests
= newtree234(sftp_reqcmp
);
295 * First-fit allocation of request IDs: always pick the lowest
296 * unused one. To do this, binary-search using the counted
297 * B-tree to find the largest ID which is in a contiguous
298 * sequence from the beginning. (Precisely everything in that
299 * sequence must have ID equal to its tree index plus
300 * REQUEST_ID_OFFSET.)
302 tsize
= count234(sftp_requests
);
306 while (high
- low
> 1) {
307 mid
= (high
+ low
) / 2;
308 r
= index234(sftp_requests
, mid
);
309 if (r
->id
== mid
+ REQUEST_ID_OFFSET
)
310 low
= mid
; /* this one is fine */
312 high
= mid
; /* this one is past it */
315 * Now low points to either -1, or the tree index of the
316 * largest ID in the initial sequence.
319 unsigned i
= low
+ 1 + REQUEST_ID_OFFSET
;
320 assert(NULL
== find234(sftp_requests
, &i
, sftp_reqfind
));
324 * So the request ID we need to create is
325 * low + 1 + REQUEST_ID_OFFSET.
327 r
= snew(struct sftp_request
);
328 r
->id
= low
+ 1 + REQUEST_ID_OFFSET
;
330 add234(sftp_requests
, r
);
334 void sftp_register(struct sftp_request
*req
)
339 struct sftp_request
*sftp_find_request(struct sftp_packet
*pktin
)
342 struct sftp_request
*req
;
345 fxp_internal_error("did not receive a valid SFTP packet\n");
349 id
= sftp_pkt_getuint32(pktin
);
350 req
= find234(sftp_requests
, &id
, sftp_reqfind
);
352 if (!req
|| !req
->registered
) {
353 fxp_internal_error("request ID mismatch\n");
354 sftp_pkt_free(pktin
);
358 del234(sftp_requests
, req
);
363 /* ----------------------------------------------------------------------
364 * String handling routines.
367 static char *mkstr(char *s
, int len
)
369 char *p
= snewn(len
+ 1, char);
375 /* ----------------------------------------------------------------------
380 * Deal with (and free) an FXP_STATUS packet. Return 1 if
381 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
382 * Also place the status into fxp_errtype.
384 static int fxp_got_status(struct sftp_packet
*pktin
)
386 static const char *const messages
[] = {
387 /* SSH_FX_OK. The only time we will display a _message_ for this
388 * is if we were expecting something other than FXP_STATUS on
389 * success, so this is actually an error message! */
390 "unexpected OK response",
392 "no such file or directory",
398 "operation unsupported",
401 if (pktin
->type
!= SSH_FXP_STATUS
) {
402 fxp_error_message
= "expected FXP_STATUS packet";
405 fxp_errtype
= sftp_pkt_getuint32(pktin
);
406 if (fxp_errtype
< 0 ||
407 fxp_errtype
>= sizeof(messages
) / sizeof(*messages
))
408 fxp_error_message
= "unknown error code";
410 fxp_error_message
= messages
[fxp_errtype
];
413 if (fxp_errtype
== SSH_FX_OK
)
415 else if (fxp_errtype
== SSH_FX_EOF
)
421 static void fxp_internal_error(char *msg
)
423 fxp_error_message
= msg
;
427 const char *fxp_error(void)
429 return fxp_error_message
;
432 int fxp_error_type(void)
438 * Perform exchange of init/version packets. Return 0 on failure.
442 struct sftp_packet
*pktout
, *pktin
;
445 pktout
= sftp_pkt_init(SSH_FXP_INIT
);
446 sftp_pkt_adduint32(pktout
, SFTP_PROTO_VERSION
);
451 fxp_internal_error("could not connect");
454 if (pktin
->type
!= SSH_FXP_VERSION
) {
455 fxp_internal_error("did not receive FXP_VERSION");
456 sftp_pkt_free(pktin
);
459 remotever
= sftp_pkt_getuint32(pktin
);
460 if (remotever
> SFTP_PROTO_VERSION
) {
462 ("remote protocol is more advanced than we support");
463 sftp_pkt_free(pktin
);
467 * In principle, this packet might also contain extension-
468 * string pairs. We should work through them and look for any
469 * we recognise. In practice we don't currently do so because
470 * we know we don't recognise _any_.
472 sftp_pkt_free(pktin
);
478 * Canonify a pathname.
480 struct sftp_request
*fxp_realpath_send(char *path
)
482 struct sftp_request
*req
= sftp_alloc_request();
483 struct sftp_packet
*pktout
;
485 pktout
= sftp_pkt_init(SSH_FXP_REALPATH
);
486 sftp_pkt_adduint32(pktout
, req
->id
);
487 sftp_pkt_addstring_start(pktout
);
488 sftp_pkt_addstring_str(pktout
, path
);
494 char *fxp_realpath_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
498 if (pktin
->type
== SSH_FXP_NAME
) {
503 count
= sftp_pkt_getuint32(pktin
);
505 fxp_internal_error("REALPATH returned name count != 1\n");
506 sftp_pkt_free(pktin
);
509 sftp_pkt_getstring(pktin
, &path
, &len
);
511 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
512 sftp_pkt_free(pktin
);
515 path
= mkstr(path
, len
);
516 sftp_pkt_free(pktin
);
519 fxp_got_status(pktin
);
520 sftp_pkt_free(pktin
);
528 struct sftp_request
*fxp_open_send(char *path
, int type
)
530 struct sftp_request
*req
= sftp_alloc_request();
531 struct sftp_packet
*pktout
;
533 pktout
= sftp_pkt_init(SSH_FXP_OPEN
);
534 sftp_pkt_adduint32(pktout
, req
->id
);
535 sftp_pkt_addstring(pktout
, path
);
536 sftp_pkt_adduint32(pktout
, type
);
537 sftp_pkt_adduint32(pktout
, 0); /* (FIXME) empty ATTRS structure */
543 struct fxp_handle
*fxp_open_recv(struct sftp_packet
*pktin
,
544 struct sftp_request
*req
)
548 if (pktin
->type
== SSH_FXP_HANDLE
) {
550 struct fxp_handle
*handle
;
553 sftp_pkt_getstring(pktin
, &hstring
, &len
);
555 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
556 sftp_pkt_free(pktin
);
559 handle
= snew(struct fxp_handle
);
560 handle
->hstring
= mkstr(hstring
, len
);
562 sftp_pkt_free(pktin
);
565 fxp_got_status(pktin
);
566 sftp_pkt_free(pktin
);
574 struct sftp_request
*fxp_opendir_send(char *path
)
576 struct sftp_request
*req
= sftp_alloc_request();
577 struct sftp_packet
*pktout
;
579 pktout
= sftp_pkt_init(SSH_FXP_OPENDIR
);
580 sftp_pkt_adduint32(pktout
, req
->id
);
581 sftp_pkt_addstring(pktout
, path
);
587 struct fxp_handle
*fxp_opendir_recv(struct sftp_packet
*pktin
,
588 struct sftp_request
*req
)
591 if (pktin
->type
== SSH_FXP_HANDLE
) {
593 struct fxp_handle
*handle
;
596 sftp_pkt_getstring(pktin
, &hstring
, &len
);
598 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
599 sftp_pkt_free(pktin
);
602 handle
= snew(struct fxp_handle
);
603 handle
->hstring
= mkstr(hstring
, len
);
605 sftp_pkt_free(pktin
);
608 fxp_got_status(pktin
);
609 sftp_pkt_free(pktin
);
617 struct sftp_request
*fxp_close_send(struct fxp_handle
*handle
)
619 struct sftp_request
*req
= sftp_alloc_request();
620 struct sftp_packet
*pktout
;
622 pktout
= sftp_pkt_init(SSH_FXP_CLOSE
);
623 sftp_pkt_adduint32(pktout
, req
->id
);
624 sftp_pkt_addstring_start(pktout
);
625 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
628 sfree(handle
->hstring
);
634 void fxp_close_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
637 fxp_got_status(pktin
);
638 sftp_pkt_free(pktin
);
641 struct sftp_request
*fxp_mkdir_send(char *path
)
643 struct sftp_request
*req
= sftp_alloc_request();
644 struct sftp_packet
*pktout
;
646 pktout
= sftp_pkt_init(SSH_FXP_MKDIR
);
647 sftp_pkt_adduint32(pktout
, req
->id
);
648 sftp_pkt_addstring(pktout
, path
);
649 sftp_pkt_adduint32(pktout
, 0); /* (FIXME) empty ATTRS structure */
655 int fxp_mkdir_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
659 id
= fxp_got_status(pktin
);
660 sftp_pkt_free(pktin
);
667 struct sftp_request
*fxp_rmdir_send(char *path
)
669 struct sftp_request
*req
= sftp_alloc_request();
670 struct sftp_packet
*pktout
;
672 pktout
= sftp_pkt_init(SSH_FXP_RMDIR
);
673 sftp_pkt_adduint32(pktout
, req
->id
);
674 sftp_pkt_addstring(pktout
, path
);
680 int fxp_rmdir_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
684 id
= fxp_got_status(pktin
);
685 sftp_pkt_free(pktin
);
692 struct sftp_request
*fxp_remove_send(char *fname
)
694 struct sftp_request
*req
= sftp_alloc_request();
695 struct sftp_packet
*pktout
;
697 pktout
= sftp_pkt_init(SSH_FXP_REMOVE
);
698 sftp_pkt_adduint32(pktout
, req
->id
);
699 sftp_pkt_addstring(pktout
, fname
);
705 int fxp_remove_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
709 id
= fxp_got_status(pktin
);
710 sftp_pkt_free(pktin
);
717 struct sftp_request
*fxp_rename_send(char *srcfname
, char *dstfname
)
719 struct sftp_request
*req
= sftp_alloc_request();
720 struct sftp_packet
*pktout
;
722 pktout
= sftp_pkt_init(SSH_FXP_RENAME
);
723 sftp_pkt_adduint32(pktout
, req
->id
);
724 sftp_pkt_addstring(pktout
, srcfname
);
725 sftp_pkt_addstring(pktout
, dstfname
);
731 int fxp_rename_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
735 id
= fxp_got_status(pktin
);
736 sftp_pkt_free(pktin
);
744 * Retrieve the attributes of a file. We have fxp_stat which works
745 * on filenames, and fxp_fstat which works on open file handles.
747 struct sftp_request
*fxp_stat_send(char *fname
)
749 struct sftp_request
*req
= sftp_alloc_request();
750 struct sftp_packet
*pktout
;
752 pktout
= sftp_pkt_init(SSH_FXP_STAT
);
753 sftp_pkt_adduint32(pktout
, req
->id
);
754 sftp_pkt_addstring(pktout
, fname
);
760 int fxp_stat_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
,
761 struct fxp_attrs
*attrs
)
764 if (pktin
->type
== SSH_FXP_ATTRS
) {
765 *attrs
= sftp_pkt_getattrs(pktin
);
766 sftp_pkt_free(pktin
);
769 fxp_got_status(pktin
);
770 sftp_pkt_free(pktin
);
775 struct sftp_request
*fxp_fstat_send(struct fxp_handle
*handle
)
777 struct sftp_request
*req
= sftp_alloc_request();
778 struct sftp_packet
*pktout
;
780 pktout
= sftp_pkt_init(SSH_FXP_FSTAT
);
781 sftp_pkt_adduint32(pktout
, req
->id
);
782 sftp_pkt_addstring_start(pktout
);
783 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
789 int fxp_fstat_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
,
790 struct fxp_attrs
*attrs
)
793 if (pktin
->type
== SSH_FXP_ATTRS
) {
794 *attrs
= sftp_pkt_getattrs(pktin
);
795 sftp_pkt_free(pktin
);
798 fxp_got_status(pktin
);
799 sftp_pkt_free(pktin
);
805 * Set the attributes of a file.
807 struct sftp_request
*fxp_setstat_send(char *fname
, struct fxp_attrs attrs
)
809 struct sftp_request
*req
= sftp_alloc_request();
810 struct sftp_packet
*pktout
;
812 pktout
= sftp_pkt_init(SSH_FXP_SETSTAT
);
813 sftp_pkt_adduint32(pktout
, req
->id
);
814 sftp_pkt_addstring(pktout
, fname
);
815 sftp_pkt_addattrs(pktout
, attrs
);
821 int fxp_setstat_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
825 id
= fxp_got_status(pktin
);
826 sftp_pkt_free(pktin
);
833 struct sftp_request
*fxp_fsetstat_send(struct fxp_handle
*handle
,
834 struct fxp_attrs attrs
)
836 struct sftp_request
*req
= sftp_alloc_request();
837 struct sftp_packet
*pktout
;
839 pktout
= sftp_pkt_init(SSH_FXP_FSETSTAT
);
840 sftp_pkt_adduint32(pktout
, req
->id
);
841 sftp_pkt_addstring_start(pktout
);
842 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
843 sftp_pkt_addattrs(pktout
, attrs
);
849 int fxp_fsetstat_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
853 id
= fxp_got_status(pktin
);
854 sftp_pkt_free(pktin
);
862 * Read from a file. Returns the number of bytes read, or -1 on an
863 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
864 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
865 * error indicator. It might even depend on the SFTP server.)
867 struct sftp_request
*fxp_read_send(struct fxp_handle
*handle
,
868 uint64 offset
, int len
)
870 struct sftp_request
*req
= sftp_alloc_request();
871 struct sftp_packet
*pktout
;
873 pktout
= sftp_pkt_init(SSH_FXP_READ
);
874 sftp_pkt_adduint32(pktout
, req
->id
);
875 sftp_pkt_addstring_start(pktout
);
876 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
877 sftp_pkt_adduint64(pktout
, offset
);
878 sftp_pkt_adduint32(pktout
, len
);
884 int fxp_read_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
,
885 char *buffer
, int len
)
888 if (pktin
->type
== SSH_FXP_DATA
) {
892 sftp_pkt_getstring(pktin
, &str
, &rlen
);
894 if (rlen
> len
|| rlen
< 0) {
895 fxp_internal_error("READ returned more bytes than requested");
896 sftp_pkt_free(pktin
);
900 memcpy(buffer
, str
, rlen
);
901 sftp_pkt_free(pktin
);
904 fxp_got_status(pktin
);
905 sftp_pkt_free(pktin
);
911 * Read from a directory.
913 struct sftp_request
*fxp_readdir_send(struct fxp_handle
*handle
)
915 struct sftp_request
*req
= sftp_alloc_request();
916 struct sftp_packet
*pktout
;
918 pktout
= sftp_pkt_init(SSH_FXP_READDIR
);
919 sftp_pkt_adduint32(pktout
, req
->id
);
920 sftp_pkt_addstring_start(pktout
);
921 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
927 struct fxp_names
*fxp_readdir_recv(struct sftp_packet
*pktin
,
928 struct sftp_request
*req
)
931 if (pktin
->type
== SSH_FXP_NAME
) {
932 struct fxp_names
*ret
;
934 ret
= snew(struct fxp_names
);
935 ret
->nnames
= sftp_pkt_getuint32(pktin
);
936 ret
->names
= snewn(ret
->nnames
, struct fxp_name
);
937 for (i
= 0; i
< ret
->nnames
; i
++) {
940 sftp_pkt_getstring(pktin
, &str
, &len
);
941 ret
->names
[i
].filename
= mkstr(str
, len
);
942 sftp_pkt_getstring(pktin
, &str
, &len
);
943 ret
->names
[i
].longname
= mkstr(str
, len
);
944 ret
->names
[i
].attrs
= sftp_pkt_getattrs(pktin
);
946 sftp_pkt_free(pktin
);
949 fxp_got_status(pktin
);
950 sftp_pkt_free(pktin
);
956 * Write to a file. Returns 0 on error, 1 on OK.
958 struct sftp_request
*fxp_write_send(struct fxp_handle
*handle
,
959 char *buffer
, uint64 offset
, int len
)
961 struct sftp_request
*req
= sftp_alloc_request();
962 struct sftp_packet
*pktout
;
964 pktout
= sftp_pkt_init(SSH_FXP_WRITE
);
965 sftp_pkt_adduint32(pktout
, req
->id
);
966 sftp_pkt_addstring_start(pktout
);
967 sftp_pkt_addstring_data(pktout
, handle
->hstring
, handle
->hlen
);
968 sftp_pkt_adduint64(pktout
, offset
);
969 sftp_pkt_addstring_start(pktout
);
970 sftp_pkt_addstring_data(pktout
, buffer
, len
);
976 int fxp_write_recv(struct sftp_packet
*pktin
, struct sftp_request
*req
)
979 fxp_got_status(pktin
);
980 sftp_pkt_free(pktin
);
981 return fxp_errtype
== SSH_FX_OK
;
985 * Free up an fxp_names structure.
987 void fxp_free_names(struct fxp_names
*names
)
991 for (i
= 0; i
< names
->nnames
; i
++) {
992 sfree(names
->names
[i
].filename
);
993 sfree(names
->names
[i
].longname
);
1000 * Duplicate an fxp_name structure.
1002 struct fxp_name
*fxp_dup_name(struct fxp_name
*name
)
1004 struct fxp_name
*ret
;
1005 ret
= snew(struct fxp_name
);
1006 ret
->filename
= dupstr(name
->filename
);
1007 ret
->longname
= dupstr(name
->longname
);
1008 ret
->attrs
= name
->attrs
; /* structure copy */
1013 * Free up an fxp_name structure.
1015 void fxp_free_name(struct fxp_name
*name
)
1017 sfree(name
->filename
);
1018 sfree(name
->longname
);