2 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* XXX: signed vs unsigned */
19 /* XXX: remove all logging, only return status codes */
20 /* XXX: copy between two remote sites */
24 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
30 #include "openbsd-compat/sys-queue.h"
38 #include "progressmeter.h"
41 #include "sftp-common.h"
42 #include "sftp-client.h"
44 extern volatile sig_atomic_t interrupted;
45 extern int showprogress;
47 /* Minimum amount of data to read at a time */
48 #define MIN_READ_SIZE 512
53 u_int transfer_buflen;
60 send_msg(int fd, Buffer *m)
64 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
65 fatal("Outbound message too long %u", buffer_len(m));
67 /* Send length first */
68 PUT_32BIT(mlen, buffer_len(m));
69 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
70 fatal("Couldn't send packet: %s", strerror(errno));
72 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
73 fatal("Couldn't send packet: %s", strerror(errno));
79 get_msg(int fd, Buffer *m)
83 buffer_append_space(m, 4);
84 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
86 fatal("Connection closed");
88 fatal("Couldn't read packet: %s", strerror(errno));
91 msg_len = buffer_get_int(m);
92 if (msg_len > SFTP_MAX_MSG_LENGTH)
93 fatal("Received message too long %u", msg_len);
95 buffer_append_space(m, msg_len);
96 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
98 fatal("Connection closed");
100 fatal("Read packet: %s", strerror(errno));
105 send_string_request(int fd, u_int id, u_int code, char *s,
111 buffer_put_char(&msg, code);
112 buffer_put_int(&msg, id);
113 buffer_put_string(&msg, s, len);
115 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
120 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
121 u_int len, Attrib *a)
126 buffer_put_char(&msg, code);
127 buffer_put_int(&msg, id);
128 buffer_put_string(&msg, s, len);
129 encode_attrib(&msg, a);
131 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
136 get_status(int fd, u_int expected_id)
139 u_int type, id, status;
143 type = buffer_get_char(&msg);
144 id = buffer_get_int(&msg);
146 if (id != expected_id)
147 fatal("ID mismatch (%u != %u)", id, expected_id);
148 if (type != SSH2_FXP_STATUS)
149 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
150 SSH2_FXP_STATUS, type);
152 status = buffer_get_int(&msg);
155 debug3("SSH2_FXP_STATUS %u", status);
161 get_handle(int fd, u_int expected_id, u_int *len)
169 type = buffer_get_char(&msg);
170 id = buffer_get_int(&msg);
172 if (id != expected_id)
173 fatal("ID mismatch (%u != %u)", id, expected_id);
174 if (type == SSH2_FXP_STATUS) {
175 int status = buffer_get_int(&msg);
177 error("Couldn't get handle: %s", fx2txt(status));
180 } else if (type != SSH2_FXP_HANDLE)
181 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
182 SSH2_FXP_HANDLE, type);
184 handle = buffer_get_string(&msg, len);
191 get_decode_stat(int fd, u_int expected_id, int quiet)
200 type = buffer_get_char(&msg);
201 id = buffer_get_int(&msg);
203 debug3("Received stat reply T:%u I:%u", type, id);
204 if (id != expected_id)
205 fatal("ID mismatch (%u != %u)", id, expected_id);
206 if (type == SSH2_FXP_STATUS) {
207 int status = buffer_get_int(&msg);
210 debug("Couldn't stat remote file: %s", fx2txt(status));
212 error("Couldn't stat remote file: %s", fx2txt(status));
215 } else if (type != SSH2_FXP_ATTRS) {
216 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
217 SSH2_FXP_ATTRS, type);
219 a = decode_attrib(&msg);
226 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
231 struct sftp_conn *ret;
234 buffer_put_char(&msg, SSH2_FXP_INIT);
235 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
236 send_msg(fd_out, &msg);
240 get_msg(fd_in, &msg);
242 /* Expecting a VERSION reply */
243 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
244 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
249 version = buffer_get_int(&msg);
251 debug2("Remote version: %d", version);
253 /* Check for extensions */
254 while (buffer_len(&msg) > 0) {
255 char *name = buffer_get_string(&msg, NULL);
256 char *value = buffer_get_string(&msg, NULL);
258 debug2("Init extension: \"%s\"", name);
265 ret = xmalloc(sizeof(*ret));
267 ret->fd_out = fd_out;
268 ret->transfer_buflen = transfer_buflen;
269 ret->num_requests = num_requests;
270 ret->version = version;
273 /* Some filexfer v.0 servers don't support large packets */
275 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
281 sftp_proto_version(struct sftp_conn *conn)
283 return(conn->version);
287 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
295 buffer_put_char(&msg, SSH2_FXP_CLOSE);
296 buffer_put_int(&msg, id);
297 buffer_put_string(&msg, handle, handle_len);
298 send_msg(conn->fd_out, &msg);
299 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
301 status = get_status(conn->fd_in, id);
302 if (status != SSH2_FX_OK)
303 error("Couldn't close file: %s", fx2txt(status));
312 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
316 u_int count, type, id, handle_len, i, expected_id, ents = 0;
322 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
323 buffer_put_int(&msg, id);
324 buffer_put_cstring(&msg, path);
325 send_msg(conn->fd_out, &msg);
329 handle = get_handle(conn->fd_in, id, &handle_len);
335 *dir = xmalloc(sizeof(**dir));
339 for (; !interrupted;) {
340 id = expected_id = conn->msg_id++;
342 debug3("Sending SSH2_FXP_READDIR I:%u", id);
345 buffer_put_char(&msg, SSH2_FXP_READDIR);
346 buffer_put_int(&msg, id);
347 buffer_put_string(&msg, handle, handle_len);
348 send_msg(conn->fd_out, &msg);
352 get_msg(conn->fd_in, &msg);
354 type = buffer_get_char(&msg);
355 id = buffer_get_int(&msg);
357 debug3("Received reply T:%u I:%u", type, id);
359 if (id != expected_id)
360 fatal("ID mismatch (%u != %u)", id, expected_id);
362 if (type == SSH2_FXP_STATUS) {
363 int status = buffer_get_int(&msg);
365 debug3("Received SSH2_FXP_STATUS %d", status);
367 if (status == SSH2_FX_EOF) {
370 error("Couldn't read directory: %s",
372 do_close(conn, handle, handle_len);
376 } else if (type != SSH2_FXP_NAME)
377 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
378 SSH2_FXP_NAME, type);
380 count = buffer_get_int(&msg);
383 debug3("Received %d SSH2_FXP_NAME responses", count);
384 for (i = 0; i < count; i++) {
385 char *filename, *longname;
388 filename = buffer_get_string(&msg, NULL);
389 longname = buffer_get_string(&msg, NULL);
390 a = decode_attrib(&msg);
393 printf("%s\n", longname);
396 *dir = xrealloc(*dir, sizeof(**dir) *
398 (*dir)[ents] = xmalloc(sizeof(***dir));
399 (*dir)[ents]->filename = xstrdup(filename);
400 (*dir)[ents]->longname = xstrdup(longname);
401 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
402 (*dir)[++ents] = NULL;
411 do_close(conn, handle, handle_len);
414 /* Don't return partial matches on interrupt */
415 if (interrupted && dir != NULL && *dir != NULL) {
416 free_sftp_dirents(*dir);
417 *dir = xmalloc(sizeof(**dir));
425 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
427 return(do_lsreaddir(conn, path, 0, dir));
430 void free_sftp_dirents(SFTP_DIRENT **s)
434 for (i = 0; s[i]; i++) {
435 xfree(s[i]->filename);
436 xfree(s[i]->longname);
443 do_rm(struct sftp_conn *conn, char *path)
447 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
450 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
452 status = get_status(conn->fd_in, id);
453 if (status != SSH2_FX_OK)
454 error("Couldn't delete file: %s", fx2txt(status));
459 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
464 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
467 status = get_status(conn->fd_in, id);
468 if (status != SSH2_FX_OK)
469 error("Couldn't create directory: %s", fx2txt(status));
475 do_rmdir(struct sftp_conn *conn, char *path)
480 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
483 status = get_status(conn->fd_in, id);
484 if (status != SSH2_FX_OK)
485 error("Couldn't remove directory: %s", fx2txt(status));
491 do_stat(struct sftp_conn *conn, char *path, int quiet)
497 send_string_request(conn->fd_out, id,
498 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
501 return(get_decode_stat(conn->fd_in, id, quiet));
505 do_lstat(struct sftp_conn *conn, char *path, int quiet)
509 if (conn->version == 0) {
511 debug("Server version does not support lstat operation");
513 logit("Server version does not support lstat operation");
514 return(do_stat(conn, path, quiet));
518 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
521 return(get_decode_stat(conn->fd_in, id, quiet));
525 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
530 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
533 return(get_decode_stat(conn->fd_in, id, quiet));
537 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
542 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
545 status = get_status(conn->fd_in, id);
546 if (status != SSH2_FX_OK)
547 error("Couldn't setstat on \"%s\": %s", path,
554 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
560 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
563 status = get_status(conn->fd_in, id);
564 if (status != SSH2_FX_OK)
565 error("Couldn't fsetstat: %s", fx2txt(status));
571 do_realpath(struct sftp_conn *conn, char *path)
574 u_int type, expected_id, count, id;
575 char *filename, *longname;
578 expected_id = id = conn->msg_id++;
579 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
584 get_msg(conn->fd_in, &msg);
585 type = buffer_get_char(&msg);
586 id = buffer_get_int(&msg);
588 if (id != expected_id)
589 fatal("ID mismatch (%u != %u)", id, expected_id);
591 if (type == SSH2_FXP_STATUS) {
592 u_int status = buffer_get_int(&msg);
594 error("Couldn't canonicalise: %s", fx2txt(status));
596 } else if (type != SSH2_FXP_NAME)
597 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
598 SSH2_FXP_NAME, type);
600 count = buffer_get_int(&msg);
602 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
604 filename = buffer_get_string(&msg, NULL);
605 longname = buffer_get_string(&msg, NULL);
606 a = decode_attrib(&msg);
608 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
618 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
625 /* Send rename request */
627 buffer_put_char(&msg, SSH2_FXP_RENAME);
628 buffer_put_int(&msg, id);
629 buffer_put_cstring(&msg, oldpath);
630 buffer_put_cstring(&msg, newpath);
631 send_msg(conn->fd_out, &msg);
632 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
636 status = get_status(conn->fd_in, id);
637 if (status != SSH2_FX_OK)
638 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
639 newpath, fx2txt(status));
645 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
650 if (conn->version < 3) {
651 error("This server does not support the symlink operation");
652 return(SSH2_FX_OP_UNSUPPORTED);
657 /* Send symlink request */
659 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
660 buffer_put_int(&msg, id);
661 buffer_put_cstring(&msg, oldpath);
662 buffer_put_cstring(&msg, newpath);
663 send_msg(conn->fd_out, &msg);
664 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
668 status = get_status(conn->fd_in, id);
669 if (status != SSH2_FX_OK)
670 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
671 newpath, fx2txt(status));
677 do_readlink(struct sftp_conn *conn, char *path)
680 u_int type, expected_id, count, id;
681 char *filename, *longname;
684 expected_id = id = conn->msg_id++;
685 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
690 get_msg(conn->fd_in, &msg);
691 type = buffer_get_char(&msg);
692 id = buffer_get_int(&msg);
694 if (id != expected_id)
695 fatal("ID mismatch (%u != %u)", id, expected_id);
697 if (type == SSH2_FXP_STATUS) {
698 u_int status = buffer_get_int(&msg);
700 error("Couldn't readlink: %s", fx2txt(status));
702 } else if (type != SSH2_FXP_NAME)
703 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
704 SSH2_FXP_NAME, type);
706 count = buffer_get_int(&msg);
708 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
710 filename = buffer_get_string(&msg, NULL);
711 longname = buffer_get_string(&msg, NULL);
712 a = decode_attrib(&msg);
714 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
724 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
725 char *handle, u_int handle_len)
731 buffer_put_char(&msg, SSH2_FXP_READ);
732 buffer_put_int(&msg, id);
733 buffer_put_string(&msg, handle, handle_len);
734 buffer_put_int64(&msg, offset);
735 buffer_put_int(&msg, len);
736 send_msg(fd_out, &msg);
741 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
747 int local_fd, status = 0, write_error;
748 int read_error, write_errno;
749 u_int64_t offset, size;
750 u_int handle_len, mode, type, id, buflen, num_req, max_req;
751 off_t progress_counter;
756 TAILQ_ENTRY(request) tq;
758 TAILQ_HEAD(reqhead, request) requests;
761 TAILQ_INIT(&requests);
763 a = do_stat(conn, remote_path, 0);
767 /* XXX: should we preserve set[ug]id? */
768 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
769 mode = a->perm & 0777;
773 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
774 (!S_ISREG(a->perm))) {
775 error("Cannot download non-regular file: %s", remote_path);
779 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
784 buflen = conn->transfer_buflen;
787 /* Send open request */
789 buffer_put_char(&msg, SSH2_FXP_OPEN);
790 buffer_put_int(&msg, id);
791 buffer_put_cstring(&msg, remote_path);
792 buffer_put_int(&msg, SSH2_FXF_READ);
793 attrib_clear(&junk); /* Send empty attributes */
794 encode_attrib(&msg, &junk);
795 send_msg(conn->fd_out, &msg);
796 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
798 handle = get_handle(conn->fd_in, id, &handle_len);
799 if (handle == NULL) {
804 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
806 if (local_fd == -1) {
807 error("Couldn't open local file \"%s\" for writing: %s",
808 local_path, strerror(errno));
814 /* Read from remote and write to local */
815 write_error = read_error = write_errno = num_req = offset = 0;
817 progress_counter = 0;
819 if (showprogress && size != 0)
820 start_progress_meter(remote_path, size, &progress_counter);
822 while (num_req > 0 || max_req > 0) {
827 * Simulate EOF on interrupt: stop sending new requests and
828 * allow outstanding requests to drain gracefully
831 if (num_req == 0) /* If we haven't started yet... */
836 /* Send some more requests */
837 while (num_req < max_req) {
838 debug3("Request range %llu -> %llu (%d/%d)",
839 (unsigned long long)offset,
840 (unsigned long long)offset + buflen - 1,
842 req = xmalloc(sizeof(*req));
843 req->id = conn->msg_id++;
845 req->offset = offset;
848 TAILQ_INSERT_TAIL(&requests, req, tq);
849 send_read_request(conn->fd_out, req->id, req->offset,
850 req->len, handle, handle_len);
854 get_msg(conn->fd_in, &msg);
855 type = buffer_get_char(&msg);
856 id = buffer_get_int(&msg);
857 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
859 /* Find the request in our queue */
860 for (req = TAILQ_FIRST(&requests);
861 req != NULL && req->id != id;
862 req = TAILQ_NEXT(req, tq))
865 fatal("Unexpected reply %u", id);
868 case SSH2_FXP_STATUS:
869 status = buffer_get_int(&msg);
870 if (status != SSH2_FX_EOF)
873 TAILQ_REMOVE(&requests, req, tq);
878 data = buffer_get_string(&msg, &len);
879 debug3("Received data %llu -> %llu",
880 (unsigned long long)req->offset,
881 (unsigned long long)req->offset + len - 1);
883 fatal("Received more data than asked for "
884 "%u > %u", len, req->len);
885 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
886 atomicio(vwrite, local_fd, data, len) != len) &&
892 progress_counter += len;
895 if (len == req->len) {
896 TAILQ_REMOVE(&requests, req, tq);
900 /* Resend the request for the missing data */
901 debug3("Short data block, re-requesting "
902 "%llu -> %llu (%2d)",
903 (unsigned long long)req->offset + len,
904 (unsigned long long)req->offset +
905 req->len - 1, num_req);
906 req->id = conn->msg_id++;
909 send_read_request(conn->fd_out, req->id,
910 req->offset, req->len, handle, handle_len);
911 /* Reduce the request size */
913 buflen = MAX(MIN_READ_SIZE, len);
915 if (max_req > 0) { /* max_req = 0 iff EOF received */
916 if (size > 0 && offset > size) {
917 /* Only one request at a time
918 * after the expected EOF */
919 debug3("Finish at %llu (%2d)",
920 (unsigned long long)offset,
923 } else if (max_req <= conn->num_requests) {
929 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
930 SSH2_FXP_DATA, type);
934 if (showprogress && size)
935 stop_progress_meter();
938 if (TAILQ_FIRST(&requests) != NULL)
939 fatal("Transfer complete, but requests still in queue");
942 error("Couldn't read from remote file \"%s\" : %s",
943 remote_path, fx2txt(status));
944 do_close(conn, handle, handle_len);
945 } else if (write_error) {
946 error("Couldn't write to \"%s\": %s", local_path,
947 strerror(write_errno));
949 do_close(conn, handle, handle_len);
951 status = do_close(conn, handle, handle_len);
953 /* Override umask and utimes if asked */
955 if (pflag && fchmod(local_fd, mode) == -1)
957 if (pflag && chmod(local_path, mode) == -1)
958 #endif /* HAVE_FCHMOD */
959 error("Couldn't set mode on \"%s\": %s", local_path,
961 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
962 struct timeval tv[2];
963 tv[0].tv_sec = a->atime;
964 tv[1].tv_sec = a->mtime;
965 tv[0].tv_usec = tv[1].tv_usec = 0;
966 if (utimes(local_path, tv) == -1)
967 error("Can't set times on \"%s\": %s",
968 local_path, strerror(errno));
979 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
982 int local_fd, status;
983 u_int handle_len, id, type;
991 struct outstanding_ack {
995 TAILQ_ENTRY(outstanding_ack) tq;
997 TAILQ_HEAD(ackhead, outstanding_ack) acks;
998 struct outstanding_ack *ack = NULL;
1002 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1003 error("Couldn't open local file \"%s\" for reading: %s",
1004 local_path, strerror(errno));
1007 if (fstat(local_fd, &sb) == -1) {
1008 error("Couldn't fstat local file \"%s\": %s",
1009 local_path, strerror(errno));
1013 if (!S_ISREG(sb.st_mode)) {
1014 error("%s is not a regular file", local_path);
1018 stat_to_attrib(&sb, &a);
1020 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1021 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1024 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1028 /* Send open request */
1029 id = conn->msg_id++;
1030 buffer_put_char(&msg, SSH2_FXP_OPEN);
1031 buffer_put_int(&msg, id);
1032 buffer_put_cstring(&msg, remote_path);
1033 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1034 encode_attrib(&msg, &a);
1035 send_msg(conn->fd_out, &msg);
1036 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1040 handle = get_handle(conn->fd_in, id, &handle_len);
1041 if (handle == NULL) {
1047 startid = ackid = id + 1;
1048 data = xmalloc(conn->transfer_buflen);
1050 /* Read from local and write to remote */
1053 start_progress_meter(local_path, sb.st_size, &offset);
1059 * Can't use atomicio here because it returns 0 on EOF,
1060 * thus losing the last block of the file.
1061 * Simulate an EOF on interrupt, allowing ACKs from the
1067 len = read(local_fd, data, conn->transfer_buflen);
1068 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1071 fatal("Couldn't read from \"%s\": %s", local_path,
1075 ack = xmalloc(sizeof(*ack));
1077 ack->offset = offset;
1079 TAILQ_INSERT_TAIL(&acks, ack, tq);
1082 buffer_put_char(&msg, SSH2_FXP_WRITE);
1083 buffer_put_int(&msg, ack->id);
1084 buffer_put_string(&msg, handle, handle_len);
1085 buffer_put_int64(&msg, offset);
1086 buffer_put_string(&msg, data, len);
1087 send_msg(conn->fd_out, &msg);
1088 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1089 id, (unsigned long long)offset, len);
1090 } else if (TAILQ_FIRST(&acks) == NULL)
1094 fatal("Unexpected ACK %u", id);
1096 if (id == startid || len == 0 ||
1097 id - ackid >= conn->num_requests) {
1101 get_msg(conn->fd_in, &msg);
1102 type = buffer_get_char(&msg);
1103 r_id = buffer_get_int(&msg);
1105 if (type != SSH2_FXP_STATUS)
1106 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1107 "got %d", SSH2_FXP_STATUS, type);
1109 status = buffer_get_int(&msg);
1110 debug3("SSH2_FXP_STATUS %d", status);
1112 /* Find the request in our queue */
1113 for (ack = TAILQ_FIRST(&acks);
1114 ack != NULL && ack->id != r_id;
1115 ack = TAILQ_NEXT(ack, tq))
1118 fatal("Can't find request for ID %u", r_id);
1119 TAILQ_REMOVE(&acks, ack, tq);
1121 if (status != SSH2_FX_OK) {
1122 error("Couldn't write to remote file \"%s\": %s",
1123 remote_path, fx2txt(status));
1124 do_close(conn, handle, handle_len);
1130 debug3("In write loop, ack for %u %u bytes at %llu",
1131 ack->id, ack->len, (unsigned long long)ack->offset);
1138 stop_progress_meter();
1141 if (close(local_fd) == -1) {
1142 error("Couldn't close local file \"%s\": %s", local_path,
1144 do_close(conn, handle, handle_len);
1149 /* Override umask and utimes if asked */
1151 do_fsetstat(conn, handle, handle_len, &a);
1153 status = do_close(conn, handle, handle_len);