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.35 2002/09/11 22:41:49 djm Exp $");
33 #include "openbsd-compat/sys-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_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
420 return(do_lsreaddir(conn, path, 0, dir));
423 void free_sftp_dirents(SFTP_DIRENT **s)
427 for (i = 0; s[i]; i++) {
428 xfree(s[i]->filename);
429 xfree(s[i]->longname);
436 do_rm(struct sftp_conn *conn, char *path)
440 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
443 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
445 status = get_status(conn->fd_in, id);
446 if (status != SSH2_FX_OK)
447 error("Couldn't delete file: %s", fx2txt(status));
452 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
457 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
460 status = get_status(conn->fd_in, id);
461 if (status != SSH2_FX_OK)
462 error("Couldn't create directory: %s", fx2txt(status));
468 do_rmdir(struct sftp_conn *conn, char *path)
473 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
476 status = get_status(conn->fd_in, id);
477 if (status != SSH2_FX_OK)
478 error("Couldn't remove directory: %s", fx2txt(status));
484 do_stat(struct sftp_conn *conn, char *path, int quiet)
490 send_string_request(conn->fd_out, id,
491 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
494 return(get_decode_stat(conn->fd_in, id, quiet));
498 do_lstat(struct sftp_conn *conn, char *path, int quiet)
502 if (conn->version == 0) {
504 debug("Server version does not support lstat operation");
506 log("Server version does not support lstat operation");
507 return(do_stat(conn, path, quiet));
511 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
514 return(get_decode_stat(conn->fd_in, id, quiet));
518 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
523 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
526 return(get_decode_stat(conn->fd_in, id, quiet));
530 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
535 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
538 status = get_status(conn->fd_in, id);
539 if (status != SSH2_FX_OK)
540 error("Couldn't setstat on \"%s\": %s", path,
547 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
553 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
556 status = get_status(conn->fd_in, id);
557 if (status != SSH2_FX_OK)
558 error("Couldn't fsetstat: %s", fx2txt(status));
564 do_realpath(struct sftp_conn *conn, char *path)
567 u_int type, expected_id, count, id;
568 char *filename, *longname;
571 expected_id = id = conn->msg_id++;
572 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
577 get_msg(conn->fd_in, &msg);
578 type = buffer_get_char(&msg);
579 id = buffer_get_int(&msg);
581 if (id != expected_id)
582 fatal("ID mismatch (%u != %u)", id, expected_id);
584 if (type == SSH2_FXP_STATUS) {
585 u_int status = buffer_get_int(&msg);
587 error("Couldn't canonicalise: %s", fx2txt(status));
589 } else if (type != SSH2_FXP_NAME)
590 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
591 SSH2_FXP_NAME, type);
593 count = buffer_get_int(&msg);
595 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
597 filename = buffer_get_string(&msg, NULL);
598 longname = buffer_get_string(&msg, NULL);
599 a = decode_attrib(&msg);
601 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
611 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
618 /* Send rename request */
620 buffer_put_char(&msg, SSH2_FXP_RENAME);
621 buffer_put_int(&msg, id);
622 buffer_put_cstring(&msg, oldpath);
623 buffer_put_cstring(&msg, newpath);
624 send_msg(conn->fd_out, &msg);
625 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
629 status = get_status(conn->fd_in, id);
630 if (status != SSH2_FX_OK)
631 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
632 newpath, fx2txt(status));
638 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
643 if (conn->version < 3) {
644 error("This server does not support the symlink operation");
645 return(SSH2_FX_OP_UNSUPPORTED);
650 /* Send rename request */
652 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
653 buffer_put_int(&msg, id);
654 buffer_put_cstring(&msg, oldpath);
655 buffer_put_cstring(&msg, newpath);
656 send_msg(conn->fd_out, &msg);
657 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
661 status = get_status(conn->fd_in, id);
662 if (status != SSH2_FX_OK)
663 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
664 newpath, fx2txt(status));
670 do_readlink(struct sftp_conn *conn, char *path)
673 u_int type, expected_id, count, id;
674 char *filename, *longname;
677 expected_id = id = conn->msg_id++;
678 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
683 get_msg(conn->fd_in, &msg);
684 type = buffer_get_char(&msg);
685 id = buffer_get_int(&msg);
687 if (id != expected_id)
688 fatal("ID mismatch (%u != %u)", id, expected_id);
690 if (type == SSH2_FXP_STATUS) {
691 u_int status = buffer_get_int(&msg);
693 error("Couldn't readlink: %s", fx2txt(status));
695 } else if (type != SSH2_FXP_NAME)
696 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
697 SSH2_FXP_NAME, type);
699 count = buffer_get_int(&msg);
701 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
703 filename = buffer_get_string(&msg, NULL);
704 longname = buffer_get_string(&msg, NULL);
705 a = decode_attrib(&msg);
707 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
717 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
718 char *handle, u_int handle_len)
724 buffer_put_char(&msg, SSH2_FXP_READ);
725 buffer_put_int(&msg, id);
726 buffer_put_string(&msg, handle, handle_len);
727 buffer_put_int64(&msg, offset);
728 buffer_put_int(&msg, len);
729 send_msg(fd_out, &msg);
734 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
740 int local_fd, status, num_req, max_req, write_error;
741 int read_error, write_errno;
742 u_int64_t offset, size;
743 u_int handle_len, mode, type, id, buflen;
748 TAILQ_ENTRY(request) tq;
750 TAILQ_HEAD(reqhead, request) requests;
753 TAILQ_INIT(&requests);
755 a = do_stat(conn, remote_path, 0);
759 /* XXX: should we preserve set[ug]id? */
760 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
761 mode = S_IWRITE | (a->perm & 0777);
765 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
766 (a->perm & S_IFDIR)) {
767 error("Cannot download a directory: %s", remote_path);
771 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
776 buflen = conn->transfer_buflen;
779 /* Send open request */
781 buffer_put_char(&msg, SSH2_FXP_OPEN);
782 buffer_put_int(&msg, id);
783 buffer_put_cstring(&msg, remote_path);
784 buffer_put_int(&msg, SSH2_FXF_READ);
785 attrib_clear(&junk); /* Send empty attributes */
786 encode_attrib(&msg, &junk);
787 send_msg(conn->fd_out, &msg);
788 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
790 handle = get_handle(conn->fd_in, id, &handle_len);
791 if (handle == NULL) {
796 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
797 if (local_fd == -1) {
798 error("Couldn't open local file \"%s\" for writing: %s",
799 local_path, strerror(errno));
805 /* Read from remote and write to local */
806 write_error = read_error = write_errno = num_req = offset = 0;
808 while (num_req > 0 || max_req > 0) {
812 /* Send some more requests */
813 while (num_req < max_req) {
814 debug3("Request range %llu -> %llu (%d/%d)",
815 (unsigned long long)offset,
816 (unsigned long long)offset + buflen - 1,
818 req = xmalloc(sizeof(*req));
819 req->id = conn->msg_id++;
821 req->offset = offset;
824 TAILQ_INSERT_TAIL(&requests, req, tq);
825 send_read_request(conn->fd_out, req->id, req->offset,
826 req->len, handle, handle_len);
830 get_msg(conn->fd_in, &msg);
831 type = buffer_get_char(&msg);
832 id = buffer_get_int(&msg);
833 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
835 /* Find the request in our queue */
836 for(req = TAILQ_FIRST(&requests);
837 req != NULL && req->id != id;
838 req = TAILQ_NEXT(req, tq))
841 fatal("Unexpected reply %u", id);
844 case SSH2_FXP_STATUS:
845 status = buffer_get_int(&msg);
846 if (status != SSH2_FX_EOF)
849 TAILQ_REMOVE(&requests, req, tq);
854 data = buffer_get_string(&msg, &len);
855 debug3("Received data %llu -> %llu",
856 (unsigned long long)req->offset,
857 (unsigned long long)req->offset + len - 1);
859 fatal("Received more data than asked for "
860 "%u > %u", len, req->len);
861 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
862 atomicio(write, local_fd, data, len) != len) &&
870 if (len == req->len) {
871 TAILQ_REMOVE(&requests, req, tq);
875 /* Resend the request for the missing data */
876 debug3("Short data block, re-requesting "
877 "%llu -> %llu (%2d)",
878 (unsigned long long)req->offset + len,
879 (unsigned long long)req->offset +
880 req->len - 1, num_req);
881 req->id = conn->msg_id++;
884 send_read_request(conn->fd_out, req->id,
885 req->offset, req->len, handle, handle_len);
886 /* Reduce the request size */
888 buflen = MAX(MIN_READ_SIZE, len);
890 if (max_req > 0) { /* max_req = 0 iff EOF received */
891 if (size > 0 && offset > size) {
892 /* Only one request at a time
893 * after the expected EOF */
894 debug3("Finish at %llu (%2d)",
895 (unsigned long long)offset,
899 else if (max_req < conn->num_requests + 1) {
905 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
906 SSH2_FXP_DATA, type);
911 if (TAILQ_FIRST(&requests) != NULL)
912 fatal("Transfer complete, but requests still in queue");
915 error("Couldn't read from remote file \"%s\" : %s",
916 remote_path, fx2txt(status));
917 do_close(conn, handle, handle_len);
918 } else if (write_error) {
919 error("Couldn't write to \"%s\": %s", local_path,
920 strerror(write_errno));
922 do_close(conn, handle, handle_len);
924 status = do_close(conn, handle, handle_len);
926 /* Override umask and utimes if asked */
928 if (pflag && fchmod(local_fd, mode) == -1)
930 if (pflag && chmod(local_path, mode) == -1)
931 #endif /* HAVE_FCHMOD */
932 error("Couldn't set mode on \"%s\": %s", local_path,
934 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
935 struct timeval tv[2];
936 tv[0].tv_sec = a->atime;
937 tv[1].tv_sec = a->mtime;
938 tv[0].tv_usec = tv[1].tv_usec = 0;
939 if (utimes(local_path, tv) == -1)
940 error("Can't set times on \"%s\": %s",
941 local_path, strerror(errno));
952 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
955 int local_fd, status;
956 u_int handle_len, id, type;
964 struct outstanding_ack {
968 TAILQ_ENTRY(outstanding_ack) tq;
970 TAILQ_HEAD(ackhead, outstanding_ack) acks;
971 struct outstanding_ack *ack;
975 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
976 error("Couldn't open local file \"%s\" for reading: %s",
977 local_path, strerror(errno));
980 if (fstat(local_fd, &sb) == -1) {
981 error("Couldn't fstat local file \"%s\": %s",
982 local_path, strerror(errno));
986 stat_to_attrib(&sb, &a);
988 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
989 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
992 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
996 /* Send open request */
998 buffer_put_char(&msg, SSH2_FXP_OPEN);
999 buffer_put_int(&msg, id);
1000 buffer_put_cstring(&msg, remote_path);
1001 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1002 encode_attrib(&msg, &a);
1003 send_msg(conn->fd_out, &msg);
1004 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1008 handle = get_handle(conn->fd_in, id, &handle_len);
1009 if (handle == NULL) {
1015 startid = ackid = id + 1;
1016 data = xmalloc(conn->transfer_buflen);
1018 /* Read from local and write to remote */
1024 * Can't use atomicio here because it returns 0 on EOF, thus losing
1025 * the last block of the file
1028 len = read(local_fd, data, conn->transfer_buflen);
1029 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1032 fatal("Couldn't read from \"%s\": %s", local_path,
1036 ack = xmalloc(sizeof(*ack));
1038 ack->offset = offset;
1040 TAILQ_INSERT_TAIL(&acks, ack, tq);
1043 buffer_put_char(&msg, SSH2_FXP_WRITE);
1044 buffer_put_int(&msg, ack->id);
1045 buffer_put_string(&msg, handle, handle_len);
1046 buffer_put_int64(&msg, offset);
1047 buffer_put_string(&msg, data, len);
1048 send_msg(conn->fd_out, &msg);
1049 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1050 id, (unsigned long long)offset, len);
1051 } else if (TAILQ_FIRST(&acks) == NULL)
1055 fatal("Unexpected ACK %u", id);
1057 if (id == startid || len == 0 ||
1058 id - ackid >= conn->num_requests) {
1062 get_msg(conn->fd_in, &msg);
1063 type = buffer_get_char(&msg);
1064 r_id = buffer_get_int(&msg);
1066 if (type != SSH2_FXP_STATUS)
1067 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1068 "got %d", SSH2_FXP_STATUS, type);
1070 status = buffer_get_int(&msg);
1071 debug3("SSH2_FXP_STATUS %d", status);
1073 /* Find the request in our queue */
1074 for(ack = TAILQ_FIRST(&acks);
1075 ack != NULL && ack->id != r_id;
1076 ack = TAILQ_NEXT(ack, tq))
1079 fatal("Can't find request for ID %u", r_id);
1080 TAILQ_REMOVE(&acks, ack, tq);
1082 if (status != SSH2_FX_OK) {
1083 error("Couldn't write to remote file \"%s\": %s",
1084 remote_path, fx2txt(status));
1085 do_close(conn, handle, handle_len);
1089 debug3("In write loop, ack for %u %u bytes at %llu",
1090 ack->id, ack->len, (unsigned long long)ack->offset);
1098 if (close(local_fd) == -1) {
1099 error("Couldn't close local file \"%s\": %s", local_path,
1101 do_close(conn, handle, handle_len);
1106 /* Override umask and utimes if asked */
1108 do_fsetstat(conn, handle, handle_len, &a);
1110 status = do_close(conn, handle, handle_len);