Blame SOURCES/openssh-8.7p1-scp-clears-file.patch

d4fbec
diff --color -rup a/scp.c b/scp.c
d4fbec
--- a/scp.c	2022-07-26 14:51:40.560120817 +0200
d4fbec
+++ b/scp.c	2022-07-26 14:52:37.118213004 +0200
d4fbec
@@ -1324,12 +1324,12 @@ source_sftp(int argc, char *src, char *t
d4fbec
 
d4fbec
 	if (src_is_dir && iamrecursive) {
d4fbec
 		if (upload_dir(conn, src, abs_dst, pflag,
d4fbec
-		    SFTP_PROGRESS_ONLY, 0, 0, 1, 1) != 0) {
d4fbec
+		    SFTP_PROGRESS_ONLY, 0, 0, 1, 1, 1) != 0) {
d4fbec
 			error("failed to upload directory %s to %s",
d4fbec
 				src, abs_dst);
d4fbec
 			errs = 1;
d4fbec
 		}
d4fbec
-	} else if (do_upload(conn, src, abs_dst, pflag, 0, 0) != 0) {
d4fbec
+	} else if (do_upload(conn, src, abs_dst, pflag, 0, 0, 1) != 0) {
d4fbec
 		error("failed to upload file %s to %s", src, abs_dst);
d4fbec
 		errs = 1;
d4fbec
 	}
d4fbec
@@ -1566,11 +1566,11 @@ sink_sftp(int argc, char *dst, const cha
d4fbec
 		debug("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
d4fbec
 		if (globpath_is_dir(g.gl_pathv[i]) && iamrecursive) {
d4fbec
 			if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
d4fbec
-			    pflag, SFTP_PROGRESS_ONLY, 0, 0, 1) == -1)
d4fbec
+			    pflag, SFTP_PROGRESS_ONLY, 0, 0, 1, 1) == -1)
d4fbec
 				err = -1;
d4fbec
 		} else {
d4fbec
 			if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
d4fbec
-			    pflag, 0, 0) == -1)
d4fbec
+			    pflag, 0, 0, 1) == -1)
d4fbec
 				err = -1;
d4fbec
 		}
d4fbec
 		free(abs_dst);
d4fbec
diff --color -rup a/sftp.c b/sftp.c
d4fbec
--- a/sftp.c	2022-07-26 14:51:40.561120836 +0200
d4fbec
+++ b/sftp.c	2022-07-26 14:52:37.119213023 +0200
d4fbec
@@ -666,12 +666,12 @@ process_get(struct sftp_conn *conn, cons
d4fbec
 		if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
d4fbec
 			if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
d4fbec
 			    pflag || global_pflag, 1, resume,
d4fbec
-			    fflag || global_fflag, 0) == -1)
d4fbec
+			    fflag || global_fflag, 0, 0) == -1)
d4fbec
 				err = -1;
d4fbec
 		} else {
d4fbec
 			if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
d4fbec
 			    pflag || global_pflag, resume,
d4fbec
-			    fflag || global_fflag) == -1)
d4fbec
+			    fflag || global_fflag, 0) == -1)
d4fbec
 				err = -1;
d4fbec
 		}
d4fbec
 		free(abs_dst);
d4fbec
@@ -760,12 +760,12 @@ process_put(struct sftp_conn *conn, cons
d4fbec
 		if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
d4fbec
 			if (upload_dir(conn, g.gl_pathv[i], abs_dst,
d4fbec
 			    pflag || global_pflag, 1, resume,
d4fbec
-			    fflag || global_fflag, 0, 0) == -1)
d4fbec
+			    fflag || global_fflag, 0, 0, 0) == -1)
d4fbec
 				err = -1;
d4fbec
 		} else {
d4fbec
 			if (do_upload(conn, g.gl_pathv[i], abs_dst,
d4fbec
 			    pflag || global_pflag, resume,
d4fbec
-			    fflag || global_fflag) == -1)
d4fbec
+			    fflag || global_fflag, 0) == -1)
d4fbec
 				err = -1;
d4fbec
 		}
d4fbec
 	}
d4fbec
diff --color -rup a/sftp-client.c b/sftp-client.c
d4fbec
--- a/sftp-client.c	2022-07-26 14:51:40.561120836 +0200
d4fbec
+++ b/sftp-client.c	2022-07-26 15:09:54.825295533 +0200
d4fbec
@@ -1454,7 +1454,7 @@ progress_meter_path(const char *path)
d4fbec
 int
d4fbec
 do_download(struct sftp_conn *conn, const char *remote_path,
d4fbec
     const char *local_path, Attrib *a, int preserve_flag, int resume_flag,
d4fbec
-    int fsync_flag)
d4fbec
+    int fsync_flag, int inplace_flag)
d4fbec
 {
d4fbec
 	struct sshbuf *msg;
d4fbec
 	u_char *handle;
d4fbec
@@ -1498,8 +1498,8 @@ do_download(struct sftp_conn *conn, cons
d4fbec
 	    &handle, &handle_len) != 0)
d4fbec
 		return -1;
d4fbec
 
d4fbec
-	local_fd = open(local_path,
d4fbec
-	    O_WRONLY | O_CREAT | (resume_flag ? 0 : O_TRUNC), mode | S_IWUSR);
d4fbec
+	local_fd = open(local_path, O_WRONLY | O_CREAT |
d4fbec
+	((resume_flag || inplace_flag) ? 0 : O_TRUNC), mode | S_IWUSR);
d4fbec
 	if (local_fd == -1) {
d4fbec
 		error("Couldn't open local file \"%s\" for writing: %s",
d4fbec
 		    local_path, strerror(errno));
d4fbec
@@ -1661,8 +1661,11 @@ do_download(struct sftp_conn *conn, cons
d4fbec
 	/* Sanity check */
d4fbec
 	if (TAILQ_FIRST(&requests) != NULL)
d4fbec
 		fatal("Transfer complete, but requests still in queue");
d4fbec
-	/* Truncate at highest contiguous point to avoid holes on interrupt */
d4fbec
-	if (read_error || write_error || interrupted) {
d4fbec
+	/*
d4fbec
+	 * Truncate at highest contiguous point to avoid holes on interrupt,
d4fbec
+	 * or unconditionally if writing in place.
d4fbec
+	 */
d4fbec
+	if (inplace_flag || read_error || write_error || interrupted) {
d4fbec
 		if (reordered && resume_flag) {
d4fbec
 			error("Unable to resume download of \"%s\": "
d4fbec
 			    "server reordered requests", local_path);
d4fbec
@@ -1724,7 +1727,7 @@ do_download(struct sftp_conn *conn, cons
d4fbec
 static int
d4fbec
 download_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
d4fbec
     int depth, Attrib *dirattrib, int preserve_flag, int print_flag,
d4fbec
-    int resume_flag, int fsync_flag, int follow_link_flag)
d4fbec
+    int resume_flag, int fsync_flag, int follow_link_flag, int inplace_flag)
d4fbec
 {
d4fbec
 	int i, ret = 0;
d4fbec
 	SFTP_DIRENT **dir_entries;
d4fbec
@@ -1781,7 +1784,7 @@ download_dir_internal(struct sftp_conn *
d4fbec
 			if (download_dir_internal(conn, new_src, new_dst,
d4fbec
 			    depth + 1, &(dir_entries[i]->a), preserve_flag,
d4fbec
 			    print_flag, resume_flag,
d4fbec
-			    fsync_flag, follow_link_flag) == -1)
d4fbec
+			    fsync_flag, follow_link_flag, inplace_flag) == -1)
d4fbec
 				ret = -1;
d4fbec
 		} else if (S_ISREG(dir_entries[i]->a.perm) ||
d4fbec
 		    (follow_link_flag && S_ISLNK(dir_entries[i]->a.perm))) {
d4fbec
@@ -1793,7 +1796,8 @@ download_dir_internal(struct sftp_conn *
d4fbec
 			if (do_download(conn, new_src, new_dst,
d4fbec
 			    S_ISLNK(dir_entries[i]->a.perm) ? NULL :
d4fbec
 			    &(dir_entries[i]->a),
d4fbec
-			    preserve_flag, resume_flag, fsync_flag) == -1) {
d4fbec
+			    preserve_flag, resume_flag, fsync_flag,
d4fbec
+			    inplace_flag) == -1) {
d4fbec
 				error("Download of file %s to %s failed",
d4fbec
 				    new_src, new_dst);
d4fbec
 				ret = -1;
d4fbec
@@ -1831,7 +1835,7 @@ download_dir_internal(struct sftp_conn *
d4fbec
 int
d4fbec
 download_dir(struct sftp_conn *conn, const char *src, const char *dst,
d4fbec
     Attrib *dirattrib, int preserve_flag, int print_flag, int resume_flag,
d4fbec
-    int fsync_flag, int follow_link_flag)
d4fbec
+    int fsync_flag, int follow_link_flag, int inplace_flag)
d4fbec
 {
d4fbec
 	char *src_canon;
d4fbec
 	int ret;
d4fbec
@@ -1843,26 +1847,25 @@ download_dir(struct sftp_conn *conn, con
d4fbec
 
d4fbec
 	ret = download_dir_internal(conn, src_canon, dst, 0,
d4fbec
 	    dirattrib, preserve_flag, print_flag, resume_flag, fsync_flag,
d4fbec
-	    follow_link_flag);
d4fbec
+	    follow_link_flag, inplace_flag);
d4fbec
 	free(src_canon);
d4fbec
 	return ret;
d4fbec
 }
d4fbec
 
d4fbec
 int
d4fbec
 do_upload(struct sftp_conn *conn, const char *local_path,
d4fbec
-    const char *remote_path, int preserve_flag, int resume, int fsync_flag)
d4fbec
+    const char *remote_path, int preserve_flag, int resume,
d4fbec
+    int fsync_flag, int inplace_flag)
d4fbec
 {
d4fbec
 	int r, local_fd;
d4fbec
-	u_int status = SSH2_FX_OK;
d4fbec
-	u_int id;
d4fbec
-	u_char type;
d4fbec
+	u_int openmode, id, status = SSH2_FX_OK, reordered = 0;
d4fbec
 	off_t offset, progress_counter;
d4fbec
-	u_char *handle, *data;
d4fbec
+	u_char type, *handle, *data;
d4fbec
 	struct sshbuf *msg;
d4fbec
 	struct stat sb;
d4fbec
-	Attrib a, *c = NULL;
d4fbec
-	u_int32_t startid;
d4fbec
-	u_int32_t ackid;
d4fbec
+	Attrib a, t, *c = NULL;
d4fbec
+	u_int32_t startid, ackid;
d4fbec
+	u_int64_t highwater = 0;
d4fbec
 	struct request *ack = NULL;
d4fbec
 	struct requests acks;
d4fbec
 	size_t handle_len;
d4fbec
@@ -1913,10 +1916,15 @@ do_upload(struct sftp_conn *conn, const
d4fbec
 		}
d4fbec
 	}
d4fbec
 
d4fbec
+	openmode = SSH2_FXF_WRITE|SSH2_FXF_CREAT;
d4fbec
+	if (resume)
d4fbec
+		openmode |= SSH2_FXF_APPEND;
d4fbec
+	else if (!inplace_flag)
d4fbec
+		openmode |= SSH2_FXF_TRUNC;
d4fbec
+
d4fbec
 	/* Send open request */
d4fbec
-	if (send_open(conn, remote_path, "dest", SSH2_FXF_WRITE|SSH2_FXF_CREAT|
d4fbec
-	    (resume ? SSH2_FXF_APPEND : SSH2_FXF_TRUNC),
d4fbec
-	    &a, &handle, &handle_len) != 0) {
d4fbec
+	if (send_open(conn, remote_path, "dest", openmode, &a,
d4fbec
+	    &handle, &handle_len) != 0) {
d4fbec
 		close(local_fd);
d4fbec
 		return -1;
d4fbec
 	}
d4fbec
@@ -1999,6 +2007,12 @@ do_upload(struct sftp_conn *conn, const
d4fbec
 			    ack->id, ack->len, (unsigned long long)ack->offset);
d4fbec
 			++ackid;
d4fbec
 			progress_counter += ack->len;
d4fbec
+			if (!reordered && ack->offset <= highwater)
d4fbec
+				highwater = ack->offset + ack->len;
d4fbec
+			else if (!reordered && ack->offset > highwater) {
d4fbec
+				debug3_f("server reordered ACKs");
d4fbec
+				reordered = 1;
d4fbec
+			}
d4fbec
 			free(ack);
d4fbec
 		}
d4fbec
 		offset += len;
d4fbec
@@ -2017,6 +2031,14 @@ do_upload(struct sftp_conn *conn, const
d4fbec
 		status = SSH2_FX_FAILURE;
d4fbec
 	}
d4fbec
 
d4fbec
+	if (inplace_flag || (resume && (status != SSH2_FX_OK || interrupted))) {
d4fbec
+		debug("truncating at %llu", (unsigned long long)highwater);
d4fbec
+		attrib_clear(&t);
d4fbec
+		t.flags = SSH2_FILEXFER_ATTR_SIZE;
d4fbec
+		t.size = highwater;
d4fbec
+		do_fsetstat(conn, handle, handle_len, &t);
d4fbec
+	}
d4fbec
+
d4fbec
 	if (close(local_fd) == -1) {
d4fbec
 		error("Couldn't close local file \"%s\": %s", local_path,
d4fbec
 		    strerror(errno));
d4fbec
@@ -2041,7 +2063,7 @@ do_upload(struct sftp_conn *conn, const
d4fbec
 static int
d4fbec
 upload_dir_internal(struct sftp_conn *conn, const char *src, const char *dst,
d4fbec
     int depth, int preserve_flag, int print_flag, int resume, int fsync_flag,
d4fbec
-    int follow_link_flag)
d4fbec
+    int follow_link_flag, int inplace_flag)
d4fbec
 {
d4fbec
 	int ret = 0;
d4fbec
 	DIR *dirp;
d4fbec
@@ -2119,12 +2141,13 @@ upload_dir_internal(struct sftp_conn *co
d4fbec
 
d4fbec
 			if (upload_dir_internal(conn, new_src, new_dst,
d4fbec
 			    depth + 1, preserve_flag, print_flag, resume,
d4fbec
-			    fsync_flag, follow_link_flag) == -1)
d4fbec
+			    fsync_flag, follow_link_flag, inplace_flag) == -1)
d4fbec
 				ret = -1;
d4fbec
 		} else if (S_ISREG(sb.st_mode) ||
d4fbec
 		    (follow_link_flag && S_ISLNK(sb.st_mode))) {
d4fbec
 			if (do_upload(conn, new_src, new_dst,
d4fbec
-			    preserve_flag, resume, fsync_flag) == -1) {
d4fbec
+			    preserve_flag, resume, fsync_flag,
d4fbec
+			    inplace_flag) == -1) {
d4fbec
 				error("Uploading of file %s to %s failed!",
d4fbec
 				    new_src, new_dst);
d4fbec
 				ret = -1;
d4fbec
@@ -2144,7 +2167,7 @@ upload_dir_internal(struct sftp_conn *co
d4fbec
 int
d4fbec
 upload_dir(struct sftp_conn *conn, const char *src, const char *dst,
d4fbec
     int preserve_flag, int print_flag, int resume, int fsync_flag,
d4fbec
-    int follow_link_flag, int create_dir)
d4fbec
+    int follow_link_flag, int create_dir, int inplace_flag)
d4fbec
 {
d4fbec
 	char *dst_canon;
d4fbec
 	int ret;
d4fbec
@@ -2155,7 +2178,7 @@ upload_dir(struct sftp_conn *conn, const
d4fbec
 	}
d4fbec
 
d4fbec
 	ret = upload_dir_internal(conn, src, dst_canon, 0, preserve_flag,
d4fbec
-	    print_flag, resume, fsync_flag, follow_link_flag);
d4fbec
+	    print_flag, resume, fsync_flag, follow_link_flag, inplace_flag);
d4fbec
 
d4fbec
 	free(dst_canon);
d4fbec
 	return ret;
d4fbec
diff --color -rup a/sftp-client.h b/sftp-client.h
d4fbec
--- a/sftp-client.h	2022-07-26 14:51:40.561120836 +0200
d4fbec
+++ b/sftp-client.h	2022-07-26 14:52:37.120213042 +0200
d4fbec
@@ -138,28 +138,29 @@ int do_fsync(struct sftp_conn *conn, u_c
d4fbec
  * Download 'remote_path' to 'local_path'. Preserve permissions and times
d4fbec
  * if 'pflag' is set
d4fbec
  */
d4fbec
-int do_download(struct sftp_conn *, const char *, const char *,
d4fbec
-    Attrib *, int, int, int);
d4fbec
+int do_download(struct sftp_conn *, const char *, const char *, Attrib *,
d4fbec
+    int, int, int, int);
d4fbec
 
d4fbec
 /*
d4fbec
  * Recursively download 'remote_directory' to 'local_directory'. Preserve
d4fbec
  * times if 'pflag' is set
d4fbec
  */
d4fbec
-int download_dir(struct sftp_conn *, const char *, const char *,
d4fbec
-    Attrib *, int, int, int, int, int);
d4fbec
+int download_dir(struct sftp_conn *, const char *, const char *, Attrib *,
d4fbec
+    int, int, int, int, int, int);
d4fbec
 
d4fbec
 /*
d4fbec
  * Upload 'local_path' to 'remote_path'. Preserve permissions and times
d4fbec
  * if 'pflag' is set
d4fbec
  */
d4fbec
-int do_upload(struct sftp_conn *, const char *, const char *, int, int, int);
d4fbec
+int do_upload(struct sftp_conn *, const char *, const char *,
d4fbec
+    int, int, int, int);
d4fbec
 
d4fbec
 /*
d4fbec
  * Recursively upload 'local_directory' to 'remote_directory'. Preserve
d4fbec
  * times if 'pflag' is set
d4fbec
  */
d4fbec
-int upload_dir(struct sftp_conn *, const char *, const char *, int, int, int,
d4fbec
-    int, int, int);
d4fbec
+int upload_dir(struct sftp_conn *, const char *, const char *,
d4fbec
+    int, int, int, int, int, int, int);
d4fbec
 
d4fbec
 /*
d4fbec
  * Download a 'from_path' from the 'from' connection and upload it to