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.60 2006/02/20 17:19:54 stevesk Exp $");
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
31 #include "openbsd-compat/sys-queue.h"
39 #include "progressmeter.h"
42 #include "sftp-common.h"
43 #include "sftp-client.h"
45 extern volatile sig_atomic_t interrupted;
46 extern int showprogress;
48 /* Minimum amount of data to read at a time */
49 #define MIN_READ_SIZE 512
54 u_int transfer_buflen;
61 send_msg(int fd, Buffer *m)
65 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
66 fatal("Outbound message too long %u", buffer_len(m));
68 /* Send length first */
69 PUT_32BIT(mlen, buffer_len(m));
70 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
71 fatal("Couldn't send packet: %s", strerror(errno));
73 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
74 fatal("Couldn't send packet: %s", strerror(errno));
80 get_msg(int fd, Buffer *m)
84 buffer_append_space(m, 4);
85 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
87 fatal("Connection closed");
89 fatal("Couldn't read packet: %s", strerror(errno));
92 msg_len = buffer_get_int(m);
93 if (msg_len > SFTP_MAX_MSG_LENGTH)
94 fatal("Received message too long %u", msg_len);
96 buffer_append_space(m, msg_len);
97 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
99 fatal("Connection closed");
101 fatal("Read packet: %s", strerror(errno));
106 send_string_request(int fd, u_int id, u_int code, char *s,
112 buffer_put_char(&msg, code);
113 buffer_put_int(&msg, id);
114 buffer_put_string(&msg, s, len);
116 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
121 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
122 u_int len, Attrib *a)
127 buffer_put_char(&msg, code);
128 buffer_put_int(&msg, id);
129 buffer_put_string(&msg, s, len);
130 encode_attrib(&msg, a);
132 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
137 get_status(int fd, u_int expected_id)
140 u_int type, id, status;
144 type = buffer_get_char(&msg);
145 id = buffer_get_int(&msg);
147 if (id != expected_id)
148 fatal("ID mismatch (%u != %u)", id, expected_id);
149 if (type != SSH2_FXP_STATUS)
150 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
151 SSH2_FXP_STATUS, type);
153 status = buffer_get_int(&msg);
156 debug3("SSH2_FXP_STATUS %u", status);
162 get_handle(int fd, u_int expected_id, u_int *len)
170 type = buffer_get_char(&msg);
171 id = buffer_get_int(&msg);
173 if (id != expected_id)
174 fatal("ID mismatch (%u != %u)", id, expected_id);
175 if (type == SSH2_FXP_STATUS) {
176 int status = buffer_get_int(&msg);
178 error("Couldn't get handle: %s", fx2txt(status));
181 } else if (type != SSH2_FXP_HANDLE)
182 fatal("Expected SSH2_FXP_HANDLE(%u) packet, got %u",
183 SSH2_FXP_HANDLE, type);
185 handle = buffer_get_string(&msg, len);
192 get_decode_stat(int fd, u_int expected_id, int quiet)
201 type = buffer_get_char(&msg);
202 id = buffer_get_int(&msg);
204 debug3("Received stat reply T:%u I:%u", type, id);
205 if (id != expected_id)
206 fatal("ID mismatch (%u != %u)", id, expected_id);
207 if (type == SSH2_FXP_STATUS) {
208 int status = buffer_get_int(&msg);
211 debug("Couldn't stat remote file: %s", fx2txt(status));
213 error("Couldn't stat remote file: %s", fx2txt(status));
216 } else if (type != SSH2_FXP_ATTRS) {
217 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
218 SSH2_FXP_ATTRS, type);
220 a = decode_attrib(&msg);
227 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
232 struct sftp_conn *ret;
235 buffer_put_char(&msg, SSH2_FXP_INIT);
236 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
237 send_msg(fd_out, &msg);
241 get_msg(fd_in, &msg);
243 /* Expecting a VERSION reply */
244 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
245 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
250 version = buffer_get_int(&msg);
252 debug2("Remote version: %d", version);
254 /* Check for extensions */
255 while (buffer_len(&msg) > 0) {
256 char *name = buffer_get_string(&msg, NULL);
257 char *value = buffer_get_string(&msg, NULL);
259 debug2("Init extension: \"%s\"", name);
266 ret = xmalloc(sizeof(*ret));
268 ret->fd_out = fd_out;
269 ret->transfer_buflen = transfer_buflen;
270 ret->num_requests = num_requests;
271 ret->version = version;
274 /* Some filexfer v.0 servers don't support large packets */
276 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
282 sftp_proto_version(struct sftp_conn *conn)
284 return(conn->version);
288 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
296 buffer_put_char(&msg, SSH2_FXP_CLOSE);
297 buffer_put_int(&msg, id);
298 buffer_put_string(&msg, handle, handle_len);
299 send_msg(conn->fd_out, &msg);
300 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
302 status = get_status(conn->fd_in, id);
303 if (status != SSH2_FX_OK)
304 error("Couldn't close file: %s", fx2txt(status));
313 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
317 u_int count, type, id, handle_len, i, expected_id, ents = 0;
323 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
324 buffer_put_int(&msg, id);
325 buffer_put_cstring(&msg, path);
326 send_msg(conn->fd_out, &msg);
330 handle = get_handle(conn->fd_in, id, &handle_len);
336 *dir = xmalloc(sizeof(**dir));
340 for (; !interrupted;) {
341 id = expected_id = conn->msg_id++;
343 debug3("Sending SSH2_FXP_READDIR I:%u", id);
346 buffer_put_char(&msg, SSH2_FXP_READDIR);
347 buffer_put_int(&msg, id);
348 buffer_put_string(&msg, handle, handle_len);
349 send_msg(conn->fd_out, &msg);
353 get_msg(conn->fd_in, &msg);
355 type = buffer_get_char(&msg);
356 id = buffer_get_int(&msg);
358 debug3("Received reply T:%u I:%u", type, id);
360 if (id != expected_id)
361 fatal("ID mismatch (%u != %u)", id, expected_id);
363 if (type == SSH2_FXP_STATUS) {
364 int status = buffer_get_int(&msg);
366 debug3("Received SSH2_FXP_STATUS %d", status);
368 if (status == SSH2_FX_EOF) {
371 error("Couldn't read directory: %s",
373 do_close(conn, handle, handle_len);
377 } else if (type != SSH2_FXP_NAME)
378 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
379 SSH2_FXP_NAME, type);
381 count = buffer_get_int(&msg);
384 debug3("Received %d SSH2_FXP_NAME responses", count);
385 for (i = 0; i < count; i++) {
386 char *filename, *longname;
389 filename = buffer_get_string(&msg, NULL);
390 longname = buffer_get_string(&msg, NULL);
391 a = decode_attrib(&msg);
394 printf("%s\n", longname);
397 *dir = xrealloc(*dir, sizeof(**dir) *
399 (*dir)[ents] = xmalloc(sizeof(***dir));
400 (*dir)[ents]->filename = xstrdup(filename);
401 (*dir)[ents]->longname = xstrdup(longname);
402 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
403 (*dir)[++ents] = NULL;
412 do_close(conn, handle, handle_len);
415 /* Don't return partial matches on interrupt */
416 if (interrupted && dir != NULL && *dir != NULL) {
417 free_sftp_dirents(*dir);
418 *dir = xmalloc(sizeof(**dir));
426 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
428 return(do_lsreaddir(conn, path, 0, dir));
431 void free_sftp_dirents(SFTP_DIRENT **s)
435 for (i = 0; s[i]; i++) {
436 xfree(s[i]->filename);
437 xfree(s[i]->longname);
444 do_rm(struct sftp_conn *conn, char *path)
448 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
451 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
453 status = get_status(conn->fd_in, id);
454 if (status != SSH2_FX_OK)
455 error("Couldn't delete file: %s", fx2txt(status));
460 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
465 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
468 status = get_status(conn->fd_in, id);
469 if (status != SSH2_FX_OK)
470 error("Couldn't create directory: %s", fx2txt(status));
476 do_rmdir(struct sftp_conn *conn, char *path)
481 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
484 status = get_status(conn->fd_in, id);
485 if (status != SSH2_FX_OK)
486 error("Couldn't remove directory: %s", fx2txt(status));
492 do_stat(struct sftp_conn *conn, char *path, int quiet)
498 send_string_request(conn->fd_out, id,
499 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
502 return(get_decode_stat(conn->fd_in, id, quiet));
506 do_lstat(struct sftp_conn *conn, char *path, int quiet)
510 if (conn->version == 0) {
512 debug("Server version does not support lstat operation");
514 logit("Server version does not support lstat operation");
515 return(do_stat(conn, path, quiet));
519 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
522 return(get_decode_stat(conn->fd_in, id, quiet));
526 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
531 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
534 return(get_decode_stat(conn->fd_in, id, quiet));
538 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
543 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
546 status = get_status(conn->fd_in, id);
547 if (status != SSH2_FX_OK)
548 error("Couldn't setstat on \"%s\": %s", path,
555 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
561 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
564 status = get_status(conn->fd_in, id);
565 if (status != SSH2_FX_OK)
566 error("Couldn't fsetstat: %s", fx2txt(status));
572 do_realpath(struct sftp_conn *conn, char *path)
575 u_int type, expected_id, count, id;
576 char *filename, *longname;
579 expected_id = id = conn->msg_id++;
580 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
585 get_msg(conn->fd_in, &msg);
586 type = buffer_get_char(&msg);
587 id = buffer_get_int(&msg);
589 if (id != expected_id)
590 fatal("ID mismatch (%u != %u)", id, expected_id);
592 if (type == SSH2_FXP_STATUS) {
593 u_int status = buffer_get_int(&msg);
595 error("Couldn't canonicalise: %s", fx2txt(status));
597 } else if (type != SSH2_FXP_NAME)
598 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
599 SSH2_FXP_NAME, type);
601 count = buffer_get_int(&msg);
603 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
605 filename = buffer_get_string(&msg, NULL);
606 longname = buffer_get_string(&msg, NULL);
607 a = decode_attrib(&msg);
609 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
619 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
626 /* Send rename request */
628 buffer_put_char(&msg, SSH2_FXP_RENAME);
629 buffer_put_int(&msg, id);
630 buffer_put_cstring(&msg, oldpath);
631 buffer_put_cstring(&msg, newpath);
632 send_msg(conn->fd_out, &msg);
633 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
637 status = get_status(conn->fd_in, id);
638 if (status != SSH2_FX_OK)
639 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
640 newpath, fx2txt(status));
646 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
651 if (conn->version < 3) {
652 error("This server does not support the symlink operation");
653 return(SSH2_FX_OP_UNSUPPORTED);
658 /* Send symlink request */
660 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
661 buffer_put_int(&msg, id);
662 buffer_put_cstring(&msg, oldpath);
663 buffer_put_cstring(&msg, newpath);
664 send_msg(conn->fd_out, &msg);
665 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
669 status = get_status(conn->fd_in, id);
670 if (status != SSH2_FX_OK)
671 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
672 newpath, fx2txt(status));
678 do_readlink(struct sftp_conn *conn, char *path)
681 u_int type, expected_id, count, id;
682 char *filename, *longname;
685 expected_id = id = conn->msg_id++;
686 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
691 get_msg(conn->fd_in, &msg);
692 type = buffer_get_char(&msg);
693 id = buffer_get_int(&msg);
695 if (id != expected_id)
696 fatal("ID mismatch (%u != %u)", id, expected_id);
698 if (type == SSH2_FXP_STATUS) {
699 u_int status = buffer_get_int(&msg);
701 error("Couldn't readlink: %s", fx2txt(status));
703 } else if (type != SSH2_FXP_NAME)
704 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
705 SSH2_FXP_NAME, type);
707 count = buffer_get_int(&msg);
709 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
711 filename = buffer_get_string(&msg, NULL);
712 longname = buffer_get_string(&msg, NULL);
713 a = decode_attrib(&msg);
715 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
725 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
726 char *handle, u_int handle_len)
732 buffer_put_char(&msg, SSH2_FXP_READ);
733 buffer_put_int(&msg, id);
734 buffer_put_string(&msg, handle, handle_len);
735 buffer_put_int64(&msg, offset);
736 buffer_put_int(&msg, len);
737 send_msg(fd_out, &msg);
742 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
748 int local_fd, status = 0, write_error;
749 int read_error, write_errno;
750 u_int64_t offset, size;
751 u_int handle_len, mode, type, id, buflen, num_req, max_req;
752 off_t progress_counter;
757 TAILQ_ENTRY(request) tq;
759 TAILQ_HEAD(reqhead, request) requests;
762 TAILQ_INIT(&requests);
764 a = do_stat(conn, remote_path, 0);
768 /* XXX: should we preserve set[ug]id? */
769 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
770 mode = a->perm & 0777;
774 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
775 (!S_ISREG(a->perm))) {
776 error("Cannot download non-regular file: %s", remote_path);
780 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
785 buflen = conn->transfer_buflen;
788 /* Send open request */
790 buffer_put_char(&msg, SSH2_FXP_OPEN);
791 buffer_put_int(&msg, id);
792 buffer_put_cstring(&msg, remote_path);
793 buffer_put_int(&msg, SSH2_FXF_READ);
794 attrib_clear(&junk); /* Send empty attributes */
795 encode_attrib(&msg, &junk);
796 send_msg(conn->fd_out, &msg);
797 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
799 handle = get_handle(conn->fd_in, id, &handle_len);
800 if (handle == NULL) {
805 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
807 if (local_fd == -1) {
808 error("Couldn't open local file \"%s\" for writing: %s",
809 local_path, strerror(errno));
815 /* Read from remote and write to local */
816 write_error = read_error = write_errno = num_req = offset = 0;
818 progress_counter = 0;
820 if (showprogress && size != 0)
821 start_progress_meter(remote_path, size, &progress_counter);
823 while (num_req > 0 || max_req > 0) {
828 * Simulate EOF on interrupt: stop sending new requests and
829 * allow outstanding requests to drain gracefully
832 if (num_req == 0) /* If we haven't started yet... */
837 /* Send some more requests */
838 while (num_req < max_req) {
839 debug3("Request range %llu -> %llu (%d/%d)",
840 (unsigned long long)offset,
841 (unsigned long long)offset + buflen - 1,
843 req = xmalloc(sizeof(*req));
844 req->id = conn->msg_id++;
846 req->offset = offset;
849 TAILQ_INSERT_TAIL(&requests, req, tq);
850 send_read_request(conn->fd_out, req->id, req->offset,
851 req->len, handle, handle_len);
855 get_msg(conn->fd_in, &msg);
856 type = buffer_get_char(&msg);
857 id = buffer_get_int(&msg);
858 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
860 /* Find the request in our queue */
861 for (req = TAILQ_FIRST(&requests);
862 req != NULL && req->id != id;
863 req = TAILQ_NEXT(req, tq))
866 fatal("Unexpected reply %u", id);
869 case SSH2_FXP_STATUS:
870 status = buffer_get_int(&msg);
871 if (status != SSH2_FX_EOF)
874 TAILQ_REMOVE(&requests, req, tq);
879 data = buffer_get_string(&msg, &len);
880 debug3("Received data %llu -> %llu",
881 (unsigned long long)req->offset,
882 (unsigned long long)req->offset + len - 1);
884 fatal("Received more data than asked for "
885 "%u > %u", len, req->len);
886 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
887 atomicio(vwrite, local_fd, data, len) != len) &&
893 progress_counter += len;
896 if (len == req->len) {
897 TAILQ_REMOVE(&requests, req, tq);
901 /* Resend the request for the missing data */
902 debug3("Short data block, re-requesting "
903 "%llu -> %llu (%2d)",
904 (unsigned long long)req->offset + len,
905 (unsigned long long)req->offset +
906 req->len - 1, num_req);
907 req->id = conn->msg_id++;
910 send_read_request(conn->fd_out, req->id,
911 req->offset, req->len, handle, handle_len);
912 /* Reduce the request size */
914 buflen = MAX(MIN_READ_SIZE, len);
916 if (max_req > 0) { /* max_req = 0 iff EOF received */
917 if (size > 0 && offset > size) {
918 /* Only one request at a time
919 * after the expected EOF */
920 debug3("Finish at %llu (%2d)",
921 (unsigned long long)offset,
924 } else if (max_req <= conn->num_requests) {
930 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
931 SSH2_FXP_DATA, type);
935 if (showprogress && size)
936 stop_progress_meter();
939 if (TAILQ_FIRST(&requests) != NULL)
940 fatal("Transfer complete, but requests still in queue");
943 error("Couldn't read from remote file \"%s\" : %s",
944 remote_path, fx2txt(status));
945 do_close(conn, handle, handle_len);
946 } else if (write_error) {
947 error("Couldn't write to \"%s\": %s", local_path,
948 strerror(write_errno));
950 do_close(conn, handle, handle_len);
952 status = do_close(conn, handle, handle_len);
954 /* Override umask and utimes if asked */
956 if (pflag && fchmod(local_fd, mode) == -1)
958 if (pflag && chmod(local_path, mode) == -1)
959 #endif /* HAVE_FCHMOD */
960 error("Couldn't set mode on \"%s\": %s", local_path,
962 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
963 struct timeval tv[2];
964 tv[0].tv_sec = a->atime;
965 tv[1].tv_sec = a->mtime;
966 tv[0].tv_usec = tv[1].tv_usec = 0;
967 if (utimes(local_path, tv) == -1)
968 error("Can't set times on \"%s\": %s",
969 local_path, strerror(errno));
980 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
983 int local_fd, status;
984 u_int handle_len, id, type;
992 struct outstanding_ack {
996 TAILQ_ENTRY(outstanding_ack) tq;
998 TAILQ_HEAD(ackhead, outstanding_ack) acks;
999 struct outstanding_ack *ack = NULL;
1003 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1004 error("Couldn't open local file \"%s\" for reading: %s",
1005 local_path, strerror(errno));
1008 if (fstat(local_fd, &sb) == -1) {
1009 error("Couldn't fstat local file \"%s\": %s",
1010 local_path, strerror(errno));
1014 if (!S_ISREG(sb.st_mode)) {
1015 error("%s is not a regular file", local_path);
1019 stat_to_attrib(&sb, &a);
1021 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1022 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1025 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1029 /* Send open request */
1030 id = conn->msg_id++;
1031 buffer_put_char(&msg, SSH2_FXP_OPEN);
1032 buffer_put_int(&msg, id);
1033 buffer_put_cstring(&msg, remote_path);
1034 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1035 encode_attrib(&msg, &a);
1036 send_msg(conn->fd_out, &msg);
1037 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1041 handle = get_handle(conn->fd_in, id, &handle_len);
1042 if (handle == NULL) {
1048 startid = ackid = id + 1;
1049 data = xmalloc(conn->transfer_buflen);
1051 /* Read from local and write to remote */
1054 start_progress_meter(local_path, sb.st_size, &offset);
1060 * Can't use atomicio here because it returns 0 on EOF,
1061 * thus losing the last block of the file.
1062 * Simulate an EOF on interrupt, allowing ACKs from the
1068 len = read(local_fd, data, conn->transfer_buflen);
1069 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1072 fatal("Couldn't read from \"%s\": %s", local_path,
1076 ack = xmalloc(sizeof(*ack));
1078 ack->offset = offset;
1080 TAILQ_INSERT_TAIL(&acks, ack, tq);
1083 buffer_put_char(&msg, SSH2_FXP_WRITE);
1084 buffer_put_int(&msg, ack->id);
1085 buffer_put_string(&msg, handle, handle_len);
1086 buffer_put_int64(&msg, offset);
1087 buffer_put_string(&msg, data, len);
1088 send_msg(conn->fd_out, &msg);
1089 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1090 id, (unsigned long long)offset, len);
1091 } else if (TAILQ_FIRST(&acks) == NULL)
1095 fatal("Unexpected ACK %u", id);
1097 if (id == startid || len == 0 ||
1098 id - ackid >= conn->num_requests) {
1102 get_msg(conn->fd_in, &msg);
1103 type = buffer_get_char(&msg);
1104 r_id = buffer_get_int(&msg);
1106 if (type != SSH2_FXP_STATUS)
1107 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1108 "got %d", SSH2_FXP_STATUS, type);
1110 status = buffer_get_int(&msg);
1111 debug3("SSH2_FXP_STATUS %d", status);
1113 /* Find the request in our queue */
1114 for (ack = TAILQ_FIRST(&acks);
1115 ack != NULL && ack->id != r_id;
1116 ack = TAILQ_NEXT(ack, tq))
1119 fatal("Can't find request for ID %u", r_id);
1120 TAILQ_REMOVE(&acks, ack, tq);
1122 if (status != SSH2_FX_OK) {
1123 error("Couldn't write to remote file \"%s\": %s",
1124 remote_path, fx2txt(status));
1125 do_close(conn, handle, handle_len);
1131 debug3("In write loop, ack for %u %u bytes at %llu",
1132 ack->id, ack->len, (unsigned long long)ack->offset);
1139 stop_progress_meter();
1142 if (close(local_fd) == -1) {
1143 error("Couldn't close local file \"%s\": %s", local_path,
1145 do_close(conn, handle, handle_len);
1150 /* Override umask and utimes if asked */
1152 do_fsetstat(conn, handle, handle_len, &a);
1154 status = do_close(conn, handle, handle_len);