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.33 2002/06/23 09:30:14 deraadt 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 %u", 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:%u I:%u", 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:%u I:%u", fd, code, id);
137 get_status(int fd, u_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 (%u != %u)", id, expected_id);
149 if (type != SSH2_FXP_STATUS)
150 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
151 SSH2_FXP_STATUS, type);
153 status = buffer_get_int(&msg);
156 debug3("SSH2_FXP_STATUS %u", 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 (%u != %u)", 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(%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));
214 } else if (type != SSH2_FXP_ATTRS) {
215 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
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)
230 struct sftp_conn *ret;
233 buffer_put_char(&msg, SSH2_FXP_INIT);
234 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
235 send_msg(fd_out, &msg);
239 get_msg(fd_in, &msg);
241 /* Expecting a VERSION reply */
242 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
243 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
248 version = buffer_get_int(&msg);
250 debug2("Remote version: %d", version);
252 /* Check for extensions */
253 while (buffer_len(&msg) > 0) {
254 char *name = buffer_get_string(&msg, NULL);
255 char *value = buffer_get_string(&msg, NULL);
257 debug2("Init extension: \"%s\"", name);
264 ret = xmalloc(sizeof(*ret));
266 ret->fd_out = fd_out;
267 ret->transfer_buflen = transfer_buflen;
268 ret->num_requests = num_requests;
269 ret->version = version;
272 /* Some filexfer v.0 servers don't support large packets */
274 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
280 sftp_proto_version(struct sftp_conn *conn)
282 return(conn->version);
286 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
294 buffer_put_char(&msg, SSH2_FXP_CLOSE);
295 buffer_put_int(&msg, id);
296 buffer_put_string(&msg, handle, handle_len);
297 send_msg(conn->fd_out, &msg);
298 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
300 status = get_status(conn->fd_in, id);
301 if (status != SSH2_FX_OK)
302 error("Couldn't close file: %s", fx2txt(status));
311 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
315 u_int type, id, handle_len, i, expected_id, ents = 0;
321 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
322 buffer_put_int(&msg, id);
323 buffer_put_cstring(&msg, path);
324 send_msg(conn->fd_out, &msg);
328 handle = get_handle(conn->fd_in, id, &handle_len);
334 *dir = xmalloc(sizeof(**dir));
341 id = expected_id = conn->msg_id++;
343 debug3("Sending SSH2_FXP_READDIR I:%u", id);
346 buffer_put_char(&msg, SSH2_FXP_READDIR);
347 buffer_put_int(&msg, id);
348 buffer_put_string(&msg, handle, handle_len);
349 send_msg(conn->fd_out, &msg);
353 get_msg(conn->fd_in, &msg);
355 type = buffer_get_char(&msg);
356 id = buffer_get_int(&msg);
358 debug3("Received reply T:%u I:%u", type, id);
360 if (id != expected_id)
361 fatal("ID mismatch (%u != %u)", id, expected_id);
363 if (type == SSH2_FXP_STATUS) {
364 int status = buffer_get_int(&msg);
366 debug3("Received SSH2_FXP_STATUS %d", status);
368 if (status == SSH2_FX_EOF) {
371 error("Couldn't read directory: %s",
373 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);
418 do_ls(struct sftp_conn *conn, char *path)
420 return(do_lsreaddir(conn, path, 1, NULL));
424 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
426 return(do_lsreaddir(conn, path, 0, dir));
429 void free_sftp_dirents(SFTP_DIRENT **s)
433 for (i = 0; s[i]; i++) {
434 xfree(s[i]->filename);
435 xfree(s[i]->longname);
442 do_rm(struct sftp_conn *conn, char *path)
446 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
449 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
451 status = get_status(conn->fd_in, id);
452 if (status != SSH2_FX_OK)
453 error("Couldn't delete file: %s", fx2txt(status));
458 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
463 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
466 status = get_status(conn->fd_in, id);
467 if (status != SSH2_FX_OK)
468 error("Couldn't create directory: %s", fx2txt(status));
474 do_rmdir(struct sftp_conn *conn, char *path)
479 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
482 status = get_status(conn->fd_in, id);
483 if (status != SSH2_FX_OK)
484 error("Couldn't remove directory: %s", fx2txt(status));
490 do_stat(struct sftp_conn *conn, char *path, int quiet)
496 send_string_request(conn->fd_out, id,
497 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
500 return(get_decode_stat(conn->fd_in, id, quiet));
504 do_lstat(struct sftp_conn *conn, char *path, int quiet)
508 if (conn->version == 0) {
510 debug("Server version does not support lstat operation");
512 log("Server version does not support lstat operation");
513 return(do_stat(conn, path, quiet));
517 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
520 return(get_decode_stat(conn->fd_in, id, quiet));
524 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
529 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
532 return(get_decode_stat(conn->fd_in, id, quiet));
536 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
541 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
544 status = get_status(conn->fd_in, id);
545 if (status != SSH2_FX_OK)
546 error("Couldn't setstat on \"%s\": %s", path,
553 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
559 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
562 status = get_status(conn->fd_in, id);
563 if (status != SSH2_FX_OK)
564 error("Couldn't fsetstat: %s", fx2txt(status));
570 do_realpath(struct sftp_conn *conn, char *path)
573 u_int type, expected_id, count, id;
574 char *filename, *longname;
577 expected_id = id = conn->msg_id++;
578 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
583 get_msg(conn->fd_in, &msg);
584 type = buffer_get_char(&msg);
585 id = buffer_get_int(&msg);
587 if (id != expected_id)
588 fatal("ID mismatch (%u != %u)", id, expected_id);
590 if (type == SSH2_FXP_STATUS) {
591 u_int status = buffer_get_int(&msg);
593 error("Couldn't canonicalise: %s", fx2txt(status));
595 } else if (type != SSH2_FXP_NAME)
596 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
597 SSH2_FXP_NAME, type);
599 count = buffer_get_int(&msg);
601 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
603 filename = buffer_get_string(&msg, NULL);
604 longname = buffer_get_string(&msg, NULL);
605 a = decode_attrib(&msg);
607 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
617 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
624 /* Send rename request */
626 buffer_put_char(&msg, SSH2_FXP_RENAME);
627 buffer_put_int(&msg, id);
628 buffer_put_cstring(&msg, oldpath);
629 buffer_put_cstring(&msg, newpath);
630 send_msg(conn->fd_out, &msg);
631 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
635 status = get_status(conn->fd_in, id);
636 if (status != SSH2_FX_OK)
637 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
638 newpath, fx2txt(status));
644 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
649 if (conn->version < 3) {
650 error("This server does not support the symlink operation");
651 return(SSH2_FX_OP_UNSUPPORTED);
656 /* Send rename request */
658 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
659 buffer_put_int(&msg, id);
660 buffer_put_cstring(&msg, oldpath);
661 buffer_put_cstring(&msg, newpath);
662 send_msg(conn->fd_out, &msg);
663 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
667 status = get_status(conn->fd_in, id);
668 if (status != SSH2_FX_OK)
669 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
670 newpath, fx2txt(status));
676 do_readlink(struct sftp_conn *conn, char *path)
679 u_int type, expected_id, count, id;
680 char *filename, *longname;
683 expected_id = id = conn->msg_id++;
684 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
689 get_msg(conn->fd_in, &msg);
690 type = buffer_get_char(&msg);
691 id = buffer_get_int(&msg);
693 if (id != expected_id)
694 fatal("ID mismatch (%u != %u)", id, expected_id);
696 if (type == SSH2_FXP_STATUS) {
697 u_int status = buffer_get_int(&msg);
699 error("Couldn't readlink: %s", fx2txt(status));
701 } else if (type != SSH2_FXP_NAME)
702 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
703 SSH2_FXP_NAME, type);
705 count = buffer_get_int(&msg);
707 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
709 filename = buffer_get_string(&msg, NULL);
710 longname = buffer_get_string(&msg, NULL);
711 a = decode_attrib(&msg);
713 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
723 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
724 char *handle, u_int handle_len)
730 buffer_put_char(&msg, SSH2_FXP_READ);
731 buffer_put_int(&msg, id);
732 buffer_put_string(&msg, handle, handle_len);
733 buffer_put_int64(&msg, offset);
734 buffer_put_int(&msg, len);
735 send_msg(fd_out, &msg);
740 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
746 int local_fd, status, num_req, max_req, write_error;
747 int read_error, write_errno;
748 u_int64_t offset, size;
749 u_int handle_len, mode, type, id, buflen;
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 = S_IWRITE | (a->perm & 0777);
771 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
772 (a->perm & S_IFDIR)) {
773 error("Cannot download a directory: %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, mode);
803 if (local_fd == -1) {
804 error("Couldn't open local file \"%s\" for writing: %s",
805 local_path, strerror(errno));
811 /* Read from remote and write to local */
812 write_error = read_error = write_errno = num_req = offset = 0;
814 while (num_req > 0 || max_req > 0) {
818 /* Send some more requests */
819 while (num_req < max_req) {
820 debug3("Request range %llu -> %llu (%d/%d)",
821 (unsigned long long)offset,
822 (unsigned long long)offset + buflen - 1,
824 req = xmalloc(sizeof(*req));
825 req->id = conn->msg_id++;
827 req->offset = offset;
830 TAILQ_INSERT_TAIL(&requests, req, tq);
831 send_read_request(conn->fd_out, req->id, req->offset,
832 req->len, handle, handle_len);
836 get_msg(conn->fd_in, &msg);
837 type = buffer_get_char(&msg);
838 id = buffer_get_int(&msg);
839 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
841 /* Find the request in our queue */
842 for(req = TAILQ_FIRST(&requests);
843 req != NULL && req->id != id;
844 req = TAILQ_NEXT(req, tq))
847 fatal("Unexpected reply %u", id);
850 case SSH2_FXP_STATUS:
851 status = buffer_get_int(&msg);
852 if (status != SSH2_FX_EOF)
855 TAILQ_REMOVE(&requests, req, tq);
860 data = buffer_get_string(&msg, &len);
861 debug3("Received data %llu -> %llu",
862 (unsigned long long)req->offset,
863 (unsigned long long)req->offset + len - 1);
865 fatal("Received more data than asked for "
866 "%u > %u", len, req->len);
867 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
868 atomicio(write, local_fd, data, len) != len) &&
876 if (len == req->len) {
877 TAILQ_REMOVE(&requests, req, tq);
881 /* Resend the request for the missing data */
882 debug3("Short data block, re-requesting "
883 "%llu -> %llu (%2d)",
884 (unsigned long long)req->offset + len,
885 (unsigned long long)req->offset +
886 req->len - 1, num_req);
887 req->id = conn->msg_id++;
890 send_read_request(conn->fd_out, req->id,
891 req->offset, req->len, handle, handle_len);
892 /* Reduce the request size */
894 buflen = MAX(MIN_READ_SIZE, len);
896 if (max_req > 0) { /* max_req = 0 iff EOF received */
897 if (size > 0 && offset > size) {
898 /* Only one request at a time
899 * after the expected EOF */
900 debug3("Finish at %llu (%2d)",
901 (unsigned long long)offset,
905 else if (max_req < conn->num_requests + 1) {
911 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
912 SSH2_FXP_DATA, type);
917 if (TAILQ_FIRST(&requests) != NULL)
918 fatal("Transfer complete, but requests still in queue");
921 error("Couldn't read from remote file \"%s\" : %s",
922 remote_path, fx2txt(status));
923 do_close(conn, handle, handle_len);
924 } else if (write_error) {
925 error("Couldn't write to \"%s\": %s", local_path,
926 strerror(write_errno));
928 do_close(conn, handle, handle_len);
930 status = do_close(conn, handle, handle_len);
932 /* Override umask and utimes if asked */
934 if (pflag && fchmod(local_fd, mode) == -1)
936 if (pflag && chmod(local_path, mode) == -1)
937 #endif /* HAVE_FCHMOD */
938 error("Couldn't set mode on \"%s\": %s", local_path,
940 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
941 struct timeval tv[2];
942 tv[0].tv_sec = a->atime;
943 tv[1].tv_sec = a->mtime;
944 tv[0].tv_usec = tv[1].tv_usec = 0;
945 if (utimes(local_path, tv) == -1)
946 error("Can't set times on \"%s\": %s",
947 local_path, strerror(errno));
958 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
961 int local_fd, status;
962 u_int handle_len, id, type;
970 struct outstanding_ack {
974 TAILQ_ENTRY(outstanding_ack) tq;
976 TAILQ_HEAD(ackhead, outstanding_ack) acks;
977 struct outstanding_ack *ack;
981 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
982 error("Couldn't open local file \"%s\" for reading: %s",
983 local_path, strerror(errno));
986 if (fstat(local_fd, &sb) == -1) {
987 error("Couldn't fstat local file \"%s\": %s",
988 local_path, strerror(errno));
992 stat_to_attrib(&sb, &a);
994 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
995 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
998 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1002 /* Send open request */
1003 id = conn->msg_id++;
1004 buffer_put_char(&msg, SSH2_FXP_OPEN);
1005 buffer_put_int(&msg, id);
1006 buffer_put_cstring(&msg, remote_path);
1007 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1008 encode_attrib(&msg, &a);
1009 send_msg(conn->fd_out, &msg);
1010 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1014 handle = get_handle(conn->fd_in, id, &handle_len);
1015 if (handle == NULL) {
1021 startid = ackid = id + 1;
1022 data = xmalloc(conn->transfer_buflen);
1024 /* Read from local and write to remote */
1030 * Can't use atomicio here because it returns 0 on EOF, thus losing
1031 * the last block of the file
1034 len = read(local_fd, data, conn->transfer_buflen);
1035 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1038 fatal("Couldn't read from \"%s\": %s", local_path,
1042 ack = xmalloc(sizeof(*ack));
1044 ack->offset = offset;
1046 TAILQ_INSERT_TAIL(&acks, ack, tq);
1049 buffer_put_char(&msg, SSH2_FXP_WRITE);
1050 buffer_put_int(&msg, ack->id);
1051 buffer_put_string(&msg, handle, handle_len);
1052 buffer_put_int64(&msg, offset);
1053 buffer_put_string(&msg, data, len);
1054 send_msg(conn->fd_out, &msg);
1055 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1056 id, (unsigned long long)offset, len);
1057 } else if (TAILQ_FIRST(&acks) == NULL)
1061 fatal("Unexpected ACK %u", id);
1063 if (id == startid || len == 0 ||
1064 id - ackid >= conn->num_requests) {
1068 get_msg(conn->fd_in, &msg);
1069 type = buffer_get_char(&msg);
1070 r_id = buffer_get_int(&msg);
1072 if (type != SSH2_FXP_STATUS)
1073 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1074 "got %d", SSH2_FXP_STATUS, type);
1076 status = buffer_get_int(&msg);
1077 debug3("SSH2_FXP_STATUS %d", status);
1079 /* Find the request in our queue */
1080 for(ack = TAILQ_FIRST(&acks);
1081 ack != NULL && ack->id != r_id;
1082 ack = TAILQ_NEXT(ack, tq))
1085 fatal("Can't find request for ID %u", r_id);
1086 TAILQ_REMOVE(&acks, ack, tq);
1088 if (status != SSH2_FX_OK) {
1089 error("Couldn't write to remote file \"%s\": %s",
1090 remote_path, fx2txt(status));
1091 do_close(conn, handle, handle_len);
1095 debug3("In write loop, ack for %u %u bytes at %llu",
1096 ack->id, ack->len, (unsigned long long)ack->offset);
1104 if (close(local_fd) == -1) {
1105 error("Couldn't close local file \"%s\": %s", local_path,
1107 do_close(conn, handle, handle_len);
1112 /* Override umask and utimes if asked */
1114 do_fsetstat(conn, handle, handle_len, &a);
1116 status = do_close(conn, handle, handle_len);