This is an automated email from the ASF dual-hosted git repository.

twolf pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 52bf8602730ac549c535690bf859bc9d9bc87c4d
Author: Thomas Wolf <tw...@apache.org>
AuthorDate: Wed Jul 28 23:26:34 2021 +0200

    [SSHD-1188] Fix OpenMode.Exclusive for SFTP >=V5
    
    Setting the flags to SSH_FXF_READ if they're zero must be done only
    for SFTP < V5. For >= V5, zero is a valid value indicating
    SSH_FXF_CREATE_NEW, whereas SSH_FXF_READ (< V5, value 1) is the same as
    SSH_FXF_CREATE_TRUNCATE (>= V5, value 1).
---
 .../sftp/server/AbstractSftpSubsystemHelper.java   |  5 +----
 .../apache/sshd/sftp/client/SftpVersionsTest.java  | 23 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
index dbac259..f4e9e18 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
@@ -446,12 +446,9 @@ public abstract class AbstractSftpSubsystemHelper
         }
 
         int pflags = buffer.getInt();
-        if (pflags == 0) {
-            pflags = SftpConstants.SSH_FXF_READ;
-        }
 
         if (version < SftpConstants.SFTP_V5) {
-            int flags = pflags;
+            int flags = pflags == 0 ? SftpConstants.SSH_FXF_READ : pflags;
             pflags = 0;
             switch (flags & (SftpConstants.SSH_FXF_READ | 
SftpConstants.SSH_FXF_WRITE)) {
                 case SftpConstants.SSH_FXF_READ:
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
index feee734..7ce296e 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
@@ -58,6 +58,7 @@ import org.apache.sshd.sftp.client.SftpClient.CloseableHandle;
 import org.apache.sshd.sftp.client.SftpClient.DirEntry;
 import org.apache.sshd.sftp.client.SftpClient.OpenMode;
 import org.apache.sshd.sftp.common.SftpConstants;
+import org.apache.sshd.sftp.common.SftpException;
 import org.apache.sshd.sftp.common.SftpHelper;
 import org.apache.sshd.sftp.server.AbstractSftpEventListenerAdapter;
 import org.apache.sshd.sftp.server.DefaultGroupPrincipal;
@@ -136,6 +137,28 @@ public class SftpVersionsTest extends 
AbstractSftpClientTestSupport {
     }
 
     @Test
+    public void testSftpCreateNew() throws Exception {
+        Path targetPath = detectTargetFolder();
+        Path lclSftp = CommonTestSupportUtils.resolve(targetPath, 
SftpConstants.SFTP_SUBSYSTEM_NAME,
+                getClass().getSimpleName());
+        Path lclParent = assertHierarchyTargetFolderExists(lclSftp);
+        Path lclFile = lclParent.resolve(getCurrentTestName() + "-" + 
getTestedVersion() + ".txt");
+        Files.write(lclFile, Collections.singleton("existing"));
+
+        Path parentPath = targetPath.getParent();
+        String remotePath = 
CommonTestSupportUtils.resolveRelativeRemotePath(parentPath, lclFile);
+        try (ClientSession session = createAuthenticatedClientSession();
+             SftpClient sftp = createSftpClient(session, getTestedVersion())) {
+            SftpException ex = assertThrows(SftpException.class, () -> {
+                try (OutputStream out = sftp.write(remotePath, 
OpenMode.Create, OpenMode.Write, OpenMode.Exclusive)) {
+                    
out.write(getCurrentTestName().getBytes(StandardCharsets.UTF_8));
+                }
+            });
+            assertEquals(SftpConstants.SSH_FX_FILE_ALREADY_EXISTS, 
ex.getStatus());
+        }
+    }
+
+    @Test
     public void testSftpVersionSelector() throws Exception {
         try (ClientSession session = createAuthenticatedClientSession();
              SftpClient sftp = createSftpClient(session, getTestedVersion())) {

Reply via email to