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.51 2004/07/11 17:48:47 deraadt 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)) <= 0)
68 fatal("Couldn't send packet: %s", strerror(errno));
70 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) <= 0)
71 fatal("Couldn't send packet: %s", strerror(errno));
77 get_msg(int fd, Buffer *m)
82 buffer_append_space(m, 4);
83 len = atomicio(read, fd, buffer_ptr(m), 4);
85 fatal("Connection closed");
87 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 len = atomicio(read, fd, buffer_ptr(m), msg_len);
96 fatal("Connection closed");
98 fatal("Read packet: %s", strerror(errno));
102 send_string_request(int fd, u_int id, u_int code, char *s,
108 buffer_put_char(&msg, code);
109 buffer_put_int(&msg, id);
110 buffer_put_string(&msg, s, len);
112 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
117 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
118 u_int len, Attrib *a)
123 buffer_put_char(&msg, code);
124 buffer_put_int(&msg, id);
125 buffer_put_string(&msg, s, len);
126 encode_attrib(&msg, a);
128 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
133 get_status(int fd, u_int expected_id)
136 u_int type, id, status;
140 type = buffer_get_char(&msg);
141 id = buffer_get_int(&msg);
143 if (id != expected_id)
144 fatal("ID mismatch (%u != %u)", id, expected_id);
145 if (type != SSH2_FXP_STATUS)
146 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
147 SSH2_FXP_STATUS, type);
149 status = buffer_get_int(&msg);
152 debug3("SSH2_FXP_STATUS %u", status);
158 get_handle(int fd, u_int expected_id, u_int *len)
166 type = buffer_get_char(&msg);
167 id = buffer_get_int(&msg);
169 if (id != expected_id)
170 fatal("ID mismatch (%u != %u)", id, expected_id);
171 if (type == SSH2_FXP_STATUS) {
172 int status = buffer_get_int(&msg);
174 error("Couldn't get handle: %s", fx2txt(status));
176 } else if (type != SSH2_FXP_HANDLE)
177 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
178 SSH2_FXP_HANDLE, type);
180 handle = buffer_get_string(&msg, len);
187 get_decode_stat(int fd, u_int expected_id, int quiet)
196 type = buffer_get_char(&msg);
197 id = buffer_get_int(&msg);
199 debug3("Received stat reply T:%u I:%u", type, id);
200 if (id != expected_id)
201 fatal("ID mismatch (%u != %u)", id, expected_id);
202 if (type == SSH2_FXP_STATUS) {
203 int status = buffer_get_int(&msg);
206 debug("Couldn't stat remote file: %s", fx2txt(status));
208 error("Couldn't stat remote file: %s", fx2txt(status));
210 } else if (type != SSH2_FXP_ATTRS) {
211 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
212 SSH2_FXP_ATTRS, type);
214 a = decode_attrib(&msg);
221 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
226 struct sftp_conn *ret;
229 buffer_put_char(&msg, SSH2_FXP_INIT);
230 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
231 send_msg(fd_out, &msg);
235 get_msg(fd_in, &msg);
237 /* Expecting a VERSION reply */
238 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
239 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
244 version = buffer_get_int(&msg);
246 debug2("Remote version: %d", version);
248 /* Check for extensions */
249 while (buffer_len(&msg) > 0) {
250 char *name = buffer_get_string(&msg, NULL);
251 char *value = buffer_get_string(&msg, NULL);
253 debug2("Init extension: \"%s\"", name);
260 ret = xmalloc(sizeof(*ret));
262 ret->fd_out = fd_out;
263 ret->transfer_buflen = transfer_buflen;
264 ret->num_requests = num_requests;
265 ret->version = version;
268 /* Some filexfer v.0 servers don't support large packets */
270 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
276 sftp_proto_version(struct sftp_conn *conn)
278 return(conn->version);
282 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
290 buffer_put_char(&msg, SSH2_FXP_CLOSE);
291 buffer_put_int(&msg, id);
292 buffer_put_string(&msg, handle, handle_len);
293 send_msg(conn->fd_out, &msg);
294 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
296 status = get_status(conn->fd_in, id);
297 if (status != SSH2_FX_OK)
298 error("Couldn't close file: %s", fx2txt(status));
307 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
311 u_int type, id, handle_len, i, expected_id, ents = 0;
317 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
318 buffer_put_int(&msg, id);
319 buffer_put_cstring(&msg, path);
320 send_msg(conn->fd_out, &msg);
324 handle = get_handle(conn->fd_in, id, &handle_len);
330 *dir = xmalloc(sizeof(**dir));
334 for (; !interrupted;) {
337 id = expected_id = conn->msg_id++;
339 debug3("Sending SSH2_FXP_READDIR I:%u", id);
342 buffer_put_char(&msg, SSH2_FXP_READDIR);
343 buffer_put_int(&msg, id);
344 buffer_put_string(&msg, handle, handle_len);
345 send_msg(conn->fd_out, &msg);
349 get_msg(conn->fd_in, &msg);
351 type = buffer_get_char(&msg);
352 id = buffer_get_int(&msg);
354 debug3("Received reply T:%u I:%u", type, id);
356 if (id != expected_id)
357 fatal("ID mismatch (%u != %u)", id, expected_id);
359 if (type == SSH2_FXP_STATUS) {
360 int status = buffer_get_int(&msg);
362 debug3("Received SSH2_FXP_STATUS %d", status);
364 if (status == SSH2_FX_EOF) {
367 error("Couldn't read directory: %s",
369 do_close(conn, handle, handle_len);
373 } else if (type != SSH2_FXP_NAME)
374 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
375 SSH2_FXP_NAME, type);
377 count = buffer_get_int(&msg);
380 debug3("Received %d SSH2_FXP_NAME responses", count);
381 for (i = 0; i < count; i++) {
382 char *filename, *longname;
385 filename = buffer_get_string(&msg, NULL);
386 longname = buffer_get_string(&msg, NULL);
387 a = decode_attrib(&msg);
390 printf("%s\n", longname);
393 *dir = xrealloc(*dir, sizeof(**dir) *
395 (*dir)[ents] = xmalloc(sizeof(***dir));
396 (*dir)[ents]->filename = xstrdup(filename);
397 (*dir)[ents]->longname = xstrdup(longname);
398 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
399 (*dir)[++ents] = NULL;
408 do_close(conn, handle, handle_len);
411 /* Don't return partial matches on interrupt */
412 if (interrupted && dir != NULL && *dir != NULL) {
413 free_sftp_dirents(*dir);
414 *dir = xmalloc(sizeof(**dir));
422 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
424 return(do_lsreaddir(conn, path, 0, dir));
427 void free_sftp_dirents(SFTP_DIRENT **s)
431 for (i = 0; s[i]; i++) {
432 xfree(s[i]->filename);
433 xfree(s[i]->longname);
440 do_rm(struct sftp_conn *conn, char *path)
444 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
447 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
449 status = get_status(conn->fd_in, id);
450 if (status != SSH2_FX_OK)
451 error("Couldn't delete file: %s", fx2txt(status));
456 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
461 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
464 status = get_status(conn->fd_in, id);
465 if (status != SSH2_FX_OK)
466 error("Couldn't create directory: %s", fx2txt(status));
472 do_rmdir(struct sftp_conn *conn, char *path)
477 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
480 status = get_status(conn->fd_in, id);
481 if (status != SSH2_FX_OK)
482 error("Couldn't remove directory: %s", fx2txt(status));
488 do_stat(struct sftp_conn *conn, char *path, int quiet)
494 send_string_request(conn->fd_out, id,
495 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
498 return(get_decode_stat(conn->fd_in, id, quiet));
502 do_lstat(struct sftp_conn *conn, char *path, int quiet)
506 if (conn->version == 0) {
508 debug("Server version does not support lstat operation");
510 logit("Server version does not support lstat operation");
511 return(do_stat(conn, path, quiet));
515 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
518 return(get_decode_stat(conn->fd_in, id, quiet));
522 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
527 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
530 return(get_decode_stat(conn->fd_in, id, quiet));
534 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
539 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
542 status = get_status(conn->fd_in, id);
543 if (status != SSH2_FX_OK)
544 error("Couldn't setstat on \"%s\": %s", path,
551 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
557 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
560 status = get_status(conn->fd_in, id);
561 if (status != SSH2_FX_OK)
562 error("Couldn't fsetstat: %s", fx2txt(status));
568 do_realpath(struct sftp_conn *conn, char *path)
571 u_int type, expected_id, count, id;
572 char *filename, *longname;
575 expected_id = id = conn->msg_id++;
576 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
581 get_msg(conn->fd_in, &msg);
582 type = buffer_get_char(&msg);
583 id = buffer_get_int(&msg);
585 if (id != expected_id)
586 fatal("ID mismatch (%u != %u)", id, expected_id);
588 if (type == SSH2_FXP_STATUS) {
589 u_int status = buffer_get_int(&msg);
591 error("Couldn't canonicalise: %s", fx2txt(status));
593 } else if (type != SSH2_FXP_NAME)
594 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
595 SSH2_FXP_NAME, type);
597 count = buffer_get_int(&msg);
599 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
601 filename = buffer_get_string(&msg, NULL);
602 longname = buffer_get_string(&msg, NULL);
603 a = decode_attrib(&msg);
605 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
615 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
622 /* Send rename request */
624 buffer_put_char(&msg, SSH2_FXP_RENAME);
625 buffer_put_int(&msg, id);
626 buffer_put_cstring(&msg, oldpath);
627 buffer_put_cstring(&msg, newpath);
628 send_msg(conn->fd_out, &msg);
629 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
633 status = get_status(conn->fd_in, id);
634 if (status != SSH2_FX_OK)
635 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
636 newpath, fx2txt(status));
642 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
647 if (conn->version < 3) {
648 error("This server does not support the symlink operation");
649 return(SSH2_FX_OP_UNSUPPORTED);
654 /* Send symlink request */
656 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
657 buffer_put_int(&msg, id);
658 buffer_put_cstring(&msg, oldpath);
659 buffer_put_cstring(&msg, newpath);
660 send_msg(conn->fd_out, &msg);
661 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
665 status = get_status(conn->fd_in, id);
666 if (status != SSH2_FX_OK)
667 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
668 newpath, fx2txt(status));
674 do_readlink(struct sftp_conn *conn, char *path)
677 u_int type, expected_id, count, id;
678 char *filename, *longname;
681 expected_id = id = conn->msg_id++;
682 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
687 get_msg(conn->fd_in, &msg);
688 type = buffer_get_char(&msg);
689 id = buffer_get_int(&msg);
691 if (id != expected_id)
692 fatal("ID mismatch (%u != %u)", id, expected_id);
694 if (type == SSH2_FXP_STATUS) {
695 u_int status = buffer_get_int(&msg);
697 error("Couldn't readlink: %s", fx2txt(status));
699 } else if (type != SSH2_FXP_NAME)
700 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
701 SSH2_FXP_NAME, type);
703 count = buffer_get_int(&msg);
705 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
707 filename = buffer_get_string(&msg, NULL);
708 longname = buffer_get_string(&msg, NULL);
709 a = decode_attrib(&msg);
711 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
721 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
722 char *handle, u_int handle_len)
728 buffer_put_char(&msg, SSH2_FXP_READ);
729 buffer_put_int(&msg, id);
730 buffer_put_string(&msg, handle, handle_len);
731 buffer_put_int64(&msg, offset);
732 buffer_put_int(&msg, len);
733 send_msg(fd_out, &msg);
738 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
744 int local_fd, status, num_req, max_req, write_error;
745 int read_error, write_errno;
746 u_int64_t offset, size;
747 u_int handle_len, mode, type, id, buflen;
748 off_t progress_counter;
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 = a->perm & 0777;
770 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
771 (!S_ISREG(a->perm))) {
772 error("Cannot download non-regular file: %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:%u 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,
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 progress_counter = 0;
816 if (showprogress && size != 0)
817 start_progress_meter(remote_path, size, &progress_counter);
819 while (num_req > 0 || max_req > 0) {
824 * Simulate EOF on interrupt: stop sending new requests and
825 * allow outstanding requests to drain gracefully
828 if (num_req == 0) /* If we haven't started yet... */
833 /* Send some more requests */
834 while (num_req < max_req) {
835 debug3("Request range %llu -> %llu (%d/%d)",
836 (unsigned long long)offset,
837 (unsigned long long)offset + buflen - 1,
839 req = xmalloc(sizeof(*req));
840 req->id = conn->msg_id++;
842 req->offset = offset;
845 TAILQ_INSERT_TAIL(&requests, req, tq);
846 send_read_request(conn->fd_out, req->id, req->offset,
847 req->len, handle, handle_len);
851 get_msg(conn->fd_in, &msg);
852 type = buffer_get_char(&msg);
853 id = buffer_get_int(&msg);
854 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
856 /* Find the request in our queue */
857 for(req = TAILQ_FIRST(&requests);
858 req != NULL && req->id != id;
859 req = TAILQ_NEXT(req, tq))
862 fatal("Unexpected reply %u", id);
865 case SSH2_FXP_STATUS:
866 status = buffer_get_int(&msg);
867 if (status != SSH2_FX_EOF)
870 TAILQ_REMOVE(&requests, req, tq);
875 data = buffer_get_string(&msg, &len);
876 debug3("Received data %llu -> %llu",
877 (unsigned long long)req->offset,
878 (unsigned long long)req->offset + len - 1);
880 fatal("Received more data than asked for "
881 "%u > %u", len, req->len);
882 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
883 atomicio(vwrite, local_fd, data, len) != len) &&
889 progress_counter += len;
892 if (len == req->len) {
893 TAILQ_REMOVE(&requests, req, tq);
897 /* Resend the request for the missing data */
898 debug3("Short data block, re-requesting "
899 "%llu -> %llu (%2d)",
900 (unsigned long long)req->offset + len,
901 (unsigned long long)req->offset +
902 req->len - 1, num_req);
903 req->id = conn->msg_id++;
906 send_read_request(conn->fd_out, req->id,
907 req->offset, req->len, handle, handle_len);
908 /* Reduce the request size */
910 buflen = MAX(MIN_READ_SIZE, len);
912 if (max_req > 0) { /* max_req = 0 iff EOF received */
913 if (size > 0 && offset > size) {
914 /* Only one request at a time
915 * after the expected EOF */
916 debug3("Finish at %llu (%2d)",
917 (unsigned long long)offset,
920 } else if (max_req <= conn->num_requests) {
926 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
927 SSH2_FXP_DATA, type);
931 if (showprogress && size)
932 stop_progress_meter();
935 if (TAILQ_FIRST(&requests) != NULL)
936 fatal("Transfer complete, but requests still in queue");
939 error("Couldn't read from remote file \"%s\" : %s",
940 remote_path, fx2txt(status));
941 do_close(conn, handle, handle_len);
942 } else if (write_error) {
943 error("Couldn't write to \"%s\": %s", local_path,
944 strerror(write_errno));
946 do_close(conn, handle, handle_len);
948 status = do_close(conn, handle, handle_len);
950 /* Override umask and utimes if asked */
952 if (pflag && fchmod(local_fd, mode) == -1)
954 if (pflag && chmod(local_path, mode) == -1)
955 #endif /* HAVE_FCHMOD */
956 error("Couldn't set mode on \"%s\": %s", local_path,
958 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
959 struct timeval tv[2];
960 tv[0].tv_sec = a->atime;
961 tv[1].tv_sec = a->mtime;
962 tv[0].tv_usec = tv[1].tv_usec = 0;
963 if (utimes(local_path, tv) == -1)
964 error("Can't set times on \"%s\": %s",
965 local_path, strerror(errno));
976 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
979 int local_fd, status;
980 u_int handle_len, id, type;
988 struct outstanding_ack {
992 TAILQ_ENTRY(outstanding_ack) tq;
994 TAILQ_HEAD(ackhead, outstanding_ack) acks;
995 struct outstanding_ack *ack = NULL;
999 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1000 error("Couldn't open local file \"%s\" for reading: %s",
1001 local_path, strerror(errno));
1004 if (fstat(local_fd, &sb) == -1) {
1005 error("Couldn't fstat local file \"%s\": %s",
1006 local_path, strerror(errno));
1010 if (!S_ISREG(sb.st_mode)) {
1011 error("%s is not a regular file", local_path);
1015 stat_to_attrib(&sb, &a);
1017 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1018 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1021 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1025 /* Send open request */
1026 id = conn->msg_id++;
1027 buffer_put_char(&msg, SSH2_FXP_OPEN);
1028 buffer_put_int(&msg, id);
1029 buffer_put_cstring(&msg, remote_path);
1030 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1031 encode_attrib(&msg, &a);
1032 send_msg(conn->fd_out, &msg);
1033 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1037 handle = get_handle(conn->fd_in, id, &handle_len);
1038 if (handle == NULL) {
1044 startid = ackid = id + 1;
1045 data = xmalloc(conn->transfer_buflen);
1047 /* Read from local and write to remote */
1050 start_progress_meter(local_path, sb.st_size, &offset);
1056 * Can't use atomicio here because it returns 0 on EOF,
1057 * thus losing the last block of the file.
1058 * Simulate an EOF on interrupt, allowing ACKs from the
1064 len = read(local_fd, data, conn->transfer_buflen);
1065 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1068 fatal("Couldn't read from \"%s\": %s", local_path,
1072 ack = xmalloc(sizeof(*ack));
1074 ack->offset = offset;
1076 TAILQ_INSERT_TAIL(&acks, ack, tq);
1079 buffer_put_char(&msg, SSH2_FXP_WRITE);
1080 buffer_put_int(&msg, ack->id);
1081 buffer_put_string(&msg, handle, handle_len);
1082 buffer_put_int64(&msg, offset);
1083 buffer_put_string(&msg, data, len);
1084 send_msg(conn->fd_out, &msg);
1085 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1086 id, (unsigned long long)offset, len);
1087 } else if (TAILQ_FIRST(&acks) == NULL)
1091 fatal("Unexpected ACK %u", id);
1093 if (id == startid || len == 0 ||
1094 id - ackid >= conn->num_requests) {
1098 get_msg(conn->fd_in, &msg);
1099 type = buffer_get_char(&msg);
1100 r_id = buffer_get_int(&msg);
1102 if (type != SSH2_FXP_STATUS)
1103 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1104 "got %d", SSH2_FXP_STATUS, type);
1106 status = buffer_get_int(&msg);
1107 debug3("SSH2_FXP_STATUS %d", status);
1109 /* Find the request in our queue */
1110 for(ack = TAILQ_FIRST(&acks);
1111 ack != NULL && ack->id != r_id;
1112 ack = TAILQ_NEXT(ack, tq))
1115 fatal("Can't find request for ID %u", r_id);
1116 TAILQ_REMOVE(&acks, ack, tq);
1118 if (status != SSH2_FX_OK) {
1119 error("Couldn't write to remote file \"%s\": %s",
1120 remote_path, fx2txt(status));
1121 do_close(conn, handle, handle_len);
1127 debug3("In write loop, ack for %u %u bytes at %llu",
1128 ack->id, ack->len, (unsigned long long)ack->offset);
1135 stop_progress_meter();
1138 if (close(local_fd) == -1) {
1139 error("Couldn't close local file \"%s\": %s", local_path,
1141 do_close(conn, handle, handle_len);
1146 /* Override umask and utimes if asked */
1148 do_fsetstat(conn, handle, handle_len, &a);
1150 status = do_close(conn, handle, handle_len);