1 /* $OpenBSD: sftp-client.c,v 1.67 2006/07/11 20:07:25 stevesk Exp $ */
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* XXX: signed vs unsigned */
20 /* XXX: remove all logging, only return status codes */
21 /* XXX: copy between two remote sites */
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
34 #include "openbsd-compat/sys-queue.h"
41 #include "progressmeter.h"
45 #include "sftp-common.h"
46 #include "sftp-client.h"
48 extern volatile sig_atomic_t interrupted;
49 extern int showprogress;
51 /* Minimum amount of data to read at a time */
52 #define MIN_READ_SIZE 512
57 u_int transfer_buflen;
64 send_msg(int fd, Buffer *m)
69 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
70 fatal("Outbound message too long %u", buffer_len(m));
72 /* Send length first */
73 put_u32(mlen, buffer_len(m));
74 iov[0].iov_base = mlen;
75 iov[0].iov_len = sizeof(mlen);
76 iov[1].iov_base = buffer_ptr(m);
77 iov[1].iov_len = buffer_len(m);
79 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
80 fatal("Couldn't send packet: %s", strerror(errno));
86 get_msg(int fd, Buffer *m)
90 buffer_append_space(m, 4);
91 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
93 fatal("Connection closed");
95 fatal("Couldn't read packet: %s", strerror(errno));
98 msg_len = buffer_get_int(m);
99 if (msg_len > SFTP_MAX_MSG_LENGTH)
100 fatal("Received message too long %u", msg_len);
102 buffer_append_space(m, msg_len);
103 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
105 fatal("Connection closed");
107 fatal("Read packet: %s", strerror(errno));
112 send_string_request(int fd, u_int id, u_int code, char *s,
118 buffer_put_char(&msg, code);
119 buffer_put_int(&msg, id);
120 buffer_put_string(&msg, s, len);
122 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
127 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
128 u_int len, Attrib *a)
133 buffer_put_char(&msg, code);
134 buffer_put_int(&msg, id);
135 buffer_put_string(&msg, s, len);
136 encode_attrib(&msg, a);
138 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
143 get_status(int fd, u_int expected_id)
146 u_int type, id, status;
150 type = buffer_get_char(&msg);
151 id = buffer_get_int(&msg);
153 if (id != expected_id)
154 fatal("ID mismatch (%u != %u)", id, expected_id);
155 if (type != SSH2_FXP_STATUS)
156 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
157 SSH2_FXP_STATUS, type);
159 status = buffer_get_int(&msg);
162 debug3("SSH2_FXP_STATUS %u", status);
168 get_handle(int fd, u_int expected_id, u_int *len)
176 type = buffer_get_char(&msg);
177 id = buffer_get_int(&msg);
179 if (id != expected_id)
180 fatal("ID mismatch (%u != %u)", id, expected_id);
181 if (type == SSH2_FXP_STATUS) {
182 int status = buffer_get_int(&msg);
184 error("Couldn't get handle: %s", fx2txt(status));
187 } else if (type != SSH2_FXP_HANDLE)
188 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
189 SSH2_FXP_HANDLE, type);
191 handle = buffer_get_string(&msg, len);
198 get_decode_stat(int fd, u_int expected_id, int quiet)
207 type = buffer_get_char(&msg);
208 id = buffer_get_int(&msg);
210 debug3("Received stat reply T:%u I:%u", type, id);
211 if (id != expected_id)
212 fatal("ID mismatch (%u != %u)", id, expected_id);
213 if (type == SSH2_FXP_STATUS) {
214 int status = buffer_get_int(&msg);
217 debug("Couldn't stat remote file: %s", fx2txt(status));
219 error("Couldn't stat remote file: %s", fx2txt(status));
222 } else if (type != SSH2_FXP_ATTRS) {
223 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
224 SSH2_FXP_ATTRS, type);
226 a = decode_attrib(&msg);
233 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
238 struct sftp_conn *ret;
241 buffer_put_char(&msg, SSH2_FXP_INIT);
242 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
243 send_msg(fd_out, &msg);
247 get_msg(fd_in, &msg);
249 /* Expecting a VERSION reply */
250 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
251 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
256 version = buffer_get_int(&msg);
258 debug2("Remote version: %d", version);
260 /* Check for extensions */
261 while (buffer_len(&msg) > 0) {
262 char *name = buffer_get_string(&msg, NULL);
263 char *value = buffer_get_string(&msg, NULL);
265 debug2("Init extension: \"%s\"", name);
272 ret = xmalloc(sizeof(*ret));
274 ret->fd_out = fd_out;
275 ret->transfer_buflen = transfer_buflen;
276 ret->num_requests = num_requests;
277 ret->version = version;
280 /* Some filexfer v.0 servers don't support large packets */
282 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
288 sftp_proto_version(struct sftp_conn *conn)
290 return(conn->version);
294 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
302 buffer_put_char(&msg, SSH2_FXP_CLOSE);
303 buffer_put_int(&msg, id);
304 buffer_put_string(&msg, handle, handle_len);
305 send_msg(conn->fd_out, &msg);
306 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
308 status = get_status(conn->fd_in, id);
309 if (status != SSH2_FX_OK)
310 error("Couldn't close file: %s", fx2txt(status));
319 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
323 u_int count, type, id, handle_len, i, expected_id, ents = 0;
329 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
330 buffer_put_int(&msg, id);
331 buffer_put_cstring(&msg, path);
332 send_msg(conn->fd_out, &msg);
336 handle = get_handle(conn->fd_in, id, &handle_len);
342 *dir = xmalloc(sizeof(**dir));
346 for (; !interrupted;) {
347 id = expected_id = conn->msg_id++;
349 debug3("Sending SSH2_FXP_READDIR I:%u", id);
352 buffer_put_char(&msg, SSH2_FXP_READDIR);
353 buffer_put_int(&msg, id);
354 buffer_put_string(&msg, handle, handle_len);
355 send_msg(conn->fd_out, &msg);
359 get_msg(conn->fd_in, &msg);
361 type = buffer_get_char(&msg);
362 id = buffer_get_int(&msg);
364 debug3("Received reply T:%u I:%u", type, id);
366 if (id != expected_id)
367 fatal("ID mismatch (%u != %u)", id, expected_id);
369 if (type == SSH2_FXP_STATUS) {
370 int status = buffer_get_int(&msg);
372 debug3("Received SSH2_FXP_STATUS %d", status);
374 if (status == SSH2_FX_EOF) {
377 error("Couldn't read directory: %s",
379 do_close(conn, handle, handle_len);
383 } else if (type != SSH2_FXP_NAME)
384 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
385 SSH2_FXP_NAME, type);
387 count = buffer_get_int(&msg);
390 debug3("Received %d SSH2_FXP_NAME responses", count);
391 for (i = 0; i < count; i++) {
392 char *filename, *longname;
395 filename = buffer_get_string(&msg, NULL);
396 longname = buffer_get_string(&msg, NULL);
397 a = decode_attrib(&msg);
400 printf("%s\n", longname);
403 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
404 (*dir)[ents] = xmalloc(sizeof(***dir));
405 (*dir)[ents]->filename = xstrdup(filename);
406 (*dir)[ents]->longname = xstrdup(longname);
407 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
408 (*dir)[++ents] = NULL;
417 do_close(conn, handle, handle_len);
420 /* Don't return partial matches on interrupt */
421 if (interrupted && dir != NULL && *dir != NULL) {
422 free_sftp_dirents(*dir);
423 *dir = xmalloc(sizeof(**dir));
431 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
433 return(do_lsreaddir(conn, path, 0, dir));
436 void free_sftp_dirents(SFTP_DIRENT **s)
440 for (i = 0; s[i]; i++) {
441 xfree(s[i]->filename);
442 xfree(s[i]->longname);
449 do_rm(struct sftp_conn *conn, char *path)
453 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
456 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
458 status = get_status(conn->fd_in, id);
459 if (status != SSH2_FX_OK)
460 error("Couldn't delete file: %s", fx2txt(status));
465 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
470 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
473 status = get_status(conn->fd_in, id);
474 if (status != SSH2_FX_OK)
475 error("Couldn't create directory: %s", fx2txt(status));
481 do_rmdir(struct sftp_conn *conn, char *path)
486 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
489 status = get_status(conn->fd_in, id);
490 if (status != SSH2_FX_OK)
491 error("Couldn't remove directory: %s", fx2txt(status));
497 do_stat(struct sftp_conn *conn, char *path, int quiet)
503 send_string_request(conn->fd_out, id,
504 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
507 return(get_decode_stat(conn->fd_in, id, quiet));
511 do_lstat(struct sftp_conn *conn, char *path, int quiet)
515 if (conn->version == 0) {
517 debug("Server version does not support lstat operation");
519 logit("Server version does not support lstat operation");
520 return(do_stat(conn, path, quiet));
524 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
527 return(get_decode_stat(conn->fd_in, id, quiet));
531 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
536 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
539 return(get_decode_stat(conn->fd_in, id, quiet));
543 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
548 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
551 status = get_status(conn->fd_in, id);
552 if (status != SSH2_FX_OK)
553 error("Couldn't setstat on \"%s\": %s", path,
560 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
566 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
569 status = get_status(conn->fd_in, id);
570 if (status != SSH2_FX_OK)
571 error("Couldn't fsetstat: %s", fx2txt(status));
577 do_realpath(struct sftp_conn *conn, char *path)
580 u_int type, expected_id, count, id;
581 char *filename, *longname;
584 expected_id = id = conn->msg_id++;
585 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
590 get_msg(conn->fd_in, &msg);
591 type = buffer_get_char(&msg);
592 id = buffer_get_int(&msg);
594 if (id != expected_id)
595 fatal("ID mismatch (%u != %u)", id, expected_id);
597 if (type == SSH2_FXP_STATUS) {
598 u_int status = buffer_get_int(&msg);
600 error("Couldn't canonicalise: %s", fx2txt(status));
602 } else if (type != SSH2_FXP_NAME)
603 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
604 SSH2_FXP_NAME, type);
606 count = buffer_get_int(&msg);
608 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
610 filename = buffer_get_string(&msg, NULL);
611 longname = buffer_get_string(&msg, NULL);
612 a = decode_attrib(&msg);
614 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
624 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
631 /* Send rename request */
633 buffer_put_char(&msg, SSH2_FXP_RENAME);
634 buffer_put_int(&msg, id);
635 buffer_put_cstring(&msg, oldpath);
636 buffer_put_cstring(&msg, newpath);
637 send_msg(conn->fd_out, &msg);
638 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
642 status = get_status(conn->fd_in, id);
643 if (status != SSH2_FX_OK)
644 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
645 newpath, fx2txt(status));
651 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
656 if (conn->version < 3) {
657 error("This server does not support the symlink operation");
658 return(SSH2_FX_OP_UNSUPPORTED);
663 /* Send symlink request */
665 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
666 buffer_put_int(&msg, id);
667 buffer_put_cstring(&msg, oldpath);
668 buffer_put_cstring(&msg, newpath);
669 send_msg(conn->fd_out, &msg);
670 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
674 status = get_status(conn->fd_in, id);
675 if (status != SSH2_FX_OK)
676 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
677 newpath, fx2txt(status));
683 do_readlink(struct sftp_conn *conn, char *path)
686 u_int type, expected_id, count, id;
687 char *filename, *longname;
690 expected_id = id = conn->msg_id++;
691 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
696 get_msg(conn->fd_in, &msg);
697 type = buffer_get_char(&msg);
698 id = buffer_get_int(&msg);
700 if (id != expected_id)
701 fatal("ID mismatch (%u != %u)", id, expected_id);
703 if (type == SSH2_FXP_STATUS) {
704 u_int status = buffer_get_int(&msg);
706 error("Couldn't readlink: %s", fx2txt(status));
708 } else if (type != SSH2_FXP_NAME)
709 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
710 SSH2_FXP_NAME, type);
712 count = buffer_get_int(&msg);
714 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
716 filename = buffer_get_string(&msg, NULL);
717 longname = buffer_get_string(&msg, NULL);
718 a = decode_attrib(&msg);
720 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
730 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
731 char *handle, u_int handle_len)
737 buffer_put_char(&msg, SSH2_FXP_READ);
738 buffer_put_int(&msg, id);
739 buffer_put_string(&msg, handle, handle_len);
740 buffer_put_int64(&msg, offset);
741 buffer_put_int(&msg, len);
742 send_msg(fd_out, &msg);
747 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
753 int local_fd, status = 0, write_error;
754 int read_error, write_errno;
755 u_int64_t offset, size;
756 u_int handle_len, mode, type, id, buflen, num_req, max_req;
757 off_t progress_counter;
762 TAILQ_ENTRY(request) tq;
764 TAILQ_HEAD(reqhead, request) requests;
767 TAILQ_INIT(&requests);
769 a = do_stat(conn, remote_path, 0);
773 /* XXX: should we preserve set[ug]id? */
774 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
775 mode = a->perm & 0777;
779 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
780 (!S_ISREG(a->perm))) {
781 error("Cannot download non-regular file: %s", remote_path);
785 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
790 buflen = conn->transfer_buflen;
793 /* Send open request */
795 buffer_put_char(&msg, SSH2_FXP_OPEN);
796 buffer_put_int(&msg, id);
797 buffer_put_cstring(&msg, remote_path);
798 buffer_put_int(&msg, SSH2_FXF_READ);
799 attrib_clear(&junk); /* Send empty attributes */
800 encode_attrib(&msg, &junk);
801 send_msg(conn->fd_out, &msg);
802 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
804 handle = get_handle(conn->fd_in, id, &handle_len);
805 if (handle == NULL) {
810 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
812 if (local_fd == -1) {
813 error("Couldn't open local file \"%s\" for writing: %s",
814 local_path, strerror(errno));
820 /* Read from remote and write to local */
821 write_error = read_error = write_errno = num_req = offset = 0;
823 progress_counter = 0;
825 if (showprogress && size != 0)
826 start_progress_meter(remote_path, size, &progress_counter);
828 while (num_req > 0 || max_req > 0) {
833 * Simulate EOF on interrupt: stop sending new requests and
834 * allow outstanding requests to drain gracefully
837 if (num_req == 0) /* If we haven't started yet... */
842 /* Send some more requests */
843 while (num_req < max_req) {
844 debug3("Request range %llu -> %llu (%d/%d)",
845 (unsigned long long)offset,
846 (unsigned long long)offset + buflen - 1,
848 req = xmalloc(sizeof(*req));
849 req->id = conn->msg_id++;
851 req->offset = offset;
854 TAILQ_INSERT_TAIL(&requests, req, tq);
855 send_read_request(conn->fd_out, req->id, req->offset,
856 req->len, handle, handle_len);
860 get_msg(conn->fd_in, &msg);
861 type = buffer_get_char(&msg);
862 id = buffer_get_int(&msg);
863 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
865 /* Find the request in our queue */
866 for (req = TAILQ_FIRST(&requests);
867 req != NULL && req->id != id;
868 req = TAILQ_NEXT(req, tq))
871 fatal("Unexpected reply %u", id);
874 case SSH2_FXP_STATUS:
875 status = buffer_get_int(&msg);
876 if (status != SSH2_FX_EOF)
879 TAILQ_REMOVE(&requests, req, tq);
884 data = buffer_get_string(&msg, &len);
885 debug3("Received data %llu -> %llu",
886 (unsigned long long)req->offset,
887 (unsigned long long)req->offset + len - 1);
889 fatal("Received more data than asked for "
890 "%u > %u", len, req->len);
891 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
892 atomicio(vwrite, local_fd, data, len) != len) &&
898 progress_counter += len;
901 if (len == req->len) {
902 TAILQ_REMOVE(&requests, req, tq);
906 /* Resend the request for the missing data */
907 debug3("Short data block, re-requesting "
908 "%llu -> %llu (%2d)",
909 (unsigned long long)req->offset + len,
910 (unsigned long long)req->offset +
911 req->len - 1, num_req);
912 req->id = conn->msg_id++;
915 send_read_request(conn->fd_out, req->id,
916 req->offset, req->len, handle, handle_len);
917 /* Reduce the request size */
919 buflen = MAX(MIN_READ_SIZE, len);
921 if (max_req > 0) { /* max_req = 0 iff EOF received */
922 if (size > 0 && offset > size) {
923 /* Only one request at a time
924 * after the expected EOF */
925 debug3("Finish at %llu (%2d)",
926 (unsigned long long)offset,
929 } else if (max_req <= conn->num_requests) {
935 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
936 SSH2_FXP_DATA, type);
940 if (showprogress && size)
941 stop_progress_meter();
944 if (TAILQ_FIRST(&requests) != NULL)
945 fatal("Transfer complete, but requests still in queue");
948 error("Couldn't read from remote file \"%s\" : %s",
949 remote_path, fx2txt(status));
950 do_close(conn, handle, handle_len);
951 } else if (write_error) {
952 error("Couldn't write to \"%s\": %s", local_path,
953 strerror(write_errno));
955 do_close(conn, handle, handle_len);
957 status = do_close(conn, handle, handle_len);
959 /* Override umask and utimes if asked */
961 if (pflag && fchmod(local_fd, mode) == -1)
963 if (pflag && chmod(local_path, mode) == -1)
964 #endif /* HAVE_FCHMOD */
965 error("Couldn't set mode on \"%s\": %s", local_path,
967 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
968 struct timeval tv[2];
969 tv[0].tv_sec = a->atime;
970 tv[1].tv_sec = a->mtime;
971 tv[0].tv_usec = tv[1].tv_usec = 0;
972 if (utimes(local_path, tv) == -1)
973 error("Can't set times on \"%s\": %s",
974 local_path, strerror(errno));
985 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
988 int local_fd, status;
989 u_int handle_len, id, type;
997 struct outstanding_ack {
1001 TAILQ_ENTRY(outstanding_ack) tq;
1003 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1004 struct outstanding_ack *ack = NULL;
1008 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1009 error("Couldn't open local file \"%s\" for reading: %s",
1010 local_path, strerror(errno));
1013 if (fstat(local_fd, &sb) == -1) {
1014 error("Couldn't fstat local file \"%s\": %s",
1015 local_path, strerror(errno));
1019 if (!S_ISREG(sb.st_mode)) {
1020 error("%s is not a regular file", local_path);
1024 stat_to_attrib(&sb, &a);
1026 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1027 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1030 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1034 /* Send open request */
1035 id = conn->msg_id++;
1036 buffer_put_char(&msg, SSH2_FXP_OPEN);
1037 buffer_put_int(&msg, id);
1038 buffer_put_cstring(&msg, remote_path);
1039 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1040 encode_attrib(&msg, &a);
1041 send_msg(conn->fd_out, &msg);
1042 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1046 handle = get_handle(conn->fd_in, id, &handle_len);
1047 if (handle == NULL) {
1053 startid = ackid = id + 1;
1054 data = xmalloc(conn->transfer_buflen);
1056 /* Read from local and write to remote */
1059 start_progress_meter(local_path, sb.st_size, &offset);
1065 * Can't use atomicio here because it returns 0 on EOF,
1066 * thus losing the last block of the file.
1067 * Simulate an EOF on interrupt, allowing ACKs from the
1073 len = read(local_fd, data, conn->transfer_buflen);
1074 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1077 fatal("Couldn't read from \"%s\": %s", local_path,
1081 ack = xmalloc(sizeof(*ack));
1083 ack->offset = offset;
1085 TAILQ_INSERT_TAIL(&acks, ack, tq);
1088 buffer_put_char(&msg, SSH2_FXP_WRITE);
1089 buffer_put_int(&msg, ack->id);
1090 buffer_put_string(&msg, handle, handle_len);
1091 buffer_put_int64(&msg, offset);
1092 buffer_put_string(&msg, data, len);
1093 send_msg(conn->fd_out, &msg);
1094 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1095 id, (unsigned long long)offset, len);
1096 } else if (TAILQ_FIRST(&acks) == NULL)
1100 fatal("Unexpected ACK %u", id);
1102 if (id == startid || len == 0 ||
1103 id - ackid >= conn->num_requests) {
1107 get_msg(conn->fd_in, &msg);
1108 type = buffer_get_char(&msg);
1109 r_id = buffer_get_int(&msg);
1111 if (type != SSH2_FXP_STATUS)
1112 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1113 "got %d", SSH2_FXP_STATUS, type);
1115 status = buffer_get_int(&msg);
1116 debug3("SSH2_FXP_STATUS %d", status);
1118 /* Find the request in our queue */
1119 for (ack = TAILQ_FIRST(&acks);
1120 ack != NULL && ack->id != r_id;
1121 ack = TAILQ_NEXT(ack, tq))
1124 fatal("Can't find request for ID %u", r_id);
1125 TAILQ_REMOVE(&acks, ack, tq);
1127 if (status != SSH2_FX_OK) {
1128 error("Couldn't write to remote file \"%s\": %s",
1129 remote_path, fx2txt(status));
1130 do_close(conn, handle, handle_len);
1136 debug3("In write loop, ack for %u %u bytes at %llu",
1137 ack->id, ack->len, (unsigned long long)ack->offset);
1144 stop_progress_meter();
1147 if (close(local_fd) == -1) {
1148 error("Couldn't close local file \"%s\": %s", local_path,
1150 do_close(conn, handle, handle_len);
1155 /* Override umask and utimes if asked */
1157 do_fsetstat(conn, handle, handle_len, &a);
1159 status = do_close(conn, handle, handle_len);