2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /* XXX: signed vs unsigned */
27 /* XXX: remove all logging, only return status codes */
28 /* XXX: copy between two remote sites */
31 RCSID("$OpenBSD: sftp-client.c,v 1.31 2002/04/06 00:30:08 djm Exp $");
33 #include "openbsd-compat/fake-queue.h"
43 #include "sftp-common.h"
44 #include "sftp-client.h"
46 /* Minimum amount of data to read at at time */
47 #define MIN_READ_SIZE 512
52 u_int transfer_buflen;
59 send_msg(int fd, Buffer *m)
61 int mlen = buffer_len(m);
66 buffer_put_int(&oqueue, mlen);
67 buffer_append(&oqueue, buffer_ptr(m), mlen);
68 buffer_consume(m, mlen);
70 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
72 fatal("Couldn't send packet: %s", strerror(errno));
78 get_msg(int fd, Buffer *m)
81 unsigned char buf[4096];
83 len = atomicio(read, fd, buf, 4);
85 fatal("Connection closed");
87 fatal("Couldn't read packet: %s", strerror(errno));
89 msg_len = GET_32BIT(buf);
90 if (msg_len > 256 * 1024)
91 fatal("Received message too long %d", msg_len);
94 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
96 fatal("Connection closed");
98 fatal("Couldn't read packet: %s", strerror(errno));
101 buffer_append(m, buf, len);
106 send_string_request(int fd, u_int id, u_int code, char *s,
112 buffer_put_char(&msg, code);
113 buffer_put_int(&msg, id);
114 buffer_put_string(&msg, s, len);
116 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
121 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
122 u_int len, Attrib *a)
127 buffer_put_char(&msg, code);
128 buffer_put_int(&msg, id);
129 buffer_put_string(&msg, s, len);
130 encode_attrib(&msg, a);
132 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
137 get_status(int fd, int expected_id)
140 u_int type, id, status;
144 type = buffer_get_char(&msg);
145 id = buffer_get_int(&msg);
147 if (id != expected_id)
148 fatal("ID mismatch (%d != %d)", id, expected_id);
149 if (type != SSH2_FXP_STATUS)
150 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
151 SSH2_FXP_STATUS, type);
153 status = buffer_get_int(&msg);
156 debug3("SSH2_FXP_STATUS %d", status);
162 get_handle(int fd, u_int expected_id, u_int *len)
170 type = buffer_get_char(&msg);
171 id = buffer_get_int(&msg);
173 if (id != expected_id)
174 fatal("ID mismatch (%d != %d)", id, expected_id);
175 if (type == SSH2_FXP_STATUS) {
176 int status = buffer_get_int(&msg);
178 error("Couldn't get handle: %s", fx2txt(status));
180 } else if (type != SSH2_FXP_HANDLE)
181 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
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:%d I:%d", type, id);
204 if (id != expected_id)
205 fatal("ID mismatch (%d != %d)", 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));
214 } else if (type != SSH2_FXP_ATTRS) {
215 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
216 SSH2_FXP_ATTRS, type);
218 a = decode_attrib(&msg);
225 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 %d)",
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:%d", 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 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));
340 id = expected_id = conn->msg_id++;
342 debug3("Sending SSH2_FXP_READDIR I:%d", 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:%d I:%d", type, id);
359 if (id != expected_id)
360 fatal("ID mismatch (%d != %d)", 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);
375 } else if (type != SSH2_FXP_NAME)
376 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
377 SSH2_FXP_NAME, type);
379 count = buffer_get_int(&msg);
382 debug3("Received %d SSH2_FXP_NAME responses", count);
383 for (i = 0; i < count; i++) {
384 char *filename, *longname;
387 filename = buffer_get_string(&msg, NULL);
388 longname = buffer_get_string(&msg, NULL);
389 a = decode_attrib(&msg);
392 printf("%s\n", longname);
395 *dir = xrealloc(*dir, sizeof(**dir) *
397 (*dir)[ents] = xmalloc(sizeof(***dir));
398 (*dir)[ents]->filename = xstrdup(filename);
399 (*dir)[ents]->longname = xstrdup(longname);
400 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
401 (*dir)[++ents] = NULL;
410 do_close(conn, handle, handle_len);
417 do_ls(struct sftp_conn *conn, char *path)
419 return(do_lsreaddir(conn, path, 1, NULL));
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 log("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 (%d != %d)", 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(%d) packet, got %d",
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 rename 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 rename 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 (%d != %d)", 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(%d) packet, got %d",
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, num_req, max_req, write_error;
746 int read_error, write_errno;
747 u_int64_t offset, size;
748 u_int handle_len, mode, type, id, buflen;
753 TAILQ_ENTRY(request) tq;
755 TAILQ_HEAD(reqhead, request) requests;
758 TAILQ_INIT(&requests);
760 a = do_stat(conn, remote_path, 0);
764 /* XXX: should we preserve set[ug]id? */
765 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
766 mode = S_IWRITE | (a->perm & 0777);
770 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
771 (a->perm & S_IFDIR)) {
772 error("Cannot download a directory: %s", remote_path);
776 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
781 buflen = conn->transfer_buflen;
784 /* Send open request */
786 buffer_put_char(&msg, SSH2_FXP_OPEN);
787 buffer_put_int(&msg, id);
788 buffer_put_cstring(&msg, remote_path);
789 buffer_put_int(&msg, SSH2_FXF_READ);
790 attrib_clear(&junk); /* Send empty attributes */
791 encode_attrib(&msg, &junk);
792 send_msg(conn->fd_out, &msg);
793 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
795 handle = get_handle(conn->fd_in, id, &handle_len);
796 if (handle == NULL) {
801 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
802 if (local_fd == -1) {
803 error("Couldn't open local file \"%s\" for writing: %s",
804 local_path, strerror(errno));
810 /* Read from remote and write to local */
811 write_error = read_error = write_errno = num_req = offset = 0;
813 while (num_req > 0 || max_req > 0) {
817 /* Send some more requests */
818 while (num_req < max_req) {
819 debug3("Request range %llu -> %llu (%d/%d)",
820 (unsigned long long)offset,
821 (unsigned long long)offset + buflen - 1,
823 req = xmalloc(sizeof(*req));
824 req->id = conn->msg_id++;
826 req->offset = offset;
829 TAILQ_INSERT_TAIL(&requests, req, tq);
830 send_read_request(conn->fd_out, req->id, req->offset,
831 req->len, handle, handle_len);
835 get_msg(conn->fd_in, &msg);
836 type = buffer_get_char(&msg);
837 id = buffer_get_int(&msg);
838 debug3("Received reply T:%d I:%d R:%d", type, id, max_req);
840 /* Find the request in our queue */
841 for(req = TAILQ_FIRST(&requests);
842 req != NULL && req->id != id;
843 req = TAILQ_NEXT(req, tq))
846 fatal("Unexpected reply %u", id);
849 case SSH2_FXP_STATUS:
850 status = buffer_get_int(&msg);
851 if (status != SSH2_FX_EOF)
854 TAILQ_REMOVE(&requests, req, tq);
859 data = buffer_get_string(&msg, &len);
860 debug3("Received data %llu -> %llu",
861 (unsigned long long)req->offset,
862 (unsigned long long)req->offset + len - 1);
864 fatal("Received more data than asked for "
865 "%d > %d", len, req->len);
866 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
867 atomicio(write, local_fd, data, len) != len) &&
875 if (len == req->len) {
876 TAILQ_REMOVE(&requests, req, tq);
880 /* Resend the request for the missing data */
881 debug3("Short data block, re-requesting "
882 "%llu -> %llu (%2d)",
883 (unsigned long long)req->offset + len,
884 (unsigned long long)req->offset +
885 req->len - 1, num_req);
886 req->id = conn->msg_id++;
889 send_read_request(conn->fd_out, req->id,
890 req->offset, req->len, handle, handle_len);
891 /* Reduce the request size */
893 buflen = MAX(MIN_READ_SIZE, len);
895 if (max_req > 0) { /* max_req = 0 iff EOF received */
896 if (size > 0 && offset > size) {
897 /* Only one request at a time
898 * after the expected EOF */
899 debug3("Finish at %llu (%2d)",
900 (unsigned long long)offset,
904 else if (max_req < conn->num_requests + 1) {
910 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
911 SSH2_FXP_DATA, type);
916 if (TAILQ_FIRST(&requests) != NULL)
917 fatal("Transfer complete, but requests still in queue");
920 error("Couldn't read from remote file \"%s\" : %s",
921 remote_path, fx2txt(status));
922 do_close(conn, handle, handle_len);
923 } else if (write_error) {
924 error("Couldn't write to \"%s\": %s", local_path,
925 strerror(write_errno));
927 do_close(conn, handle, handle_len);
929 status = do_close(conn, handle, handle_len);
931 /* Override umask and utimes if asked */
933 if (pflag && fchmod(local_fd, mode) == -1)
935 if (pflag && chmod(local_path, mode) == -1)
936 #endif /* HAVE_FCHMOD */
937 error("Couldn't set mode on \"%s\": %s", local_path,
939 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
940 struct timeval tv[2];
941 tv[0].tv_sec = a->atime;
942 tv[1].tv_sec = a->mtime;
943 tv[0].tv_usec = tv[1].tv_usec = 0;
944 if (utimes(local_path, tv) == -1)
945 error("Can't set times on \"%s\": %s",
946 local_path, strerror(errno));
957 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
960 int local_fd, status;
961 u_int handle_len, id, type;
969 struct outstanding_ack {
973 TAILQ_ENTRY(outstanding_ack) tq;
975 TAILQ_HEAD(ackhead, outstanding_ack) acks;
976 struct outstanding_ack *ack;
980 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
981 error("Couldn't open local file \"%s\" for reading: %s",
982 local_path, strerror(errno));
985 if (fstat(local_fd, &sb) == -1) {
986 error("Couldn't fstat local file \"%s\": %s",
987 local_path, strerror(errno));
991 stat_to_attrib(&sb, &a);
993 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
994 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
997 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1001 /* Send open request */
1002 id = conn->msg_id++;
1003 buffer_put_char(&msg, SSH2_FXP_OPEN);
1004 buffer_put_int(&msg, id);
1005 buffer_put_cstring(&msg, remote_path);
1006 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1007 encode_attrib(&msg, &a);
1008 send_msg(conn->fd_out, &msg);
1009 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
1013 handle = get_handle(conn->fd_in, id, &handle_len);
1014 if (handle == NULL) {
1020 startid = ackid = id + 1;
1021 data = xmalloc(conn->transfer_buflen);
1023 /* Read from local and write to remote */
1029 * Can't use atomicio here because it returns 0 on EOF, thus losing
1030 * the last block of the file
1033 len = read(local_fd, data, conn->transfer_buflen);
1034 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1037 fatal("Couldn't read from \"%s\": %s", local_path,
1041 ack = xmalloc(sizeof(*ack));
1043 ack->offset = offset;
1045 TAILQ_INSERT_TAIL(&acks, ack, tq);
1048 buffer_put_char(&msg, SSH2_FXP_WRITE);
1049 buffer_put_int(&msg, ack->id);
1050 buffer_put_string(&msg, handle, handle_len);
1051 buffer_put_int64(&msg, offset);
1052 buffer_put_string(&msg, data, len);
1053 send_msg(conn->fd_out, &msg);
1054 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
1055 id, (unsigned long long)offset, len);
1056 } else if (TAILQ_FIRST(&acks) == NULL)
1060 fatal("Unexpected ACK %u", id);
1062 if (id == startid || len == 0 ||
1063 id - ackid >= conn->num_requests) {
1067 get_msg(conn->fd_in, &msg);
1068 type = buffer_get_char(&msg);
1069 r_id = buffer_get_int(&msg);
1071 if (type != SSH2_FXP_STATUS)
1072 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1073 "got %d", SSH2_FXP_STATUS, type);
1075 status = buffer_get_int(&msg);
1076 debug3("SSH2_FXP_STATUS %d", status);
1078 /* Find the request in our queue */
1079 for(ack = TAILQ_FIRST(&acks);
1080 ack != NULL && ack->id != r_id;
1081 ack = TAILQ_NEXT(ack, tq))
1084 fatal("Can't find request for ID %d", r_id);
1085 TAILQ_REMOVE(&acks, ack, tq);
1087 if (status != SSH2_FX_OK) {
1088 error("Couldn't write to remote file \"%s\": %s",
1089 remote_path, fx2txt(status));
1090 do_close(conn, handle, handle_len);
1094 debug3("In write loop, ack for %u %d bytes at %llu",
1095 ack->id, ack->len, (unsigned long long)ack->offset);
1103 if (close(local_fd) == -1) {
1104 error("Couldn't close local file \"%s\": %s", local_path,
1106 do_close(conn, handle, handle_len);
1111 /* Override umask and utimes if asked */
1113 do_fsetstat(conn, handle, handle_len, &a);
1115 status = do_close(conn, handle, handle_len);