Repository: mina-sshd Updated Branches: refs/heads/master 2cd0ebbaf -> 0639c7c0b
[SSHD-429] Take into account local file separator when sending/receiving files via SCP Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/0639c7c0 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/0639c7c0 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/0639c7c0 Branch: refs/heads/master Commit: 0639c7c0b645db20529ab57299e030584d24cfdd Parents: 2cd0ebb Author: Guillaume Nodet <[email protected]> Authored: Wed Mar 18 11:57:27 2015 +0100 Committer: Guillaume Nodet <[email protected]> Committed: Wed Mar 18 11:57:27 2015 +0100 ---------------------------------------------------------------------- .../org/apache/sshd/common/scp/ScpHelper.java | 27 +++++++++++++++----- .../apache/sshd/server/command/ScpCommand.java | 2 +- .../src/test/java/org/apache/sshd/ScpTest.java | 20 ++++++++++----- 3 files changed, 34 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/0639c7c0/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java index 0583928..7c8897c 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java @@ -42,6 +42,7 @@ import java.util.concurrent.TimeUnit; import org.apache.sshd.common.SshException; import org.apache.sshd.common.scp.ScpTransferEventListener.FileOperation; import org.apache.sshd.common.util.DirectoryScanner; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.IoUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -265,10 +266,14 @@ public class ScpHelper { } else { throw new IOException("Can not write to " + path); } - if (Files.exists(file) && Files.isDirectory(file)) { - throw new IOException("File is a directory: " + file); - } else if (Files.exists(file) && !Files.isWritable(file)) { - throw new IOException("Can not write to file: " + file); + + if (Files.exists(file)) { + if (Files.isDirectory(file)) { + throw new IOException("File is a directory: " + file); + } + if (!Files.isWritable(file)) { + throw new IOException("Can not write to file: " + file); + } } try ( @@ -335,7 +340,7 @@ public class ScpHelper { } String[] included = new DirectoryScanner(basedir, pattern).scan(); for (String path : included) { - Path file = resolveLocalPath(basedir + "/" + path); + Path file = resolveLocalPath(basedir, path); if (Files.isRegularFile(file)) { sendFile(file, preserve, bufferSize); } else if (Files.isDirectory(file)) { @@ -357,7 +362,7 @@ public class ScpHelper { basedir = pattern.substring(0, lastSep); pattern = pattern.substring(lastSep + 1); } - Path file = resolveLocalPath(basedir + "/" + pattern); + Path file = resolveLocalPath(basedir, pattern); if (!Files.exists(file)) { throw new IOException(file + ": no such file or directory"); } @@ -376,7 +381,15 @@ public class ScpHelper { } } - protected Path resolveLocalPath(String path) { + public Path resolveLocalPath(String basedir, String subpath) { + if (GenericUtils.isEmpty(basedir)) { + return resolveLocalPath(subpath); + } else { + return resolveLocalPath(basedir + "/" + subpath); + } + } + + public Path resolveLocalPath(String path) { String localPath = (path == null) ? null : path.replace('/', File.separatorChar); return fileSystem.getPath(localPath); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/0639c7c0/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java b/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java index ade3866..f8d3635 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/command/ScpCommand.java @@ -212,7 +212,7 @@ public class ScpCommand implements Command, Runnable, FileSystemAware { ScpHelper helper = new ScpHelper(in, out, fileSystem, listener); try { if (optT) { - helper.receive(fileSystem.getPath(path), optR, optD, optP, receiveBufferSize); + helper.receive(helper.resolveLocalPath(path), optR, optD, optP, receiveBufferSize); } else if (optF) { helper.send(Collections.singletonList(path), optR, optP, sendBufferSize); } else { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/0639c7c0/sshd-core/src/test/java/org/apache/sshd/ScpTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java index 7b33ca1..f6b6ae2 100644 --- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java @@ -122,15 +122,21 @@ public class ScpTest extends BaseTest { File root = new File("target/scp"); Utils.deleteRecursive(root); - assertHierarchyTargetFolderExists(new File(root, "local")); + + File localDir = assertHierarchyTargetFolderExists(new File(root, "local")); assertTrue("Root folder not created", root.exists()); - writeFile(new File("target/scp/local/out.txt"), data); - assertHierarchyTargetFolderExists(new File(root, "remote")); - scp.upload(new File("target/scp/local/out.txt").getAbsolutePath(), "/" + new File("target/scp/remote/out.txt").getAbsolutePath().replace(File.separatorChar, '/')); - assertFileLength(new File("target/scp/remote/out.txt"), data.length(), 5000); - scp.upload(new File("target/scp/local/out.txt").getAbsolutePath(), new File("target/scp/remote/out2.txt").getAbsolutePath()); - assertFileLength(new File("target/scp/remote/out2.txt"), data.length(), 5000); + File localFile = new File(localDir, "out.txt"); + writeFile(localFile, data); + + File remoteDir = assertHierarchyTargetFolderExists(new File(root, "remote")); + File remoteFile = new File(remoteDir, "out.txt"); + scp.upload(localFile.getAbsolutePath(), remoteFile.getAbsolutePath().replace(File.separatorChar, '/')); + assertFileLength(remoteFile, data.length(), 5000); + + File secondRemote = new File(remoteDir, "out2.txt"); + scp.upload(localFile.getAbsolutePath(), secondRemote.getAbsolutePath().replace(File.separatorChar, '/')); + assertFileLength(secondRemote, data.length(), 5000); } } finally { client.stop();
