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 039457e1ea573e229ccae36462caeaf55c305c31
Author: The-Yoda <[email protected]>
AuthorDate: Wed Jun 26 11:56:51 2019 +0300

    [SSHD-929] Provide file/directory flag indicator to SFTP event listener 
callback for removal
---
 CHANGES.md                                         |  5 ++++
 .../helper/SftpServerSubSystemEventListener.java   |  2 +-
 .../sftp/SimpleAccessControlSftpEventListener.java | 12 ++++++--
 .../SimpleAccessControlSftpEventListenerTest.java  |  9 ++++--
 .../sftp/AbstractSftpEventListenerAdapter.java     |  8 ++---
 .../sftp/AbstractSftpSubsystemHelper.java          | 35 ++++++++++++----------
 .../server/subsystem/sftp/SftpEventListener.java   | 24 +++++++++------
 .../sshd/client/subsystem/sftp/SftpTest.java       | 33 +++++++++++++-------
 8 files changed, 83 insertions(+), 45 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index f27c9bb..96fd0ac 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -49,6 +49,9 @@ may be available in the relevant data file.
 * `org.apache.sshd.agent.unix.AgentClient` constructor expects a non-*null* 
`FactoryManager` instance which
 it then exposes via its `getFactoryManager`.
 
+* `SftpEventListener#removing/removed` callbacks accept an `isDirectory` flag 
indicating the type of `Path` being
+removed - file or directory.
+
 ## Minor code helpers
 
 * The `Session` object provides a `isServerSession` method that can be used to 
distinguish between
@@ -145,3 +148,5 @@ and therefore closing all currently tracked file/directory 
handles.
 * [SSHD-921](https://issues.apache.org/jira/browse/SSHD-921) - Do not send 
session disconnect message due to timeout expiration if already done so.
 
 * [SSHD-923](https://issues.apache.org/jira/browse/SSHD-923) - Added agent 
close detection mechanisms to avoid infinite waits on incoming messages.
+
+* [SSHD-929](https://issues.apache.org/jira/browse/SSHD-929) - Provide 
file/directory flag indicator to SFTP event listener callback for removal.
diff --git 
a/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
 
b/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
index 5a43e5f..7410511 100644
--- 
a/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
+++ 
b/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
@@ -73,7 +73,7 @@ public class SftpServerSubSystemEventListener extends 
ServerEventListenerHelper
     }
 
     @Override
-    public void removed(ServerSession session, Path path, Throwable thrown) 
throws IOException {
+    public void removed(ServerSession session, Path path, boolean isDirectory, 
Throwable thrown) throws IOException {
         if (thrown == null) {
             outputDebugMessage("Session %s removed %s", session, path);
         } else {
diff --git 
a/sshd-contrib/src/main/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListener.java
 
b/sshd-contrib/src/main/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListener.java
index f1fa81f..cf24403 100644
--- 
a/sshd-contrib/src/main/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListener.java
+++ 
b/sshd-contrib/src/main/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListener.java
@@ -161,8 +161,16 @@ public abstract class SimpleAccessControlSftpEventListener 
extends AbstractSftpE
     }
 
     @Override
-    public void removing(ServerSession session, Path path) throws IOException {
-        super.removing(session, path);
+    public void removing(ServerSession session, Path path, boolean 
isDirectory) throws IOException {
+        super.removing(session, path, isDirectory);
+        if (!isModificationAllowed(session, path.toString(), path)) {
+            throw new AccessDeniedException(path.toString());
+        }
+    }
+
+    @Override
+    public void removed(ServerSession session, Path path, boolean isDirectory, 
Throwable thrown) throws IOException {
+        super.removed(session, path, isDirectory, thrown);
         if (!isModificationAllowed(session, path.toString(), path)) {
             throw new AccessDeniedException(path.toString());
         }
diff --git 
a/sshd-contrib/src/test/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListenerTest.java
 
b/sshd-contrib/src/test/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListenerTest.java
index f824811..f3bd551 100644
--- 
a/sshd-contrib/src/test/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListenerTest.java
+++ 
b/sshd-contrib/src/test/java/org/apache/sshd/server/subsystem/sftp/SimpleAccessControlSftpEventListenerTest.java
@@ -139,7 +139,8 @@ public class SimpleAccessControlSftpEventListenerTest 
extends BaseTestSupport {
     public void testReadOnlyDirectoryAccess() throws Exception {
         Path targetPath = detectTargetFolder();
         Path parentPath = targetPath.getParent();
-        Path lclSftp = CommonTestSupportUtils.resolve(targetPath, 
SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName(), 
getCurrentTestName());
+        Path lclSftp = CommonTestSupportUtils.resolve(
+            targetPath, SftpConstants.SFTP_SUBSYSTEM_NAME, 
getClass().getSimpleName(), getCurrentTestName());
         Path testFile = 
assertHierarchyTargetFolderExists(lclSftp).resolve("file.txt");
         byte[] data = (getClass().getName() + "#" + 
getCurrentTestName()).getBytes(StandardCharsets.UTF_8);
         Files.deleteIfExists(testFile);
@@ -148,7 +149,9 @@ public class SimpleAccessControlSftpEventListenerTest 
extends BaseTestSupport {
         try (SshClient client = setupTestClient()) {
             client.start();
 
-            try (ClientSession session = client.connect(getCurrentTestName(), 
TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
+            try (ClientSession session = client.connect(getCurrentTestName(), 
TEST_LOCALHOST, port)
+                    .verify(7L, TimeUnit.SECONDS)
+                    .getSession()) {
                 session.addPasswordIdentity(getCurrentTestName());
                 session.auth().verify(5L, TimeUnit.SECONDS);
 
@@ -177,7 +180,7 @@ public class SimpleAccessControlSftpEventListenerTest 
extends BaseTestSupport {
 
                     try {
                         sftp.rmdir(folder);
-                        fail("Unexpected folder creation success");
+                        fail("Unexpected folder removal success");
                     } catch (SftpException e) {
                         int status = e.getStatus();
                         assertEquals("Unexpected rmdir SFTP status code", 
SftpConstants.SSH_FX_PERMISSION_DENIED, status);
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpEventListenerAdapter.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpEventListenerAdapter.java
index 7251dff..4c6eca3 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpEventListenerAdapter.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpEventListenerAdapter.java
@@ -207,18 +207,18 @@ public abstract class AbstractSftpEventListenerAdapter 
extends AbstractLoggingBe
     }
 
     @Override
-    public void removing(ServerSession session, Path path)
+    public void removing(ServerSession session, Path path, boolean isDirectory)
             throws IOException {
         if (log.isTraceEnabled()) {
-            log.trace("removing(" + session + ") " + path);
+            log.trace("removing(" + session + ")[dir=" + isDirectory + "] " + 
path);
         }
     }
 
     @Override
-    public void removed(ServerSession session, Path path, Throwable thrown)
+    public void removed(ServerSession session, Path path, boolean isDirectory, 
Throwable thrown)
             throws IOException {
         if (log.isTraceEnabled()) {
-            log.trace("removed(" + session + ") " + path
+            log.trace("removed(" + session + ")[dir=" + isDirectory + "] " + 
path
                   + ((thrown == null) ? "" : (": " + 
thrown.getClass().getSimpleName() + ": " + thrown.getMessage())));
         }
     }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java
index 5bab0dc..3423398 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java
@@ -1514,9 +1514,9 @@ public abstract class AbstractSftpSubsystemHelper
                   getServerSession(), id, path, p);
         }
         if (Files.isDirectory(p, options)) {
-            doRemove(id, p);
+            doRemove(id, p, true);
         } else {
-            throw signalRemovalPreConditionFailure(id, path, p, new 
NotDirectoryException(p.toString()));
+            throw signalRemovalPreConditionFailure(id, path, p, new 
NotDirectoryException(p.toString()), true);
         }
     }
 
@@ -1525,19 +1525,21 @@ public abstract class AbstractSftpSubsystemHelper
      *
      * @param id Deletion request ID
      * @param p {@link Path} to delete
+     * @param isDirectory Whether the requested path represents a directory or 
a regular file
      * @throws IOException If failed to delete
      */
-    protected void doRemove(int id, Path p) throws IOException {
+    protected void doRemove(int id, Path p, boolean isDirectory) throws 
IOException {
         SftpEventListener listener = getSftpEventListenerProxy();
         ServerSession session = getServerSession();
-        listener.removing(session, p);
+
+        listener.removing(session, p, isDirectory);
         try {
             Files.delete(p);
         } catch (IOException | RuntimeException e) {
-            listener.removed(session, p, e);
+            listener.removed(session, p, isDirectory, e);
             throw e;
         }
-        listener.removed(session, p, null);
+        listener.removed(session, p, isDirectory, null);
     }
 
     protected void doMakeDirectory(Buffer buffer, int id) throws IOException {
@@ -1612,27 +1614,30 @@ public abstract class AbstractSftpSubsystemHelper
         Boolean status = IoUtils.checkFileExists(p, options);
         if (status == null) {
             throw signalRemovalPreConditionFailure(id, path, p,
-                new AccessDeniedException(p.toString(), p.toString(), "Cannot 
determine existence of remove candidate"));
+                new AccessDeniedException(p.toString(), p.toString(), "Cannot 
determine existence of remove candidate"), false);
         } else if (!status) {
             throw signalRemovalPreConditionFailure(id, path, p,
-                new NoSuchFileException(p.toString(), p.toString(), "Removal 
candidate not found"));
+                new NoSuchFileException(p.toString(), p.toString(), "Removal 
candidate not found"), false);
         } else if (Files.isDirectory(p, options)) {
             throw signalRemovalPreConditionFailure(id, path, p,
-                new SftpException(SftpConstants.SSH_FX_FILE_IS_A_DIRECTORY, 
p.toString() + " is a folder"));
+                new SftpException(SftpConstants.SSH_FX_FILE_IS_A_DIRECTORY, 
p.toString() + " is a folder"), false);
         } else {
-            doRemove(id, p);
+            doRemove(id, p, false);
         }
     }
 
-    protected <E extends IOException> E signalRemovalPreConditionFailure(int 
id, String pathValue, Path path, E thrown) throws IOException {
+    protected <E extends IOException> E signalRemovalPreConditionFailure(
+            int id, String pathValue, Path path, E thrown, boolean 
isRemoveDirectory)
+                throws IOException {
         SftpEventListener listener = getSftpEventListenerProxy();
         ServerSession session = getServerSession();
         if (log.isDebugEnabled()) {
-            log.debug("signalRemovalPreConditionFailure(id={})[{}] signal {} 
for {}: {}",
-                id, pathValue, thrown.getClass().getSimpleName(), path, 
thrown.getMessage());
+            log.debug("signalRemovalPreConditionFailure(id={})[{}] signal {} 
for (directory={}) {}: {}",
+                id, pathValue, thrown.getClass().getSimpleName(), 
isRemoveDirectory, path, thrown.getMessage());
         }
-        listener.removing(session, path);
-        listener.removed(session, path, thrown);
+
+        listener.removing(session, path, isRemoveDirectory);
+        listener.removed(session, path, isRemoveDirectory, thrown);
         return thrown;
     }
 
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
index 669a850..8c2b3da 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
@@ -370,26 +370,30 @@ public interface SftpEventListener extends 
SshdEventListener {
     }
 
     /**
-     * Called <U>prior</U> to removing a file / directory
+     * Called <U>prior</U> to removing a file
      *
      * @param session The {@link ServerSession} through which the request was 
handled
      * @param path    The {@link Path} about to be removed
+     * @param isDirectory  Whether this is a folder or a file
      * @throws IOException If failed to handle the call
-     * @see #removed(ServerSession, Path, Throwable)
+     * @see #removedFile(ServerSession, Path, Throwable)
      */
-    default void removing(ServerSession session, Path path) throws IOException 
{
+    default void removing(ServerSession session, Path path, boolean 
isDirectory) throws IOException {
         // ignored
     }
 
     /**
-     * Called <U>after</U> a file / directory has been removed
+     * Called <U>after</U> a file has been removed
      *
      * @param session The {@link ServerSession} through which the request was 
handled
      * @param path    The {@link Path} to be removed
+     * @param isDirectory  Whether this was a folder or a file
      * @param thrown  If not-{@code null} then the reason for the failure to 
execute
      * @throws IOException If failed to handle the call
      */
-    default void removed(ServerSession session, Path path, Throwable thrown) 
throws IOException {
+    default void removed(
+            ServerSession session, Path path, boolean isDirectory, Throwable 
thrown)
+                throws IOException {
         // ignored
     }
 
@@ -418,8 +422,9 @@ public interface SftpEventListener extends 
SshdEventListener {
      * @param thrown  If not-{@code null} then the reason for the failure to 
execute
      * @throws IOException If failed to handle the call
      */
-    default void linked(ServerSession session, Path source, Path target, 
boolean symLink, Throwable thrown)
-            throws IOException {
+    default void linked(
+            ServerSession session, Path source, Path target, boolean symLink, 
Throwable thrown)
+                throws IOException {
         // ignored
     }
 
@@ -448,8 +453,9 @@ public interface SftpEventListener extends 
SshdEventListener {
      * @param thrown  If not-{@code null} then the reason for the failure to 
execute
      * @throws IOException If failed to handle the call
      */
-    default void modifiedAttributes(ServerSession session, Path path, 
Map<String, ?> attrs, Throwable thrown)
-            throws IOException {
+    default void modifiedAttributes(
+            ServerSession session, Path path, Map<String, ?> attrs, Throwable 
thrown)
+                throws IOException {
         // ignored
     }
 
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java 
b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
index 9250ab9..4ea1003 100644
--- 
a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
+++ 
b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
@@ -736,8 +736,10 @@ public class SftpTest extends 
AbstractSftpClientTestSupport {
         AtomicInteger entriesCount = new AtomicInteger(0);
         AtomicInteger creatingCount = new AtomicInteger(0);
         AtomicInteger createdCount = new AtomicInteger(0);
-        AtomicInteger removingCount = new AtomicInteger(0);
-        AtomicInteger removedCount = new AtomicInteger(0);
+        AtomicInteger removingFileCount = new AtomicInteger(0);
+        AtomicInteger removedFileCount = new AtomicInteger(0);
+        AtomicInteger removingDirectoryCount = new AtomicInteger(0);
+        AtomicInteger removedDirectoryCount = new AtomicInteger(0);
         AtomicInteger modifyingCount = new AtomicInteger(0);
         AtomicInteger modifiedCount = new AtomicInteger(0);
         SftpEventListener listener = new AbstractSftpEventListenerAdapter() {
@@ -765,16 +767,24 @@ public class SftpTest extends 
AbstractSftpClientTestSupport {
             }
 
             @Override
-            public void removing(ServerSession session, Path path) {
-                removingCount.incrementAndGet();
-                log.info("removing(" + session + ") " + path);
+            public void removing(ServerSession session, Path path, boolean 
isDirectory) {
+                if (isDirectory) {
+                    removingDirectoryCount.incrementAndGet();
+                } else {
+                    removingFileCount.incrementAndGet();
+                }
+                log.info("removing(" + session + ")[dir=" + isDirectory + "] " 
+ path);
             }
 
             @Override
-            public void removed(ServerSession session, Path path, Throwable 
thrown) {
-                removedCount.incrementAndGet();
-                log.info("removed(" + session + ") " + path
-                       + ((thrown == null) ? "" : (": " + 
thrown.getClass().getSimpleName() + ": " + thrown.getMessage())));
+            public void removed(ServerSession session, Path path, boolean 
isDirectory, Throwable thrown) {
+                if (isDirectory) {
+                    removedDirectoryCount.incrementAndGet();
+                } else {
+                    removedFileCount.incrementAndGet();
+                }
+                log.info("removed(" + session + ")[dir=" + isDirectory + "] " 
+ path
+                        + ((thrown == null) ? "" : (": " + 
thrown.getClass().getSimpleName() + ": " + thrown.getMessage())));
             }
 
             @Override
@@ -908,8 +918,9 @@ public class SftpTest extends AbstractSftpClientTestSupport 
{
             assertTrue("No entries read", entriesCount.get() > 0);
             assertTrue("No data read", readSize.get() > 0L);
             assertTrue("No data written", writeSize.get() > 0L);
-            assertEquals("Mismatched removal counts", removingCount.get(), 
removedCount.get());
-            assertTrue("No removals signalled", removedCount.get() > 0);
+            assertEquals("Mismatched removal counts", removingFileCount.get(), 
removedFileCount.get());
+            assertEquals("Mismatched directory removal counts", 
removingDirectoryCount.get(), removedDirectoryCount.get());
+            assertTrue("No removals signalled", removedFileCount.get() > 0);
             assertEquals("Mismatched creation counts", creatingCount.get(), 
createdCount.get());
             assertTrue("No creations signalled", creatingCount.get() > 0);
             assertEquals("Mismatched modification counts", 
modifyingCount.get(), modifiedCount.get());

Reply via email to