You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2019/03/03 06:03:15 UTC
[mina-sshd] 02/02: [SSHD-904] Add option to execute 'fsync' on
modified file contents via SFTP
This is an automated email from the ASF dual-hosted git repository.
lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit 0fef3ed6c5cfbe10e8fc90b3f6d40eb4fccad52c
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Fri Mar 1 08:17:45 2019 +0200
[SSHD-904] Add option to execute 'fsync' on modified file contents via SFTP
---
CHANGES.md | 8 ++++++-
docs/event-listeners.md | 26 ++++++++++++++++++++++
docs/sftp.md | 14 ------------
.../subsystem/sftp/SftpFileSystemAccessor.java | 15 +++++++++++++
4 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 9fde4b6..13d550e 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -30,8 +30,12 @@ to override the internal default ones.
* The `SftpFileSystemAccessor` callbacks are now provided with the relevant `Handle` they are servicing
(*Note:* in special cases a `null` value is provided to indicate invocation outside the scope of such a handle).
- * In this context, closing of file channel/directory streams created by the accessor are also closed
+ * Closing of file channel/directory streams created by the accessor are also closed
via callbacks to the same accessor
+
+ * When closing a file channel that may have been potentially modified, the default implementation
+ forces a synchronization of the data with the file-system. This behavior can be modified
+ by setting the `sftp-auto-fsync-on-close` property to *false*.
## Minor code helpers
@@ -106,3 +110,5 @@ if opening a shell or command channel.
in order to avoid client-side session timeout due to no traffic from server.
* [SSHD-903](https://issues.apache.org/jira/browse/SSHD-903) - Fixed the SFTP version negotiation behavior in case client proposed version is higher than server supported one.
+
+* [SSHD-904](https://issues.apache.org/jira/browse/SSHD-904) - Add option to execute 'fsync' on modified file contents via SFTP.
\ No newline at end of file
diff --git a/docs/event-listeners.md b/docs/event-listeners.md
index eda2d27..7a2457a 100644
--- a/docs/event-listeners.md
+++ b/docs/event-listeners.md
@@ -220,6 +220,32 @@ store user-defined attributes via its `AttributeStore` implementation. The liste
whether the close attempt was successful or not. In other words, after `SftpEventListener#closed` has been
called, all attributes associated with the handle are cleared.
+### `SftpFileSystemAccessor`
+
+This is the abstraction providing the SFTP server subsystem access to files and directories. The SFTP subsystem
+uses this abstraction to obtain file channels and/or directory streams. One can override the default implementation
+and thus be able to track and/or intervene in all opened files and folders throughout the SFTP server subsystem code.
+The accessor is registered/overwritten in via the `SftpSubSystemFactory`:
+
+```java
+
+ SftpSubsystemFactory factory = new SftpSubsystemFactory.Builder()
+ .withFileSystemAccessor(new MySftpFileSystemAccessor())
+ .build();
+ server.setSubsystemFactories(Collections.singletonList(factory));
+
+```
+
+*Note:*
+
+* Closing of file channel/directory streams created by the accessor are also closed
+via callbacks to the same accessor
+
+* When closing a file channel that may have been potentially modified, the default implementation
+forces a synchronization of the data with the file-system. This behavior can be modified
+by setting the `sftp-auto-fsync-on-close` property to *false* (or by providing a customized implementation
+that involves other considerations as well).
+
### `PortForwardingEventListener`
Informs and allows tracking of port forwarding events as described in [RFC 4254 - section 7](https://tools.ietf.org/html/rfc4254#section-7)
diff --git a/docs/sftp.md b/docs/sftp.md
index 5a590c2..070fc5c 100644
--- a/docs/sftp.md
+++ b/docs/sftp.md
@@ -252,20 +252,6 @@ the (default) password-based one:
```
-#### Tracking accessed locations via `SftpFileSystemAccessor`
-
-One can override the default `SftpFileSystemAccessor` and thus be able to track all opened files and folders
-throughout the SFTP server subsystem code. The accessor is registered/overwritten in via the `SftpSubSystemFactory`:
-
-```java
-
- SftpSubsystemFactory factory = new SftpSubsystemFactory.Builder()
- .withFileSystemAccessor(new MySftpFileSystemAccessor())
- .build();
- server.setSubsystemFactories(Collections.singletonList(factory));
-
-```
-
### SFTP sent/received names encoding
By default, the SFTP client uses UTF-8 to encode/decode any referenced file/folder name. However, some servers do not properly encode such names,
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpFileSystemAccessor.java b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpFileSystemAccessor.java
index 1e6ea3d..7ee5f60 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpFileSystemAccessor.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpFileSystemAccessor.java
@@ -35,6 +35,8 @@ import java.util.List;
import java.util.NavigableMap;
import java.util.Set;
+import org.apache.sshd.common.PropertyResolverUtils;
+import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.MapEntryUtils.NavigableMapBuilder;
import org.apache.sshd.common.util.io.FileInfoExtractor;
import org.apache.sshd.common.util.io.IoUtils;
@@ -61,6 +63,12 @@ public interface SftpFileSystemAccessor {
.put("lastModifiedTime", FileInfoExtractor.LASTMODIFIED)
.immutable();
+ /** Whether to invoke {@link FileChannel#force(boolean)} on files open for write when closing */
+ String PROP_AUTO_SYNC_FILE_ON_CLOSE = "sftp-auto-fsync-on-close";
+
+ /** Default value for {@value #PROP_AUTO_SYNC_FILE_ON_CLOSE} if none set */
+ boolean DEFAULT_AUTO_SYNC_FILE_ON_CLOSE = true;
+
SftpFileSystemAccessor DEFAULT = new SftpFileSystemAccessor() {
@Override
public String toString() {
@@ -177,6 +185,13 @@ public interface SftpFileSystemAccessor {
return;
}
+ if ((channel instanceof FileChannel)
+ && GenericUtils.containsAny(options, IoUtils.WRITEABLE_OPEN_OPTIONS)
+ && PropertyResolverUtils.getBooleanProperty(
+ session, PROP_AUTO_SYNC_FILE_ON_CLOSE, DEFAULT_AUTO_SYNC_FILE_ON_CLOSE)) {
+ ((FileChannel) channel).force(true);
+ }
+
channel.close();
}