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 2018/11/26 14:41:02 UTC
[2/3] mina-sshd git commit: [SSHD-862] Added session context
parameter to ScpFileOpener methods
[SSHD-862] Added session context parameter to ScpFileOpener methods
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/857172b7
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/857172b7
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/857172b7
Branch: refs/heads/master
Commit: 857172b7835b074b6080fc93874334afbb4f1e35
Parents: ef6ea9e
Author: Lyor Goldstein <lg...@apache.org>
Authored: Mon Nov 26 16:19:54 2018 +0200
Committer: Lyor Goldstein <lg...@apache.org>
Committed: Mon Nov 26 16:40:54 2018 +0200
----------------------------------------------------------------------
CHANGES.md | 1 +
.../apache/sshd/common/scp/ScpFileOpener.java | 48 ++++++++++++++------
.../org/apache/sshd/common/scp/ScpHelper.java | 40 ++++++++--------
.../scp/helpers/DefaultScpFileOpener.java | 4 +-
.../LocalFileScpSourceStreamResolver.java | 3 +-
5 files changed, 60 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/857172b7/CHANGES.md
----------------------------------------------------------------------
diff --git a/CHANGES.md b/CHANGES.md
index e43f7b9..4daa2c7 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -79,6 +79,7 @@ and moved to `PublicKeyEntry` class.
* All methods `ScpTransferEventListener` accept an extra `Session` parameter indicating the SSH client/server
session context for the listener's invocation.
+ * Same applies for `ScpFileOpener`
## Behavioral changes and enhancements
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/857172b7/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpFileOpener.java
----------------------------------------------------------------------
diff --git a/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpFileOpener.java b/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpFileOpener.java
index 78e033f..61b696a 100644
--- a/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpFileOpener.java
+++ b/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpFileOpener.java
@@ -58,6 +58,7 @@ public interface ScpFileOpener {
/**
* Invoked when receiving a new file to via a directory command
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param localPath The target local path
* @param name The target file name
* @param preserve Whether requested to preserve the permissions and timestamp
@@ -68,8 +69,8 @@ public interface ScpFileOpener {
* @see #updateFileProperties(Path, Set, ScpTimestamp) updateFileProperties
*/
default Path resolveIncomingFilePath(
- Path localPath, String name, boolean preserve, Set<PosixFilePermission> permissions, ScpTimestamp time)
- throws IOException {
+ Session session, Path localPath, String name, boolean preserve, Set<PosixFilePermission> permissions, ScpTimestamp time)
+ throws IOException {
LinkOption[] options = IoUtils.getLinkOptions(true);
Boolean status = IoUtils.checkFileExists(localPath, options);
if (status == null) {
@@ -116,11 +117,12 @@ public interface ScpFileOpener {
/**
* Invoked when required to send a pattern of files
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param basedir The base directory - may be {@code null}/empty to indicate CWD
* @param pattern The required pattern
* @return The matching <U>relative paths</U> of the children to send
*/
- default Iterable<String> getMatchingFilesToSend(String basedir, String pattern) {
+ default Iterable<String> getMatchingFilesToSend(Session session, String basedir, String pattern) {
String[] matches = new DirectoryScanner(basedir, pattern).scan();
if (GenericUtils.isEmpty(matches)) {
return Collections.emptyList();
@@ -133,13 +135,15 @@ public interface ScpFileOpener {
* Invoked on a local path in order to decide whether it should be sent
* as a file or as a directory
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local {@link Path}
* @param options The {@link LinkOption}-s
* @return Whether to send the file as a regular one - <B>Note:</B> if {@code false}
* then the {@link #sendAsDirectory(Path, LinkOption...)} is consulted.
* @throws IOException If failed to decide
*/
- default boolean sendAsRegularFile(Path path, LinkOption... options) throws IOException {
+ default boolean sendAsRegularFile(Session session, Path path, LinkOption... options)
+ throws IOException {
return Files.isRegularFile(path, options);
}
@@ -147,20 +151,23 @@ public interface ScpFileOpener {
* Invoked on a local path in order to decide whether it should be sent
* as a file or as a directory
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local {@link Path}
* @param options The {@link LinkOption}-s
* @return Whether to send the file as a directory - <B>Note:</B> if {@code true}
* then {@link #getLocalFolderChildren(Path)} is consulted
* @throws IOException If failed to decide
*/
- default boolean sendAsDirectory(Path path, LinkOption... options) throws IOException {
+ default boolean sendAsDirectory(Session session, Path path, LinkOption... options)
+ throws IOException {
return Files.isDirectory(path, options);
}
/**
* Invoked when required to send all children of a local directory
*
- * @param path The local folder {@link Path}{
+ * @param session The client/server {@link Session} through which the transfer is being executed
+ * @param path The local folder {@link Path}
* @return The {@link DirectoryStream} of children to send - <B>Note:</B> for each child
* the decision whether to send it as a file or a directory will be reached by consulting
* the respective {@link #sendAsRegularFile(Path, LinkOption...) sendAsRegularFile} and
@@ -168,26 +175,33 @@ public interface ScpFileOpener {
* @throws IOException If failed to provide the children stream
* @see #sendAsDirectory(Path, LinkOption...) sendAsDirectory
*/
- default DirectoryStream<Path> getLocalFolderChildren(Path path) throws IOException {
+ default DirectoryStream<Path> getLocalFolderChildren(Session session, Path path) throws IOException {
return Files.newDirectoryStream(path);
}
- default BasicFileAttributes getLocalBasicFileAttributes(Path path, LinkOption... options) throws IOException {
- return Files.getFileAttributeView(path, BasicFileAttributeView.class, options).readAttributes();
+ default BasicFileAttributes getLocalBasicFileAttributes(
+ Session session, Path path, LinkOption... options)
+ throws IOException {
+ BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class, options);
+ return view.readAttributes();
}
- default Set<PosixFilePermission> getLocalFilePermissions(Path path, LinkOption... options) throws IOException {
+ default Set<PosixFilePermission> getLocalFilePermissions(
+ Session session, Path path, LinkOption... options)
+ throws IOException {
return IoUtils.getPermissions(path, options);
}
/**
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param fileSystem The <U>local</U> {@link FileSystem} on which local file should reside
* @param commandPath The command path using the <U>local</U> file separator
* @return The resolved absolute and normalized local {@link Path}
* @throws IOException If failed to resolve the path
* @throws InvalidPathException If invalid local path value
*/
- default Path resolveLocalPath(FileSystem fileSystem, String commandPath) throws IOException, InvalidPathException {
+ default Path resolveLocalPath(Session session, FileSystem fileSystem, String commandPath)
+ throws IOException, InvalidPathException {
String path = SelectorUtils.translateToLocalFileSystemPath(commandPath, File.separatorChar, fileSystem);
Path lcl = fileSystem.getPath(path);
Path abs = lcl.isAbsolute() ? lcl : lcl.toAbsolutePath();
@@ -197,6 +211,7 @@ public interface ScpFileOpener {
/**
* Invoked when a request to receive something is processed
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local target {@link Path} of the request
* @param recursive Whether the request is recursive
* @param shouldBeDir Whether target path is expected to be a directory
@@ -205,7 +220,7 @@ public interface ScpFileOpener {
* @throws IOException If failed to resolve target location
*/
default Path resolveIncomingReceiveLocation(
- Path path, boolean recursive, boolean shouldBeDir, boolean preserve)
+ Session session, Path path, boolean recursive, boolean shouldBeDir, boolean preserve)
throws IOException {
if (!shouldBeDir) {
return path;
@@ -228,12 +243,15 @@ public interface ScpFileOpener {
/**
* Called when there is a candidate file/folder for sending
*
+ * @param session The client/server {@link Session} through which the transfer is being executed
* @param localPath The original file/folder {@link Path} for sending
* @param options The {@link LinkOption}-s to use for validation
* @return The effective outgoing file path (default=same as input)
* @throws IOException If failed to resolve
*/
- default Path resolveOutgoingFilePath(Path localPath, LinkOption... options) throws IOException {
+ default Path resolveOutgoingFilePath(
+ Session session, Path localPath, LinkOption... options)
+ throws IOException {
Boolean status = IoUtils.checkFileExists(localPath, options);
if (status == null) {
throw new AccessDeniedException("Send file existence status cannot be determined: " + localPath);
@@ -256,7 +274,7 @@ public interface ScpFileOpener {
*/
InputStream openRead(Session session, Path file, OpenOption... options) throws IOException;
- ScpSourceStreamResolver createScpSourceStreamResolver(Path path) throws IOException;
+ ScpSourceStreamResolver createScpSourceStreamResolver(Session session, Path path) throws IOException;
/**
* Create an output stream to write to a file
@@ -269,7 +287,7 @@ public interface ScpFileOpener {
*/
OutputStream openWrite(Session session, Path file, OpenOption... options) throws IOException;
- ScpTargetStreamResolver createScpTargetStreamResolver(Path path) throws IOException;
+ ScpTargetStreamResolver createScpTargetStreamResolver(Session session, Path path) throws IOException;
static void updateFileProperties(Path file, Set<PosixFilePermission> perms, ScpTimestamp time) throws IOException {
IoUtils.setPermissions(file, perms);
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/857172b7/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpHelper.java b/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
index 38331e9..eb447db 100644
--- a/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
+++ b/sshd-scp/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
@@ -161,7 +161,7 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
public void receive(Path local, boolean recursive, boolean shouldBeDir, boolean preserve, int bufferSize) throws IOException {
Path localPath = Objects.requireNonNull(local, "No local path").normalize().toAbsolutePath();
- Path path = opener.resolveIncomingReceiveLocation(localPath, recursive, shouldBeDir, preserve);
+ Path path = opener.resolveIncomingReceiveLocation(getSession(), localPath, recursive, shouldBeDir, preserve);
receive((line, isDir, time) -> {
if (recursive && isDir) {
receiveDir(line, path, time, preserve, bufferSize);
@@ -243,13 +243,13 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
throw new IOException("Expected 0 length for directory=" + name + " but got " + length);
}
- Path file = opener.resolveIncomingFilePath(path, name, preserve, perms, time);
+ Session session = getSession();
+ Path file = opener.resolveIncomingFilePath(session, path, name, preserve, perms, time);
ack();
time = null;
- Session session = getSession();
listener.startFolderEvent(session, FileOperation.RECEIVE, path, perms);
try {
for (;;) {
@@ -287,7 +287,8 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
this, header, path, preserve, time, bufferSize);
}
- receiveStream(header, opener.createScpTargetStreamResolver(path), time, preserve, bufferSize);
+ ScpTargetStreamResolver targetStreamResolver = opener.createScpTargetStreamResolver(getSession(), path);
+ receiveStream(header, targetStreamResolver, time, preserve, bufferSize);
}
public void receiveStream(String header, ScpTargetStreamResolver resolver, ScpTimestamp time, boolean preserve, int bufferSize) throws IOException {
@@ -398,12 +399,13 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
pattern = pattern.substring(lastSep + 1);
}
- Iterable<String> included = opener.getMatchingFilesToSend(basedir, pattern);
+ Session session = getSession();
+ Iterable<String> included = opener.getMatchingFilesToSend(session, basedir, pattern);
for (String path : included) {
Path file = resolveLocalPath(basedir, path);
- if (opener.sendAsRegularFile(file, options)) {
+ if (opener.sendAsRegularFile(session, file, options)) {
sendFile(file, preserve, bufferSize);
- } else if (opener.sendAsDirectory(file, options)) {
+ } else if (opener.sendAsDirectory(session, file, options)) {
if (!recursive) {
if (debugEnabled) {
log.debug("send({}) {}: not a regular file", this, path);
@@ -440,10 +442,11 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
protected void send(Path local, boolean recursive, boolean preserve, int bufferSize, LinkOption... options) throws IOException {
Path localPath = Objects.requireNonNull(local, "No local path").normalize().toAbsolutePath();
- Path file = opener.resolveOutgoingFilePath(localPath, options);
- if (opener.sendAsRegularFile(file, options)) {
+ Session session = getSession();
+ Path file = opener.resolveOutgoingFilePath(session, localPath, options);
+ if (opener.sendAsRegularFile(session, file, options)) {
sendFile(file, preserve, bufferSize);
- } else if (opener.sendAsDirectory(file, options)) {
+ } else if (opener.sendAsDirectory(session, file, options)) {
if (!recursive) {
throw new IOException(file + " not a regular file");
} else {
@@ -469,7 +472,7 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
* @throws InvalidPathException If invalid local path value
*/
public Path resolveLocalPath(String commandPath) throws IOException, InvalidPathException {
- Path p = opener.resolveLocalPath(fileSystem, commandPath);
+ Path p = opener.resolveLocalPath(getSession(), fileSystem, commandPath);
if (log.isTraceEnabled()) {
log.trace("resolveLocalPath({}) {}: {}", this, commandPath, p);
}
@@ -483,7 +486,8 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
log.debug("sendFile({})[preserve={},buffer-size={}] Sending file {}", this, preserve, bufferSize, path);
}
- sendStream(opener.createScpSourceStreamResolver(path), preserve, bufferSize);
+ ScpSourceStreamResolver sourceStreamResolver = opener.createScpSourceStreamResolver(getSession(), path);
+ sendStream(sourceStreamResolver, preserve, bufferSize);
}
public void sendStream(ScpSourceStreamResolver resolver, boolean preserve, int bufferSize) throws IOException {
@@ -602,8 +606,9 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
}
LinkOption[] options = IoUtils.getLinkOptions(true);
+ Session session = getSession();
if (preserve) {
- BasicFileAttributes basic = opener.getLocalBasicFileAttributes(path, options);
+ BasicFileAttributes basic = opener.getLocalBasicFileAttributes(session, path, options);
FileTime lastModified = basic.lastModifiedTime();
FileTime lastAccess = basic.lastAccessTime();
String cmd = "T" + lastModified.to(TimeUnit.SECONDS) + " "
@@ -627,7 +632,7 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
validateAckReplyCode(cmd, path, readyCode, false);
}
- Set<PosixFilePermission> perms = opener.getLocalFilePermissions(path, options);
+ Set<PosixFilePermission> perms = opener.getLocalFilePermissions(session, path, options);
String octalPerms = ((!preserve) || GenericUtils.isEmpty(perms)) ? DEFAULT_DIR_OCTAL_PERMISSIONS : getOctalPermissions(perms);
String cmd = "D" + octalPerms + " " + "0" + " " + Objects.toString(path.getFileName(), null);
if (debugEnabled) {
@@ -644,15 +649,14 @@ public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Sess
}
validateAckReplyCode(cmd, path, readyCode, false);
- Session session = getSession();
- try (DirectoryStream<Path> children = opener.getLocalFolderChildren(path)) {
+ try (DirectoryStream<Path> children = opener.getLocalFolderChildren(session, path)) {
listener.startFolderEvent(session, FileOperation.SEND, path, perms);
try {
for (Path child : children) {
- if (opener.sendAsRegularFile(child, options)) {
+ if (opener.sendAsRegularFile(session, child, options)) {
sendFile(child, preserve, bufferSize);
- } else if (opener.sendAsDirectory(child, options)) {
+ } else if (opener.sendAsDirectory(session, child, options)) {
sendDir(child, preserve, bufferSize);
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/857172b7/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/DefaultScpFileOpener.java
----------------------------------------------------------------------
diff --git a/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/DefaultScpFileOpener.java b/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/DefaultScpFileOpener.java
index bb6ae3b..7516e8c 100644
--- a/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/DefaultScpFileOpener.java
+++ b/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/DefaultScpFileOpener.java
@@ -64,12 +64,12 @@ public class DefaultScpFileOpener extends AbstractLoggingBean implements ScpFile
}
@Override
- public ScpSourceStreamResolver createScpSourceStreamResolver(Path path) throws IOException {
+ public ScpSourceStreamResolver createScpSourceStreamResolver(Session session, Path path) throws IOException {
return new LocalFileScpSourceStreamResolver(path, this);
}
@Override
- public ScpTargetStreamResolver createScpTargetStreamResolver(Path path) throws IOException {
+ public ScpTargetStreamResolver createScpTargetStreamResolver(Session session, Path path) throws IOException {
return new LocalFileScpTargetStreamResolver(path, this);
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/857172b7/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/LocalFileScpSourceStreamResolver.java
----------------------------------------------------------------------
diff --git a/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/LocalFileScpSourceStreamResolver.java b/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/LocalFileScpSourceStreamResolver.java
index 8ce9b61..cdc47c6 100644
--- a/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/LocalFileScpSourceStreamResolver.java
+++ b/sshd-scp/src/main/java/org/apache/sshd/common/scp/helpers/LocalFileScpSourceStreamResolver.java
@@ -55,7 +55,8 @@ public class LocalFileScpSourceStreamResolver extends AbstractLoggingBean implem
this.name = path.getFileName();
this.perms = IoUtils.getPermissions(path);
- BasicFileAttributes basic = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
+ BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);
+ BasicFileAttributes basic = view.readAttributes();
this.size = basic.size();
this.time = new ScpTimestamp(basic.lastModifiedTime().toMillis(), basic.lastAccessTime().toMillis());
}