1 /* $OpenBSD: sftp-client.c,v 1.66 2006/07/09 15:15:11 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>
33 #include "openbsd-compat/sys-queue.h"
40 #include "progressmeter.h"
44 #include "sftp-common.h"
45 #include "sftp-client.h"
47 extern volatile sig_atomic_t interrupted;
48 extern int showprogress;
50 /* Minimum amount of data to read at a time */
51 #define MIN_READ_SIZE 512
56 u_int transfer_buflen;
63 send_msg(int fd, Buffer *m)
68 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
69 fatal("Outbound message too long %u", buffer_len(m));
71 /* Send length first */
72 put_u32(mlen, buffer_len(m));
73 iov[0].iov_base = mlen;
74 iov[0].iov_len = sizeof(mlen);
75 iov[1].iov_base = buffer_ptr(m);
76 iov[1].iov_len = buffer_len(m);
78 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
79 fatal("Couldn't send packet: %s", strerror(errno));
85 get_msg(int fd, Buffer *m)
89 buffer_append_space(m, 4);
90 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
92 fatal("Connection closed");
94 fatal("Couldn't read packet: %s", strerror(errno));
97 msg_len = buffer_get_int(m);
98 if (msg_len > SFTP_MAX_MSG_LENGTH)
99 fatal("Received message too long %u", msg_len);
101 buffer_append_space(m, msg_len);
102 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
104 fatal("Connection closed");
106 fatal("Read packet: %s", strerror(errno));
111 send_string_request(int fd, u_int id, u_int code, char *s,
117 buffer_put_char(&msg, code);
118 buffer_put_int(&msg, id);
119 buffer_put_string(&msg, s, len);
121 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
126 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
127 u_int len, Attrib *a)
132 buffer_put_char(&msg, code);
133 buffer_put_int(&msg, id);
134 buffer_put_string(&msg, s, len);
135 encode_attrib(&msg, a);
137 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
142 get_status(int fd, u_int expected_id)
145 u_int type, id, status;
149 type = buffer_get_char(&msg);
150 id = buffer_get_int(&msg);
152 if (id != expected_id)
153 fatal("ID mismatch (%u != %u)", id, expected_id);
154 if (type != SSH2_FXP_STATUS)
155 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
156 SSH2_FXP_STATUS, type);
158 status = buffer_get_int(&msg);
161 debug3("SSH2_FXP_STATUS %u", status);
167 get_handle(int fd, u_int expected_id, u_int *len)
175 type = buffer_get_char(&msg);
176 id = buffer_get_int(&msg);
178 if (id != expected_id)
179 fatal("ID mismatch (%u != %u)", id, expected_id);
180 if (type == SSH2_FXP_STATUS) {
181 int status = buffer_get_int(&msg);
183 error("Couldn't get handle: %s", fx2txt(status));
186 } else if (type != SSH2_FXP_HANDLE)
187 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
188 SSH2_FXP_HANDLE, type);
190 handle = buffer_get_string(&msg, len);
197 get_decode_stat(int fd, u_int expected_id, int quiet)
206 type = buffer_get_char(&msg);
207 id = buffer_get_int(&msg);
209 debug3("Received stat reply T:%u I:%u", type, id);
210 if (id != expected_id)
211 fatal("ID mismatch (%u != %u)", id, expected_id);
212 if (type == SSH2_FXP_STATUS) {
213 int status = buffer_get_int(&msg);
216 debug("Couldn't stat remote file: %s", fx2txt(status));
218 error("Couldn't stat remote file: %s", fx2txt(status));
221 } else if (type != SSH2_FXP_ATTRS) {
222 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
223 SSH2_FXP_ATTRS, type);
225 a = decode_attrib(&msg);
232 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
237 struct sftp_conn *ret;
240 buffer_put_char(&msg, SSH2_FXP_INIT);
241 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
242 send_msg(fd_out, &msg);
246 get_msg(fd_in, &msg);
248 /* Expecting a VERSION reply */
249 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
250 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
255 version = buffer_get_int(&msg);
257 debug2("Remote version: %d", version);
259 /* Check for extensions */
260 while (buffer_len(&msg) > 0) {
261 char *name = buffer_get_string(&msg, NULL);
262 char *value = buffer_get_string(&msg, NULL);
264 debug2("Init extension: \"%s\"", name);
271 ret = xmalloc(sizeof(*ret));
273 ret->fd_out = fd_out;
274 ret->transfer_buflen = transfer_buflen;
275 ret->num_requests = num_requests;
276 ret->version = version;
279 /* Some filexfer v.0 servers don't support large packets */
281 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
287 sftp_proto_version(struct sftp_conn *conn)
289 return(conn->version);
293 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
301 buffer_put_char(&msg, SSH2_FXP_CLOSE);
302 buffer_put_int(&msg, id);
303 buffer_put_string(&msg, handle, handle_len);
304 send_msg(conn->fd_out, &msg);
305 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
307 status = get_status(conn->fd_in, id);
308 if (status != SSH2_FX_OK)
309 error("Couldn't close file: %s", fx2txt(status));
318 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
322 u_int count, type, id, handle_len, i, expected_id, ents = 0;
328 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
329 buffer_put_int(&msg, id);
330 buffer_put_cstring(&msg, path);
331 send_msg(conn->fd_out, &msg);
335 handle = get_handle(conn->fd_in, id, &handle_len);
341 *dir = xmalloc(sizeof(**dir));
345 for (; !interrupted;) {
346 id = expected_id = conn->msg_id++;
348 debug3("Sending SSH2_FXP_READDIR I:%u", id);
351 buffer_put_char(&msg, SSH2_FXP_READDIR);
352 buffer_put_int(&msg, id);
353 buffer_put_string(&msg, handle, handle_len);
354 send_msg(conn->fd_out, &msg);
358 get_msg(conn->fd_in, &msg);
360 type = buffer_get_char(&msg);
361 id = buffer_get_int(&msg);
363 debug3("Received reply T:%u I:%u", type, id);
365 if (id != expected_id)
366 fatal("ID mismatch (%u != %u)", id, expected_id);
368 if (type == SSH2_FXP_STATUS) {
369 int status = buffer_get_int(&msg);
371 debug3("Received SSH2_FXP_STATUS %d", status);
373 if (status == SSH2_FX_EOF) {
376 error("Couldn't read directory: %s",
378 do_close(conn, handle, handle_len);
382 } else if (type != SSH2_FXP_NAME)
383 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
384 SSH2_FXP_NAME, type);
386 count = buffer_get_int(&msg);
389 debug3("Received %d SSH2_FXP_NAME responses", count);
390 for (i = 0; i < count; i++) {
391 char *filename, *longname;
394 filename = buffer_get_string(&msg, NULL);
395 longname = buffer_get_string(&msg, NULL);
396 a = decode_attrib(&msg);
399 printf("%s\n", longname);
402 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
403 (*dir)[ents] = xmalloc(sizeof(***dir));
404 (*dir)[ents]->filename = xstrdup(filename);
405 (*dir)[ents]->longname = xstrdup(longname);
406 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
407 (*dir)[++ents] = NULL;
416 do_close(conn, handle, handle_len);
419 /* Don't return partial matches on interrupt */
420 if (interrupted && dir != NULL && *dir != NULL) {
421 free_sftp_dirents(*dir);
422 *dir = xmalloc(sizeof(**dir));
430 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
432 return(do_lsreaddir(conn, path, 0, dir));
435 void free_sftp_dirents(SFTP_DIRENT **s)
439 for (i = 0; s[i]; i++) {
440 xfree(s[i]->filename);
441 xfree(s[i]->longname);
448 do_rm(struct sftp_conn *conn, char *path)
452 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
455 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
457 status = get_status(conn->fd_in, id);
458 if (status != SSH2_FX_OK)
459 error("Couldn't delete file: %s", fx2txt(status));
464 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
469 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
472 status = get_status(conn->fd_in, id);
473 if (status != SSH2_FX_OK)
474 error("Couldn't create directory: %s", fx2txt(status));
480 do_rmdir(struct sftp_conn *conn, char *path)
485 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
488 status = get_status(conn->fd_in, id);
489 if (status != SSH2_FX_OK)
490 error("Couldn't remove directory: %s", fx2txt(status));
496 do_stat(struct sftp_conn *conn, char *path, int quiet)
502 send_string_request(conn->fd_out, id,
503 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
506 return(get_decode_stat(conn->fd_in, id, quiet));
510 do_lstat(struct sftp_conn *conn, char *path, int quiet)
514 if (conn->version == 0) {
516 debug("Server version does not support lstat operation");
518 logit("Server version does not support lstat operation");
519 return(do_stat(conn, path, quiet));
523 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
526 return(get_decode_stat(conn->fd_in, id, quiet));
530 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
535 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
538 return(get_decode_stat(conn->fd_in, id, quiet));
542 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
547 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
550 status = get_status(conn->fd_in, id);
551 if (status != SSH2_FX_OK)
552 error("Couldn't setstat on \"%s\": %s", path,
559 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
565 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
568 status = get_status(conn->fd_in, id);
569 if (status != SSH2_FX_OK)
570 error("Couldn't fsetstat: %s", fx2txt(status));
576 do_realpath(struct sftp_conn *conn, char *path)
579 u_int type, expected_id, count, id;
580 char *filename, *longname;
583 expected_id = id = conn->msg_id++;
584 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
589 get_msg(conn->fd_in, &msg);
590 type = buffer_get_char(&msg);
591 id = buffer_get_int(&msg);
593 if (id != expected_id)
594 fatal("ID mismatch (%u != %u)", id, expected_id);
596 if (type == SSH2_FXP_STATUS) {
597 u_int status = buffer_get_int(&msg);
599 error("Couldn't canonicalise: %s", fx2txt(status));
601 } else if (type != SSH2_FXP_NAME)
602 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
603 SSH2_FXP_NAME, type);
605 count = buffer_get_int(&msg);
607 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
609 filename = buffer_get_string(&msg, NULL);
610 longname = buffer_get_string(&msg, NULL);
611 a = decode_attrib(&msg);
613 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
623 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
630 /* Send rename request */
632 buffer_put_char(&msg, SSH2_FXP_RENAME);
633 buffer_put_int(&msg, id);
634 buffer_put_cstring(&msg, oldpath);
635 buffer_put_cstring(&msg, newpath);
636 send_msg(conn->fd_out, &msg);
637 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
641 status = get_status(conn->fd_in, id);
642 if (status != SSH2_FX_OK)
643 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
644 newpath, fx2txt(status));
650 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
655 if (conn->version < 3) {
656 error("This server does not support the symlink operation");
657 return(SSH2_FX_OP_UNSUPPORTED);
662 /* Send symlink request */
664 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
665 buffer_put_int(&msg, id);
666 buffer_put_cstring(&msg, oldpath);
667 buffer_put_cstring(&msg, newpath);
668 send_msg(conn->fd_out, &msg);
669 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
673 status = get_status(conn->fd_in, id);
674 if (status != SSH2_FX_OK)
675 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
676 newpath, fx2txt(status));
682 do_readlink(struct sftp_conn *conn, char *path)
685 u_int type, expected_id, count, id;
686 char *filename, *longname;
689 expected_id = id = conn->msg_id++;
690 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
695 get_msg(conn->fd_in, &msg);
696 type = buffer_get_char(&msg);
697 id = buffer_get_int(&msg);
699 if (id != expected_id)
700 fatal("ID mismatch (%u != %u)", id, expected_id);
702 if (type == SSH2_FXP_STATUS) {
703 u_int status = buffer_get_int(&msg);
705 error("Couldn't readlink: %s", fx2txt(status));
707 } else if (type != SSH2_FXP_NAME)
708 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
709 SSH2_FXP_NAME, type);
711 count = buffer_get_int(&msg);
713 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
715 filename = buffer_get_string(&msg, NULL);
716 longname = buffer_get_string(&msg, NULL);
717 a = decode_attrib(&msg);
719 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
729 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
730 char *handle, u_int handle_len)
736 buffer_put_char(&msg, SSH2_FXP_READ);
737 buffer_put_int(&msg, id);
738 buffer_put_string(&msg, handle, handle_len);
739 buffer_put_int64(&msg, offset);
740 buffer_put_int(&msg, len);
741 send_msg(fd_out, &msg);
746 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
752 int local_fd, status = 0, write_error;
753 int read_error, write_errno;
754 u_int64_t offset, size;
755 u_int handle_len, mode, type, id, buflen, num_req, max_req;
756 off_t progress_counter;
761 TAILQ_ENTRY(request) tq;
763 TAILQ_HEAD(reqhead, request) requests;
766 TAILQ_INIT(&requests);
768 a = do_stat(conn, remote_path, 0);
772 /* XXX: should we preserve set[ug]id? */
773 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
774 mode = a->perm & 0777;
778 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
779 (!S_ISREG(a->perm))) {
780 error("Cannot download non-regular file: %s", remote_path);
784 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
789 buflen = conn->transfer_buflen;
792 /* Send open request */
794 buffer_put_char(&msg, SSH2_FXP_OPEN);
795 buffer_put_int(&msg, id);
796 buffer_put_cstring(&msg, remote_path);
797 buffer_put_int(&msg, SSH2_FXF_READ);
798 attrib_clear(&junk); /* Send empty attributes */
799 encode_attrib(&msg, &junk);
800 send_msg(conn->fd_out, &msg);
801 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
803 handle = get_handle(conn->fd_in, id, &handle_len);
804 if (handle == NULL) {
809 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
811 if (local_fd == -1) {
812 error("Couldn't open local file \"%s\" for writing: %s",
813 local_path, strerror(errno));
819 /* Read from remote and write to local */
820 write_error = read_error = write_errno = num_req = offset = 0;
822 progress_counter = 0;
824 if (showprogress && size != 0)
825 start_progress_meter(remote_path, size, &progress_counter);
827 while (num_req > 0 || max_req > 0) {
832 * Simulate EOF on interrupt: stop sending new requests and
833 * allow outstanding requests to drain gracefully
836 if (num_req == 0) /* If we haven't started yet... */
841 /* Send some more requests */
842 while (num_req < max_req) {
843 debug3("Request range %llu -> %llu (%d/%d)",
844 (unsigned long long)offset,
845 (unsigned long long)offset + buflen - 1,
847 req = xmalloc(sizeof(*req));
848 req->id = conn->msg_id++;
850 req->offset = offset;
853 TAILQ_INSERT_TAIL(&requests, req, tq);
854 send_read_request(conn->fd_out, req->id, req->offset,
855 req->len, handle, handle_len);
859 get_msg(conn->fd_in, &msg);
860 type = buffer_get_char(&msg);
861 id = buffer_get_int(&msg);
862 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
864 /* Find the request in our queue */
865 for (req = TAILQ_FIRST(&requests);
866 req != NULL && req->id != id;
867 req = TAILQ_NEXT(req, tq))
870 fatal("Unexpected reply %u", id);
873 case SSH2_FXP_STATUS:
874 status = buffer_get_int(&msg);
875 if (status != SSH2_FX_EOF)
878 TAILQ_REMOVE(&requests, req, tq);
883 data = buffer_get_string(&msg, &len);
884 debug3("Received data %llu -> %llu",
885 (unsigned long long)req->offset,
886 (unsigned long long)req->offset + len - 1);
888 fatal("Received more data than asked for "
889 "%u > %u", len, req->len);
890 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
891 atomicio(vwrite, local_fd, data, len) != len) &&
897 progress_counter += len;
900 if (len == req->len) {
901 TAILQ_REMOVE(&requests, req, tq);
905 /* Resend the request for the missing data */
906 debug3("Short data block, re-requesting "
907 "%llu -> %llu (%2d)",
908 (unsigned long long)req->offset + len,
909 (unsigned long long)req->offset +
910 req->len - 1, num_req);
911 req->id = conn->msg_id++;
914 send_read_request(conn->fd_out, req->id,
915 req->offset, req->len, handle, handle_len);
916 /* Reduce the request size */
918 buflen = MAX(MIN_READ_SIZE, len);
920 if (max_req > 0) { /* max_req = 0 iff EOF received */
921 if (size > 0 && offset > size) {
922 /* Only one request at a time
923 * after the expected EOF */
924 debug3("Finish at %llu (%2d)",
925 (unsigned long long)offset,
928 } else if (max_req <= conn->num_requests) {
934 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
935 SSH2_FXP_DATA, type);
939 if (showprogress && size)
940 stop_progress_meter();
943 if (TAILQ_FIRST(&requests) != NULL)
944 fatal("Transfer complete, but requests still in queue");
947 error("Couldn't read from remote file \"%s\" : %s",
948 remote_path, fx2txt(status));
949 do_close(conn, handle, handle_len);
950 } else if (write_error) {
951 error("Couldn't write to \"%s\": %s", local_path,
952 strerror(write_errno));
954 do_close(conn, handle, handle_len);
956 status = do_close(conn, handle, handle_len);
958 /* Override umask and utimes if asked */
960 if (pflag && fchmod(local_fd, mode) == -1)
962 if (pflag && chmod(local_path, mode) == -1)
963 #endif /* HAVE_FCHMOD */
964 error("Couldn't set mode on \"%s\": %s", local_path,
966 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
967 struct timeval tv[2];
968 tv[0].tv_sec = a->atime;
969 tv[1].tv_sec = a->mtime;
970 tv[0].tv_usec = tv[1].tv_usec = 0;
971 if (utimes(local_path, tv) == -1)
972 error("Can't set times on \"%s\": %s",
973 local_path, strerror(errno));
984 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
987 int local_fd, status;
988 u_int handle_len, id, type;
996 struct outstanding_ack {
1000 TAILQ_ENTRY(outstanding_ack) tq;
1002 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1003 struct outstanding_ack *ack = NULL;
1007 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1008 error("Couldn't open local file \"%s\" for reading: %s",
1009 local_path, strerror(errno));
1012 if (fstat(local_fd, &sb) == -1) {
1013 error("Couldn't fstat local file \"%s\": %s",
1014 local_path, strerror(errno));
1018 if (!S_ISREG(sb.st_mode)) {
1019 error("%s is not a regular file", local_path);
1023 stat_to_attrib(&sb, &a);
1025 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1026 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1029 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1033 /* Send open request */
1034 id = conn->msg_id++;
1035 buffer_put_char(&msg, SSH2_FXP_OPEN);
1036 buffer_put_int(&msg, id);
1037 buffer_put_cstring(&msg, remote_path);
1038 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1039 encode_attrib(&msg, &a);
1040 send_msg(conn->fd_out, &msg);
1041 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1045 handle = get_handle(conn->fd_in, id, &handle_len);
1046 if (handle == NULL) {
1052 startid = ackid = id + 1;
1053 data = xmalloc(conn->transfer_buflen);
1055 /* Read from local and write to remote */
1058 start_progress_meter(local_path, sb.st_size, &offset);
1064 * Can't use atomicio here because it returns 0 on EOF,
1065 * thus losing the last block of the file.
1066 * Simulate an EOF on interrupt, allowing ACKs from the
1072 len = read(local_fd, data, conn->transfer_buflen);
1073 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1076 fatal("Couldn't read from \"%s\": %s", local_path,
1080 ack = xmalloc(sizeof(*ack));
1082 ack->offset = offset;
1084 TAILQ_INSERT_TAIL(&acks, ack, tq);
1087 buffer_put_char(&msg, SSH2_FXP_WRITE);
1088 buffer_put_int(&msg, ack->id);
1089 buffer_put_string(&msg, handle, handle_len);
1090 buffer_put_int64(&msg, offset);
1091 buffer_put_string(&msg, data, len);
1092 send_msg(conn->fd_out, &msg);
1093 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1094 id, (unsigned long long)offset, len);
1095 } else if (TAILQ_FIRST(&acks) == NULL)
1099 fatal("Unexpected ACK %u", id);
1101 if (id == startid || len == 0 ||
1102 id - ackid >= conn->num_requests) {
1106 get_msg(conn->fd_in, &msg);
1107 type = buffer_get_char(&msg);
1108 r_id = buffer_get_int(&msg);
1110 if (type != SSH2_FXP_STATUS)
1111 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1112 "got %d", SSH2_FXP_STATUS, type);
1114 status = buffer_get_int(&msg);
1115 debug3("SSH2_FXP_STATUS %d", status);
1117 /* Find the request in our queue */
1118 for (ack = TAILQ_FIRST(&acks);
1119 ack != NULL && ack->id != r_id;
1120 ack = TAILQ_NEXT(ack, tq))
1123 fatal("Can't find request for ID %u", r_id);
1124 TAILQ_REMOVE(&acks, ack, tq);
1126 if (status != SSH2_FX_OK) {
1127 error("Couldn't write to remote file \"%s\": %s",
1128 remote_path, fx2txt(status));
1129 do_close(conn, handle, handle_len);
1135 debug3("In write loop, ack for %u %u bytes at %llu",
1136 ack->id, ack->len, (unsigned long long)ack->offset);
1143 stop_progress_meter();
1146 if (close(local_fd) == -1) {
1147 error("Couldn't close local file \"%s\": %s", local_path,
1149 do_close(conn, handle, handle_len);
1154 /* Override umask and utimes if asked */
1156 do_fsetstat(conn, handle, handle_len, &a);
1158 status = do_close(conn, handle, handle_len);