1 /* $OpenBSD: sftp-client.c,v 1.68 2006/07/17 01:31:09 stevesk 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>
35 #include "openbsd-compat/sys-queue.h"
42 #include "progressmeter.h"
46 #include "sftp-common.h"
47 #include "sftp-client.h"
49 extern volatile sig_atomic_t interrupted;
50 extern int showprogress;
52 /* Minimum amount of data to read at a time */
53 #define MIN_READ_SIZE 512
58 u_int transfer_buflen;
65 send_msg(int fd, Buffer *m)
70 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
71 fatal("Outbound message too long %u", buffer_len(m));
73 /* Send length first */
74 put_u32(mlen, buffer_len(m));
75 iov[0].iov_base = mlen;
76 iov[0].iov_len = sizeof(mlen);
77 iov[1].iov_base = buffer_ptr(m);
78 iov[1].iov_len = buffer_len(m);
80 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
81 fatal("Couldn't send packet: %s", strerror(errno));
87 get_msg(int fd, Buffer *m)
91 buffer_append_space(m, 4);
92 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
94 fatal("Connection closed");
96 fatal("Couldn't read packet: %s", strerror(errno));
99 msg_len = buffer_get_int(m);
100 if (msg_len > SFTP_MAX_MSG_LENGTH)
101 fatal("Received message too long %u", msg_len);
103 buffer_append_space(m, msg_len);
104 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
106 fatal("Connection closed");
108 fatal("Read packet: %s", strerror(errno));
113 send_string_request(int fd, u_int id, u_int code, char *s,
119 buffer_put_char(&msg, code);
120 buffer_put_int(&msg, id);
121 buffer_put_string(&msg, s, len);
123 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
128 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
129 u_int len, Attrib *a)
134 buffer_put_char(&msg, code);
135 buffer_put_int(&msg, id);
136 buffer_put_string(&msg, s, len);
137 encode_attrib(&msg, a);
139 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
144 get_status(int fd, u_int expected_id)
147 u_int type, id, status;
151 type = buffer_get_char(&msg);
152 id = buffer_get_int(&msg);
154 if (id != expected_id)
155 fatal("ID mismatch (%u != %u)", id, expected_id);
156 if (type != SSH2_FXP_STATUS)
157 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
158 SSH2_FXP_STATUS, type);
160 status = buffer_get_int(&msg);
163 debug3("SSH2_FXP_STATUS %u", status);
169 get_handle(int fd, u_int expected_id, u_int *len)
177 type = buffer_get_char(&msg);
178 id = buffer_get_int(&msg);
180 if (id != expected_id)
181 fatal("ID mismatch (%u != %u)", id, expected_id);
182 if (type == SSH2_FXP_STATUS) {
183 int status = buffer_get_int(&msg);
185 error("Couldn't get handle: %s", fx2txt(status));
188 } else if (type != SSH2_FXP_HANDLE)
189 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
190 SSH2_FXP_HANDLE, type);
192 handle = buffer_get_string(&msg, len);
199 get_decode_stat(int fd, u_int expected_id, int quiet)
208 type = buffer_get_char(&msg);
209 id = buffer_get_int(&msg);
211 debug3("Received stat reply T:%u I:%u", type, id);
212 if (id != expected_id)
213 fatal("ID mismatch (%u != %u)", id, expected_id);
214 if (type == SSH2_FXP_STATUS) {
215 int status = buffer_get_int(&msg);
218 debug("Couldn't stat remote file: %s", fx2txt(status));
220 error("Couldn't stat remote file: %s", fx2txt(status));
223 } else if (type != SSH2_FXP_ATTRS) {
224 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
225 SSH2_FXP_ATTRS, type);
227 a = decode_attrib(&msg);
234 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
239 struct sftp_conn *ret;
242 buffer_put_char(&msg, SSH2_FXP_INIT);
243 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
244 send_msg(fd_out, &msg);
248 get_msg(fd_in, &msg);
250 /* Expecting a VERSION reply */
251 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
252 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
257 version = buffer_get_int(&msg);
259 debug2("Remote version: %d", version);
261 /* Check for extensions */
262 while (buffer_len(&msg) > 0) {
263 char *name = buffer_get_string(&msg, NULL);
264 char *value = buffer_get_string(&msg, NULL);
266 debug2("Init extension: \"%s\"", name);
273 ret = xmalloc(sizeof(*ret));
275 ret->fd_out = fd_out;
276 ret->transfer_buflen = transfer_buflen;
277 ret->num_requests = num_requests;
278 ret->version = version;
281 /* Some filexfer v.0 servers don't support large packets */
283 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
289 sftp_proto_version(struct sftp_conn *conn)
291 return(conn->version);
295 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
303 buffer_put_char(&msg, SSH2_FXP_CLOSE);
304 buffer_put_int(&msg, id);
305 buffer_put_string(&msg, handle, handle_len);
306 send_msg(conn->fd_out, &msg);
307 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
309 status = get_status(conn->fd_in, id);
310 if (status != SSH2_FX_OK)
311 error("Couldn't close file: %s", fx2txt(status));
320 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
324 u_int count, type, id, handle_len, i, expected_id, ents = 0;
330 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
331 buffer_put_int(&msg, id);
332 buffer_put_cstring(&msg, path);
333 send_msg(conn->fd_out, &msg);
337 handle = get_handle(conn->fd_in, id, &handle_len);
343 *dir = xmalloc(sizeof(**dir));
347 for (; !interrupted;) {
348 id = expected_id = conn->msg_id++;
350 debug3("Sending SSH2_FXP_READDIR I:%u", id);
353 buffer_put_char(&msg, SSH2_FXP_READDIR);
354 buffer_put_int(&msg, id);
355 buffer_put_string(&msg, handle, handle_len);
356 send_msg(conn->fd_out, &msg);
360 get_msg(conn->fd_in, &msg);
362 type = buffer_get_char(&msg);
363 id = buffer_get_int(&msg);
365 debug3("Received reply T:%u I:%u", type, id);
367 if (id != expected_id)
368 fatal("ID mismatch (%u != %u)", id, expected_id);
370 if (type == SSH2_FXP_STATUS) {
371 int status = buffer_get_int(&msg);
373 debug3("Received SSH2_FXP_STATUS %d", status);
375 if (status == SSH2_FX_EOF) {
378 error("Couldn't read directory: %s",
380 do_close(conn, handle, handle_len);
384 } else if (type != SSH2_FXP_NAME)
385 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
386 SSH2_FXP_NAME, type);
388 count = buffer_get_int(&msg);
391 debug3("Received %d SSH2_FXP_NAME responses", count);
392 for (i = 0; i < count; i++) {
393 char *filename, *longname;
396 filename = buffer_get_string(&msg, NULL);
397 longname = buffer_get_string(&msg, NULL);
398 a = decode_attrib(&msg);
401 printf("%s\n", longname);
404 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
405 (*dir)[ents] = xmalloc(sizeof(***dir));
406 (*dir)[ents]->filename = xstrdup(filename);
407 (*dir)[ents]->longname = xstrdup(longname);
408 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
409 (*dir)[++ents] = NULL;
418 do_close(conn, handle, handle_len);
421 /* Don't return partial matches on interrupt */
422 if (interrupted && dir != NULL && *dir != NULL) {
423 free_sftp_dirents(*dir);
424 *dir = xmalloc(sizeof(**dir));
432 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
434 return(do_lsreaddir(conn, path, 0, dir));
437 void free_sftp_dirents(SFTP_DIRENT **s)
441 for (i = 0; s[i]; i++) {
442 xfree(s[i]->filename);
443 xfree(s[i]->longname);
450 do_rm(struct sftp_conn *conn, char *path)
454 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
457 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
459 status = get_status(conn->fd_in, id);
460 if (status != SSH2_FX_OK)
461 error("Couldn't delete file: %s", fx2txt(status));
466 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
471 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
474 status = get_status(conn->fd_in, id);
475 if (status != SSH2_FX_OK)
476 error("Couldn't create directory: %s", fx2txt(status));
482 do_rmdir(struct sftp_conn *conn, char *path)
487 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
490 status = get_status(conn->fd_in, id);
491 if (status != SSH2_FX_OK)
492 error("Couldn't remove directory: %s", fx2txt(status));
498 do_stat(struct sftp_conn *conn, char *path, int quiet)
504 send_string_request(conn->fd_out, id,
505 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
508 return(get_decode_stat(conn->fd_in, id, quiet));
512 do_lstat(struct sftp_conn *conn, char *path, int quiet)
516 if (conn->version == 0) {
518 debug("Server version does not support lstat operation");
520 logit("Server version does not support lstat operation");
521 return(do_stat(conn, path, quiet));
525 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
528 return(get_decode_stat(conn->fd_in, id, quiet));
532 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
537 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
540 return(get_decode_stat(conn->fd_in, id, quiet));
544 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
549 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
552 status = get_status(conn->fd_in, id);
553 if (status != SSH2_FX_OK)
554 error("Couldn't setstat on \"%s\": %s", path,
561 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
567 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
570 status = get_status(conn->fd_in, id);
571 if (status != SSH2_FX_OK)
572 error("Couldn't fsetstat: %s", fx2txt(status));
578 do_realpath(struct sftp_conn *conn, char *path)
581 u_int type, expected_id, count, id;
582 char *filename, *longname;
585 expected_id = id = conn->msg_id++;
586 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
591 get_msg(conn->fd_in, &msg);
592 type = buffer_get_char(&msg);
593 id = buffer_get_int(&msg);
595 if (id != expected_id)
596 fatal("ID mismatch (%u != %u)", id, expected_id);
598 if (type == SSH2_FXP_STATUS) {
599 u_int status = buffer_get_int(&msg);
601 error("Couldn't canonicalise: %s", fx2txt(status));
603 } else if (type != SSH2_FXP_NAME)
604 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
605 SSH2_FXP_NAME, type);
607 count = buffer_get_int(&msg);
609 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
611 filename = buffer_get_string(&msg, NULL);
612 longname = buffer_get_string(&msg, NULL);
613 a = decode_attrib(&msg);
615 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
625 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
632 /* Send rename request */
634 buffer_put_char(&msg, SSH2_FXP_RENAME);
635 buffer_put_int(&msg, id);
636 buffer_put_cstring(&msg, oldpath);
637 buffer_put_cstring(&msg, newpath);
638 send_msg(conn->fd_out, &msg);
639 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
643 status = get_status(conn->fd_in, id);
644 if (status != SSH2_FX_OK)
645 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
646 newpath, fx2txt(status));
652 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
657 if (conn->version < 3) {
658 error("This server does not support the symlink operation");
659 return(SSH2_FX_OP_UNSUPPORTED);
664 /* Send symlink request */
666 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
667 buffer_put_int(&msg, id);
668 buffer_put_cstring(&msg, oldpath);
669 buffer_put_cstring(&msg, newpath);
670 send_msg(conn->fd_out, &msg);
671 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
675 status = get_status(conn->fd_in, id);
676 if (status != SSH2_FX_OK)
677 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
678 newpath, fx2txt(status));
684 do_readlink(struct sftp_conn *conn, char *path)
687 u_int type, expected_id, count, id;
688 char *filename, *longname;
691 expected_id = id = conn->msg_id++;
692 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
697 get_msg(conn->fd_in, &msg);
698 type = buffer_get_char(&msg);
699 id = buffer_get_int(&msg);
701 if (id != expected_id)
702 fatal("ID mismatch (%u != %u)", id, expected_id);
704 if (type == SSH2_FXP_STATUS) {
705 u_int status = buffer_get_int(&msg);
707 error("Couldn't readlink: %s", fx2txt(status));
709 } else if (type != SSH2_FXP_NAME)
710 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
711 SSH2_FXP_NAME, type);
713 count = buffer_get_int(&msg);
715 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
717 filename = buffer_get_string(&msg, NULL);
718 longname = buffer_get_string(&msg, NULL);
719 a = decode_attrib(&msg);
721 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
731 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
732 char *handle, u_int handle_len)
738 buffer_put_char(&msg, SSH2_FXP_READ);
739 buffer_put_int(&msg, id);
740 buffer_put_string(&msg, handle, handle_len);
741 buffer_put_int64(&msg, offset);
742 buffer_put_int(&msg, len);
743 send_msg(fd_out, &msg);
748 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
754 int local_fd, status = 0, write_error;
755 int read_error, write_errno;
756 u_int64_t offset, size;
757 u_int handle_len, mode, type, id, buflen, num_req, max_req;
758 off_t progress_counter;
763 TAILQ_ENTRY(request) tq;
765 TAILQ_HEAD(reqhead, request) requests;
768 TAILQ_INIT(&requests);
770 a = do_stat(conn, remote_path, 0);
774 /* XXX: should we preserve set[ug]id? */
775 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
776 mode = a->perm & 0777;
780 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
781 (!S_ISREG(a->perm))) {
782 error("Cannot download non-regular file: %s", remote_path);
786 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
791 buflen = conn->transfer_buflen;
794 /* Send open request */
796 buffer_put_char(&msg, SSH2_FXP_OPEN);
797 buffer_put_int(&msg, id);
798 buffer_put_cstring(&msg, remote_path);
799 buffer_put_int(&msg, SSH2_FXF_READ);
800 attrib_clear(&junk); /* Send empty attributes */
801 encode_attrib(&msg, &junk);
802 send_msg(conn->fd_out, &msg);
803 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
805 handle = get_handle(conn->fd_in, id, &handle_len);
806 if (handle == NULL) {
811 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
813 if (local_fd == -1) {
814 error("Couldn't open local file \"%s\" for writing: %s",
815 local_path, strerror(errno));
821 /* Read from remote and write to local */
822 write_error = read_error = write_errno = num_req = offset = 0;
824 progress_counter = 0;
826 if (showprogress && size != 0)
827 start_progress_meter(remote_path, size, &progress_counter);
829 while (num_req > 0 || max_req > 0) {
834 * Simulate EOF on interrupt: stop sending new requests and
835 * allow outstanding requests to drain gracefully
838 if (num_req == 0) /* If we haven't started yet... */
843 /* Send some more requests */
844 while (num_req < max_req) {
845 debug3("Request range %llu -> %llu (%d/%d)",
846 (unsigned long long)offset,
847 (unsigned long long)offset + buflen - 1,
849 req = xmalloc(sizeof(*req));
850 req->id = conn->msg_id++;
852 req->offset = offset;
855 TAILQ_INSERT_TAIL(&requests, req, tq);
856 send_read_request(conn->fd_out, req->id, req->offset,
857 req->len, handle, handle_len);
861 get_msg(conn->fd_in, &msg);
862 type = buffer_get_char(&msg);
863 id = buffer_get_int(&msg);
864 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
866 /* Find the request in our queue */
867 for (req = TAILQ_FIRST(&requests);
868 req != NULL && req->id != id;
869 req = TAILQ_NEXT(req, tq))
872 fatal("Unexpected reply %u", id);
875 case SSH2_FXP_STATUS:
876 status = buffer_get_int(&msg);
877 if (status != SSH2_FX_EOF)
880 TAILQ_REMOVE(&requests, req, tq);
885 data = buffer_get_string(&msg, &len);
886 debug3("Received data %llu -> %llu",
887 (unsigned long long)req->offset,
888 (unsigned long long)req->offset + len - 1);
890 fatal("Received more data than asked for "
891 "%u > %u", len, req->len);
892 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
893 atomicio(vwrite, local_fd, data, len) != len) &&
899 progress_counter += len;
902 if (len == req->len) {
903 TAILQ_REMOVE(&requests, req, tq);
907 /* Resend the request for the missing data */
908 debug3("Short data block, re-requesting "
909 "%llu -> %llu (%2d)",
910 (unsigned long long)req->offset + len,
911 (unsigned long long)req->offset +
912 req->len - 1, num_req);
913 req->id = conn->msg_id++;
916 send_read_request(conn->fd_out, req->id,
917 req->offset, req->len, handle, handle_len);
918 /* Reduce the request size */
920 buflen = MAX(MIN_READ_SIZE, len);
922 if (max_req > 0) { /* max_req = 0 iff EOF received */
923 if (size > 0 && offset > size) {
924 /* Only one request at a time
925 * after the expected EOF */
926 debug3("Finish at %llu (%2d)",
927 (unsigned long long)offset,
930 } else if (max_req <= conn->num_requests) {
936 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
937 SSH2_FXP_DATA, type);
941 if (showprogress && size)
942 stop_progress_meter();
945 if (TAILQ_FIRST(&requests) != NULL)
946 fatal("Transfer complete, but requests still in queue");
949 error("Couldn't read from remote file \"%s\" : %s",
950 remote_path, fx2txt(status));
951 do_close(conn, handle, handle_len);
952 } else if (write_error) {
953 error("Couldn't write to \"%s\": %s", local_path,
954 strerror(write_errno));
956 do_close(conn, handle, handle_len);
958 status = do_close(conn, handle, handle_len);
960 /* Override umask and utimes if asked */
962 if (pflag && fchmod(local_fd, mode) == -1)
964 if (pflag && chmod(local_path, mode) == -1)
965 #endif /* HAVE_FCHMOD */
966 error("Couldn't set mode on \"%s\": %s", local_path,
968 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
969 struct timeval tv[2];
970 tv[0].tv_sec = a->atime;
971 tv[1].tv_sec = a->mtime;
972 tv[0].tv_usec = tv[1].tv_usec = 0;
973 if (utimes(local_path, tv) == -1)
974 error("Can't set times on \"%s\": %s",
975 local_path, strerror(errno));
986 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
989 int local_fd, status;
990 u_int handle_len, id, type;
998 struct outstanding_ack {
1002 TAILQ_ENTRY(outstanding_ack) tq;
1004 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1005 struct outstanding_ack *ack = NULL;
1009 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1010 error("Couldn't open local file \"%s\" for reading: %s",
1011 local_path, strerror(errno));
1014 if (fstat(local_fd, &sb) == -1) {
1015 error("Couldn't fstat local file \"%s\": %s",
1016 local_path, strerror(errno));
1020 if (!S_ISREG(sb.st_mode)) {
1021 error("%s is not a regular file", local_path);
1025 stat_to_attrib(&sb, &a);
1027 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1028 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1031 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1035 /* Send open request */
1036 id = conn->msg_id++;
1037 buffer_put_char(&msg, SSH2_FXP_OPEN);
1038 buffer_put_int(&msg, id);
1039 buffer_put_cstring(&msg, remote_path);
1040 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1041 encode_attrib(&msg, &a);
1042 send_msg(conn->fd_out, &msg);
1043 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1047 handle = get_handle(conn->fd_in, id, &handle_len);
1048 if (handle == NULL) {
1054 startid = ackid = id + 1;
1055 data = xmalloc(conn->transfer_buflen);
1057 /* Read from local and write to remote */
1060 start_progress_meter(local_path, sb.st_size, &offset);
1066 * Can't use atomicio here because it returns 0 on EOF,
1067 * thus losing the last block of the file.
1068 * Simulate an EOF on interrupt, allowing ACKs from the
1074 len = read(local_fd, data, conn->transfer_buflen);
1075 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1078 fatal("Couldn't read from \"%s\": %s", local_path,
1082 ack = xmalloc(sizeof(*ack));
1084 ack->offset = offset;
1086 TAILQ_INSERT_TAIL(&acks, ack, tq);
1089 buffer_put_char(&msg, SSH2_FXP_WRITE);
1090 buffer_put_int(&msg, ack->id);
1091 buffer_put_string(&msg, handle, handle_len);
1092 buffer_put_int64(&msg, offset);
1093 buffer_put_string(&msg, data, len);
1094 send_msg(conn->fd_out, &msg);
1095 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1096 id, (unsigned long long)offset, len);
1097 } else if (TAILQ_FIRST(&acks) == NULL)
1101 fatal("Unexpected ACK %u", id);
1103 if (id == startid || len == 0 ||
1104 id - ackid >= conn->num_requests) {
1108 get_msg(conn->fd_in, &msg);
1109 type = buffer_get_char(&msg);
1110 r_id = buffer_get_int(&msg);
1112 if (type != SSH2_FXP_STATUS)
1113 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1114 "got %d", SSH2_FXP_STATUS, type);
1116 status = buffer_get_int(&msg);
1117 debug3("SSH2_FXP_STATUS %d", status);
1119 /* Find the request in our queue */
1120 for (ack = TAILQ_FIRST(&acks);
1121 ack != NULL && ack->id != r_id;
1122 ack = TAILQ_NEXT(ack, tq))
1125 fatal("Can't find request for ID %u", r_id);
1126 TAILQ_REMOVE(&acks, ack, tq);
1128 if (status != SSH2_FX_OK) {
1129 error("Couldn't write to remote file \"%s\": %s",
1130 remote_path, fx2txt(status));
1131 do_close(conn, handle, handle_len);
1137 debug3("In write loop, ack for %u %u bytes at %llu",
1138 ack->id, ack->len, (unsigned long long)ack->offset);
1145 stop_progress_meter();
1148 if (close(local_fd) == -1) {
1149 error("Couldn't close local file \"%s\": %s", local_path,
1151 do_close(conn, handle, handle_len);
1156 /* Override umask and utimes if asked */
1158 do_fsetstat(conn, handle, handle_len, &a);
1160 status = do_close(conn, handle, handle_len);