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 */
23 RCSID("$OpenBSD: sftp-client.c,v 1.57 2005/07/27 10:39:03 dtucker Exp $");
25 #include "openbsd-compat/sys-queue.h"
33 #include "progressmeter.h"
36 #include "sftp-common.h"
37 #include "sftp-client.h"
39 extern volatile sig_atomic_t interrupted;
40 extern int showprogress;
42 /* Minimum amount of data to read at at time */
43 #define MIN_READ_SIZE 512
45 /* Maximum packet size */
46 #define MAX_MSG_LENGTH (256 * 1024)
51 u_int transfer_buflen;
58 send_msg(int fd, Buffer *m)
62 if (buffer_len(m) > MAX_MSG_LENGTH)
63 fatal("Outbound message too long %u", buffer_len(m));
65 /* Send length first */
66 PUT_32BIT(mlen, buffer_len(m));
67 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
68 fatal("Couldn't send packet: %s", strerror(errno));
70 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
71 fatal("Couldn't send packet: %s", strerror(errno));
77 get_msg(int fd, Buffer *m)
81 buffer_append_space(m, 4);
82 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
84 fatal("Connection closed");
86 fatal("Couldn't read packet: %s", strerror(errno));
89 msg_len = buffer_get_int(m);
90 if (msg_len > MAX_MSG_LENGTH)
91 fatal("Received message too long %u", msg_len);
93 buffer_append_space(m, msg_len);
94 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
96 fatal("Connection closed");
98 fatal("Read packet: %s", strerror(errno));
103 send_string_request(int fd, u_int id, u_int code, char *s,
109 buffer_put_char(&msg, code);
110 buffer_put_int(&msg, id);
111 buffer_put_string(&msg, s, len);
113 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
118 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
119 u_int len, Attrib *a)
124 buffer_put_char(&msg, code);
125 buffer_put_int(&msg, id);
126 buffer_put_string(&msg, s, len);
127 encode_attrib(&msg, a);
129 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
134 get_status(int fd, u_int expected_id)
137 u_int type, id, status;
141 type = buffer_get_char(&msg);
142 id = buffer_get_int(&msg);
144 if (id != expected_id)
145 fatal("ID mismatch (%u != %u)", id, expected_id);
146 if (type != SSH2_FXP_STATUS)
147 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
148 SSH2_FXP_STATUS, type);
150 status = buffer_get_int(&msg);
153 debug3("SSH2_FXP_STATUS %u", status);
159 get_handle(int fd, u_int expected_id, u_int *len)
167 type = buffer_get_char(&msg);
168 id = buffer_get_int(&msg);
170 if (id != expected_id)
171 fatal("ID mismatch (%u != %u)", id, expected_id);
172 if (type == SSH2_FXP_STATUS) {
173 int status = buffer_get_int(&msg);
175 error("Couldn't get handle: %s", fx2txt(status));
178 } else if (type != SSH2_FXP_HANDLE)
179 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
180 SSH2_FXP_HANDLE, type);
182 handle = buffer_get_string(&msg, len);
189 get_decode_stat(int fd, u_int expected_id, int quiet)
198 type = buffer_get_char(&msg);
199 id = buffer_get_int(&msg);
201 debug3("Received stat reply T:%u I:%u", type, id);
202 if (id != expected_id)
203 fatal("ID mismatch (%u != %u)", id, expected_id);
204 if (type == SSH2_FXP_STATUS) {
205 int status = buffer_get_int(&msg);
208 debug("Couldn't stat remote file: %s", fx2txt(status));
210 error("Couldn't stat remote file: %s", fx2txt(status));
213 } else if (type != SSH2_FXP_ATTRS) {
214 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
215 SSH2_FXP_ATTRS, type);
217 a = decode_attrib(&msg);
224 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
229 struct sftp_conn *ret;
232 buffer_put_char(&msg, SSH2_FXP_INIT);
233 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
234 send_msg(fd_out, &msg);
238 get_msg(fd_in, &msg);
240 /* Expecting a VERSION reply */
241 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
242 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
247 version = buffer_get_int(&msg);
249 debug2("Remote version: %d", version);
251 /* Check for extensions */
252 while (buffer_len(&msg) > 0) {
253 char *name = buffer_get_string(&msg, NULL);
254 char *value = buffer_get_string(&msg, NULL);
256 debug2("Init extension: \"%s\"", name);
263 ret = xmalloc(sizeof(*ret));
265 ret->fd_out = fd_out;
266 ret->transfer_buflen = transfer_buflen;
267 ret->num_requests = num_requests;
268 ret->version = version;
271 /* Some filexfer v.0 servers don't support large packets */
273 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
279 sftp_proto_version(struct sftp_conn *conn)
281 return(conn->version);
285 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
293 buffer_put_char(&msg, SSH2_FXP_CLOSE);
294 buffer_put_int(&msg, id);
295 buffer_put_string(&msg, handle, handle_len);
296 send_msg(conn->fd_out, &msg);
297 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
299 status = get_status(conn->fd_in, id);
300 if (status != SSH2_FX_OK)
301 error("Couldn't close file: %s", fx2txt(status));
310 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
314 u_int count, type, id, handle_len, i, expected_id, ents = 0;
320 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
321 buffer_put_int(&msg, id);
322 buffer_put_cstring(&msg, path);
323 send_msg(conn->fd_out, &msg);
327 handle = get_handle(conn->fd_in, id, &handle_len);
333 *dir = xmalloc(sizeof(**dir));
337 for (; !interrupted;) {
338 id = expected_id = conn->msg_id++;
340 debug3("Sending SSH2_FXP_READDIR I:%u", id);
343 buffer_put_char(&msg, SSH2_FXP_READDIR);
344 buffer_put_int(&msg, id);
345 buffer_put_string(&msg, handle, handle_len);
346 send_msg(conn->fd_out, &msg);
350 get_msg(conn->fd_in, &msg);
352 type = buffer_get_char(&msg);
353 id = buffer_get_int(&msg);
355 debug3("Received reply T:%u I:%u", type, id);
357 if (id != expected_id)
358 fatal("ID mismatch (%u != %u)", id, expected_id);
360 if (type == SSH2_FXP_STATUS) {
361 int status = buffer_get_int(&msg);
363 debug3("Received SSH2_FXP_STATUS %d", status);
365 if (status == SSH2_FX_EOF) {
368 error("Couldn't read directory: %s",
370 do_close(conn, handle, handle_len);
374 } else if (type != SSH2_FXP_NAME)
375 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
376 SSH2_FXP_NAME, type);
378 count = buffer_get_int(&msg);
381 debug3("Received %d SSH2_FXP_NAME responses", count);
382 for (i = 0; i < count; i++) {
383 char *filename, *longname;
386 filename = buffer_get_string(&msg, NULL);
387 longname = buffer_get_string(&msg, NULL);
388 a = decode_attrib(&msg);
391 printf("%s\n", longname);
394 *dir = xrealloc(*dir, sizeof(**dir) *
396 (*dir)[ents] = xmalloc(sizeof(***dir));
397 (*dir)[ents]->filename = xstrdup(filename);
398 (*dir)[ents]->longname = xstrdup(longname);
399 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
400 (*dir)[++ents] = NULL;
409 do_close(conn, handle, handle_len);
412 /* Don't return partial matches on interrupt */
413 if (interrupted && dir != NULL && *dir != NULL) {
414 free_sftp_dirents(*dir);
415 *dir = xmalloc(sizeof(**dir));
423 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
425 return(do_lsreaddir(conn, path, 0, dir));
428 void free_sftp_dirents(SFTP_DIRENT **s)
432 for (i = 0; s[i]; i++) {
433 xfree(s[i]->filename);
434 xfree(s[i]->longname);
441 do_rm(struct sftp_conn *conn, char *path)
445 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
448 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
450 status = get_status(conn->fd_in, id);
451 if (status != SSH2_FX_OK)
452 error("Couldn't delete file: %s", fx2txt(status));
457 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
462 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
465 status = get_status(conn->fd_in, id);
466 if (status != SSH2_FX_OK)
467 error("Couldn't create directory: %s", fx2txt(status));
473 do_rmdir(struct sftp_conn *conn, char *path)
478 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
481 status = get_status(conn->fd_in, id);
482 if (status != SSH2_FX_OK)
483 error("Couldn't remove directory: %s", fx2txt(status));
489 do_stat(struct sftp_conn *conn, char *path, int quiet)
495 send_string_request(conn->fd_out, id,
496 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
499 return(get_decode_stat(conn->fd_in, id, quiet));
503 do_lstat(struct sftp_conn *conn, char *path, int quiet)
507 if (conn->version == 0) {
509 debug("Server version does not support lstat operation");
511 logit("Server version does not support lstat operation");
512 return(do_stat(conn, path, quiet));
516 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
519 return(get_decode_stat(conn->fd_in, id, quiet));
523 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
528 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
531 return(get_decode_stat(conn->fd_in, id, quiet));
535 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
540 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
543 status = get_status(conn->fd_in, id);
544 if (status != SSH2_FX_OK)
545 error("Couldn't setstat on \"%s\": %s", path,
552 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
558 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
561 status = get_status(conn->fd_in, id);
562 if (status != SSH2_FX_OK)
563 error("Couldn't fsetstat: %s", fx2txt(status));
569 do_realpath(struct sftp_conn *conn, char *path)
572 u_int type, expected_id, count, id;
573 char *filename, *longname;
576 expected_id = id = conn->msg_id++;
577 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
582 get_msg(conn->fd_in, &msg);
583 type = buffer_get_char(&msg);
584 id = buffer_get_int(&msg);
586 if (id != expected_id)
587 fatal("ID mismatch (%u != %u)", id, expected_id);
589 if (type == SSH2_FXP_STATUS) {
590 u_int status = buffer_get_int(&msg);
592 error("Couldn't canonicalise: %s", fx2txt(status));
594 } else if (type != SSH2_FXP_NAME)
595 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
596 SSH2_FXP_NAME, type);
598 count = buffer_get_int(&msg);
600 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
602 filename = buffer_get_string(&msg, NULL);
603 longname = buffer_get_string(&msg, NULL);
604 a = decode_attrib(&msg);
606 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
616 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
623 /* Send rename request */
625 buffer_put_char(&msg, SSH2_FXP_RENAME);
626 buffer_put_int(&msg, id);
627 buffer_put_cstring(&msg, oldpath);
628 buffer_put_cstring(&msg, newpath);
629 send_msg(conn->fd_out, &msg);
630 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
634 status = get_status(conn->fd_in, id);
635 if (status != SSH2_FX_OK)
636 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
637 newpath, fx2txt(status));
643 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
648 if (conn->version < 3) {
649 error("This server does not support the symlink operation");
650 return(SSH2_FX_OP_UNSUPPORTED);
655 /* Send symlink request */
657 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
658 buffer_put_int(&msg, id);
659 buffer_put_cstring(&msg, oldpath);
660 buffer_put_cstring(&msg, newpath);
661 send_msg(conn->fd_out, &msg);
662 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
666 status = get_status(conn->fd_in, id);
667 if (status != SSH2_FX_OK)
668 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
669 newpath, fx2txt(status));
675 do_readlink(struct sftp_conn *conn, char *path)
678 u_int type, expected_id, count, id;
679 char *filename, *longname;
682 expected_id = id = conn->msg_id++;
683 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
688 get_msg(conn->fd_in, &msg);
689 type = buffer_get_char(&msg);
690 id = buffer_get_int(&msg);
692 if (id != expected_id)
693 fatal("ID mismatch (%u != %u)", id, expected_id);
695 if (type == SSH2_FXP_STATUS) {
696 u_int status = buffer_get_int(&msg);
698 error("Couldn't readlink: %s", fx2txt(status));
700 } else if (type != SSH2_FXP_NAME)
701 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
702 SSH2_FXP_NAME, type);
704 count = buffer_get_int(&msg);
706 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
708 filename = buffer_get_string(&msg, NULL);
709 longname = buffer_get_string(&msg, NULL);
710 a = decode_attrib(&msg);
712 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
722 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
723 char *handle, u_int handle_len)
729 buffer_put_char(&msg, SSH2_FXP_READ);
730 buffer_put_int(&msg, id);
731 buffer_put_string(&msg, handle, handle_len);
732 buffer_put_int64(&msg, offset);
733 buffer_put_int(&msg, len);
734 send_msg(fd_out, &msg);
739 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
745 int local_fd, status = 0, write_error;
746 int read_error, write_errno;
747 u_int64_t offset, size;
748 u_int handle_len, mode, type, id, buflen, num_req, max_req;
749 off_t progress_counter;
754 TAILQ_ENTRY(request) tq;
756 TAILQ_HEAD(reqhead, request) requests;
759 TAILQ_INIT(&requests);
761 a = do_stat(conn, remote_path, 0);
765 /* XXX: should we preserve set[ug]id? */
766 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
767 mode = a->perm & 0777;
771 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
772 (!S_ISREG(a->perm))) {
773 error("Cannot download non-regular file: %s", remote_path);
777 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
782 buflen = conn->transfer_buflen;
785 /* Send open request */
787 buffer_put_char(&msg, SSH2_FXP_OPEN);
788 buffer_put_int(&msg, id);
789 buffer_put_cstring(&msg, remote_path);
790 buffer_put_int(&msg, SSH2_FXF_READ);
791 attrib_clear(&junk); /* Send empty attributes */
792 encode_attrib(&msg, &junk);
793 send_msg(conn->fd_out, &msg);
794 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
796 handle = get_handle(conn->fd_in, id, &handle_len);
797 if (handle == NULL) {
802 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
804 if (local_fd == -1) {
805 error("Couldn't open local file \"%s\" for writing: %s",
806 local_path, strerror(errno));
812 /* Read from remote and write to local */
813 write_error = read_error = write_errno = num_req = offset = 0;
815 progress_counter = 0;
817 if (showprogress && size != 0)
818 start_progress_meter(remote_path, size, &progress_counter);
820 while (num_req > 0 || max_req > 0) {
825 * Simulate EOF on interrupt: stop sending new requests and
826 * allow outstanding requests to drain gracefully
829 if (num_req == 0) /* If we haven't started yet... */
834 /* Send some more requests */
835 while (num_req < max_req) {
836 debug3("Request range %llu -> %llu (%d/%d)",
837 (unsigned long long)offset,
838 (unsigned long long)offset + buflen - 1,
840 req = xmalloc(sizeof(*req));
841 req->id = conn->msg_id++;
843 req->offset = offset;
846 TAILQ_INSERT_TAIL(&requests, req, tq);
847 send_read_request(conn->fd_out, req->id, req->offset,
848 req->len, handle, handle_len);
852 get_msg(conn->fd_in, &msg);
853 type = buffer_get_char(&msg);
854 id = buffer_get_int(&msg);
855 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
857 /* Find the request in our queue */
858 for (req = TAILQ_FIRST(&requests);
859 req != NULL && req->id != id;
860 req = TAILQ_NEXT(req, tq))
863 fatal("Unexpected reply %u", id);
866 case SSH2_FXP_STATUS:
867 status = buffer_get_int(&msg);
868 if (status != SSH2_FX_EOF)
871 TAILQ_REMOVE(&requests, req, tq);
876 data = buffer_get_string(&msg, &len);
877 debug3("Received data %llu -> %llu",
878 (unsigned long long)req->offset,
879 (unsigned long long)req->offset + len - 1);
881 fatal("Received more data than asked for "
882 "%u > %u", len, req->len);
883 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
884 atomicio(vwrite, local_fd, data, len) != len) &&
890 progress_counter += len;
893 if (len == req->len) {
894 TAILQ_REMOVE(&requests, req, tq);
898 /* Resend the request for the missing data */
899 debug3("Short data block, re-requesting "
900 "%llu -> %llu (%2d)",
901 (unsigned long long)req->offset + len,
902 (unsigned long long)req->offset +
903 req->len - 1, num_req);
904 req->id = conn->msg_id++;
907 send_read_request(conn->fd_out, req->id,
908 req->offset, req->len, handle, handle_len);
909 /* Reduce the request size */
911 buflen = MAX(MIN_READ_SIZE, len);
913 if (max_req > 0) { /* max_req = 0 iff EOF received */
914 if (size > 0 && offset > size) {
915 /* Only one request at a time
916 * after the expected EOF */
917 debug3("Finish at %llu (%2d)",
918 (unsigned long long)offset,
921 } else if (max_req <= conn->num_requests) {
927 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
928 SSH2_FXP_DATA, type);
932 if (showprogress && size)
933 stop_progress_meter();
936 if (TAILQ_FIRST(&requests) != NULL)
937 fatal("Transfer complete, but requests still in queue");
940 error("Couldn't read from remote file \"%s\" : %s",
941 remote_path, fx2txt(status));
942 do_close(conn, handle, handle_len);
943 } else if (write_error) {
944 error("Couldn't write to \"%s\": %s", local_path,
945 strerror(write_errno));
947 do_close(conn, handle, handle_len);
949 status = do_close(conn, handle, handle_len);
951 /* Override umask and utimes if asked */
953 if (pflag && fchmod(local_fd, mode) == -1)
955 if (pflag && chmod(local_path, mode) == -1)
956 #endif /* HAVE_FCHMOD */
957 error("Couldn't set mode on \"%s\": %s", local_path,
959 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
960 struct timeval tv[2];
961 tv[0].tv_sec = a->atime;
962 tv[1].tv_sec = a->mtime;
963 tv[0].tv_usec = tv[1].tv_usec = 0;
964 if (utimes(local_path, tv) == -1)
965 error("Can't set times on \"%s\": %s",
966 local_path, strerror(errno));
977 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
980 int local_fd, status;
981 u_int handle_len, id, type;
989 struct outstanding_ack {
993 TAILQ_ENTRY(outstanding_ack) tq;
995 TAILQ_HEAD(ackhead, outstanding_ack) acks;
996 struct outstanding_ack *ack = NULL;
1000 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1001 error("Couldn't open local file \"%s\" for reading: %s",
1002 local_path, strerror(errno));
1005 if (fstat(local_fd, &sb) == -1) {
1006 error("Couldn't fstat local file \"%s\": %s",
1007 local_path, strerror(errno));
1011 if (!S_ISREG(sb.st_mode)) {
1012 error("%s is not a regular file", local_path);
1016 stat_to_attrib(&sb, &a);
1018 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1019 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1022 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1026 /* Send open request */
1027 id = conn->msg_id++;
1028 buffer_put_char(&msg, SSH2_FXP_OPEN);
1029 buffer_put_int(&msg, id);
1030 buffer_put_cstring(&msg, remote_path);
1031 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1032 encode_attrib(&msg, &a);
1033 send_msg(conn->fd_out, &msg);
1034 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1038 handle = get_handle(conn->fd_in, id, &handle_len);
1039 if (handle == NULL) {
1045 startid = ackid = id + 1;
1046 data = xmalloc(conn->transfer_buflen);
1048 /* Read from local and write to remote */
1051 start_progress_meter(local_path, sb.st_size, &offset);
1057 * Can't use atomicio here because it returns 0 on EOF,
1058 * thus losing the last block of the file.
1059 * Simulate an EOF on interrupt, allowing ACKs from the
1065 len = read(local_fd, data, conn->transfer_buflen);
1066 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1069 fatal("Couldn't read from \"%s\": %s", local_path,
1073 ack = xmalloc(sizeof(*ack));
1075 ack->offset = offset;
1077 TAILQ_INSERT_TAIL(&acks, ack, tq);
1080 buffer_put_char(&msg, SSH2_FXP_WRITE);
1081 buffer_put_int(&msg, ack->id);
1082 buffer_put_string(&msg, handle, handle_len);
1083 buffer_put_int64(&msg, offset);
1084 buffer_put_string(&msg, data, len);
1085 send_msg(conn->fd_out, &msg);
1086 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1087 id, (unsigned long long)offset, len);
1088 } else if (TAILQ_FIRST(&acks) == NULL)
1092 fatal("Unexpected ACK %u", id);
1094 if (id == startid || len == 0 ||
1095 id - ackid >= conn->num_requests) {
1099 get_msg(conn->fd_in, &msg);
1100 type = buffer_get_char(&msg);
1101 r_id = buffer_get_int(&msg);
1103 if (type != SSH2_FXP_STATUS)
1104 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1105 "got %d", SSH2_FXP_STATUS, type);
1107 status = buffer_get_int(&msg);
1108 debug3("SSH2_FXP_STATUS %d", status);
1110 /* Find the request in our queue */
1111 for (ack = TAILQ_FIRST(&acks);
1112 ack != NULL && ack->id != r_id;
1113 ack = TAILQ_NEXT(ack, tq))
1116 fatal("Can't find request for ID %u", r_id);
1117 TAILQ_REMOVE(&acks, ack, tq);
1119 if (status != SSH2_FX_OK) {
1120 error("Couldn't write to remote file \"%s\": %s",
1121 remote_path, fx2txt(status));
1122 do_close(conn, handle, handle_len);
1128 debug3("In write loop, ack for %u %u bytes at %llu",
1129 ack->id, ack->len, (unsigned long long)ack->offset);
1136 stop_progress_meter();
1139 if (close(local_fd) == -1) {
1140 error("Couldn't close local file \"%s\": %s", local_path,
1142 do_close(conn, handle, handle_len);
1147 /* Override umask and utimes if asked */
1149 do_fsetstat(conn, handle, handle_len, &a);
1151 status = do_close(conn, handle, handle_len);