1 /* $OpenBSD: sftp-client.c,v 1.65 2006/04/16 00:54:10 djm Exp $ */
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* XXX: signed vs unsigned */
20 /* XXX: remove all logging, only return status codes */
21 /* XXX: copy between two remote sites */
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
31 #include "openbsd-compat/sys-queue.h"
38 #include "progressmeter.h"
42 #include "sftp-common.h"
43 #include "sftp-client.h"
45 extern volatile sig_atomic_t interrupted
;
46 extern int showprogress
;
48 /* Minimum amount of data to read at a time */
49 #define MIN_READ_SIZE 512
54 u_int transfer_buflen
;
61 send_msg(int fd
, Buffer
*m
)
66 if (buffer_len(m
) > SFTP_MAX_MSG_LENGTH
)
67 fatal("Outbound message too long %u", buffer_len(m
));
69 /* Send length first */
70 put_u32(mlen
, buffer_len(m
));
71 iov
[0].iov_base
= mlen
;
72 iov
[0].iov_len
= sizeof(mlen
);
73 iov
[1].iov_base
= buffer_ptr(m
);
74 iov
[1].iov_len
= buffer_len(m
);
76 if (atomiciov(writev
, fd
, iov
, 2) != buffer_len(m
) + sizeof(mlen
))
77 fatal("Couldn't send packet: %s", strerror(errno
));
83 get_msg(int fd
, Buffer
*m
)
87 buffer_append_space(m
, 4);
88 if (atomicio(read
, fd
, buffer_ptr(m
), 4) != 4) {
90 fatal("Connection closed");
92 fatal("Couldn't read packet: %s", strerror(errno
));
95 msg_len
= buffer_get_int(m
);
96 if (msg_len
> SFTP_MAX_MSG_LENGTH
)
97 fatal("Received message too long %u", msg_len
);
99 buffer_append_space(m
, msg_len
);
100 if (atomicio(read
, fd
, buffer_ptr(m
), msg_len
) != msg_len
) {
102 fatal("Connection closed");
104 fatal("Read packet: %s", strerror(errno
));
109 send_string_request(int fd
, u_int id
, u_int code
, char *s
,
115 buffer_put_char(&msg
, code
);
116 buffer_put_int(&msg
, id
);
117 buffer_put_string(&msg
, s
, len
);
119 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
124 send_string_attrs_request(int fd
, u_int id
, u_int code
, char *s
,
125 u_int len
, Attrib
*a
)
130 buffer_put_char(&msg
, code
);
131 buffer_put_int(&msg
, id
);
132 buffer_put_string(&msg
, s
, len
);
133 encode_attrib(&msg
, a
);
135 debug3("Sent message fd %d T:%u I:%u", fd
, code
, id
);
140 get_status(int fd
, u_int expected_id
)
143 u_int type
, id
, status
;
147 type
= buffer_get_char(&msg
);
148 id
= buffer_get_int(&msg
);
150 if (id
!= expected_id
)
151 fatal("ID mismatch (%u != %u)", id
, expected_id
);
152 if (type
!= SSH2_FXP_STATUS
)
153 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
154 SSH2_FXP_STATUS
, type
);
156 status
= buffer_get_int(&msg
);
159 debug3("SSH2_FXP_STATUS %u", status
);
165 get_handle(int fd
, u_int expected_id
, u_int
*len
)
173 type
= buffer_get_char(&msg
);
174 id
= buffer_get_int(&msg
);
176 if (id
!= expected_id
)
177 fatal("ID mismatch (%u != %u)", id
, expected_id
);
178 if (type
== SSH2_FXP_STATUS
) {
179 int status
= buffer_get_int(&msg
);
181 error("Couldn't get handle: %s", fx2txt(status
));
184 } else if (type
!= SSH2_FXP_HANDLE
)
185 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
186 SSH2_FXP_HANDLE
, type
);
188 handle
= buffer_get_string(&msg
, len
);
195 get_decode_stat(int fd
, u_int expected_id
, int quiet
)
204 type
= buffer_get_char(&msg
);
205 id
= buffer_get_int(&msg
);
207 debug3("Received stat reply T:%u I:%u", type
, id
);
208 if (id
!= expected_id
)
209 fatal("ID mismatch (%u != %u)", id
, expected_id
);
210 if (type
== SSH2_FXP_STATUS
) {
211 int status
= buffer_get_int(&msg
);
214 debug("Couldn't stat remote file: %s", fx2txt(status
));
216 error("Couldn't stat remote file: %s", fx2txt(status
));
219 } else if (type
!= SSH2_FXP_ATTRS
) {
220 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
221 SSH2_FXP_ATTRS
, type
);
223 a
= decode_attrib(&msg
);
230 do_init(int fd_in
, int fd_out
, u_int transfer_buflen
, u_int num_requests
)
235 struct sftp_conn
*ret
;
238 buffer_put_char(&msg
, SSH2_FXP_INIT
);
239 buffer_put_int(&msg
, SSH2_FILEXFER_VERSION
);
240 send_msg(fd_out
, &msg
);
244 get_msg(fd_in
, &msg
);
246 /* Expecting a VERSION reply */
247 if ((type
= buffer_get_char(&msg
)) != SSH2_FXP_VERSION
) {
248 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
253 version
= buffer_get_int(&msg
);
255 debug2("Remote version: %d", version
);
257 /* Check for extensions */
258 while (buffer_len(&msg
) > 0) {
259 char *name
= buffer_get_string(&msg
, NULL
);
260 char *value
= buffer_get_string(&msg
, NULL
);
262 debug2("Init extension: \"%s\"", name
);
269 ret
= xmalloc(sizeof(*ret
));
271 ret
->fd_out
= fd_out
;
272 ret
->transfer_buflen
= transfer_buflen
;
273 ret
->num_requests
= num_requests
;
274 ret
->version
= version
;
277 /* Some filexfer v.0 servers don't support large packets */
279 ret
->transfer_buflen
= MIN(ret
->transfer_buflen
, 20480);
285 sftp_proto_version(struct sftp_conn
*conn
)
287 return(conn
->version
);
291 do_close(struct sftp_conn
*conn
, char *handle
, u_int handle_len
)
299 buffer_put_char(&msg
, SSH2_FXP_CLOSE
);
300 buffer_put_int(&msg
, id
);
301 buffer_put_string(&msg
, handle
, handle_len
);
302 send_msg(conn
->fd_out
, &msg
);
303 debug3("Sent message SSH2_FXP_CLOSE I:%u", id
);
305 status
= get_status(conn
->fd_in
, id
);
306 if (status
!= SSH2_FX_OK
)
307 error("Couldn't close file: %s", fx2txt(status
));
316 do_lsreaddir(struct sftp_conn
*conn
, char *path
, int printflag
,
320 u_int count
, type
, id
, handle_len
, i
, expected_id
, ents
= 0;
326 buffer_put_char(&msg
, SSH2_FXP_OPENDIR
);
327 buffer_put_int(&msg
, id
);
328 buffer_put_cstring(&msg
, path
);
329 send_msg(conn
->fd_out
, &msg
);
333 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
339 *dir
= xmalloc(sizeof(**dir
));
343 for (; !interrupted
;) {
344 id
= expected_id
= conn
->msg_id
++;
346 debug3("Sending SSH2_FXP_READDIR I:%u", id
);
349 buffer_put_char(&msg
, SSH2_FXP_READDIR
);
350 buffer_put_int(&msg
, id
);
351 buffer_put_string(&msg
, handle
, handle_len
);
352 send_msg(conn
->fd_out
, &msg
);
356 get_msg(conn
->fd_in
, &msg
);
358 type
= buffer_get_char(&msg
);
359 id
= buffer_get_int(&msg
);
361 debug3("Received reply T:%u I:%u", type
, id
);
363 if (id
!= expected_id
)
364 fatal("ID mismatch (%u != %u)", id
, expected_id
);
366 if (type
== SSH2_FXP_STATUS
) {
367 int status
= buffer_get_int(&msg
);
369 debug3("Received SSH2_FXP_STATUS %d", status
);
371 if (status
== SSH2_FX_EOF
) {
374 error("Couldn't read directory: %s",
376 do_close(conn
, handle
, handle_len
);
380 } else if (type
!= SSH2_FXP_NAME
)
381 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
382 SSH2_FXP_NAME
, type
);
384 count
= buffer_get_int(&msg
);
387 debug3("Received %d SSH2_FXP_NAME responses", count
);
388 for (i
= 0; i
< count
; i
++) {
389 char *filename
, *longname
;
392 filename
= buffer_get_string(&msg
, NULL
);
393 longname
= buffer_get_string(&msg
, NULL
);
394 a
= decode_attrib(&msg
);
397 printf("%s\n", longname
);
400 *dir
= xrealloc(*dir
, ents
+ 2, sizeof(**dir
));
401 (*dir
)[ents
] = xmalloc(sizeof(***dir
));
402 (*dir
)[ents
]->filename
= xstrdup(filename
);
403 (*dir
)[ents
]->longname
= xstrdup(longname
);
404 memcpy(&(*dir
)[ents
]->a
, a
, sizeof(*a
));
405 (*dir
)[++ents
] = NULL
;
414 do_close(conn
, handle
, handle_len
);
417 /* Don't return partial matches on interrupt */
418 if (interrupted
&& dir
!= NULL
&& *dir
!= NULL
) {
419 free_sftp_dirents(*dir
);
420 *dir
= xmalloc(sizeof(**dir
));
428 do_readdir(struct sftp_conn
*conn
, char *path
, SFTP_DIRENT
***dir
)
430 return(do_lsreaddir(conn
, path
, 0, dir
));
433 void free_sftp_dirents(SFTP_DIRENT
**s
)
437 for (i
= 0; s
[i
]; i
++) {
438 xfree(s
[i
]->filename
);
439 xfree(s
[i
]->longname
);
446 do_rm(struct sftp_conn
*conn
, char *path
)
450 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path
);
453 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REMOVE
, path
,
455 status
= get_status(conn
->fd_in
, id
);
456 if (status
!= SSH2_FX_OK
)
457 error("Couldn't delete file: %s", fx2txt(status
));
462 do_mkdir(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
467 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_MKDIR
, path
,
470 status
= get_status(conn
->fd_in
, id
);
471 if (status
!= SSH2_FX_OK
)
472 error("Couldn't create directory: %s", fx2txt(status
));
478 do_rmdir(struct sftp_conn
*conn
, char *path
)
483 send_string_request(conn
->fd_out
, id
, SSH2_FXP_RMDIR
, path
,
486 status
= get_status(conn
->fd_in
, id
);
487 if (status
!= SSH2_FX_OK
)
488 error("Couldn't remove directory: %s", fx2txt(status
));
494 do_stat(struct sftp_conn
*conn
, char *path
, int quiet
)
500 send_string_request(conn
->fd_out
, id
,
501 conn
->version
== 0 ? SSH2_FXP_STAT_VERSION_0
: SSH2_FXP_STAT
,
504 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
508 do_lstat(struct sftp_conn
*conn
, char *path
, int quiet
)
512 if (conn
->version
== 0) {
514 debug("Server version does not support lstat operation");
516 logit("Server version does not support lstat operation");
517 return(do_stat(conn
, path
, quiet
));
521 send_string_request(conn
->fd_out
, id
, SSH2_FXP_LSTAT
, path
,
524 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
528 do_fstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
, int quiet
)
533 send_string_request(conn
->fd_out
, id
, SSH2_FXP_FSTAT
, handle
,
536 return(get_decode_stat(conn
->fd_in
, id
, quiet
));
540 do_setstat(struct sftp_conn
*conn
, char *path
, Attrib
*a
)
545 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_SETSTAT
, path
,
548 status
= get_status(conn
->fd_in
, id
);
549 if (status
!= SSH2_FX_OK
)
550 error("Couldn't setstat on \"%s\": %s", path
,
557 do_fsetstat(struct sftp_conn
*conn
, char *handle
, u_int handle_len
,
563 send_string_attrs_request(conn
->fd_out
, id
, SSH2_FXP_FSETSTAT
, handle
,
566 status
= get_status(conn
->fd_in
, id
);
567 if (status
!= SSH2_FX_OK
)
568 error("Couldn't fsetstat: %s", fx2txt(status
));
574 do_realpath(struct sftp_conn
*conn
, char *path
)
577 u_int type
, expected_id
, count
, id
;
578 char *filename
, *longname
;
581 expected_id
= id
= conn
->msg_id
++;
582 send_string_request(conn
->fd_out
, id
, SSH2_FXP_REALPATH
, path
,
587 get_msg(conn
->fd_in
, &msg
);
588 type
= buffer_get_char(&msg
);
589 id
= buffer_get_int(&msg
);
591 if (id
!= expected_id
)
592 fatal("ID mismatch (%u != %u)", id
, expected_id
);
594 if (type
== SSH2_FXP_STATUS
) {
595 u_int status
= buffer_get_int(&msg
);
597 error("Couldn't canonicalise: %s", fx2txt(status
));
599 } else if (type
!= SSH2_FXP_NAME
)
600 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
601 SSH2_FXP_NAME
, type
);
603 count
= buffer_get_int(&msg
);
605 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count
);
607 filename
= buffer_get_string(&msg
, NULL
);
608 longname
= buffer_get_string(&msg
, NULL
);
609 a
= decode_attrib(&msg
);
611 debug3("SSH_FXP_REALPATH %s -> %s", path
, filename
);
621 do_rename(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
628 /* Send rename request */
630 buffer_put_char(&msg
, SSH2_FXP_RENAME
);
631 buffer_put_int(&msg
, id
);
632 buffer_put_cstring(&msg
, oldpath
);
633 buffer_put_cstring(&msg
, newpath
);
634 send_msg(conn
->fd_out
, &msg
);
635 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath
,
639 status
= get_status(conn
->fd_in
, id
);
640 if (status
!= SSH2_FX_OK
)
641 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath
,
642 newpath
, fx2txt(status
));
648 do_symlink(struct sftp_conn
*conn
, char *oldpath
, char *newpath
)
653 if (conn
->version
< 3) {
654 error("This server does not support the symlink operation");
655 return(SSH2_FX_OP_UNSUPPORTED
);
660 /* Send symlink request */
662 buffer_put_char(&msg
, SSH2_FXP_SYMLINK
);
663 buffer_put_int(&msg
, id
);
664 buffer_put_cstring(&msg
, oldpath
);
665 buffer_put_cstring(&msg
, newpath
);
666 send_msg(conn
->fd_out
, &msg
);
667 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath
,
671 status
= get_status(conn
->fd_in
, id
);
672 if (status
!= SSH2_FX_OK
)
673 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath
,
674 newpath
, fx2txt(status
));
680 do_readlink(struct sftp_conn
*conn
, char *path
)
683 u_int type
, expected_id
, count
, id
;
684 char *filename
, *longname
;
687 expected_id
= id
= conn
->msg_id
++;
688 send_string_request(conn
->fd_out
, id
, SSH2_FXP_READLINK
, path
,
693 get_msg(conn
->fd_in
, &msg
);
694 type
= buffer_get_char(&msg
);
695 id
= buffer_get_int(&msg
);
697 if (id
!= expected_id
)
698 fatal("ID mismatch (%u != %u)", id
, expected_id
);
700 if (type
== SSH2_FXP_STATUS
) {
701 u_int status
= buffer_get_int(&msg
);
703 error("Couldn't readlink: %s", fx2txt(status
));
705 } else if (type
!= SSH2_FXP_NAME
)
706 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
707 SSH2_FXP_NAME
, type
);
709 count
= buffer_get_int(&msg
);
711 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count
);
713 filename
= buffer_get_string(&msg
, NULL
);
714 longname
= buffer_get_string(&msg
, NULL
);
715 a
= decode_attrib(&msg
);
717 debug3("SSH_FXP_READLINK %s -> %s", path
, filename
);
727 send_read_request(int fd_out
, u_int id
, u_int64_t offset
, u_int len
,
728 char *handle
, u_int handle_len
)
734 buffer_put_char(&msg
, SSH2_FXP_READ
);
735 buffer_put_int(&msg
, id
);
736 buffer_put_string(&msg
, handle
, handle_len
);
737 buffer_put_int64(&msg
, offset
);
738 buffer_put_int(&msg
, len
);
739 send_msg(fd_out
, &msg
);
744 do_download(struct sftp_conn
*conn
, char *remote_path
, char *local_path
,
750 int local_fd
, status
= 0, write_error
;
751 int read_error
, write_errno
;
752 u_int64_t offset
, size
;
753 u_int handle_len
, mode
, type
, id
, buflen
, num_req
, max_req
;
754 off_t progress_counter
;
759 TAILQ_ENTRY(request
) tq
;
761 TAILQ_HEAD(reqhead
, request
) requests
;
764 TAILQ_INIT(&requests
);
766 a
= do_stat(conn
, remote_path
, 0);
770 /* XXX: should we preserve set[ug]id? */
771 if (a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
)
772 mode
= a
->perm
& 0777;
776 if ((a
->flags
& SSH2_FILEXFER_ATTR_PERMISSIONS
) &&
777 (!S_ISREG(a
->perm
))) {
778 error("Cannot download non-regular file: %s", remote_path
);
782 if (a
->flags
& SSH2_FILEXFER_ATTR_SIZE
)
787 buflen
= conn
->transfer_buflen
;
790 /* Send open request */
792 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
793 buffer_put_int(&msg
, id
);
794 buffer_put_cstring(&msg
, remote_path
);
795 buffer_put_int(&msg
, SSH2_FXF_READ
);
796 attrib_clear(&junk
); /* Send empty attributes */
797 encode_attrib(&msg
, &junk
);
798 send_msg(conn
->fd_out
, &msg
);
799 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
801 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
802 if (handle
== NULL
) {
807 local_fd
= open(local_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
809 if (local_fd
== -1) {
810 error("Couldn't open local file \"%s\" for writing: %s",
811 local_path
, strerror(errno
));
817 /* Read from remote and write to local */
818 write_error
= read_error
= write_errno
= num_req
= offset
= 0;
820 progress_counter
= 0;
822 if (showprogress
&& size
!= 0)
823 start_progress_meter(remote_path
, size
, &progress_counter
);
825 while (num_req
> 0 || max_req
> 0) {
830 * Simulate EOF on interrupt: stop sending new requests and
831 * allow outstanding requests to drain gracefully
834 if (num_req
== 0) /* If we haven't started yet... */
839 /* Send some more requests */
840 while (num_req
< max_req
) {
841 debug3("Request range %llu -> %llu (%d/%d)",
842 (unsigned long long)offset
,
843 (unsigned long long)offset
+ buflen
- 1,
845 req
= xmalloc(sizeof(*req
));
846 req
->id
= conn
->msg_id
++;
848 req
->offset
= offset
;
851 TAILQ_INSERT_TAIL(&requests
, req
, tq
);
852 send_read_request(conn
->fd_out
, req
->id
, req
->offset
,
853 req
->len
, handle
, handle_len
);
857 get_msg(conn
->fd_in
, &msg
);
858 type
= buffer_get_char(&msg
);
859 id
= buffer_get_int(&msg
);
860 debug3("Received reply T:%u I:%u R:%d", type
, id
, max_req
);
862 /* Find the request in our queue */
863 for (req
= TAILQ_FIRST(&requests
);
864 req
!= NULL
&& req
->id
!= id
;
865 req
= TAILQ_NEXT(req
, tq
))
868 fatal("Unexpected reply %u", id
);
871 case SSH2_FXP_STATUS
:
872 status
= buffer_get_int(&msg
);
873 if (status
!= SSH2_FX_EOF
)
876 TAILQ_REMOVE(&requests
, req
, tq
);
881 data
= buffer_get_string(&msg
, &len
);
882 debug3("Received data %llu -> %llu",
883 (unsigned long long)req
->offset
,
884 (unsigned long long)req
->offset
+ len
- 1);
886 fatal("Received more data than asked for "
887 "%u > %u", len
, req
->len
);
888 if ((lseek(local_fd
, req
->offset
, SEEK_SET
) == -1 ||
889 atomicio(vwrite
, local_fd
, data
, len
) != len
) &&
895 progress_counter
+= len
;
898 if (len
== req
->len
) {
899 TAILQ_REMOVE(&requests
, req
, tq
);
903 /* Resend the request for the missing data */
904 debug3("Short data block, re-requesting "
905 "%llu -> %llu (%2d)",
906 (unsigned long long)req
->offset
+ len
,
907 (unsigned long long)req
->offset
+
908 req
->len
- 1, num_req
);
909 req
->id
= conn
->msg_id
++;
912 send_read_request(conn
->fd_out
, req
->id
,
913 req
->offset
, req
->len
, handle
, handle_len
);
914 /* Reduce the request size */
916 buflen
= MAX(MIN_READ_SIZE
, len
);
918 if (max_req
> 0) { /* max_req = 0 iff EOF received */
919 if (size
> 0 && offset
> size
) {
920 /* Only one request at a time
921 * after the expected EOF */
922 debug3("Finish at %llu (%2d)",
923 (unsigned long long)offset
,
926 } else if (max_req
<= conn
->num_requests
) {
932 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
933 SSH2_FXP_DATA
, type
);
937 if (showprogress
&& size
)
938 stop_progress_meter();
941 if (TAILQ_FIRST(&requests
) != NULL
)
942 fatal("Transfer complete, but requests still in queue");
945 error("Couldn't read from remote file \"%s\" : %s",
946 remote_path
, fx2txt(status
));
947 do_close(conn
, handle
, handle_len
);
948 } else if (write_error
) {
949 error("Couldn't write to \"%s\": %s", local_path
,
950 strerror(write_errno
));
952 do_close(conn
, handle
, handle_len
);
954 status
= do_close(conn
, handle
, handle_len
);
956 /* Override umask and utimes if asked */
958 if (pflag
&& fchmod(local_fd
, mode
) == -1)
960 if (pflag
&& chmod(local_path
, mode
) == -1)
961 #endif /* HAVE_FCHMOD */
962 error("Couldn't set mode on \"%s\": %s", local_path
,
964 if (pflag
&& (a
->flags
& SSH2_FILEXFER_ATTR_ACMODTIME
)) {
965 struct timeval tv
[2];
966 tv
[0].tv_sec
= a
->atime
;
967 tv
[1].tv_sec
= a
->mtime
;
968 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
969 if (utimes(local_path
, tv
) == -1)
970 error("Can't set times on \"%s\": %s",
971 local_path
, strerror(errno
));
982 do_upload(struct sftp_conn
*conn
, char *local_path
, char *remote_path
,
985 int local_fd
, status
;
986 u_int handle_len
, id
, type
;
994 struct outstanding_ack
{
998 TAILQ_ENTRY(outstanding_ack
) tq
;
1000 TAILQ_HEAD(ackhead
, outstanding_ack
) acks
;
1001 struct outstanding_ack
*ack
= NULL
;
1005 if ((local_fd
= open(local_path
, O_RDONLY
, 0)) == -1) {
1006 error("Couldn't open local file \"%s\" for reading: %s",
1007 local_path
, strerror(errno
));
1010 if (fstat(local_fd
, &sb
) == -1) {
1011 error("Couldn't fstat local file \"%s\": %s",
1012 local_path
, strerror(errno
));
1016 if (!S_ISREG(sb
.st_mode
)) {
1017 error("%s is not a regular file", local_path
);
1021 stat_to_attrib(&sb
, &a
);
1023 a
.flags
&= ~SSH2_FILEXFER_ATTR_SIZE
;
1024 a
.flags
&= ~SSH2_FILEXFER_ATTR_UIDGID
;
1027 a
.flags
&= ~SSH2_FILEXFER_ATTR_ACMODTIME
;
1031 /* Send open request */
1032 id
= conn
->msg_id
++;
1033 buffer_put_char(&msg
, SSH2_FXP_OPEN
);
1034 buffer_put_int(&msg
, id
);
1035 buffer_put_cstring(&msg
, remote_path
);
1036 buffer_put_int(&msg
, SSH2_FXF_WRITE
|SSH2_FXF_CREAT
|SSH2_FXF_TRUNC
);
1037 encode_attrib(&msg
, &a
);
1038 send_msg(conn
->fd_out
, &msg
);
1039 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id
, remote_path
);
1043 handle
= get_handle(conn
->fd_in
, id
, &handle_len
);
1044 if (handle
== NULL
) {
1050 startid
= ackid
= id
+ 1;
1051 data
= xmalloc(conn
->transfer_buflen
);
1053 /* Read from local and write to remote */
1056 start_progress_meter(local_path
, sb
.st_size
, &offset
);
1062 * Can't use atomicio here because it returns 0 on EOF,
1063 * thus losing the last block of the file.
1064 * Simulate an EOF on interrupt, allowing ACKs from the
1070 len
= read(local_fd
, data
, conn
->transfer_buflen
);
1071 while ((len
== -1) && (errno
== EINTR
|| errno
== EAGAIN
));
1074 fatal("Couldn't read from \"%s\": %s", local_path
,
1078 ack
= xmalloc(sizeof(*ack
));
1080 ack
->offset
= offset
;
1082 TAILQ_INSERT_TAIL(&acks
, ack
, tq
);
1085 buffer_put_char(&msg
, SSH2_FXP_WRITE
);
1086 buffer_put_int(&msg
, ack
->id
);
1087 buffer_put_string(&msg
, handle
, handle_len
);
1088 buffer_put_int64(&msg
, offset
);
1089 buffer_put_string(&msg
, data
, len
);
1090 send_msg(conn
->fd_out
, &msg
);
1091 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1092 id
, (unsigned long long)offset
, len
);
1093 } else if (TAILQ_FIRST(&acks
) == NULL
)
1097 fatal("Unexpected ACK %u", id
);
1099 if (id
== startid
|| len
== 0 ||
1100 id
- ackid
>= conn
->num_requests
) {
1104 get_msg(conn
->fd_in
, &msg
);
1105 type
= buffer_get_char(&msg
);
1106 r_id
= buffer_get_int(&msg
);
1108 if (type
!= SSH2_FXP_STATUS
)
1109 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1110 "got %d", SSH2_FXP_STATUS
, type
);
1112 status
= buffer_get_int(&msg
);
1113 debug3("SSH2_FXP_STATUS %d", status
);
1115 /* Find the request in our queue */
1116 for (ack
= TAILQ_FIRST(&acks
);
1117 ack
!= NULL
&& ack
->id
!= r_id
;
1118 ack
= TAILQ_NEXT(ack
, tq
))
1121 fatal("Can't find request for ID %u", r_id
);
1122 TAILQ_REMOVE(&acks
, ack
, tq
);
1124 if (status
!= SSH2_FX_OK
) {
1125 error("Couldn't write to remote file \"%s\": %s",
1126 remote_path
, fx2txt(status
));
1127 do_close(conn
, handle
, handle_len
);
1133 debug3("In write loop, ack for %u %u bytes at %llu",
1134 ack
->id
, ack
->len
, (unsigned long long)ack
->offset
);
1141 stop_progress_meter();
1144 if (close(local_fd
) == -1) {
1145 error("Couldn't close local file \"%s\": %s", local_path
,
1147 do_close(conn
, handle
, handle_len
);
1152 /* Override umask and utimes if asked */
1154 do_fsetstat(conn
, handle
, handle_len
, &a
);
1156 status
= do_close(conn
, handle
, handle_len
);