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 2016/01/04 13:46:04 UTC

[2/2] mina-sshd git commit: Added '-r' flag support to SFTP 'rm' command

Added '-r' flag support to SFTP 'rm' command


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/926b16be
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/926b16be
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/926b16be

Branch: refs/heads/master
Commit: 926b16be1cbf8e0cd68459817abc0417dd5ba87d
Parents: a535e45
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Mon Jan 4 14:45:53 2016 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Mon Jan 4 14:45:53 2016 +0200

----------------------------------------------------------------------
 .../sshd/client/subsystem/sftp/SftpCommand.java | 76 +++++++++++++++++---
 1 file changed, 68 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/926b16be/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
index 59b7622..4cc7d6b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java
@@ -36,6 +36,7 @@ import java.util.logging.Level;
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.subsystem.sftp.SftpClient.Attributes;
+import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry;
 import org.apache.sshd.client.subsystem.sftp.extensions.openssh.OpenSSHStatExtensionInfo;
 import org.apache.sshd.client.subsystem.sftp.extensions.openssh.OpenSSHStatPathExtension;
 import org.apache.sshd.common.NamedResource;
@@ -297,7 +298,7 @@ public class SftpCommand implements Channel {
 
         @Override
         public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
-            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified", args);
+            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
 
             String newPath = resolveRemotePath(args);
             SftpClient sftp = getClient();
@@ -314,7 +315,7 @@ public class SftpCommand implements Channel {
 
         @Override
         public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
-            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified", args);
+            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
 
             String path = resolveRemotePath(args);
             SftpClient sftp = getClient();
@@ -359,13 +360,70 @@ public class SftpCommand implements Channel {
 
         @Override
         public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
-            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified", args);
+            String[] comps = GenericUtils.split(args, ' ');
+            int numArgs = GenericUtils.length(comps);
+            ValidateUtils.checkTrue(numArgs >= 1, "No arguments");
+            ValidateUtils.checkTrue(numArgs <= 2, "Too many arguments: %s", args);
+
+            String remotePath = comps[0];
+            boolean recursive = false;
+            boolean verbose = false;
+            if (remotePath.charAt(0) == '-') {
+                ValidateUtils.checkTrue(remotePath.length() > 1, "Missing flags specification: %s", args);
+                ValidateUtils.checkTrue(numArgs == 2, "Missing remote directory: %s", args);
+
+                for (int index = 1; index < remotePath.length(); index++) {
+                    char ch = remotePath.charAt(index);
+                    switch(ch) {
+                        case 'r' :
+                            recursive = true;
+                            break;
+                        case 'v':
+                            verbose = true;
+                            break;
+                        default:
+                            throw new IllegalArgumentException("Unknown flag (" + String.valueOf(ch) + ")");
+                    }
+                }
+                remotePath = comps[1];
+            }
 
-            String path = resolveRemotePath(args);
+            String path = resolveRemotePath(remotePath);
             SftpClient sftp = getClient();
-            sftp.remove(path);
+            if (recursive) {
+                Attributes attrs = sftp.stat(path);
+                ValidateUtils.checkTrue(attrs.isDirectory(), "Remote path not a directory: %s", args);
+                removeRecursive(sftp, path, attrs, stdout, verbose);
+            } else {
+                sftp.remove(path);
+                if (verbose) {
+                    stdout.append('\t').append("Removed ").println(path);
+                }
+            }
+
             return false;
         }
+
+        private void removeRecursive(SftpClient sftp, String path, Attributes attrs, PrintStream stdout, boolean verbose) throws IOException {
+            if (attrs.isDirectory()) {
+                for (DirEntry entry : sftp.readDir(path)) {
+                    String name = entry.getFilename();
+                    if (".".equals(name) || "..".equals(name)) {
+                        continue;
+                    }
+
+                    removeRecursive(sftp, path + "/" + name, entry.getAttributes(), stdout, verbose);
+                }
+
+                sftp.rmdir(path);
+            } else {
+                sftp.remove(path);
+            }
+
+            if (verbose) {
+                stdout.append('\t').append("Removed ").println(path);
+            }
+        }
     }
 
     private class RmdirCommandExecutor implements CommandExecutor {
@@ -376,7 +434,7 @@ public class SftpCommand implements Channel {
 
         @Override
         public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
-            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified", args);
+            ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
 
             String path = resolveRemotePath(args);
             SftpClient sftp = getClient();
@@ -413,13 +471,15 @@ public class SftpCommand implements Channel {
         @Override
         public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
             String[] comps = GenericUtils.split(args, ' ');
-            ValidateUtils.checkTrue(GenericUtils.length(comps) == 1, "Invalid number of arguments: %s", args);
+            int numArgs = GenericUtils.length(comps);
+            ValidateUtils.checkTrue(numArgs <= 1, "Invalid number of arguments: %s", args);
 
             SftpClient sftp = getClient();
             OpenSSHStatPathExtension ext = sftp.getExtension(OpenSSHStatPathExtension.class);
             ValidateUtils.checkTrue(ext.isSupported(), "Extension not supported by server: %s", ext.getName());
 
-            OpenSSHStatExtensionInfo info = ext.stat(resolveRemotePath(GenericUtils.trimToEmpty(comps[0])));
+            String remPath = resolveRemotePath((numArgs >= 1) ? GenericUtils.trimToEmpty(comps[0]) :  GenericUtils.trimToEmpty(args));
+            OpenSSHStatExtensionInfo info = ext.stat(remPath);
             Field[] fields = info.getClass().getFields();
             for (Field f : fields) {
                 String name = f.getName();