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 2021/03/25 19:47:41 UTC

[mina-sshd] 05/05: [SSHD-1147] Using SftpClientHolder marker interface where applicable

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 3b5c8e1a868c5fff8e9deb8159ee8b13f123b5f0
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Thu Mar 25 21:46:24 2021 +0200

    [SSHD-1147] Using SftpClientHolder marker interface where applicable
---
 CHANGES.md                                                 |  1 +
 .../java/org/apache/sshd/cli/client/SftpCommandMain.java   |  4 +++-
 .../SftpClientExtension.java => SftpClientHolder.java}     | 12 +++---------
 .../sshd/sftp/client/extensions/SftpClientExtension.java   |  9 +++------
 .../apache/sshd/sftp/client/fs/SftpDirectoryStream.java    |  9 +++------
 .../apache/sshd/sftp/client/impl/SftpDirEntryIterator.java |  9 +++------
 .../apache/sshd/sftp/client/impl/SftpInputStreamAsync.java | 14 +++++++-------
 .../apache/sshd/sftp/client/impl/SftpIterableDirEntry.java |  9 +++------
 .../sshd/sftp/client/impl/SftpOutputStreamAsync.java       |  9 +++------
 .../sshd/sftp/client/impl/StfpIterableDirHandle.java       |  9 +++------
 .../sshd/sftp/client/SftpInputStreamWithChannel.java       |  8 ++------
 .../sshd/sftp/client/SftpOutputStreamWithChannel.java      |  8 ++------
 12 files changed, 36 insertions(+), 65 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 09cdcc2..3d9e0e2 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -54,3 +54,4 @@
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for parsing incoming commands to the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for returning environment variables related data from the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for handling the SCP protocol textual commands and responses
+* [SSHD-1147](https://issues.apache.org/jira/browse/SSHD-1147) SftpInputStreamAsync: get file size before SSH_FXP_OPEN
\ No newline at end of file
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
index 066181f..f81d4b2 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
@@ -73,6 +73,7 @@ import org.apache.sshd.sftp.client.SftpClient;
 import org.apache.sshd.sftp.client.SftpClient.Attributes;
 import org.apache.sshd.sftp.client.SftpClient.DirEntry;
 import org.apache.sshd.sftp.client.SftpClientFactory;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 import org.apache.sshd.sftp.client.SftpVersionSelector;
 import org.apache.sshd.sftp.client.SftpVersionSelector.NamedVersionSelector;
 import org.apache.sshd.sftp.client.extensions.openssh.OpenSSHStatExtensionInfo;
@@ -89,7 +90,7 @@ import org.slf4j.Logger;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpCommandMain extends SshClientCliSupport implements Channel {
+public class SftpCommandMain extends SshClientCliSupport implements SftpClientHolder, Channel {
     /**
      * Command line option used to indicate a non-default port number
      */
@@ -135,6 +136,7 @@ public class SftpCommandMain extends SshClientCliSupport implements Channel {
         cwdLocal = System.getProperty("user.dir");
     }
 
+    @Override
     public final SftpClient getClient() {
         return client;
     }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/SftpClientHolder.java
similarity index 72%
copy from sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java
copy to sshd-sftp/src/main/java/org/apache/sshd/sftp/client/SftpClientHolder.java
index 6a63ddc..2c9d9d1 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/SftpClientHolder.java
@@ -17,18 +17,12 @@
  * under the License.
  */
 
-package org.apache.sshd.sftp.client.extensions;
-
-import org.apache.sshd.common.NamedResource;
-import org.apache.sshd.common.OptionalFeature;
-import org.apache.sshd.sftp.client.SftpClient;
+package org.apache.sshd.sftp.client;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public interface SftpClientExtension extends NamedResource, OptionalFeature {
-    /**
-     * @return The {@link SftpClient} used to issue the extended command
-     */
+@FunctionalInterface
+public interface SftpClientHolder {
     SftpClient getClient();
 }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java
index 6a63ddc..43c1c41 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/SftpClientExtension.java
@@ -21,14 +21,11 @@ package org.apache.sshd.sftp.client.extensions;
 
 import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.OptionalFeature;
-import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public interface SftpClientExtension extends NamedResource, OptionalFeature {
-    /**
-     * @return The {@link SftpClient} used to issue the extended command
-     */
-    SftpClient getClient();
+public interface SftpClientExtension extends NamedResource, OptionalFeature, SftpClientHolder {
+    // nothing extra
 }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpDirectoryStream.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpDirectoryStream.java
index c53625b..d06d87c 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpDirectoryStream.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpDirectoryStream.java
@@ -25,13 +25,14 @@ import java.util.Iterator;
 import java.util.Objects;
 
 import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 
 /**
  * Implements a remote {@link DirectoryStream}
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpDirectoryStream implements DirectoryStream<Path> {
+public class SftpDirectoryStream implements SftpClientHolder, DirectoryStream<Path> {
     protected SftpPathIterator pathIterator;
 
     private final SftpPath path;
@@ -64,11 +65,7 @@ public class SftpDirectoryStream implements DirectoryStream<Path> {
         pathIterator = new SftpPathIterator(getRootPath(), iter, getFilter());
     }
 
-    /**
-     * Client instance used to access the remote directory
-     *
-     * @return The {@link SftpClient} instance used to access the remote directory
-     */
+    @Override
     public final SftpClient getClient() {
         return sftp;
     }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpDirEntryIterator.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpDirEntryIterator.java
index 10eefd1..a8d2c64 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpDirEntryIterator.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpDirEntryIterator.java
@@ -32,6 +32,7 @@ import org.apache.sshd.common.util.logging.AbstractLoggingBean;
 import org.apache.sshd.sftp.client.SftpClient;
 import org.apache.sshd.sftp.client.SftpClient.DirEntry;
 import org.apache.sshd.sftp.client.SftpClient.Handle;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 
 /**
  * Iterates over the available directory entries for a given path. <B>Note:</B> if the iteration is carried out until no
@@ -40,7 +41,7 @@ import org.apache.sshd.sftp.client.SftpClient.Handle;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpDirEntryIterator extends AbstractLoggingBean implements Iterator<DirEntry>, Channel {
+public class SftpDirEntryIterator extends AbstractLoggingBean implements SftpClientHolder, Iterator<DirEntry>, Channel {
     private final AtomicReference<Boolean> eolIndicator = new AtomicReference<>();
     private final AtomicBoolean open = new AtomicBoolean(true);
     private final SftpClient client;
@@ -81,11 +82,7 @@ public class SftpDirEntryIterator extends AbstractLoggingBean implements Iterato
         this.dirEntries = load(dirHandle);
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote folder
-     */
+    @Override
     public final SftpClient getClient() {
         return client;
     }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
index 0de0b5e..f7d1456 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
@@ -36,14 +36,16 @@ import org.apache.sshd.common.util.buffer.Buffer;
 import org.apache.sshd.common.util.buffer.ByteArrayBuffer;
 import org.apache.sshd.common.util.io.InputStreamWithChannel;
 import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.SftpClient.Attributes;
 import org.apache.sshd.sftp.client.SftpClient.CloseableHandle;
 import org.apache.sshd.sftp.client.SftpClient.OpenMode;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 import org.apache.sshd.sftp.common.SftpConstants;
 import org.apache.sshd.sftp.common.SftpHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class SftpInputStreamAsync extends InputStreamWithChannel {
+public class SftpInputStreamAsync extends InputStreamWithChannel implements SftpClientHolder {
     protected final Logger log;
     protected final byte[] bb = new byte[1];
     protected final int bufferSize;
@@ -63,7 +65,8 @@ public class SftpInputStreamAsync extends InputStreamWithChannel {
         this.log = LoggerFactory.getLogger(getClass());
         this.clientInstance = Objects.requireNonNull(client, "No SFTP client instance");
         this.path = path;
-        this.fileSize = client.stat(path).getSize();
+        Attributes attrs = client.stat(path);
+        this.fileSize = attrs.getSize();
         this.handle = client.open(path, mode);
         this.bufferSize = bufferSize;
     }
@@ -79,11 +82,7 @@ public class SftpInputStreamAsync extends InputStreamWithChannel {
         this.fileSize = fileSize;
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final AbstractSftpClient getClient() {
         return clientInstance;
     }
@@ -184,6 +183,7 @@ public class SftpInputStreamAsync extends InputStreamWithChannel {
         return numXfered;
     }
 
+    @Override
     @SuppressWarnings("PMD.MissingOverride")
     public long transferTo(OutputStream out) throws IOException {
         if (!isOpen()) {
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpIterableDirEntry.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpIterableDirEntry.java
index 1e02ae7..5c1a83d 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpIterableDirEntry.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpIterableDirEntry.java
@@ -24,13 +24,14 @@ import java.util.Objects;
 import org.apache.sshd.common.util.ValidateUtils;
 import org.apache.sshd.sftp.client.SftpClient;
 import org.apache.sshd.sftp.client.SftpClient.DirEntry;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 
 /**
  * Provides an {@link Iterable} implementation of the {@link DirEntry}-ies for a remote directory
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpIterableDirEntry implements Iterable<DirEntry> {
+public class SftpIterableDirEntry implements SftpClientHolder, Iterable<DirEntry> {
     private final SftpClient client;
     private final String path;
 
@@ -43,11 +44,7 @@ public class SftpIterableDirEntry implements Iterable<DirEntry> {
         this.path = ValidateUtils.checkNotNullAndNotEmpty(path, "No remote path");
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final SftpClient getClient() {
         return client;
     }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpOutputStreamAsync.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpOutputStreamAsync.java
index 7028fe7..627d3f4 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpOutputStreamAsync.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpOutputStreamAsync.java
@@ -32,6 +32,7 @@ import org.apache.sshd.common.util.io.OutputStreamWithChannel;
 import org.apache.sshd.sftp.client.SftpClient;
 import org.apache.sshd.sftp.client.SftpClient.CloseableHandle;
 import org.apache.sshd.sftp.client.SftpClient.OpenMode;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 import org.apache.sshd.sftp.common.SftpConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -41,7 +42,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpOutputStreamAsync extends OutputStreamWithChannel {
+public class SftpOutputStreamAsync extends OutputStreamWithChannel implements SftpClientHolder {
     protected final Logger log;
     protected final byte[] bb = new byte[1];
     protected final int bufferSize;
@@ -71,11 +72,7 @@ public class SftpOutputStreamAsync extends OutputStreamWithChannel {
         this.bufferSize = bufferSize;
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final AbstractSftpClient getClient() {
         return clientInstance;
     }
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/StfpIterableDirHandle.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/StfpIterableDirHandle.java
index b2ef893..04daae7 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/StfpIterableDirHandle.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/StfpIterableDirHandle.java
@@ -23,8 +23,9 @@ import java.util.Objects;
 import org.apache.sshd.sftp.client.SftpClient;
 import org.apache.sshd.sftp.client.SftpClient.DirEntry;
 import org.apache.sshd.sftp.client.SftpClient.Handle;
+import org.apache.sshd.sftp.client.SftpClientHolder;
 
-public class StfpIterableDirHandle implements Iterable<DirEntry> {
+public class StfpIterableDirHandle implements SftpClientHolder, Iterable<DirEntry> {
     private final SftpClient client;
     private final Handle handle;
 
@@ -37,11 +38,7 @@ public class StfpIterableDirHandle implements Iterable<DirEntry> {
         this.handle = handle;
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final SftpClient getClient() {
         return client;
     }
diff --git a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpInputStreamWithChannel.java b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpInputStreamWithChannel.java
index ae8b6f2..e0ff3ab 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpInputStreamWithChannel.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpInputStreamWithChannel.java
@@ -31,7 +31,7 @@ import org.apache.sshd.sftp.client.SftpClient.OpenMode;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpInputStreamWithChannel extends InputStreamWithChannel {
+public class SftpInputStreamWithChannel extends InputStreamWithChannel implements SftpClientHolder {
     private final SftpClient client;
     private final String path;
     private byte[] bb;
@@ -50,11 +50,7 @@ public class SftpInputStreamWithChannel extends InputStreamWithChannel {
         handle = client.open(path, mode);
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final SftpClient getClient() {
         return client;
     }
diff --git a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpOutputStreamWithChannel.java b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpOutputStreamWithChannel.java
index 03f4300..ee4b40a 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpOutputStreamWithChannel.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpOutputStreamWithChannel.java
@@ -31,7 +31,7 @@ import org.apache.sshd.sftp.client.SftpClient.OpenMode;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SftpOutputStreamWithChannel extends OutputStreamWithChannel {
+public class SftpOutputStreamWithChannel extends OutputStreamWithChannel implements SftpClientHolder {
     private final SftpClient client;
     private final String path;
     private final byte[] bb = new byte[1];
@@ -48,11 +48,7 @@ public class SftpOutputStreamWithChannel extends OutputStreamWithChannel {
         handle = client.open(path, mode);
     }
 
-    /**
-     * The client instance
-     *
-     * @return {@link SftpClient} instance used to access the remote file
-     */
+    @Override
     public final SftpClient getClient() {
         return client;
     }