- djm@cvs.openbsd.org 2013/10/17 00:30:13
     [PROTOCOL sftp-client.c sftp-client.h sftp-server.c sftp.1 sftp.c]
     fsync@openssh.com protocol extension for sftp-server
     client support to allow calling fsync() faster successful transfer
     patch mostly by imorgan AT nas.nasa.gov; bz#1798
     "fine" markus@ "grumble OK" deraadt@ "doesn't sound bad to me" millert@
diff --git a/sftp-server.c b/sftp-server.c
index 3056c45..ad158f8 100644
--- a/sftp-server.c
+++ b/sftp-server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sftp-server.c,v 1.101 2013/10/14 23:28:23 djm Exp $ */
+/* $OpenBSD: sftp-server.c,v 1.102 2013/10/17 00:30:13 djm Exp $ */
 /*
  * Copyright (c) 2000-2004 Markus Friedl.  All rights reserved.
  *
@@ -112,6 +112,7 @@
 static void process_extended_statvfs(u_int32_t id);
 static void process_extended_fstatvfs(u_int32_t id);
 static void process_extended_hardlink(u_int32_t id);
+static void process_extended_fsync(u_int32_t id);
 static void process_extended(u_int32_t id);
 
 struct sftp_handler {
@@ -152,6 +153,7 @@
 	{ "statvfs", "statvfs@openssh.com", 0, process_extended_statvfs, 0 },
 	{ "fstatvfs", "fstatvfs@openssh.com", 0, process_extended_fstatvfs, 0 },
 	{ "hardlink", "hardlink@openssh.com", 0, process_extended_hardlink, 1 },
+	{ "fsync", "fsync@openssh.com", 0, process_extended_fsync, 1 },
 	{ NULL, NULL, 0, NULL, 0 }
 };
 
@@ -652,6 +654,9 @@
 	/* hardlink extension */
 	buffer_put_cstring(&msg, "hardlink@openssh.com");
 	buffer_put_cstring(&msg, "1"); /* version */
+	/* fsync extension */
+	buffer_put_cstring(&msg, "fsync@openssh.com");
+	buffer_put_cstring(&msg, "1"); /* version */
 	send_msg(&msg);
 	buffer_free(&msg);
 }
@@ -1298,6 +1303,23 @@
 }
 
 static void
+process_extended_fsync(u_int32_t id)
+{
+	int handle, fd, ret, status = SSH2_FX_OP_UNSUPPORTED;
+
+	handle = get_handle();
+	debug3("request %u: fsync (handle %u)", id, handle);
+	verbose("fsync \"%s\"", handle_to_name(handle));
+	if ((fd = handle_to_fd(handle)) < 0)
+		status = SSH2_FX_NO_SUCH_FILE;
+	else if (handle_is_ok(handle, HANDLE_FILE)) {
+		ret = fsync(fd);
+		status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
+	}
+	send_status(id, status);
+}
+
+static void
 process_extended(u_int32_t id)
 {
 	char *request;