1 /* $OpenBSD: sftp-client.c,v 1.75 2006/10/22 02:25:50 djm 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 #include <sys/param.h>
27 #include "openbsd-compat/sys-queue.h"
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
48 #include "progressmeter.h"
52 #include "sftp-common.h"
53 #include "sftp-client.h"
55 extern volatile sig_atomic_t interrupted;
56 extern int showprogress;
58 /* Minimum amount of data to read at a time */
59 #define MIN_READ_SIZE 512
64 u_int transfer_buflen;
71 send_msg(int fd, Buffer *m)
76 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
77 fatal("Outbound message too long %u", buffer_len(m));
79 /* Send length first */
80 put_u32(mlen, buffer_len(m));
81 iov[0].iov_base = mlen;
82 iov[0].iov_len = sizeof(mlen);
83 iov[1].iov_base = buffer_ptr(m);
84 iov[1].iov_len = buffer_len(m);
86 if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
87 fatal("Couldn't send packet: %s", strerror(errno));
93 get_msg(int fd, Buffer *m)
97 buffer_append_space(m, 4);
98 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
100 fatal("Connection closed");
102 fatal("Couldn't read packet: %s", strerror(errno));
105 msg_len = buffer_get_int(m);
106 if (msg_len > SFTP_MAX_MSG_LENGTH)
107 fatal("Received message too long %u", msg_len);
109 buffer_append_space(m, msg_len);
110 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
112 fatal("Connection closed");
114 fatal("Read packet: %s", strerror(errno));
119 send_string_request(int fd, u_int id, u_int code, char *s,
125 buffer_put_char(&msg, code);
126 buffer_put_int(&msg, id);
127 buffer_put_string(&msg, s, len);
129 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
134 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
135 u_int len, Attrib *a)
140 buffer_put_char(&msg, code);
141 buffer_put_int(&msg, id);
142 buffer_put_string(&msg, s, len);
143 encode_attrib(&msg, a);
145 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
150 get_status(int fd, u_int expected_id)
153 u_int type, id, status;
157 type = buffer_get_char(&msg);
158 id = buffer_get_int(&msg);
160 if (id != expected_id)
161 fatal("ID mismatch (%u != %u)", id, expected_id);
162 if (type != SSH2_FXP_STATUS)
163 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
164 SSH2_FXP_STATUS, type);
166 status = buffer_get_int(&msg);
169 debug3("SSH2_FXP_STATUS %u", status);
175 get_handle(int fd, u_int expected_id, u_int *len)
183 type = buffer_get_char(&msg);
184 id = buffer_get_int(&msg);
186 if (id != expected_id)
187 fatal("ID mismatch (%u != %u)", id, expected_id);
188 if (type == SSH2_FXP_STATUS) {
189 int status = buffer_get_int(&msg);
191 error("Couldn't get handle: %s", fx2txt(status));
194 } else if (type != SSH2_FXP_HANDLE)
195 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
196 SSH2_FXP_HANDLE, type);
198 handle = buffer_get_string(&msg, len);
205 get_decode_stat(int fd, u_int expected_id, int quiet)
214 type = buffer_get_char(&msg);
215 id = buffer_get_int(&msg);
217 debug3("Received stat reply T:%u I:%u", type, id);
218 if (id != expected_id)
219 fatal("ID mismatch (%u != %u)", id, expected_id);
220 if (type == SSH2_FXP_STATUS) {
221 int status = buffer_get_int(&msg);
224 debug("Couldn't stat remote file: %s", fx2txt(status));
226 error("Couldn't stat remote file: %s", fx2txt(status));
229 } else if (type != SSH2_FXP_ATTRS) {
230 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
231 SSH2_FXP_ATTRS, type);
233 a = decode_attrib(&msg);
240 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
245 struct sftp_conn *ret;
248 buffer_put_char(&msg, SSH2_FXP_INIT);
249 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
250 send_msg(fd_out, &msg);
254 get_msg(fd_in, &msg);
256 /* Expecting a VERSION reply */
257 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
258 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
263 version = buffer_get_int(&msg);
265 debug2("Remote version: %d", version);
267 /* Check for extensions */
268 while (buffer_len(&msg) > 0) {
269 char *name = buffer_get_string(&msg, NULL);
270 char *value = buffer_get_string(&msg, NULL);
272 debug2("Init extension: \"%s\"", name);
279 ret = xmalloc(sizeof(*ret));
281 ret->fd_out = fd_out;
282 ret->transfer_buflen = transfer_buflen;
283 ret->num_requests = num_requests;
284 ret->version = version;
287 /* Some filexfer v.0 servers don't support large packets */
289 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
295 sftp_proto_version(struct sftp_conn *conn)
297 return(conn->version);
301 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
309 buffer_put_char(&msg, SSH2_FXP_CLOSE);
310 buffer_put_int(&msg, id);
311 buffer_put_string(&msg, handle, handle_len);
312 send_msg(conn->fd_out, &msg);
313 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
315 status = get_status(conn->fd_in, id);
316 if (status != SSH2_FX_OK)
317 error("Couldn't close file: %s", fx2txt(status));
326 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
330 u_int count, type, id, handle_len, i, expected_id, ents = 0;
336 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
337 buffer_put_int(&msg, id);
338 buffer_put_cstring(&msg, path);
339 send_msg(conn->fd_out, &msg);
343 handle = get_handle(conn->fd_in, id, &handle_len);
349 *dir = xmalloc(sizeof(**dir));
353 for (; !interrupted;) {
354 id = expected_id = conn->msg_id++;
356 debug3("Sending SSH2_FXP_READDIR I:%u", id);
359 buffer_put_char(&msg, SSH2_FXP_READDIR);
360 buffer_put_int(&msg, id);
361 buffer_put_string(&msg, handle, handle_len);
362 send_msg(conn->fd_out, &msg);
366 get_msg(conn->fd_in, &msg);
368 type = buffer_get_char(&msg);
369 id = buffer_get_int(&msg);
371 debug3("Received reply T:%u I:%u", type, id);
373 if (id != expected_id)
374 fatal("ID mismatch (%u != %u)", id, expected_id);
376 if (type == SSH2_FXP_STATUS) {
377 int status = buffer_get_int(&msg);
379 debug3("Received SSH2_FXP_STATUS %d", status);
381 if (status == SSH2_FX_EOF) {
384 error("Couldn't read directory: %s",
386 do_close(conn, handle, handle_len);
390 } else if (type != SSH2_FXP_NAME)
391 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
392 SSH2_FXP_NAME, type);
394 count = buffer_get_int(&msg);
397 debug3("Received %d SSH2_FXP_NAME responses", count);
398 for (i = 0; i < count; i++) {
399 char *filename, *longname;
402 filename = buffer_get_string(&msg, NULL);
403 longname = buffer_get_string(&msg, NULL);
404 a = decode_attrib(&msg);
407 printf("%s\n", longname);
410 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
411 (*dir)[ents] = xmalloc(sizeof(***dir));
412 (*dir)[ents]->filename = xstrdup(filename);
413 (*dir)[ents]->longname = xstrdup(longname);
414 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
415 (*dir)[++ents] = NULL;
424 do_close(conn, handle, handle_len);
427 /* Don't return partial matches on interrupt */
428 if (interrupted && dir != NULL && *dir != NULL) {
429 free_sftp_dirents(*dir);
430 *dir = xmalloc(sizeof(**dir));
438 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
440 return(do_lsreaddir(conn, path, 0, dir));
443 void free_sftp_dirents(SFTP_DIRENT **s)
447 for (i = 0; s[i]; i++) {
448 xfree(s[i]->filename);
449 xfree(s[i]->longname);
456 do_rm(struct sftp_conn *conn, char *path)
460 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
463 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
465 status = get_status(conn->fd_in, id);
466 if (status != SSH2_FX_OK)
467 error("Couldn't delete file: %s", fx2txt(status));
472 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
477 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
480 status = get_status(conn->fd_in, id);
481 if (status != SSH2_FX_OK)
482 error("Couldn't create directory: %s", fx2txt(status));
488 do_rmdir(struct sftp_conn *conn, char *path)
493 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
496 status = get_status(conn->fd_in, id);
497 if (status != SSH2_FX_OK)
498 error("Couldn't remove directory: %s", fx2txt(status));
504 do_stat(struct sftp_conn *conn, char *path, int quiet)
510 send_string_request(conn->fd_out, id,
511 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
514 return(get_decode_stat(conn->fd_in, id, quiet));
518 do_lstat(struct sftp_conn *conn, char *path, int quiet)
522 if (conn->version == 0) {
524 debug("Server version does not support lstat operation");
526 logit("Server version does not support lstat operation");
527 return(do_stat(conn, path, quiet));
531 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
534 return(get_decode_stat(conn->fd_in, id, quiet));
538 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
543 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
546 return(get_decode_stat(conn->fd_in, id, quiet));
550 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
555 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
558 status = get_status(conn->fd_in, id);
559 if (status != SSH2_FX_OK)
560 error("Couldn't setstat on \"%s\": %s", path,
567 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
573 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
576 status = get_status(conn->fd_in, id);
577 if (status != SSH2_FX_OK)
578 error("Couldn't fsetstat: %s", fx2txt(status));
584 do_realpath(struct sftp_conn *conn, char *path)
587 u_int type, expected_id, count, id;
588 char *filename, *longname;
591 expected_id = id = conn->msg_id++;
592 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
597 get_msg(conn->fd_in, &msg);
598 type = buffer_get_char(&msg);
599 id = buffer_get_int(&msg);
601 if (id != expected_id)
602 fatal("ID mismatch (%u != %u)", id, expected_id);
604 if (type == SSH2_FXP_STATUS) {
605 u_int status = buffer_get_int(&msg);
607 error("Couldn't canonicalise: %s", fx2txt(status));
609 } else if (type != SSH2_FXP_NAME)
610 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
611 SSH2_FXP_NAME, type);
613 count = buffer_get_int(&msg);
615 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
617 filename = buffer_get_string(&msg, NULL);
618 longname = buffer_get_string(&msg, NULL);
619 a = decode_attrib(&msg);
621 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
631 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
638 /* Send rename request */
640 buffer_put_char(&msg, SSH2_FXP_RENAME);
641 buffer_put_int(&msg, id);
642 buffer_put_cstring(&msg, oldpath);
643 buffer_put_cstring(&msg, newpath);
644 send_msg(conn->fd_out, &msg);
645 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
649 status = get_status(conn->fd_in, id);
650 if (status != SSH2_FX_OK)
651 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
652 newpath, fx2txt(status));
658 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
663 if (conn->version < 3) {
664 error("This server does not support the symlink operation");
665 return(SSH2_FX_OP_UNSUPPORTED);
670 /* Send symlink request */
672 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
673 buffer_put_int(&msg, id);
674 buffer_put_cstring(&msg, oldpath);
675 buffer_put_cstring(&msg, newpath);
676 send_msg(conn->fd_out, &msg);
677 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
681 status = get_status(conn->fd_in, id);
682 if (status != SSH2_FX_OK)
683 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
684 newpath, fx2txt(status));
690 do_readlink(struct sftp_conn *conn, char *path)
693 u_int type, expected_id, count, id;
694 char *filename, *longname;
697 expected_id = id = conn->msg_id++;
698 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
703 get_msg(conn->fd_in, &msg);
704 type = buffer_get_char(&msg);
705 id = buffer_get_int(&msg);
707 if (id != expected_id)
708 fatal("ID mismatch (%u != %u)", id, expected_id);
710 if (type == SSH2_FXP_STATUS) {
711 u_int status = buffer_get_int(&msg);
713 error("Couldn't readlink: %s", fx2txt(status));
715 } else if (type != SSH2_FXP_NAME)
716 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
717 SSH2_FXP_NAME, type);
719 count = buffer_get_int(&msg);
721 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
723 filename = buffer_get_string(&msg, NULL);
724 longname = buffer_get_string(&msg, NULL);
725 a = decode_attrib(&msg);
727 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
737 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
738 char *handle, u_int handle_len)
744 buffer_put_char(&msg, SSH2_FXP_READ);
745 buffer_put_int(&msg, id);
746 buffer_put_string(&msg, handle, handle_len);
747 buffer_put_int64(&msg, offset);
748 buffer_put_int(&msg, len);
749 send_msg(fd_out, &msg);
754 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
760 int local_fd, status = 0, write_error;
761 int read_error, write_errno;
762 u_int64_t offset, size;
763 u_int handle_len, mode, type, id, buflen, num_req, max_req;
764 off_t progress_counter;
769 TAILQ_ENTRY(request) tq;
771 TAILQ_HEAD(reqhead, request) requests;
774 TAILQ_INIT(&requests);
776 a = do_stat(conn, remote_path, 0);
780 /* XXX: should we preserve set[ug]id? */
781 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
782 mode = a->perm & 0777;
786 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
787 (!S_ISREG(a->perm))) {
788 error("Cannot download non-regular file: %s", remote_path);
792 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
797 buflen = conn->transfer_buflen;
800 /* Send open request */
802 buffer_put_char(&msg, SSH2_FXP_OPEN);
803 buffer_put_int(&msg, id);
804 buffer_put_cstring(&msg, remote_path);
805 buffer_put_int(&msg, SSH2_FXF_READ);
806 attrib_clear(&junk); /* Send empty attributes */
807 encode_attrib(&msg, &junk);
808 send_msg(conn->fd_out, &msg);
809 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
811 handle = get_handle(conn->fd_in, id, &handle_len);
812 if (handle == NULL) {
817 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
819 if (local_fd == -1) {
820 error("Couldn't open local file \"%s\" for writing: %s",
821 local_path, strerror(errno));
827 /* Read from remote and write to local */
828 write_error = read_error = write_errno = num_req = offset = 0;
830 progress_counter = 0;
832 if (showprogress && size != 0)
833 start_progress_meter(remote_path, size, &progress_counter);
835 while (num_req > 0 || max_req > 0) {
840 * Simulate EOF on interrupt: stop sending new requests and
841 * allow outstanding requests to drain gracefully
844 if (num_req == 0) /* If we haven't started yet... */
849 /* Send some more requests */
850 while (num_req < max_req) {
851 debug3("Request range %llu -> %llu (%d/%d)",
852 (unsigned long long)offset,
853 (unsigned long long)offset + buflen - 1,
855 req = xmalloc(sizeof(*req));
856 req->id = conn->msg_id++;
858 req->offset = offset;
861 TAILQ_INSERT_TAIL(&requests, req, tq);
862 send_read_request(conn->fd_out, req->id, req->offset,
863 req->len, handle, handle_len);
867 get_msg(conn->fd_in, &msg);
868 type = buffer_get_char(&msg);
869 id = buffer_get_int(&msg);
870 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
872 /* Find the request in our queue */
873 for (req = TAILQ_FIRST(&requests);
874 req != NULL && req->id != id;
875 req = TAILQ_NEXT(req, tq))
878 fatal("Unexpected reply %u", id);
881 case SSH2_FXP_STATUS:
882 status = buffer_get_int(&msg);
883 if (status != SSH2_FX_EOF)
886 TAILQ_REMOVE(&requests, req, tq);
891 data = buffer_get_string(&msg, &len);
892 debug3("Received data %llu -> %llu",
893 (unsigned long long)req->offset,
894 (unsigned long long)req->offset + len - 1);
896 fatal("Received more data than asked for "
897 "%u > %u", len, req->len);
898 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
899 atomicio(vwrite, local_fd, data, len) != len) &&
905 progress_counter += len;
908 if (len == req->len) {
909 TAILQ_REMOVE(&requests, req, tq);
913 /* Resend the request for the missing data */
914 debug3("Short data block, re-requesting "
915 "%llu -> %llu (%2d)",
916 (unsigned long long)req->offset + len,
917 (unsigned long long)req->offset +
918 req->len - 1, num_req);
919 req->id = conn->msg_id++;
922 send_read_request(conn->fd_out, req->id,
923 req->offset, req->len, handle, handle_len);
924 /* Reduce the request size */
926 buflen = MAX(MIN_READ_SIZE, len);
928 if (max_req > 0) { /* max_req = 0 iff EOF received */
929 if (size > 0 && offset > size) {
930 /* Only one request at a time
931 * after the expected EOF */
932 debug3("Finish at %llu (%2d)",
933 (unsigned long long)offset,
936 } else if (max_req <= conn->num_requests) {
942 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
943 SSH2_FXP_DATA, type);
947 if (showprogress && size)
948 stop_progress_meter();
951 if (TAILQ_FIRST(&requests) != NULL)
952 fatal("Transfer complete, but requests still in queue");
955 error("Couldn't read from remote file \"%s\" : %s",
956 remote_path, fx2txt(status));
957 do_close(conn, handle, handle_len);
958 } else if (write_error) {
959 error("Couldn't write to \"%s\": %s", local_path,
960 strerror(write_errno));
962 do_close(conn, handle, handle_len);
964 status = do_close(conn, handle, handle_len);
966 /* Override umask and utimes if asked */
968 if (pflag && fchmod(local_fd, mode) == -1)
970 if (pflag && chmod(local_path, mode) == -1)
971 #endif /* HAVE_FCHMOD */
972 error("Couldn't set mode on \"%s\": %s", local_path,
974 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
975 struct timeval tv[2];
976 tv[0].tv_sec = a->atime;
977 tv[1].tv_sec = a->mtime;
978 tv[0].tv_usec = tv[1].tv_usec = 0;
979 if (utimes(local_path, tv) == -1)
980 error("Can't set times on \"%s\": %s",
981 local_path, strerror(errno));
992 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
995 int local_fd, status;
996 u_int handle_len, id, type;
1004 struct outstanding_ack {
1008 TAILQ_ENTRY(outstanding_ack) tq;
1010 TAILQ_HEAD(ackhead, outstanding_ack) acks;
1011 struct outstanding_ack *ack = NULL;
1015 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1016 error("Couldn't open local file \"%s\" for reading: %s",
1017 local_path, strerror(errno));
1020 if (fstat(local_fd, &sb) == -1) {
1021 error("Couldn't fstat local file \"%s\": %s",
1022 local_path, strerror(errno));
1026 if (!S_ISREG(sb.st_mode)) {
1027 error("%s is not a regular file", local_path);
1031 stat_to_attrib(&sb, &a);
1033 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1034 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1037 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1041 /* Send open request */
1042 id = conn->msg_id++;
1043 buffer_put_char(&msg, SSH2_FXP_OPEN);
1044 buffer_put_int(&msg, id);
1045 buffer_put_cstring(&msg, remote_path);
1046 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1047 encode_attrib(&msg, &a);
1048 send_msg(conn->fd_out, &msg);
1049 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1053 handle = get_handle(conn->fd_in, id, &handle_len);
1054 if (handle == NULL) {
1060 startid = ackid = id + 1;
1061 data = xmalloc(conn->transfer_buflen);
1063 /* Read from local and write to remote */
1066 start_progress_meter(local_path, sb.st_size, &offset);
1072 * Can't use atomicio here because it returns 0 on EOF,
1073 * thus losing the last block of the file.
1074 * Simulate an EOF on interrupt, allowing ACKs from the
1080 len = read(local_fd, data, conn->transfer_buflen);
1081 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1084 fatal("Couldn't read from \"%s\": %s", local_path,
1088 ack = xmalloc(sizeof(*ack));
1090 ack->offset = offset;
1092 TAILQ_INSERT_TAIL(&acks, ack, tq);
1095 buffer_put_char(&msg, SSH2_FXP_WRITE);
1096 buffer_put_int(&msg, ack->id);
1097 buffer_put_string(&msg, handle, handle_len);
1098 buffer_put_int64(&msg, offset);
1099 buffer_put_string(&msg, data, len);
1100 send_msg(conn->fd_out, &msg);
1101 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1102 id, (unsigned long long)offset, len);
1103 } else if (TAILQ_FIRST(&acks) == NULL)
1107 fatal("Unexpected ACK %u", id);
1109 if (id == startid || len == 0 ||
1110 id - ackid >= conn->num_requests) {
1114 get_msg(conn->fd_in, &msg);
1115 type = buffer_get_char(&msg);
1116 r_id = buffer_get_int(&msg);
1118 if (type != SSH2_FXP_STATUS)
1119 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1120 "got %d", SSH2_FXP_STATUS, type);
1122 status = buffer_get_int(&msg);
1123 debug3("SSH2_FXP_STATUS %d", status);
1125 /* Find the request in our queue */
1126 for (ack = TAILQ_FIRST(&acks);
1127 ack != NULL && ack->id != r_id;
1128 ack = TAILQ_NEXT(ack, tq))
1131 fatal("Can't find request for ID %u", r_id);
1132 TAILQ_REMOVE(&acks, ack, tq);
1134 if (status != SSH2_FX_OK) {
1135 error("Couldn't write to remote file \"%s\": %s",
1136 remote_path, fx2txt(status));
1138 stop_progress_meter();
1139 do_close(conn, handle, handle_len);
1145 debug3("In write loop, ack for %u %u bytes at %llu",
1146 ack->id, ack->len, (unsigned long long)ack->offset);
1153 stop_progress_meter();
1156 if (close(local_fd) == -1) {
1157 error("Couldn't close local file \"%s\": %s", local_path,
1159 do_close(conn, handle, handle_len);
1164 /* Override umask and utimes if asked */
1166 do_fsetstat(conn, handle, handle_len, &a);
1168 status = do_close(conn, handle, handle_len);