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.58 2006/01/02 01:20:31 djm 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
48 u_int transfer_buflen;
55 send_msg(int fd, Buffer *m)
59 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
60 fatal("Outbound message too long %u", buffer_len(m));
62 /* Send length first */
63 PUT_32BIT(mlen, buffer_len(m));
64 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
65 fatal("Couldn't send packet: %s", strerror(errno));
67 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
68 fatal("Couldn't send packet: %s", strerror(errno));
74 get_msg(int fd, Buffer *m)
78 buffer_append_space(m, 4);
79 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
81 fatal("Connection closed");
83 fatal("Couldn't read packet: %s", strerror(errno));
86 msg_len = buffer_get_int(m);
87 if (msg_len > SFTP_MAX_MSG_LENGTH)
88 fatal("Received message too long %u", msg_len);
90 buffer_append_space(m, msg_len);
91 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
93 fatal("Connection closed");
95 fatal("Read packet: %s", strerror(errno));
100 send_string_request(int fd, u_int id, u_int code, char *s,
106 buffer_put_char(&msg, code);
107 buffer_put_int(&msg, id);
108 buffer_put_string(&msg, s, len);
110 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
115 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
116 u_int len, Attrib *a)
121 buffer_put_char(&msg, code);
122 buffer_put_int(&msg, id);
123 buffer_put_string(&msg, s, len);
124 encode_attrib(&msg, a);
126 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
131 get_status(int fd, u_int expected_id)
134 u_int type, id, status;
138 type = buffer_get_char(&msg);
139 id = buffer_get_int(&msg);
141 if (id != expected_id)
142 fatal("ID mismatch (%u != %u)", id, expected_id);
143 if (type != SSH2_FXP_STATUS)
144 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
145 SSH2_FXP_STATUS, type);
147 status = buffer_get_int(&msg);
150 debug3("SSH2_FXP_STATUS %u", status);
156 get_handle(int fd, u_int expected_id, u_int *len)
164 type = buffer_get_char(&msg);
165 id = buffer_get_int(&msg);
167 if (id != expected_id)
168 fatal("ID mismatch (%u != %u)", id, expected_id);
169 if (type == SSH2_FXP_STATUS) {
170 int status = buffer_get_int(&msg);
172 error("Couldn't get handle: %s", fx2txt(status));
175 } else if (type != SSH2_FXP_HANDLE)
176 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
177 SSH2_FXP_HANDLE, type);
179 handle = buffer_get_string(&msg, len);
186 get_decode_stat(int fd, u_int expected_id, int quiet)
195 type = buffer_get_char(&msg);
196 id = buffer_get_int(&msg);
198 debug3("Received stat reply T:%u I:%u", type, id);
199 if (id != expected_id)
200 fatal("ID mismatch (%u != %u)", id, expected_id);
201 if (type == SSH2_FXP_STATUS) {
202 int status = buffer_get_int(&msg);
205 debug("Couldn't stat remote file: %s", fx2txt(status));
207 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 count, 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;) {
335 id = expected_id = conn->msg_id++;
337 debug3("Sending SSH2_FXP_READDIR I:%u", id);
340 buffer_put_char(&msg, SSH2_FXP_READDIR);
341 buffer_put_int(&msg, id);
342 buffer_put_string(&msg, handle, handle_len);
343 send_msg(conn->fd_out, &msg);
347 get_msg(conn->fd_in, &msg);
349 type = buffer_get_char(&msg);
350 id = buffer_get_int(&msg);
352 debug3("Received reply T:%u I:%u", type, id);
354 if (id != expected_id)
355 fatal("ID mismatch (%u != %u)", id, expected_id);
357 if (type == SSH2_FXP_STATUS) {
358 int status = buffer_get_int(&msg);
360 debug3("Received SSH2_FXP_STATUS %d", status);
362 if (status == SSH2_FX_EOF) {
365 error("Couldn't read directory: %s",
367 do_close(conn, handle, handle_len);
371 } else if (type != SSH2_FXP_NAME)
372 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
373 SSH2_FXP_NAME, type);
375 count = buffer_get_int(&msg);
378 debug3("Received %d SSH2_FXP_NAME responses", count);
379 for (i = 0; i < count; i++) {
380 char *filename, *longname;
383 filename = buffer_get_string(&msg, NULL);
384 longname = buffer_get_string(&msg, NULL);
385 a = decode_attrib(&msg);
388 printf("%s\n", longname);
391 *dir = xrealloc(*dir, sizeof(**dir) *
393 (*dir)[ents] = xmalloc(sizeof(***dir));
394 (*dir)[ents]->filename = xstrdup(filename);
395 (*dir)[ents]->longname = xstrdup(longname);
396 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
397 (*dir)[++ents] = NULL;
406 do_close(conn, handle, handle_len);
409 /* Don't return partial matches on interrupt */
410 if (interrupted && dir != NULL && *dir != NULL) {
411 free_sftp_dirents(*dir);
412 *dir = xmalloc(sizeof(**dir));
420 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
422 return(do_lsreaddir(conn, path, 0, dir));
425 void free_sftp_dirents(SFTP_DIRENT **s)
429 for (i = 0; s[i]; i++) {
430 xfree(s[i]->filename);
431 xfree(s[i]->longname);
438 do_rm(struct sftp_conn *conn, char *path)
442 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
445 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
447 status = get_status(conn->fd_in, id);
448 if (status != SSH2_FX_OK)
449 error("Couldn't delete file: %s", fx2txt(status));
454 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
459 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
462 status = get_status(conn->fd_in, id);
463 if (status != SSH2_FX_OK)
464 error("Couldn't create directory: %s", fx2txt(status));
470 do_rmdir(struct sftp_conn *conn, char *path)
475 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
478 status = get_status(conn->fd_in, id);
479 if (status != SSH2_FX_OK)
480 error("Couldn't remove directory: %s", fx2txt(status));
486 do_stat(struct sftp_conn *conn, char *path, int quiet)
492 send_string_request(conn->fd_out, id,
493 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
496 return(get_decode_stat(conn->fd_in, id, quiet));
500 do_lstat(struct sftp_conn *conn, char *path, int quiet)
504 if (conn->version == 0) {
506 debug("Server version does not support lstat operation");
508 logit("Server version does not support lstat operation");
509 return(do_stat(conn, path, quiet));
513 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
516 return(get_decode_stat(conn->fd_in, id, quiet));
520 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
525 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
528 return(get_decode_stat(conn->fd_in, id, quiet));
532 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
537 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
540 status = get_status(conn->fd_in, id);
541 if (status != SSH2_FX_OK)
542 error("Couldn't setstat on \"%s\": %s", path,
549 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
555 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
558 status = get_status(conn->fd_in, id);
559 if (status != SSH2_FX_OK)
560 error("Couldn't fsetstat: %s", fx2txt(status));
566 do_realpath(struct sftp_conn *conn, char *path)
569 u_int type, expected_id, count, id;
570 char *filename, *longname;
573 expected_id = id = conn->msg_id++;
574 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
579 get_msg(conn->fd_in, &msg);
580 type = buffer_get_char(&msg);
581 id = buffer_get_int(&msg);
583 if (id != expected_id)
584 fatal("ID mismatch (%u != %u)", id, expected_id);
586 if (type == SSH2_FXP_STATUS) {
587 u_int status = buffer_get_int(&msg);
589 error("Couldn't canonicalise: %s", fx2txt(status));
591 } else if (type != SSH2_FXP_NAME)
592 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
593 SSH2_FXP_NAME, type);
595 count = buffer_get_int(&msg);
597 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
599 filename = buffer_get_string(&msg, NULL);
600 longname = buffer_get_string(&msg, NULL);
601 a = decode_attrib(&msg);
603 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
613 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
620 /* Send rename request */
622 buffer_put_char(&msg, SSH2_FXP_RENAME);
623 buffer_put_int(&msg, id);
624 buffer_put_cstring(&msg, oldpath);
625 buffer_put_cstring(&msg, newpath);
626 send_msg(conn->fd_out, &msg);
627 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
631 status = get_status(conn->fd_in, id);
632 if (status != SSH2_FX_OK)
633 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
634 newpath, fx2txt(status));
640 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
645 if (conn->version < 3) {
646 error("This server does not support the symlink operation");
647 return(SSH2_FX_OP_UNSUPPORTED);
652 /* Send symlink request */
654 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
655 buffer_put_int(&msg, id);
656 buffer_put_cstring(&msg, oldpath);
657 buffer_put_cstring(&msg, newpath);
658 send_msg(conn->fd_out, &msg);
659 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
663 status = get_status(conn->fd_in, id);
664 if (status != SSH2_FX_OK)
665 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
666 newpath, fx2txt(status));
672 do_readlink(struct sftp_conn *conn, char *path)
675 u_int type, expected_id, count, id;
676 char *filename, *longname;
679 expected_id = id = conn->msg_id++;
680 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
685 get_msg(conn->fd_in, &msg);
686 type = buffer_get_char(&msg);
687 id = buffer_get_int(&msg);
689 if (id != expected_id)
690 fatal("ID mismatch (%u != %u)", id, expected_id);
692 if (type == SSH2_FXP_STATUS) {
693 u_int status = buffer_get_int(&msg);
695 error("Couldn't readlink: %s", fx2txt(status));
697 } else if (type != SSH2_FXP_NAME)
698 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
699 SSH2_FXP_NAME, type);
701 count = buffer_get_int(&msg);
703 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
705 filename = buffer_get_string(&msg, NULL);
706 longname = buffer_get_string(&msg, NULL);
707 a = decode_attrib(&msg);
709 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
719 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
720 char *handle, u_int handle_len)
726 buffer_put_char(&msg, SSH2_FXP_READ);
727 buffer_put_int(&msg, id);
728 buffer_put_string(&msg, handle, handle_len);
729 buffer_put_int64(&msg, offset);
730 buffer_put_int(&msg, len);
731 send_msg(fd_out, &msg);
736 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
742 int local_fd, status = 0, write_error;
743 int read_error, write_errno;
744 u_int64_t offset, size;
745 u_int handle_len, mode, type, id, buflen, num_req, max_req;
746 off_t progress_counter;
751 TAILQ_ENTRY(request) tq;
753 TAILQ_HEAD(reqhead, request) requests;
756 TAILQ_INIT(&requests);
758 a = do_stat(conn, remote_path, 0);
762 /* XXX: should we preserve set[ug]id? */
763 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
764 mode = a->perm & 0777;
768 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
769 (!S_ISREG(a->perm))) {
770 error("Cannot download non-regular file: %s", remote_path);
774 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
779 buflen = conn->transfer_buflen;
782 /* Send open request */
784 buffer_put_char(&msg, SSH2_FXP_OPEN);
785 buffer_put_int(&msg, id);
786 buffer_put_cstring(&msg, remote_path);
787 buffer_put_int(&msg, SSH2_FXF_READ);
788 attrib_clear(&junk); /* Send empty attributes */
789 encode_attrib(&msg, &junk);
790 send_msg(conn->fd_out, &msg);
791 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
793 handle = get_handle(conn->fd_in, id, &handle_len);
794 if (handle == NULL) {
799 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
801 if (local_fd == -1) {
802 error("Couldn't open local file \"%s\" for writing: %s",
803 local_path, strerror(errno));
809 /* Read from remote and write to local */
810 write_error = read_error = write_errno = num_req = offset = 0;
812 progress_counter = 0;
814 if (showprogress && size != 0)
815 start_progress_meter(remote_path, size, &progress_counter);
817 while (num_req > 0 || max_req > 0) {
822 * Simulate EOF on interrupt: stop sending new requests and
823 * allow outstanding requests to drain gracefully
826 if (num_req == 0) /* If we haven't started yet... */
831 /* Send some more requests */
832 while (num_req < max_req) {
833 debug3("Request range %llu -> %llu (%d/%d)",
834 (unsigned long long)offset,
835 (unsigned long long)offset + buflen - 1,
837 req = xmalloc(sizeof(*req));
838 req->id = conn->msg_id++;
840 req->offset = offset;
843 TAILQ_INSERT_TAIL(&requests, req, tq);
844 send_read_request(conn->fd_out, req->id, req->offset,
845 req->len, handle, handle_len);
849 get_msg(conn->fd_in, &msg);
850 type = buffer_get_char(&msg);
851 id = buffer_get_int(&msg);
852 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
854 /* Find the request in our queue */
855 for (req = TAILQ_FIRST(&requests);
856 req != NULL && req->id != id;
857 req = TAILQ_NEXT(req, tq))
860 fatal("Unexpected reply %u", id);
863 case SSH2_FXP_STATUS:
864 status = buffer_get_int(&msg);
865 if (status != SSH2_FX_EOF)
868 TAILQ_REMOVE(&requests, req, tq);
873 data = buffer_get_string(&msg, &len);
874 debug3("Received data %llu -> %llu",
875 (unsigned long long)req->offset,
876 (unsigned long long)req->offset + len - 1);
878 fatal("Received more data than asked for "
879 "%u > %u", len, req->len);
880 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
881 atomicio(vwrite, local_fd, data, len) != len) &&
887 progress_counter += len;
890 if (len == req->len) {
891 TAILQ_REMOVE(&requests, req, tq);
895 /* Resend the request for the missing data */
896 debug3("Short data block, re-requesting "
897 "%llu -> %llu (%2d)",
898 (unsigned long long)req->offset + len,
899 (unsigned long long)req->offset +
900 req->len - 1, num_req);
901 req->id = conn->msg_id++;
904 send_read_request(conn->fd_out, req->id,
905 req->offset, req->len, handle, handle_len);
906 /* Reduce the request size */
908 buflen = MAX(MIN_READ_SIZE, len);
910 if (max_req > 0) { /* max_req = 0 iff EOF received */
911 if (size > 0 && offset > size) {
912 /* Only one request at a time
913 * after the expected EOF */
914 debug3("Finish at %llu (%2d)",
915 (unsigned long long)offset,
918 } else if (max_req <= conn->num_requests) {
924 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
925 SSH2_FXP_DATA, type);
929 if (showprogress && size)
930 stop_progress_meter();
933 if (TAILQ_FIRST(&requests) != NULL)
934 fatal("Transfer complete, but requests still in queue");
937 error("Couldn't read from remote file \"%s\" : %s",
938 remote_path, fx2txt(status));
939 do_close(conn, handle, handle_len);
940 } else if (write_error) {
941 error("Couldn't write to \"%s\": %s", local_path,
942 strerror(write_errno));
944 do_close(conn, handle, handle_len);
946 status = do_close(conn, handle, handle_len);
948 /* Override umask and utimes if asked */
950 if (pflag && fchmod(local_fd, mode) == -1)
952 if (pflag && chmod(local_path, mode) == -1)
953 #endif /* HAVE_FCHMOD */
954 error("Couldn't set mode on \"%s\": %s", local_path,
956 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
957 struct timeval tv[2];
958 tv[0].tv_sec = a->atime;
959 tv[1].tv_sec = a->mtime;
960 tv[0].tv_usec = tv[1].tv_usec = 0;
961 if (utimes(local_path, tv) == -1)
962 error("Can't set times on \"%s\": %s",
963 local_path, strerror(errno));
974 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
977 int local_fd, status;
978 u_int handle_len, id, type;
986 struct outstanding_ack {
990 TAILQ_ENTRY(outstanding_ack) tq;
992 TAILQ_HEAD(ackhead, outstanding_ack) acks;
993 struct outstanding_ack *ack = NULL;
997 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
998 error("Couldn't open local file \"%s\" for reading: %s",
999 local_path, strerror(errno));
1002 if (fstat(local_fd, &sb) == -1) {
1003 error("Couldn't fstat local file \"%s\": %s",
1004 local_path, strerror(errno));
1008 if (!S_ISREG(sb.st_mode)) {
1009 error("%s is not a regular file", local_path);
1013 stat_to_attrib(&sb, &a);
1015 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1016 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1019 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1023 /* Send open request */
1024 id = conn->msg_id++;
1025 buffer_put_char(&msg, SSH2_FXP_OPEN);
1026 buffer_put_int(&msg, id);
1027 buffer_put_cstring(&msg, remote_path);
1028 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1029 encode_attrib(&msg, &a);
1030 send_msg(conn->fd_out, &msg);
1031 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1035 handle = get_handle(conn->fd_in, id, &handle_len);
1036 if (handle == NULL) {
1042 startid = ackid = id + 1;
1043 data = xmalloc(conn->transfer_buflen);
1045 /* Read from local and write to remote */
1048 start_progress_meter(local_path, sb.st_size, &offset);
1054 * Can't use atomicio here because it returns 0 on EOF,
1055 * thus losing the last block of the file.
1056 * Simulate an EOF on interrupt, allowing ACKs from the
1062 len = read(local_fd, data, conn->transfer_buflen);
1063 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1066 fatal("Couldn't read from \"%s\": %s", local_path,
1070 ack = xmalloc(sizeof(*ack));
1072 ack->offset = offset;
1074 TAILQ_INSERT_TAIL(&acks, ack, tq);
1077 buffer_put_char(&msg, SSH2_FXP_WRITE);
1078 buffer_put_int(&msg, ack->id);
1079 buffer_put_string(&msg, handle, handle_len);
1080 buffer_put_int64(&msg, offset);
1081 buffer_put_string(&msg, data, len);
1082 send_msg(conn->fd_out, &msg);
1083 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1084 id, (unsigned long long)offset, len);
1085 } else if (TAILQ_FIRST(&acks) == NULL)
1089 fatal("Unexpected ACK %u", id);
1091 if (id == startid || len == 0 ||
1092 id - ackid >= conn->num_requests) {
1096 get_msg(conn->fd_in, &msg);
1097 type = buffer_get_char(&msg);
1098 r_id = buffer_get_int(&msg);
1100 if (type != SSH2_FXP_STATUS)
1101 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1102 "got %d", SSH2_FXP_STATUS, type);
1104 status = buffer_get_int(&msg);
1105 debug3("SSH2_FXP_STATUS %d", status);
1107 /* Find the request in our queue */
1108 for (ack = TAILQ_FIRST(&acks);
1109 ack != NULL && ack->id != r_id;
1110 ack = TAILQ_NEXT(ack, tq))
1113 fatal("Can't find request for ID %u", r_id);
1114 TAILQ_REMOVE(&acks, ack, tq);
1116 if (status != SSH2_FX_OK) {
1117 error("Couldn't write to remote file \"%s\": %s",
1118 remote_path, fx2txt(status));
1119 do_close(conn, handle, handle_len);
1125 debug3("In write loop, ack for %u %u bytes at %llu",
1126 ack->id, ack->len, (unsigned long long)ack->offset);
1133 stop_progress_meter();
1136 if (close(local_fd) == -1) {
1137 error("Couldn't close local file \"%s\": %s", local_path,
1139 do_close(conn, handle, handle_len);
1144 /* Override umask and utimes if asked */
1146 do_fsetstat(conn, handle, handle_len, &a);
1148 status = do_close(conn, handle, handle_len);