1 /* $OpenBSD: sftp-client.c,v 1.70 2006/07/23 01:11:05 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>
36 #include "openbsd-compat/sys-queue.h"
43 #include "progressmeter.h"
47 #include "sftp-common.h"
48 #include "sftp-client.h"
50 extern volatile sig_atomic_t interrupted;
51 extern int showprogress;
53 /* Minimum amount of data to read at a time */
54 #define MIN_READ_SIZE 512
59 u_int transfer_buflen;
66 send_msg(int fd, Buffer *m)
71 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
72 fatal("Outbound message too long %u", buffer_len(m));
74 /* Send length first */
75 put_u32(mlen, buffer_len(m));
76 iov[0].iov_base = mlen;
77 iov[0].iov_len = sizeof(mlen);
78 iov[1].iov_base = buffer_ptr(m);
79 iov[1].iov_len = buffer_len(m);
81 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
82 fatal("Couldn't send packet: %s", strerror(errno));
88 get_msg(int fd, Buffer *m)
92 buffer_append_space(m, 4);
93 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
95 fatal("Connection closed");
97 fatal("Couldn't read packet: %s", strerror(errno));
100 msg_len = buffer_get_int(m);
101 if (msg_len > SFTP_MAX_MSG_LENGTH)
102 fatal("Received message too long %u", msg_len);
104 buffer_append_space(m, msg_len);
105 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
107 fatal("Connection closed");
109 fatal("Read packet: %s", strerror(errno));
114 send_string_request(int fd, u_int id, u_int code, char *s,
120 buffer_put_char(&msg, code);
121 buffer_put_int(&msg, id);
122 buffer_put_string(&msg, s, len);
124 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
129 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
130 u_int len, Attrib *a)
135 buffer_put_char(&msg, code);
136 buffer_put_int(&msg, id);
137 buffer_put_string(&msg, s, len);
138 encode_attrib(&msg, a);
140 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
145 get_status(int fd, u_int expected_id)
148 u_int type, id, status;
152 type = buffer_get_char(&msg);
153 id = buffer_get_int(&msg);
155 if (id != expected_id)
156 fatal("ID mismatch (%u != %u)", id, expected_id);
157 if (type != SSH2_FXP_STATUS)
158 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
159 SSH2_FXP_STATUS, type);
161 status = buffer_get_int(&msg);
164 debug3("SSH2_FXP_STATUS %u", status);
170 get_handle(int fd, u_int expected_id, u_int *len)
178 type = buffer_get_char(&msg);
179 id = buffer_get_int(&msg);
181 if (id != expected_id)
182 fatal("ID mismatch (%u != %u)", id, expected_id);
183 if (type == SSH2_FXP_STATUS) {
184 int status = buffer_get_int(&msg);
186 error("Couldn't get handle: %s", fx2txt(status));
189 } else if (type != SSH2_FXP_HANDLE)
190 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
191 SSH2_FXP_HANDLE, type);
193 handle = buffer_get_string(&msg, len);
200 get_decode_stat(int fd, u_int expected_id, int quiet)
209 type = buffer_get_char(&msg);
210 id = buffer_get_int(&msg);
212 debug3("Received stat reply T:%u I:%u", type, id);
213 if (id != expected_id)
214 fatal("ID mismatch (%u != %u)", id, expected_id);
215 if (type == SSH2_FXP_STATUS) {
216 int status = buffer_get_int(&msg);
219 debug("Couldn't stat remote file: %s", fx2txt(status));
221 error("Couldn't stat remote file: %s", fx2txt(status));
224 } else if (type != SSH2_FXP_ATTRS) {
225 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
226 SSH2_FXP_ATTRS, type);
228 a = decode_attrib(&msg);
235 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
240 struct sftp_conn *ret;
243 buffer_put_char(&msg, SSH2_FXP_INIT);
244 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
245 send_msg(fd_out, &msg);
249 get_msg(fd_in, &msg);
251 /* Expecting a VERSION reply */
252 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
253 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
258 version = buffer_get_int(&msg);
260 debug2("Remote version: %d", version);
262 /* Check for extensions */
263 while (buffer_len(&msg) > 0) {
264 char *name = buffer_get_string(&msg, NULL);
265 char *value = buffer_get_string(&msg, NULL);
267 debug2("Init extension: \"%s\"", name);
274 ret = xmalloc(sizeof(*ret));
276 ret->fd_out = fd_out;
277 ret->transfer_buflen = transfer_buflen;
278 ret->num_requests = num_requests;
279 ret->version = version;
282 /* Some filexfer v.0 servers don't support large packets */
284 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
290 sftp_proto_version(struct sftp_conn *conn)
292 return(conn->version);
296 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
304 buffer_put_char(&msg, SSH2_FXP_CLOSE);
305 buffer_put_int(&msg, id);
306 buffer_put_string(&msg, handle, handle_len);
307 send_msg(conn->fd_out, &msg);
308 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
310 status = get_status(conn->fd_in, id);
311 if (status != SSH2_FX_OK)
312 error("Couldn't close file: %s", fx2txt(status));
321 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
325 u_int count, type, id, handle_len, i, expected_id, ents = 0;
331 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
332 buffer_put_int(&msg, id);
333 buffer_put_cstring(&msg, path);
334 send_msg(conn->fd_out, &msg);
338 handle = get_handle(conn->fd_in, id, &handle_len);
344 *dir = xmalloc(sizeof(**dir));
348 for (; !interrupted;) {
349 id = expected_id = conn->msg_id++;
351 debug3("Sending SSH2_FXP_READDIR I:%u", id);
354 buffer_put_char(&msg, SSH2_FXP_READDIR);
355 buffer_put_int(&msg, id);
356 buffer_put_string(&msg, handle, handle_len);
357 send_msg(conn->fd_out, &msg);
361 get_msg(conn->fd_in, &msg);
363 type = buffer_get_char(&msg);
364 id = buffer_get_int(&msg);
366 debug3("Received reply T:%u I:%u", type, id);
368 if (id != expected_id)
369 fatal("ID mismatch (%u != %u)", id, expected_id);
371 if (type == SSH2_FXP_STATUS) {
372 int status = buffer_get_int(&msg);
374 debug3("Received SSH2_FXP_STATUS %d", status);
376 if (status == SSH2_FX_EOF) {
379 error("Couldn't read directory: %s",
381 do_close(conn, handle, handle_len);
385 } else if (type != SSH2_FXP_NAME)
386 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
387 SSH2_FXP_NAME, type);
389 count = buffer_get_int(&msg);
392 debug3("Received %d SSH2_FXP_NAME responses", count);
393 for (i = 0; i < count; i++) {
394 char *filename, *longname;
397 filename = buffer_get_string(&msg, NULL);
398 longname = buffer_get_string(&msg, NULL);
399 a = decode_attrib(&msg);
402 printf("%s\n", longname);
405 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
406 (*dir)[ents] = xmalloc(sizeof(***dir));
407 (*dir)[ents]->filename = xstrdup(filename);
408 (*dir)[ents]->longname = xstrdup(longname);
409 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
410 (*dir)[++ents] = NULL;
419 do_close(conn, handle, handle_len);
422 /* Don't return partial matches on interrupt */
423 if (interrupted && dir != NULL && *dir != NULL) {
424 free_sftp_dirents(*dir);
425 *dir = xmalloc(sizeof(**dir));
433 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
435 return(do_lsreaddir(conn, path, 0, dir));
438 void free_sftp_dirents(SFTP_DIRENT **s)
442 for (i = 0; s[i]; i++) {
443 xfree(s[i]->filename);
444 xfree(s[i]->longname);
451 do_rm(struct sftp_conn *conn, char *path)
455 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
458 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
460 status = get_status(conn->fd_in, id);
461 if (status != SSH2_FX_OK)
462 error("Couldn't delete file: %s", fx2txt(status));
467 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
472 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
475 status = get_status(conn->fd_in, id);
476 if (status != SSH2_FX_OK)
477 error("Couldn't create directory: %s", fx2txt(status));
483 do_rmdir(struct sftp_conn *conn, char *path)
488 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
491 status = get_status(conn->fd_in, id);
492 if (status != SSH2_FX_OK)
493 error("Couldn't remove directory: %s", fx2txt(status));
499 do_stat(struct sftp_conn *conn, char *path, int quiet)
505 send_string_request(conn->fd_out, id,
506 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
509 return(get_decode_stat(conn->fd_in, id, quiet));
513 do_lstat(struct sftp_conn *conn, char *path, int quiet)
517 if (conn->version == 0) {
519 debug("Server version does not support lstat operation");
521 logit("Server version does not support lstat operation");
522 return(do_stat(conn, path, quiet));
526 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
529 return(get_decode_stat(conn->fd_in, id, quiet));
533 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
538 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
541 return(get_decode_stat(conn->fd_in, id, quiet));
545 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
550 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
553 status = get_status(conn->fd_in, id);
554 if (status != SSH2_FX_OK)
555 error("Couldn't setstat on \"%s\": %s", path,
562 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
568 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
571 status = get_status(conn->fd_in, id);
572 if (status != SSH2_FX_OK)
573 error("Couldn't fsetstat: %s", fx2txt(status));
579 do_realpath(struct sftp_conn *conn, char *path)
582 u_int type, expected_id, count, id;
583 char *filename, *longname;
586 expected_id = id = conn->msg_id++;
587 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
592 get_msg(conn->fd_in, &msg);
593 type = buffer_get_char(&msg);
594 id = buffer_get_int(&msg);
596 if (id != expected_id)
597 fatal("ID mismatch (%u != %u)", id, expected_id);
599 if (type == SSH2_FXP_STATUS) {
600 u_int status = buffer_get_int(&msg);
602 error("Couldn't canonicalise: %s", fx2txt(status));
604 } else if (type != SSH2_FXP_NAME)
605 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
606 SSH2_FXP_NAME, type);
608 count = buffer_get_int(&msg);
610 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
612 filename = buffer_get_string(&msg, NULL);
613 longname = buffer_get_string(&msg, NULL);
614 a = decode_attrib(&msg);
616 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
626 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
633 /* Send rename request */
635 buffer_put_char(&msg, SSH2_FXP_RENAME);
636 buffer_put_int(&msg, id);
637 buffer_put_cstring(&msg, oldpath);
638 buffer_put_cstring(&msg, newpath);
639 send_msg(conn->fd_out, &msg);
640 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
644 status = get_status(conn->fd_in, id);
645 if (status != SSH2_FX_OK)
646 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
647 newpath, fx2txt(status));
653 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
658 if (conn->version < 3) {
659 error("This server does not support the symlink operation");
660 return(SSH2_FX_OP_UNSUPPORTED);
665 /* Send symlink request */
667 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
668 buffer_put_int(&msg, id);
669 buffer_put_cstring(&msg, oldpath);
670 buffer_put_cstring(&msg, newpath);
671 send_msg(conn->fd_out, &msg);
672 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
676 status = get_status(conn->fd_in, id);
677 if (status != SSH2_FX_OK)
678 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
679 newpath, fx2txt(status));
685 do_readlink(struct sftp_conn *conn, char *path)
688 u_int type, expected_id, count, id;
689 char *filename, *longname;
692 expected_id = id = conn->msg_id++;
693 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
698 get_msg(conn->fd_in, &msg);
699 type = buffer_get_char(&msg);
700 id = buffer_get_int(&msg);
702 if (id != expected_id)
703 fatal("ID mismatch (%u != %u)", id, expected_id);
705 if (type == SSH2_FXP_STATUS) {
706 u_int status = buffer_get_int(&msg);
708 error("Couldn't readlink: %s", fx2txt(status));
710 } else if (type != SSH2_FXP_NAME)
711 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
712 SSH2_FXP_NAME, type);
714 count = buffer_get_int(&msg);
716 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
718 filename = buffer_get_string(&msg, NULL);
719 longname = buffer_get_string(&msg, NULL);
720 a = decode_attrib(&msg);
722 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
732 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
733 char *handle, u_int handle_len)
739 buffer_put_char(&msg, SSH2_FXP_READ);
740 buffer_put_int(&msg, id);
741 buffer_put_string(&msg, handle, handle_len);
742 buffer_put_int64(&msg, offset);
743 buffer_put_int(&msg, len);
744 send_msg(fd_out, &msg);
749 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
755 int local_fd, status = 0, write_error;
756 int read_error, write_errno;
757 u_int64_t offset, size;
758 u_int handle_len, mode, type, id, buflen, num_req, max_req;
759 off_t progress_counter;
764 TAILQ_ENTRY(request) tq;
766 TAILQ_HEAD(reqhead, request) requests;
769 TAILQ_INIT(&requests);
771 a = do_stat(conn, remote_path, 0);
775 /* XXX: should we preserve set[ug]id? */
776 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
777 mode = a->perm & 0777;
781 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
782 (!S_ISREG(a->perm))) {
783 error("Cannot download non-regular file: %s", remote_path);
787 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
792 buflen = conn->transfer_buflen;
795 /* Send open request */
797 buffer_put_char(&msg, SSH2_FXP_OPEN);
798 buffer_put_int(&msg, id);
799 buffer_put_cstring(&msg, remote_path);
800 buffer_put_int(&msg, SSH2_FXF_READ);
801 attrib_clear(&junk); /* Send empty attributes */
802 encode_attrib(&msg, &junk);
803 send_msg(conn->fd_out, &msg);
804 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
806 handle = get_handle(conn->fd_in, id, &handle_len);
807 if (handle == NULL) {
812 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
814 if (local_fd == -1) {
815 error("Couldn't open local file \"%s\" for writing: %s",
816 local_path, strerror(errno));
822 /* Read from remote and write to local */
823 write_error = read_error = write_errno = num_req = offset = 0;
825 progress_counter = 0;
827 if (showprogress && size != 0)
828 start_progress_meter(remote_path, size, &progress_counter);
830 while (num_req > 0 || max_req > 0) {
835 * Simulate EOF on interrupt: stop sending new requests and
836 * allow outstanding requests to drain gracefully
839 if (num_req == 0) /* If we haven't started yet... */
844 /* Send some more requests */
845 while (num_req < max_req) {
846 debug3("Request range %llu -> %llu (%d/%d)",
847 (unsigned long long)offset,
848 (unsigned long long)offset + buflen - 1,
850 req = xmalloc(sizeof(*req));
851 req->id = conn->msg_id++;
853 req->offset = offset;
856 TAILQ_INSERT_TAIL(&requests, req, tq);
857 send_read_request(conn->fd_out, req->id, req->offset,
858 req->len, handle, handle_len);
862 get_msg(conn->fd_in, &msg);
863 type = buffer_get_char(&msg);
864 id = buffer_get_int(&msg);
865 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
867 /* Find the request in our queue */
868 for (req = TAILQ_FIRST(&requests);
869 req != NULL && req->id != id;
870 req = TAILQ_NEXT(req, tq))
873 fatal("Unexpected reply %u", id);
876 case SSH2_FXP_STATUS:
877 status = buffer_get_int(&msg);
878 if (status != SSH2_FX_EOF)
881 TAILQ_REMOVE(&requests, req, tq);
886 data = buffer_get_string(&msg, &len);
887 debug3("Received data %llu -> %llu",
888 (unsigned long long)req->offset,
889 (unsigned long long)req->offset + len - 1);
891 fatal("Received more data than asked for "
892 "%u > %u", len, req->len);
893 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
894 atomicio(vwrite, local_fd, data, len) != len) &&
900 progress_counter += len;
903 if (len == req->len) {
904 TAILQ_REMOVE(&requests, req, tq);
908 /* Resend the request for the missing data */
909 debug3("Short data block, re-requesting "
910 "%llu -> %llu (%2d)",
911 (unsigned long long)req->offset + len,
912 (unsigned long long)req->offset +
913 req->len - 1, num_req);
914 req->id = conn->msg_id++;
917 send_read_request(conn->fd_out, req->id,
918 req->offset, req->len, handle, handle_len);
919 /* Reduce the request size */
921 buflen = MAX(MIN_READ_SIZE, len);
923 if (max_req > 0) { /* max_req = 0 iff EOF received */
924 if (size > 0 && offset > size) {
925 /* Only one request at a time
926 * after the expected EOF */
927 debug3("Finish at %llu (%2d)",
928 (unsigned long long)offset,
931 } else if (max_req <= conn->num_requests) {
937 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
938 SSH2_FXP_DATA, type);
942 if (showprogress && size)
943 stop_progress_meter();
946 if (TAILQ_FIRST(&requests) != NULL)
947 fatal("Transfer complete, but requests still in queue");
950 error("Couldn't read from remote file \"%s\" : %s",
951 remote_path, fx2txt(status));
952 do_close(conn, handle, handle_len);
953 } else if (write_error) {
954 error("Couldn't write to \"%s\": %s", local_path,
955 strerror(write_errno));
957 do_close(conn, handle, handle_len);
959 status = do_close(conn, handle, handle_len);
961 /* Override umask and utimes if asked */
963 if (pflag && fchmod(local_fd, mode) == -1)
965 if (pflag && chmod(local_path, mode) == -1)
966 #endif /* HAVE_FCHMOD */
967 error("Couldn't set mode on \"%s\": %s", local_path,
969 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
970 struct timeval tv[2];
971 tv[0].tv_sec = a->atime;
972 tv[1].tv_sec = a->mtime;
973 tv[0].tv_usec = tv[1].tv_usec = 0;
974 if (utimes(local_path, tv) == -1)
975 error("Can't set times on \"%s\": %s",
976 local_path, strerror(errno));
987 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
990 int local_fd, status;
991 u_int handle_len, id, type;
999 struct outstanding_ack {
1003 TAILQ_ENTRY(outstanding_ack) tq;
1005 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1006 struct outstanding_ack *ack = NULL;
1010 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1011 error("Couldn't open local file \"%s\" for reading: %s",
1012 local_path, strerror(errno));
1015 if (fstat(local_fd, &sb) == -1) {
1016 error("Couldn't fstat local file \"%s\": %s",
1017 local_path, strerror(errno));
1021 if (!S_ISREG(sb.st_mode)) {
1022 error("%s is not a regular file", local_path);
1026 stat_to_attrib(&sb, &a);
1028 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1029 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1032 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1036 /* Send open request */
1037 id = conn->msg_id++;
1038 buffer_put_char(&msg, SSH2_FXP_OPEN);
1039 buffer_put_int(&msg, id);
1040 buffer_put_cstring(&msg, remote_path);
1041 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1042 encode_attrib(&msg, &a);
1043 send_msg(conn->fd_out, &msg);
1044 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1048 handle = get_handle(conn->fd_in, id, &handle_len);
1049 if (handle == NULL) {
1055 startid = ackid = id + 1;
1056 data = xmalloc(conn->transfer_buflen);
1058 /* Read from local and write to remote */
1061 start_progress_meter(local_path, sb.st_size, &offset);
1067 * Can't use atomicio here because it returns 0 on EOF,
1068 * thus losing the last block of the file.
1069 * Simulate an EOF on interrupt, allowing ACKs from the
1075 len = read(local_fd, data, conn->transfer_buflen);
1076 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1079 fatal("Couldn't read from \"%s\": %s", local_path,
1083 ack = xmalloc(sizeof(*ack));
1085 ack->offset = offset;
1087 TAILQ_INSERT_TAIL(&acks, ack, tq);
1090 buffer_put_char(&msg, SSH2_FXP_WRITE);
1091 buffer_put_int(&msg, ack->id);
1092 buffer_put_string(&msg, handle, handle_len);
1093 buffer_put_int64(&msg, offset);
1094 buffer_put_string(&msg, data, len);
1095 send_msg(conn->fd_out, &msg);
1096 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1097 id, (unsigned long long)offset, len);
1098 } else if (TAILQ_FIRST(&acks) == NULL)
1102 fatal("Unexpected ACK %u", id);
1104 if (id == startid || len == 0 ||
1105 id - ackid >= conn->num_requests) {
1109 get_msg(conn->fd_in, &msg);
1110 type = buffer_get_char(&msg);
1111 r_id = buffer_get_int(&msg);
1113 if (type != SSH2_FXP_STATUS)
1114 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1115 "got %d", SSH2_FXP_STATUS, type);
1117 status = buffer_get_int(&msg);
1118 debug3("SSH2_FXP_STATUS %d", status);
1120 /* Find the request in our queue */
1121 for (ack = TAILQ_FIRST(&acks);
1122 ack != NULL && ack->id != r_id;
1123 ack = TAILQ_NEXT(ack, tq))
1126 fatal("Can't find request for ID %u", r_id);
1127 TAILQ_REMOVE(&acks, ack, tq);
1129 if (status != SSH2_FX_OK) {
1130 error("Couldn't write to remote file \"%s\": %s",
1131 remote_path, fx2txt(status));
1132 do_close(conn, handle, handle_len);
1138 debug3("In write loop, ack for %u %u bytes at %llu",
1139 ack->id, ack->len, (unsigned long long)ack->offset);
1146 stop_progress_meter();
1149 if (close(local_fd) == -1) {
1150 error("Couldn't close local file \"%s\": %s", local_path,
1152 do_close(conn, handle, handle_len);
1157 /* Override umask and utimes if asked */
1159 do_fsetstat(conn, handle, handle_len, &a);
1161 status = do_close(conn, handle, handle_len);