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.46 2004/02/17 05:39:51 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 int showprogress;
41 /* Minimum amount of data to read at at time */
42 #define MIN_READ_SIZE 512
44 /* Maximum packet size */
45 #define MAX_MSG_LENGTH (256 * 1024)
50 u_int transfer_buflen;
57 send_msg(int fd, Buffer *m)
61 if (buffer_len(m) > MAX_MSG_LENGTH)
62 fatal("Outbound message too long %u", buffer_len(m));
64 /* Send length first */
65 PUT_32BIT(mlen, buffer_len(m));
66 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) <= 0)
67 fatal("Couldn't send packet: %s", strerror(errno));
69 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) <= 0)
70 fatal("Couldn't send packet: %s", strerror(errno));
76 get_msg(int fd, Buffer *m)
81 buffer_append_space(m, 4);
82 len = atomicio(read, fd, buffer_ptr(m), 4);
84 fatal("Connection closed");
86 fatal("Couldn't read packet: %s", strerror(errno));
88 msg_len = buffer_get_int(m);
89 if (msg_len > MAX_MSG_LENGTH)
90 fatal("Received message too long %u", msg_len);
92 buffer_append_space(m, msg_len);
93 len = atomicio(read, fd, buffer_ptr(m), msg_len);
95 fatal("Connection closed");
97 fatal("Read packet: %s", strerror(errno));
101 send_string_request(int fd, u_int id, u_int code, char *s,
107 buffer_put_char(&msg, code);
108 buffer_put_int(&msg, id);
109 buffer_put_string(&msg, s, len);
111 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
116 send_string_attrs_request(int fd, u_int id, u_int code, char *s,
117 u_int len, Attrib *a)
122 buffer_put_char(&msg, code);
123 buffer_put_int(&msg, id);
124 buffer_put_string(&msg, s, len);
125 encode_attrib(&msg, a);
127 debug3("Sent message fd %d T:%u I:%u", fd, code, id);
132 get_status(int fd, u_int expected_id)
135 u_int type, id, status;
139 type = buffer_get_char(&msg);
140 id = buffer_get_int(&msg);
142 if (id != expected_id)
143 fatal("ID mismatch (%u != %u)", id, expected_id);
144 if (type != SSH2_FXP_STATUS)
145 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
146 SSH2_FXP_STATUS, type);
148 status = buffer_get_int(&msg);
151 debug3("SSH2_FXP_STATUS %u", status);
157 get_handle(int fd, u_int expected_id, u_int *len)
165 type = buffer_get_char(&msg);
166 id = buffer_get_int(&msg);
168 if (id != expected_id)
169 fatal("ID mismatch (%u != %u)", id, expected_id);
170 if (type == SSH2_FXP_STATUS) {
171 int status = buffer_get_int(&msg);
173 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));
209 } else if (type != SSH2_FXP_ATTRS) {
210 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
211 SSH2_FXP_ATTRS, type);
213 a = decode_attrib(&msg);
220 do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests)
225 struct sftp_conn *ret;
228 buffer_put_char(&msg, SSH2_FXP_INIT);
229 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
230 send_msg(fd_out, &msg);
234 get_msg(fd_in, &msg);
236 /* Expecting a VERSION reply */
237 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
238 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
243 version = buffer_get_int(&msg);
245 debug2("Remote version: %d", version);
247 /* Check for extensions */
248 while (buffer_len(&msg) > 0) {
249 char *name = buffer_get_string(&msg, NULL);
250 char *value = buffer_get_string(&msg, NULL);
252 debug2("Init extension: \"%s\"", name);
259 ret = xmalloc(sizeof(*ret));
261 ret->fd_out = fd_out;
262 ret->transfer_buflen = transfer_buflen;
263 ret->num_requests = num_requests;
264 ret->version = version;
267 /* Some filexfer v.0 servers don't support large packets */
269 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
275 sftp_proto_version(struct sftp_conn *conn)
277 return(conn->version);
281 do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
289 buffer_put_char(&msg, SSH2_FXP_CLOSE);
290 buffer_put_int(&msg, id);
291 buffer_put_string(&msg, handle, handle_len);
292 send_msg(conn->fd_out, &msg);
293 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
295 status = get_status(conn->fd_in, id);
296 if (status != SSH2_FX_OK)
297 error("Couldn't close file: %s", fx2txt(status));
306 do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
310 u_int type, id, handle_len, i, expected_id, ents = 0;
316 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
317 buffer_put_int(&msg, id);
318 buffer_put_cstring(&msg, path);
319 send_msg(conn->fd_out, &msg);
323 handle = get_handle(conn->fd_in, id, &handle_len);
329 *dir = xmalloc(sizeof(**dir));
336 id = expected_id = conn->msg_id++;
338 debug3("Sending SSH2_FXP_READDIR I:%u", id);
341 buffer_put_char(&msg, SSH2_FXP_READDIR);
342 buffer_put_int(&msg, id);
343 buffer_put_string(&msg, handle, handle_len);
344 send_msg(conn->fd_out, &msg);
348 get_msg(conn->fd_in, &msg);
350 type = buffer_get_char(&msg);
351 id = buffer_get_int(&msg);
353 debug3("Received reply T:%u I:%u", type, id);
355 if (id != expected_id)
356 fatal("ID mismatch (%u != %u)", id, expected_id);
358 if (type == SSH2_FXP_STATUS) {
359 int status = buffer_get_int(&msg);
361 debug3("Received SSH2_FXP_STATUS %d", status);
363 if (status == SSH2_FX_EOF) {
366 error("Couldn't read directory: %s",
368 do_close(conn, handle, handle_len);
372 } else if (type != SSH2_FXP_NAME)
373 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
374 SSH2_FXP_NAME, type);
376 count = buffer_get_int(&msg);
379 debug3("Received %d SSH2_FXP_NAME responses", count);
380 for (i = 0; i < count; i++) {
381 char *filename, *longname;
384 filename = buffer_get_string(&msg, NULL);
385 longname = buffer_get_string(&msg, NULL);
386 a = decode_attrib(&msg);
389 printf("%s\n", longname);
392 *dir = xrealloc(*dir, sizeof(**dir) *
394 (*dir)[ents] = xmalloc(sizeof(***dir));
395 (*dir)[ents]->filename = xstrdup(filename);
396 (*dir)[ents]->longname = xstrdup(longname);
397 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
398 (*dir)[++ents] = NULL;
407 do_close(conn, handle, handle_len);
414 do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
416 return(do_lsreaddir(conn, path, 0, dir));
419 void free_sftp_dirents(SFTP_DIRENT **s)
423 for (i = 0; s[i]; i++) {
424 xfree(s[i]->filename);
425 xfree(s[i]->longname);
432 do_rm(struct sftp_conn *conn, char *path)
436 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
439 send_string_request(conn->fd_out, id, SSH2_FXP_REMOVE, path,
441 status = get_status(conn->fd_in, id);
442 if (status != SSH2_FX_OK)
443 error("Couldn't delete file: %s", fx2txt(status));
448 do_mkdir(struct sftp_conn *conn, char *path, Attrib *a)
453 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_MKDIR, path,
456 status = get_status(conn->fd_in, id);
457 if (status != SSH2_FX_OK)
458 error("Couldn't create directory: %s", fx2txt(status));
464 do_rmdir(struct sftp_conn *conn, char *path)
469 send_string_request(conn->fd_out, id, SSH2_FXP_RMDIR, path,
472 status = get_status(conn->fd_in, id);
473 if (status != SSH2_FX_OK)
474 error("Couldn't remove directory: %s", fx2txt(status));
480 do_stat(struct sftp_conn *conn, char *path, int quiet)
486 send_string_request(conn->fd_out, id,
487 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
490 return(get_decode_stat(conn->fd_in, id, quiet));
494 do_lstat(struct sftp_conn *conn, char *path, int quiet)
498 if (conn->version == 0) {
500 debug("Server version does not support lstat operation");
502 logit("Server version does not support lstat operation");
503 return(do_stat(conn, path, quiet));
507 send_string_request(conn->fd_out, id, SSH2_FXP_LSTAT, path,
510 return(get_decode_stat(conn->fd_in, id, quiet));
514 do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
519 send_string_request(conn->fd_out, id, SSH2_FXP_FSTAT, handle,
522 return(get_decode_stat(conn->fd_in, id, quiet));
526 do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
531 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_SETSTAT, path,
534 status = get_status(conn->fd_in, id);
535 if (status != SSH2_FX_OK)
536 error("Couldn't setstat on \"%s\": %s", path,
543 do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
549 send_string_attrs_request(conn->fd_out, id, SSH2_FXP_FSETSTAT, handle,
552 status = get_status(conn->fd_in, id);
553 if (status != SSH2_FX_OK)
554 error("Couldn't fsetstat: %s", fx2txt(status));
560 do_realpath(struct sftp_conn *conn, char *path)
563 u_int type, expected_id, count, id;
564 char *filename, *longname;
567 expected_id = id = conn->msg_id++;
568 send_string_request(conn->fd_out, id, SSH2_FXP_REALPATH, path,
573 get_msg(conn->fd_in, &msg);
574 type = buffer_get_char(&msg);
575 id = buffer_get_int(&msg);
577 if (id != expected_id)
578 fatal("ID mismatch (%u != %u)", id, expected_id);
580 if (type == SSH2_FXP_STATUS) {
581 u_int status = buffer_get_int(&msg);
583 error("Couldn't canonicalise: %s", fx2txt(status));
585 } else if (type != SSH2_FXP_NAME)
586 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
587 SSH2_FXP_NAME, type);
589 count = buffer_get_int(&msg);
591 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
593 filename = buffer_get_string(&msg, NULL);
594 longname = buffer_get_string(&msg, NULL);
595 a = decode_attrib(&msg);
597 debug3("SSH_FXP_REALPATH %s -> %s", path, filename);
607 do_rename(struct sftp_conn *conn, char *oldpath, char *newpath)
614 /* Send rename request */
616 buffer_put_char(&msg, SSH2_FXP_RENAME);
617 buffer_put_int(&msg, id);
618 buffer_put_cstring(&msg, oldpath);
619 buffer_put_cstring(&msg, newpath);
620 send_msg(conn->fd_out, &msg);
621 debug3("Sent message SSH2_FXP_RENAME \"%s\" -> \"%s\"", oldpath,
625 status = get_status(conn->fd_in, id);
626 if (status != SSH2_FX_OK)
627 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
628 newpath, fx2txt(status));
634 do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
639 if (conn->version < 3) {
640 error("This server does not support the symlink operation");
641 return(SSH2_FX_OP_UNSUPPORTED);
646 /* Send rename request */
648 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
649 buffer_put_int(&msg, id);
650 buffer_put_cstring(&msg, oldpath);
651 buffer_put_cstring(&msg, newpath);
652 send_msg(conn->fd_out, &msg);
653 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
657 status = get_status(conn->fd_in, id);
658 if (status != SSH2_FX_OK)
659 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
660 newpath, fx2txt(status));
666 do_readlink(struct sftp_conn *conn, char *path)
669 u_int type, expected_id, count, id;
670 char *filename, *longname;
673 expected_id = id = conn->msg_id++;
674 send_string_request(conn->fd_out, id, SSH2_FXP_READLINK, path,
679 get_msg(conn->fd_in, &msg);
680 type = buffer_get_char(&msg);
681 id = buffer_get_int(&msg);
683 if (id != expected_id)
684 fatal("ID mismatch (%u != %u)", id, expected_id);
686 if (type == SSH2_FXP_STATUS) {
687 u_int status = buffer_get_int(&msg);
689 error("Couldn't readlink: %s", fx2txt(status));
691 } else if (type != SSH2_FXP_NAME)
692 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
693 SSH2_FXP_NAME, type);
695 count = buffer_get_int(&msg);
697 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
699 filename = buffer_get_string(&msg, NULL);
700 longname = buffer_get_string(&msg, NULL);
701 a = decode_attrib(&msg);
703 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
713 send_read_request(int fd_out, u_int id, u_int64_t offset, u_int len,
714 char *handle, u_int handle_len)
720 buffer_put_char(&msg, SSH2_FXP_READ);
721 buffer_put_int(&msg, id);
722 buffer_put_string(&msg, handle, handle_len);
723 buffer_put_int64(&msg, offset);
724 buffer_put_int(&msg, len);
725 send_msg(fd_out, &msg);
730 do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
736 int local_fd, status, num_req, max_req, write_error;
737 int read_error, write_errno;
738 u_int64_t offset, size;
739 u_int handle_len, mode, type, id, buflen;
740 off_t progress_counter;
745 TAILQ_ENTRY(request) tq;
747 TAILQ_HEAD(reqhead, request) requests;
750 TAILQ_INIT(&requests);
752 a = do_stat(conn, remote_path, 0);
756 /* XXX: should we preserve set[ug]id? */
757 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
758 mode = a->perm & 0777;
762 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
763 (!S_ISREG(a->perm))) {
764 error("Cannot download non-regular file: %s", remote_path);
768 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
773 buflen = conn->transfer_buflen;
776 /* Send open request */
778 buffer_put_char(&msg, SSH2_FXP_OPEN);
779 buffer_put_int(&msg, id);
780 buffer_put_cstring(&msg, remote_path);
781 buffer_put_int(&msg, SSH2_FXF_READ);
782 attrib_clear(&junk); /* Send empty attributes */
783 encode_attrib(&msg, &junk);
784 send_msg(conn->fd_out, &msg);
785 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
787 handle = get_handle(conn->fd_in, id, &handle_len);
788 if (handle == NULL) {
793 local_fd = open(local_path, O_WRONLY | O_CREAT | O_TRUNC,
795 if (local_fd == -1) {
796 error("Couldn't open local file \"%s\" for writing: %s",
797 local_path, strerror(errno));
803 /* Read from remote and write to local */
804 write_error = read_error = write_errno = num_req = offset = 0;
806 progress_counter = 0;
810 start_progress_meter(remote_path, size,
813 printf("Fetching %s to %s\n", remote_path, local_path);
816 while (num_req > 0 || max_req > 0) {
820 /* Send some more requests */
821 while (num_req < max_req) {
822 debug3("Request range %llu -> %llu (%d/%d)",
823 (unsigned long long)offset,
824 (unsigned long long)offset + buflen - 1,
826 req = xmalloc(sizeof(*req));
827 req->id = conn->msg_id++;
829 req->offset = offset;
832 TAILQ_INSERT_TAIL(&requests, req, tq);
833 send_read_request(conn->fd_out, req->id, req->offset,
834 req->len, handle, handle_len);
838 get_msg(conn->fd_in, &msg);
839 type = buffer_get_char(&msg);
840 id = buffer_get_int(&msg);
841 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
843 /* Find the request in our queue */
844 for(req = TAILQ_FIRST(&requests);
845 req != NULL && req->id != id;
846 req = TAILQ_NEXT(req, tq))
849 fatal("Unexpected reply %u", id);
852 case SSH2_FXP_STATUS:
853 status = buffer_get_int(&msg);
854 if (status != SSH2_FX_EOF)
857 TAILQ_REMOVE(&requests, req, tq);
862 data = buffer_get_string(&msg, &len);
863 debug3("Received data %llu -> %llu",
864 (unsigned long long)req->offset,
865 (unsigned long long)req->offset + len - 1);
867 fatal("Received more data than asked for "
868 "%u > %u", len, req->len);
869 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
870 atomicio(vwrite, local_fd, data, len) != len) &&
876 progress_counter += len;
879 if (len == req->len) {
880 TAILQ_REMOVE(&requests, req, tq);
884 /* Resend the request for the missing data */
885 debug3("Short data block, re-requesting "
886 "%llu -> %llu (%2d)",
887 (unsigned long long)req->offset + len,
888 (unsigned long long)req->offset +
889 req->len - 1, num_req);
890 req->id = conn->msg_id++;
893 send_read_request(conn->fd_out, req->id,
894 req->offset, req->len, handle, handle_len);
895 /* Reduce the request size */
897 buflen = MAX(MIN_READ_SIZE, len);
899 if (max_req > 0) { /* max_req = 0 iff EOF received */
900 if (size > 0 && offset > size) {
901 /* Only one request at a time
902 * after the expected EOF */
903 debug3("Finish at %llu (%2d)",
904 (unsigned long long)offset,
908 else if (max_req < conn->num_requests + 1) {
914 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
915 SSH2_FXP_DATA, type);
919 if (showprogress && size)
920 stop_progress_meter();
923 if (TAILQ_FIRST(&requests) != NULL)
924 fatal("Transfer complete, but requests still in queue");
927 error("Couldn't read from remote file \"%s\" : %s",
928 remote_path, fx2txt(status));
929 do_close(conn, handle, handle_len);
930 } else if (write_error) {
931 error("Couldn't write to \"%s\": %s", local_path,
932 strerror(write_errno));
934 do_close(conn, handle, handle_len);
936 status = do_close(conn, handle, handle_len);
938 /* Override umask and utimes if asked */
940 if (pflag && fchmod(local_fd, mode) == -1)
942 if (pflag && chmod(local_path, mode) == -1)
943 #endif /* HAVE_FCHMOD */
944 error("Couldn't set mode on \"%s\": %s", local_path,
946 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
947 struct timeval tv[2];
948 tv[0].tv_sec = a->atime;
949 tv[1].tv_sec = a->mtime;
950 tv[0].tv_usec = tv[1].tv_usec = 0;
951 if (utimes(local_path, tv) == -1)
952 error("Can't set times on \"%s\": %s",
953 local_path, strerror(errno));
964 do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
967 int local_fd, status;
968 u_int handle_len, id, type;
976 struct outstanding_ack {
980 TAILQ_ENTRY(outstanding_ack) tq;
982 TAILQ_HEAD(ackhead, outstanding_ack) acks;
983 struct outstanding_ack *ack;
987 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
988 error("Couldn't open local file \"%s\" for reading: %s",
989 local_path, strerror(errno));
992 if (fstat(local_fd, &sb) == -1) {
993 error("Couldn't fstat local file \"%s\": %s",
994 local_path, strerror(errno));
998 if (!S_ISREG(sb.st_mode)) {
999 error("%s is not a regular file", local_path);
1003 stat_to_attrib(&sb, &a);
1005 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1006 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1009 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1013 /* Send open request */
1014 id = conn->msg_id++;
1015 buffer_put_char(&msg, SSH2_FXP_OPEN);
1016 buffer_put_int(&msg, id);
1017 buffer_put_cstring(&msg, remote_path);
1018 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1019 encode_attrib(&msg, &a);
1020 send_msg(conn->fd_out, &msg);
1021 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
1025 handle = get_handle(conn->fd_in, id, &handle_len);
1026 if (handle == NULL) {
1032 startid = ackid = id + 1;
1033 data = xmalloc(conn->transfer_buflen);
1035 /* Read from local and write to remote */
1038 start_progress_meter(local_path, sb.st_size, &offset);
1040 printf("Uploading %s to %s\n", local_path, remote_path);
1046 * Can't use atomicio here because it returns 0 on EOF, thus losing
1047 * the last block of the file
1050 len = read(local_fd, data, conn->transfer_buflen);
1051 while ((len == -1) && (errno == EINTR || errno == EAGAIN));
1054 fatal("Couldn't read from \"%s\": %s", local_path,
1058 ack = xmalloc(sizeof(*ack));
1060 ack->offset = offset;
1062 TAILQ_INSERT_TAIL(&acks, ack, tq);
1065 buffer_put_char(&msg, SSH2_FXP_WRITE);
1066 buffer_put_int(&msg, ack->id);
1067 buffer_put_string(&msg, handle, handle_len);
1068 buffer_put_int64(&msg, offset);
1069 buffer_put_string(&msg, data, len);
1070 send_msg(conn->fd_out, &msg);
1071 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
1072 id, (unsigned long long)offset, len);
1073 } else if (TAILQ_FIRST(&acks) == NULL)
1077 fatal("Unexpected ACK %u", id);
1079 if (id == startid || len == 0 ||
1080 id - ackid >= conn->num_requests) {
1084 get_msg(conn->fd_in, &msg);
1085 type = buffer_get_char(&msg);
1086 r_id = buffer_get_int(&msg);
1088 if (type != SSH2_FXP_STATUS)
1089 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1090 "got %d", SSH2_FXP_STATUS, type);
1092 status = buffer_get_int(&msg);
1093 debug3("SSH2_FXP_STATUS %d", status);
1095 /* Find the request in our queue */
1096 for(ack = TAILQ_FIRST(&acks);
1097 ack != NULL && ack->id != r_id;
1098 ack = TAILQ_NEXT(ack, tq))
1101 fatal("Can't find request for ID %u", r_id);
1102 TAILQ_REMOVE(&acks, ack, tq);
1104 if (status != SSH2_FX_OK) {
1105 error("Couldn't write to remote file \"%s\": %s",
1106 remote_path, fx2txt(status));
1107 do_close(conn, handle, handle_len);
1113 debug3("In write loop, ack for %u %u bytes at %llu",
1114 ack->id, ack->len, (unsigned long long)ack->offset);
1121 stop_progress_meter();
1124 if (close(local_fd) == -1) {
1125 error("Couldn't close local file \"%s\": %s", local_path,
1127 do_close(conn, handle, handle_len);
1132 /* Override umask and utimes if asked */
1134 do_fsetstat(conn, handle, handle_len, &a);
1136 status = do_close(conn, handle, handle_len);