Blame SOURCES/0003-sftp-Add-support-for-fsync-OpenSSH-extension.patch

68eb44
From 6e0d757f24a45252c4cae9ea09732eda2562c767 Mon Sep 17 00:00:00 2001
68eb44
From: "Richard W.M. Jones" <rjones@redhat.com>
68eb44
Date: Tue, 9 Apr 2013 11:42:09 +0200
68eb44
Subject: [PATCH 3/3] sftp: Add support for fsync (OpenSSH extension).
68eb44
68eb44
The new libssh2_sftp_fsync API causes data and metadata in the
68eb44
currently open file to be committed to disk at the server.
68eb44
68eb44
This is an OpenSSH extension to the SFTP protocol.  See:
68eb44
68eb44
https://bugzilla.mindrot.org/show_bug.cgi?id=1798
68eb44
---
68eb44
 docs/Makefile.am          |  1 +
68eb44
 docs/libssh2_sftp_fsync.3 | 39 +++++++++++++++++++
68eb44
 include/libssh2_sftp.h    |  1 +
68eb44
 src/sftp.c                | 97 +++++++++++++++++++++++++++++++++++++++++++++++
68eb44
 src/sftp.h                |  5 +++
68eb44
 5 files changed, 143 insertions(+)
68eb44
 create mode 100644 docs/libssh2_sftp_fsync.3
68eb44
68eb44
diff --git a/docs/Makefile.am b/docs/Makefile.am
68eb44
index e4cf487..e6ab394 100644
68eb44
--- a/docs/Makefile.am
68eb44
+++ b/docs/Makefile.am
68eb44
@@ -120,6 +120,7 @@ dist_man_MANS = \
68eb44
 	libssh2_sftp_fstat.3 \
68eb44
 	libssh2_sftp_fstat_ex.3 \
68eb44
 	libssh2_sftp_fstatvfs.3 \
68eb44
+	libssh2_sftp_fsync.3 \
68eb44
 	libssh2_sftp_get_channel.3 \
68eb44
 	libssh2_sftp_init.3 \
68eb44
 	libssh2_sftp_last_error.3 \
68eb44
diff --git a/docs/libssh2_sftp_fsync.3 b/docs/libssh2_sftp_fsync.3
68eb44
new file mode 100644
68eb44
index 0000000..646760a
68eb44
--- /dev/null
68eb44
+++ b/docs/libssh2_sftp_fsync.3
68eb44
@@ -0,0 +1,39 @@
68eb44
+.TH libssh2_sftp_fsync 3 "8 Apr 2013" "libssh2 1.4.4" "libssh2 manual"
68eb44
+.SH NAME
68eb44
+libssh2_sftp_fsync - synchronize file to disk
68eb44
+.SH SYNOPSIS
68eb44
+.nf
68eb44
+#include <libssh2.h>
68eb44
+#include <libssh2_sftp.h>
68eb44
+
68eb44
+int
68eb44
+libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *handle)
68eb44
+.fi
68eb44
+.SH DESCRIPTION
68eb44
+This function causes the remote server to synchronize the file
68eb44
+data and metadata to disk (like fsync(2)).
68eb44
+
68eb44
+For this to work requires fsync@openssh.com support on the server.
68eb44
+
68eb44
+\fIhandle\fP - SFTP File Handle as returned by
68eb44
+.BR libssh2_sftp_open_ex(3)
68eb44
+
68eb44
+.SH RETURN VALUE
68eb44
+Returns 0 on success or negative on failure. If used in non-blocking mode, it
68eb44
+returns LIBSSH2_ERROR_EAGAIN when it would otherwise block. While
68eb44
+LIBSSH2_ERROR_EAGAIN is a negative number, it isn't really a failure per se.
68eb44
+.SH ERRORS
68eb44
+\fILIBSSH2_ERROR_ALLOC\fP -  An internal memory allocation call failed.
68eb44
+
68eb44
+\fILIBSSH2_ERROR_SOCKET_SEND\fP - Unable to send data on socket.
68eb44
+
68eb44
+\fILIBSSH2_ERROR_SFTP_PROTOCOL\fP - An invalid SFTP protocol response
68eb44
+was received on the socket, or an SFTP operation caused an errorcode
68eb44
+to be returned by the server.  In particular, this can be returned if
68eb44
+the SSH server does not support the fsync operation: the SFTP subcode
68eb44
+\fILIBSSH2_FX_OP_UNSUPPORTED\fP will be returned in this case.
68eb44
+
68eb44
+.SH AVAILABILITY
68eb44
+Added in libssh2 1.4.4 and OpenSSH 6.3.
68eb44
+.SH SEE ALSO
68eb44
+.BR fsync(2)
68eb44
diff --git a/include/libssh2_sftp.h b/include/libssh2_sftp.h
68eb44
index 74884fb..677faf2 100644
68eb44
--- a/include/libssh2_sftp.h
68eb44
+++ b/include/libssh2_sftp.h
68eb44
@@ -247,6 +247,7 @@ LIBSSH2_API int libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *handle, \
68eb44
 
68eb44
 LIBSSH2_API ssize_t libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *handle,
68eb44
                                        const char *buffer, size_t count);
68eb44
+LIBSSH2_API int libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *handle);
68eb44
 
68eb44
 LIBSSH2_API int libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle);
68eb44
 #define libssh2_sftp_close(handle) libssh2_sftp_close_handle(handle)
68eb44
diff --git a/src/sftp.c b/src/sftp.c
68eb44
index 65fa77a..01017fd 100644
68eb44
--- a/src/sftp.c
68eb44
+++ b/src/sftp.c
68eb44
@@ -986,6 +986,10 @@ sftp_shutdown(LIBSSH2_SFTP *sftp)
68eb44
         LIBSSH2_FREE(session, sftp->symlink_packet);
68eb44
         sftp->symlink_packet = NULL;
68eb44
     }
68eb44
+    if (sftp->fsync_packet) {
68eb44
+        LIBSSH2_FREE(session, sftp->fsync_packet);
68eb44
+        sftp->fsync_packet = NULL;
68eb44
+    }
68eb44
 
68eb44
     sftp_packet_flush(sftp);
68eb44
 
68eb44
@@ -2014,6 +2018,99 @@ libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *hnd, const char *buffer,
68eb44
 
68eb44
 }
68eb44
 
68eb44
+static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle)
68eb44
+{
68eb44
+    LIBSSH2_SFTP *sftp = handle->sftp;
68eb44
+    LIBSSH2_CHANNEL *channel = sftp->channel;
68eb44
+    LIBSSH2_SESSION *session = channel->session;
68eb44
+    /* 34 = packet_len(4) + packet_type(1) + request_id(4) +
68eb44
+       string_len(4) + strlen("fsync@openssh.com")(17) + handle_len(4) */
68eb44
+    uint32_t packet_len = handle->handle_len + 34;
68eb44
+    size_t data_len;
68eb44
+    unsigned char *packet, *s, *data;
68eb44
+    ssize_t rc;
68eb44
+    uint32_t retcode;
68eb44
+
68eb44
+    if (sftp->fsync_state == libssh2_NB_state_idle) {
68eb44
+        _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
68eb44
+                       "Issuing fsync command");
68eb44
+        s = packet = LIBSSH2_ALLOC(session, packet_len);
68eb44
+        if (!packet) {
68eb44
+            return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
68eb44
+                                  "Unable to allocate memory for FXP_EXTENDED "
68eb44
+                                  "packet");
68eb44
+        }
68eb44
+
68eb44
+        _libssh2_store_u32(&s, packet_len - 4);
68eb44
+        *(s++) = SSH_FXP_EXTENDED;
68eb44
+        sftp->fsync_request_id = sftp->request_id++;
68eb44
+        _libssh2_store_u32(&s, sftp->fsync_request_id);
68eb44
+        _libssh2_store_str(&s, "fsync@openssh.com", 17);
68eb44
+        _libssh2_store_str(&s, handle->handle, handle->handle_len);
68eb44
+
68eb44
+        sftp->fsync_state = libssh2_NB_state_created;
68eb44
+    } else {
68eb44
+        packet = sftp->fsync_packet;
68eb44
+    }
68eb44
+
68eb44
+    if (sftp->fsync_state == libssh2_NB_state_created) {
68eb44
+        rc = _libssh2_channel_write(channel, 0, packet, packet_len);
68eb44
+        if (rc == LIBSSH2_ERROR_EAGAIN ||
68eb44
+            (0 <= rc && rc < (ssize_t)packet_len)) {
68eb44
+            sftp->fsync_packet = packet;
68eb44
+            return LIBSSH2_ERROR_EAGAIN;
68eb44
+        }
68eb44
+
68eb44
+        LIBSSH2_FREE(session, packet);
68eb44
+        sftp->fsync_packet = NULL;
68eb44
+
68eb44
+        if (rc < 0) {
68eb44
+            sftp->fsync_state = libssh2_NB_state_idle;
68eb44
+            return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
68eb44
+                                  "_libssh2_channel_write() failed");
68eb44
+        }
68eb44
+        sftp->fsync_state = libssh2_NB_state_sent;
68eb44
+    }
68eb44
+
68eb44
+    rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
68eb44
+                             sftp->fsync_request_id, &data, &data_len);
68eb44
+    if (rc == LIBSSH2_ERROR_EAGAIN) {
68eb44
+        return rc;
68eb44
+    } else if (rc) {
68eb44
+        sftp->fsync_state = libssh2_NB_state_idle;
68eb44
+        return _libssh2_error(session, rc,
68eb44
+                              "Error waiting for FXP EXTENDED REPLY");
68eb44
+    }
68eb44
+
68eb44
+    sftp->fsync_state = libssh2_NB_state_idle;
68eb44
+
68eb44
+    retcode = _libssh2_ntohu32(data + 5);
68eb44
+    LIBSSH2_FREE(session, data);
68eb44
+
68eb44
+    if (retcode != LIBSSH2_FX_OK) {
68eb44
+        sftp->last_errno = retcode;
68eb44
+        return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
68eb44
+                              "fsync failed");
68eb44
+    }
68eb44
+
68eb44
+    return 0;
68eb44
+}
68eb44
+
68eb44
+/* libssh2_sftp_fsync
68eb44
+ * Commit data on the handle to disk.
68eb44
+ */
68eb44
+LIBSSH2_API int
68eb44
+libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *hnd)
68eb44
+{
68eb44
+    int rc;
68eb44
+    if(!hnd)
68eb44
+        return LIBSSH2_ERROR_BAD_USE;
68eb44
+    BLOCK_ADJUST(rc, hnd->sftp->channel->session,
68eb44
+                 sftp_fsync(hnd));
68eb44
+    return rc;
68eb44
+}
68eb44
+
68eb44
+
68eb44
 /*
68eb44
  * sftp_fstat
68eb44
  *
68eb44
diff --git a/src/sftp.h b/src/sftp.h
68eb44
index 55bdb46..63e8139 100644
68eb44
--- a/src/sftp.h
68eb44
+++ b/src/sftp.h
68eb44
@@ -175,6 +175,11 @@ struct _LIBSSH2_SFTP
68eb44
     /* State variable used in sftp_write() */
68eb44
     libssh2_nonblocking_states write_state;
68eb44
 
68eb44
+    /* State variables used in sftp_fsync() */
68eb44
+    libssh2_nonblocking_states fsync_state;
68eb44
+    unsigned char *fsync_packet;
68eb44
+    uint32_t fsync_request_id;
68eb44
+
68eb44
     /* State variables used in libssh2_sftp_readdir() */
68eb44
     libssh2_nonblocking_states readdir_state;
68eb44
     unsigned char *readdir_packet;
68eb44
-- 
68eb44
1.8.1.4
68eb44