You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/03/17 15:09:34 UTC

[2/2] git commit: [SSHD-293] SSH_FXP_SYMLINK not supported in SftpSubsystem

[SSHD-293] SSH_FXP_SYMLINK not supported in SftpSubsystem

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

Branch: refs/heads/master
Commit: a2e38bb575057d3d2a31944653ad8773a15180d7
Parents: 290554d
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Mar 17 14:50:10 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Mar 17 14:50:10 2014 +0100

----------------------------------------------------------------------
 .../org/apache/sshd/common/file/SshFile.java    |  2 ++
 .../common/file/nativefs/NativeSshFile.java     |  4 +++
 .../common/file/nativefs/NativeSshFileNio.java  |  9 +++++++
 .../apache/sshd/server/sftp/SftpSubsystem.java  | 16 +++++++++++
 .../src/test/java/org/apache/sshd/SftpTest.java | 28 ++++++++++++++++++++
 5 files changed, 59 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java b/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
index 0b204c8..df14bb6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/SshFile.java
@@ -90,6 +90,8 @@ public interface SshFile {
 
     String readSymbolicLink() throws IOException;
 
+    void createSymbolicLink(SshFile destination) throws IOException;
+
 
     /**
      * Get the owner name of the file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
index 5d2acbe..eef3a73 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFile.java
@@ -648,4 +648,8 @@ public class NativeSshFile implements SshFile {
     public String readSymbolicLink() throws IOException {
         throw new UnsupportedOperationException();
     }
+
+    public void createSymbolicLink(SshFile destination) throws IOException {
+        throw new UnsupportedOperationException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
index bc83a97..c4cde42 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/file/nativefs/NativeSshFileNio.java
@@ -26,6 +26,7 @@ import java.nio.channels.FileChannel;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.attribute.FileTime;
 import java.nio.file.attribute.GroupPrincipal;
 import java.nio.file.attribute.PosixFilePermission;
@@ -37,6 +38,8 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.sshd.common.file.SshFile;
+
 /**
  * <strong>Internal class, do not use directly.</strong>
  * 
@@ -108,6 +111,12 @@ public class NativeSshFileNio extends NativeSshFile {
         return link.toString();
     }
 
+    public void createSymbolicLink(SshFile destination) throws IOException {
+        Path link = file.toPath();
+        Path target = Paths.get(destination.getAbsolutePath());
+        Files.createSymbolicLink(target, link);
+    }
+
     private EnumSet<Permission> fromPerms(Set<PosixFilePermission> perms) {
         EnumSet<Permission> p = EnumSet.noneOf(Permission.class);
         for (PosixFilePermission perm : perms) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
index f96e4b2..df56bd4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
@@ -770,6 +770,22 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
                 }
                 break;
             }
+            case SSH_FXP_SYMLINK: {
+                String linkpath = buffer.getString();
+                String targetpath = buffer.getString();
+                log.debug("Received SSH_FXP_SYMLINK (linkpath={}, targetpath={})", linkpath, targetpath);
+                try {
+                    SshFile link = resolveFile(linkpath);
+                    SshFile target = resolveFile(targetpath);
+                    link.createSymbolicLink(target);
+                    sendStatus(id, SSH_FX_OK, "");
+                } catch (UnsupportedOperationException e) {
+                    sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command " + type + " is unsupported or not implemented");
+                } catch (IOException e) {
+                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
+                }
+                break;
+            }
             default: {
                 log.error("Received: {}", type);
                 sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command " + type + " is unsupported or not implemented");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a2e38bb5/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index 0d687e9..256fa85 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -235,6 +235,34 @@ public class SftpTest extends BaseTest {
         System.out.println(real);
     }
 
+    @Test
+    public void testCreateSymbolicLink() throws Exception {
+        File root = new File("target/sftp");
+        String unixPath = "target/sftp/out.txt";
+        String linkUnixPath = "target/sftp/link.txt";
+        File target = new File(unixPath);
+        File link = new File(linkUnixPath);
+        Utils.deleteRecursive(root);
+        root.mkdirs();
+        assertTrue(root.exists());
+
+        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
+        c.connect();
+        c.put(new ByteArrayInputStream("0123456789".getBytes()), unixPath);
+
+        assertTrue(target.exists());
+        assertEquals("0123456789", readFile(unixPath));
+
+        c.symlink(unixPath, linkUnixPath);
+
+        assertTrue(link.exists());
+        assertEquals("0123456789", readFile(linkUnixPath));
+
+        String str1 = c.readlink(linkUnixPath);
+        String str2 = c.realpath(unixPath);
+        assertEquals(str1, str2);
+    }
+
     protected void assertFileLength(File file, long length, long timeout) throws Exception {
         boolean ok = false;
         while (timeout > 0) {