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.23 2002/02/13 00:59:23 djm Exp $");
33 #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
34 #include <sys/queue.h>
36 #include "openbsd-compat/fake-queue.h"
47 #include "sftp-common.h"
48 #include "sftp-client.h"
50 /* Minimum amount of data to read at at time */
51 #define MIN_READ_SIZE 512
56 u_int transfer_buflen;
63 send_msg(int fd, Buffer *m)
65 int mlen = buffer_len(m);
70 buffer_put_int(&oqueue, mlen);
71 buffer_append(&oqueue, buffer_ptr(m), mlen);
72 buffer_consume(m, mlen);
74 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue));
76 fatal("Couldn't send packet: %s", strerror(errno));
82 get_msg(int fd, Buffer *m)
85 unsigned char buf[4096];
87 len = atomicio(read, fd, buf, 4);
89 fatal("Connection closed");
91 fatal("Couldn't read packet: %s", strerror(errno));
93 msg_len = GET_32BIT(buf);
94 if (msg_len > 256 * 1024)
95 fatal("Received message too long %d", msg_len);
98 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf)));
100 fatal("Connection closed");
102 fatal("Couldn't read packet: %s", strerror(errno));
105 buffer_append(m, buf, len);
110 send_string_request(int fd, u_int id, u_int code, char *s,
116 buffer_put_char(&msg, code);
117 buffer_put_int(&msg, id);
118 buffer_put_string(&msg, s, len);
120 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
125 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
126 u_int len, Attrib *a)
131 buffer_put_char(&msg, code);
132 buffer_put_int(&msg, id);
133 buffer_put_string(&msg, s, len);
134 encode_attrib(&msg, a);
136 debug3("Sent message fd %d T:%d I:%d", fd, code, id);
141 get_status(int fd, int expected_id)
144 u_int type, id, status;
148 type = buffer_get_char(&msg);
149 id = buffer_get_int(&msg);
151 if (id != expected_id)
152 fatal("ID mismatch (%d != %d)", id, expected_id);
153 if (type != SSH2_FXP_STATUS)
154 fatal("Expected SSH2_FXP_STATUS(%d) packet, got %d",
155 SSH2_FXP_STATUS, type);
157 status = buffer_get_int(&msg);
160 debug3("SSH2_FXP_STATUS %d", status);
166 get_handle(int fd, u_int expected_id, u_int *len)
174 type = buffer_get_char(&msg);
175 id = buffer_get_int(&msg);
177 if (id != expected_id)
178 fatal("ID mismatch (%d != %d)", id, expected_id);
179 if (type == SSH2_FXP_STATUS) {
180 int status = buffer_get_int(&msg);
182 error("Couldn't get handle: %s", fx2txt(status));
184 } else if (type != SSH2_FXP_HANDLE)
185 fatal("Expected SSH2_FXP_HANDLE(%d) packet, got %d",
186 SSH2_FXP_HANDLE, type);
188 handle = buffer_get_string(&msg, len);
195 get_decode_stat(int fd, u_int expected_id, int quiet)
204 type = buffer_get_char(&msg);
205 id = buffer_get_int(&msg);
207 debug3("Received stat reply T:%d I:%d", type, id);
208 if (id != expected_id)
209 fatal("ID mismatch (%d != %d)", id, expected_id);
210 if (type == SSH2_FXP_STATUS) {
211 int status = buffer_get_int(&msg);
214 debug("Couldn't stat remote file: %s", fx2txt(status));
216 error("Couldn't stat remote file: %s", fx2txt(status));
218 } else if (type != SSH2_FXP_ATTRS) {
219 fatal("Expected SSH2_FXP_ATTRS(%d) packet, got %d",
220 SSH2_FXP_ATTRS, type);
222 a = decode_attrib(&msg);
229 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
233 struct sftp_conn *ret;
236 buffer_put_char(&msg, SSH2_FXP_INIT);
237 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
238 send_msg(fd_out, &msg);
242 get_msg(fd_in, &msg);
244 /* Expecting a VERSION reply */
245 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
246 error("Invalid packet back from SSH2_FXP_INIT (type %d)",
251 version = buffer_get_int(&msg);
253 debug2("Remote version: %d", version);
255 /* Check for extensions */
256 while (buffer_len(&msg) > 0) {
257 char *name = buffer_get_string(&msg, NULL);
258 char *value = buffer_get_string(&msg, NULL);
260 debug2("Init extension: \"%s\"", name);
267 ret = xmalloc(sizeof(*ret));
269 ret->fd_out = fd_out;
270 ret->transfer_buflen = transfer_buflen;
271 ret->num_requests = num_requests;
272 ret->version = version;
275 /* Some filexfer v.0 servers don't support large packets */
277 ret->transfer_buflen = MAX(ret->transfer_buflen, 20480);
283 sftp_proto_version(struct sftp_conn *conn)
285 return(conn->version);
289 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
297 buffer_put_char(&msg, SSH2_FXP_CLOSE);
298 buffer_put_int(&msg, id);
299 buffer_put_string(&msg, handle, handle_len);
300 send_msg(conn->fd_out, &msg);
301 debug3("Sent message SSH2_FXP_CLOSE I:%d", id);
303 status = get_status(conn->fd_in, id);
304 if (status != SSH2_FX_OK)
305 error("Couldn't close file: %s", fx2txt(status));
314 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
318 u_int type, id, handle_len, i, expected_id, ents = 0;
324 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
325 buffer_put_int(&msg, id);
326 buffer_put_cstring(&msg, path);
327 send_msg(conn->fd_out, &msg);
331 handle = get_handle(conn->fd_in, id, &handle_len);
337 *dir = xmalloc(sizeof(**dir));
344 id = expected_id = conn->msg_id++;
346 debug3("Sending SSH2_FXP_READDIR I:%d", id);
349 buffer_put_char(&msg, SSH2_FXP_READDIR);
350 buffer_put_int(&msg, id);
351 buffer_put_string(&msg, handle, handle_len);
352 send_msg(conn->fd_out, &msg);
356 get_msg(conn->fd_in, &msg);
358 type = buffer_get_char(&msg);
359 id = buffer_get_int(&msg);
361 debug3("Received reply T:%d I:%d", type, id);
363 if (id != expected_id)
364 fatal("ID mismatch (%d != %d)", id, expected_id);
366 if (type == SSH2_FXP_STATUS) {
367 int status = buffer_get_int(&msg);
369 debug3("Received SSH2_FXP_STATUS %d", status);
371 if (status == SSH2_FX_EOF) {
374 error("Couldn't read directory: %s",
376 do_close(conn, handle, handle_len);
379 } else if (type != SSH2_FXP_NAME)
380 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
381 SSH2_FXP_NAME, type);
383 count = buffer_get_int(&msg);
386 debug3("Received %d SSH2_FXP_NAME responses", count);
387 for (i = 0; i < count; i++) {
388 char *filename, *longname;
391 filename = buffer_get_string(&msg, NULL);
392 longname = buffer_get_string(&msg, NULL);
393 a = decode_attrib(&msg);
396 printf("%s\n", longname);
399 *dir = xrealloc(*dir, sizeof(**dir) *
401 (*dir)[ents] = xmalloc(sizeof(***dir));
402 (*dir)[ents]->filename = xstrdup(filename);
403 (*dir)[ents]->longname = xstrdup(longname);
404 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
405 (*dir)[++ents] = NULL;
414 do_close(conn, handle, handle_len);
421 do_ls(struct sftp_conn *conn, char *path)
423 return(do_lsreaddir(conn, path, 1, NULL));
427 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
429 return(do_lsreaddir(conn, path, 0, dir));
432 void free_sftp_dirents(SFTP_DIRENT **s)
436 for (i = 0; s[i]; i++) {
437 xfree(s[i]->filename);
438 xfree(s[i]->longname);
445 do_rm(struct sftp_conn *conn, char *path)
449 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
452 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
454 status = get_status(conn->fd_in, id);
455 if (status != SSH2_FX_OK)
456 error("Couldn't delete file: %s", fx2txt(status));
461 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
466 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
469 status = get_status(conn->fd_in, id);
470 if (status != SSH2_FX_OK)
471 error("Couldn't create directory: %s", fx2txt(status));
477 do_rmdir(struct sftp_conn *conn, char *path)
482 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
485 status = get_status(conn->fd_in, id);
486 if (status != SSH2_FX_OK)
487 error("Couldn't remove directory: %s", fx2txt(status));
493 do_stat(struct sftp_conn *conn, char *path, int quiet)
499 send_string_request(conn->fd_out, id,
500 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
503 return(get_decode_stat(conn->fd_in, id, quiet));
507 do_lstat(struct sftp_conn *conn, char *path, int quiet)
511 if (conn->version == 0) {
513 debug("Server version does not support lstat operation");
515 error("Server version does not support lstat operation");
520 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
523 return(get_decode_stat(conn->fd_in, id, quiet));
527 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
532 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
535 return(get_decode_stat(conn->fd_in, id, quiet));
539 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
544 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
547 status = get_status(conn->fd_in, id);
548 if (status != SSH2_FX_OK)
549 error("Couldn't setstat on \"%s\": %s", path,
556 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
562 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
565 status = get_status(conn->fd_in, id);
566 if (status != SSH2_FX_OK)
567 error("Couldn't fsetstat: %s", fx2txt(status));
573 do_realpath(struct sftp_conn *conn, char *path)
576 u_int type, expected_id, count, id;
577 char *filename, *longname;
580 expected_id = id = conn->msg_id++;
581 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
586 get_msg(conn->fd_in, &msg);
587 type = buffer_get_char(&msg);
588 id = buffer_get_int(&msg);
590 if (id != expected_id)
591 fatal("ID mismatch (%d != %d)", id, expected_id);
593 if (type == SSH2_FXP_STATUS) {
594 u_int status = buffer_get_int(&msg);
596 error("Couldn't canonicalise: %s", fx2txt(status));
598 } else if (type != SSH2_FXP_NAME)
599 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
600 SSH2_FXP_NAME, type);
602 count = buffer_get_int(&msg);
604 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
606 filename = buffer_get_string(&msg, NULL);
607 longname = buffer_get_string(&msg, NULL);
608 a = decode_attrib(&msg);
610 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
620 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
627 /* Send rename request */
629 buffer_put_char(&msg, SSH2_FXP_RENAME);
630 buffer_put_int(&msg, id);
631 buffer_put_cstring(&msg, oldpath);
632 buffer_put_cstring(&msg, newpath);
633 send_msg(conn->fd_out, &msg);
634 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
638 status = get_status(conn->fd_in, id);
639 if (status != SSH2_FX_OK)
640 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
641 newpath, fx2txt(status));
647 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
652 if (conn->version < 3) {
653 error("This server does not support the symlink operation");
654 return(SSH2_FX_OP_UNSUPPORTED);
659 /* Send rename request */
661 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
662 buffer_put_int(&msg, id);
663 buffer_put_cstring(&msg, oldpath);
664 buffer_put_cstring(&msg, newpath);
665 send_msg(conn->fd_out, &msg);
666 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
670 status = get_status(conn->fd_in, id);
671 if (status != SSH2_FX_OK)
672 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
673 newpath, fx2txt(status));
679 do_readlink(struct sftp_conn *conn, char *path)
682 u_int type, expected_id, count, id;
683 char *filename, *longname;
686 expected_id = id = conn->msg_id++;
687 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
692 get_msg(conn->fd_in, &msg);
693 type = buffer_get_char(&msg);
694 id = buffer_get_int(&msg);
696 if (id != expected_id)
697 fatal("ID mismatch (%d != %d)", id, expected_id);
699 if (type == SSH2_FXP_STATUS) {
700 u_int status = buffer_get_int(&msg);
702 error("Couldn't readlink: %s", fx2txt(status));
704 } else if (type != SSH2_FXP_NAME)
705 fatal("Expected SSH2_FXP_NAME(%d) packet, got %d",
706 SSH2_FXP_NAME, type);
708 count = buffer_get_int(&msg);
710 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
712 filename = buffer_get_string(&msg, NULL);
713 longname = buffer_get_string(&msg, NULL);
714 a = decode_attrib(&msg);
716 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
726 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
727 char *handle, u_int handle_len)
733 buffer_put_char(&msg, SSH2_FXP_READ);
734 buffer_put_int(&msg, id);
735 buffer_put_string(&msg, handle, handle_len);
736 buffer_put_int64(&msg, offset);
737 buffer_put_int(&msg, len);
738 send_msg(fd_out, &msg);
743 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
749 int local_fd, status, num_req, max_req, write_error;
750 int read_error, write_errno;
751 u_int64_t offset, size;
752 u_int handle_len, mode, type, id, buflen;
757 TAILQ_ENTRY(request) tq;
759 TAILQ_HEAD(reqhead, request) requests;
762 TAILQ_INIT(&requests);
764 a = do_stat(conn, remote_path, 0);
768 /* XXX: should we preserve set[ug]id? */
769 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
770 mode = S_IWRITE | (a->perm & 0777);
774 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
775 (a->perm & S_IFDIR)) {
776 error("Cannot download a directory: %s", remote_path);
780 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
785 buflen = conn->transfer_buflen;
788 /* Send open request */
790 buffer_put_char(&msg, SSH2_FXP_OPEN);
791 buffer_put_int(&msg, id);
792 buffer_put_cstring(&msg, remote_path);
793 buffer_put_int(&msg, SSH2_FXF_READ);
794 attrib_clear(&junk); /* Send empty attributes */
795 encode_attrib(&msg, &junk);
796 send_msg(conn->fd_out, &msg);
797 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
799 handle = get_handle(conn->fd_in, id, &handle_len);
800 if (handle == NULL) {
806 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
807 if (local_fd == -1) {
808 error("Couldn't open local file \"%s\" for writing: %s",
809 local_path, strerror(errno));
813 /* Read from remote and write to local */
814 write_error = read_error = write_errno = num_req = offset = 0;
816 while (num_req > 0 || max_req > 0) {
820 /* Send some more requests */
821 while (num_req < max_req) {
822 debug3("Request range %llu -> %llu (%d/%d)",
823 offset, offset + buflen - 1, num_req, max_req);
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:%d I:%d 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", req->offset,
862 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)", req->offset + len,
883 req->offset + req->len - 1, num_req);
884 req->id = conn->msg_id++;
887 send_read_request(conn->fd_out, req->id,
888 req->offset, req->len, handle, handle_len);
889 /* Reduce the request size */
891 buflen = MAX(MIN_READ_SIZE, len);
893 if (max_req > 0) { /* max_req = 0 iff EOF received */
894 if (size > 0 && offset > size) {
895 /* Only one request at a time
896 * after the expected EOF */
897 debug3("Finish at %llu (%2d)",
901 else if (max_req < conn->num_requests + 1) {
907 fatal("Expected SSH2_FXP_DATA(%d) packet, got %d",
908 SSH2_FXP_DATA, type);
913 if (TAILQ_FIRST(&requests) != NULL)
914 fatal("Transfer complete, but requests still in queue");
917 error("Couldn't read from remote file \"%s\" : %s",
918 remote_path, fx2txt(status));
919 do_close(conn, handle, handle_len);
920 } else if (write_error) {
921 error("Couldn't write to \"%s\": %s", local_path,
922 strerror(write_errno));
924 do_close(conn, handle, handle_len);
926 status = do_close(conn, handle, handle_len);
928 /* Override umask and utimes if asked */
930 if (pflag && fchmod(local_fd, mode) == -1)
932 if (pflag && chmod(local_path, mode) == -1)
933 #endif /* HAVE_FCHMOD */
934 error("Couldn't set mode on \"%s\": %s", local_path,
936 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
937 struct timeval tv[2];
938 tv[0].tv_sec = a->atime;
939 tv[1].tv_sec = a->mtime;
940 tv[0].tv_usec = tv[1].tv_usec = 0;
941 if (utimes(local_path, tv) == -1)
942 error("Can't set times on \"%s\": %s",
943 local_path, strerror(errno));
954 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
957 int local_fd, status;
958 u_int handle_len, id, type;
966 struct outstanding_ack {
970 TAILQ_ENTRY(outstanding_ack) tq;
972 TAILQ_HEAD(ackhead, outstanding_ack) acks;
973 struct outstanding_ack *ack;
977 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
978 error("Couldn't open local file \"%s\" for reading: %s",
979 local_path, strerror(errno));
982 if (fstat(local_fd, &sb) == -1) {
983 error("Couldn't fstat local file \"%s\": %s",
984 local_path, strerror(errno));
988 stat_to_attrib(&sb, &a);
990 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
991 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
994 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
998 /* Send open request */
1000 buffer_put_char(&msg, SSH2_FXP_OPEN);
1001 buffer_put_int(&msg, id);
1002 buffer_put_cstring(&msg, remote_path);
1003 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1004 encode_attrib(&msg, &a);
1005 send_msg(conn->fd_out, &msg);
1006 debug3("Sent message SSH2_FXP_OPEN I:%d P:%s", id, remote_path);
1010 handle = get_handle(conn->fd_in, id, &handle_len);
1011 if (handle == NULL) {
1017 startid = ackid = id + 1;
1018 data = xmalloc(conn->transfer_buflen);
1020 /* Read from local and write to remote */
1026 * Can't use atomicio here because it returns 0 on EOF, thus losing
1027 * the last block of the file
1030 len = read(local_fd, data, conn->transfer_buflen);
1031 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1034 fatal("Couldn't read from \"%s\": %s", local_path,
1038 ack = xmalloc(sizeof(*ack));
1040 ack->offset = offset;
1042 TAILQ_INSERT_TAIL(&acks, ack, tq);
1045 buffer_put_char(&msg, SSH2_FXP_WRITE);
1046 buffer_put_int(&msg, ack->id);
1047 buffer_put_string(&msg, handle, handle_len);
1048 buffer_put_int64(&msg, offset);
1049 buffer_put_string(&msg, data, len);
1050 send_msg(conn->fd_out, &msg);
1051 debug3("Sent message SSH2_FXP_WRITE I:%d O:%llu S:%u",
1052 id, (u_int64_t)offset, len);
1053 } else if (TAILQ_FIRST(&acks) == NULL)
1057 fatal("Unexpected ACK %u", id);
1059 if (id == startid || len == 0 ||
1060 id - ackid >= conn->num_requests) {
1062 get_msg(conn->fd_in, &msg);
1063 type = buffer_get_char(&msg);
1064 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 != id;
1076 ack = TAILQ_NEXT(ack, tq))
1079 fatal("Can't find request for ID %d", 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 %d bytes at %llu",
1090 ack->id, ack->len, 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);