diff --git a/pom.xml b/pom.xml
index 0214f42..17b397a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -356,6 +356,11 @@
junit
4.12
+
+ org.hamcrest
+ hamcrest-library
+ 1.3
+
diff --git a/sftp/pom.xml b/sftp/pom.xml
index 7a7af74..be4b62d 100644
--- a/sftp/pom.xml
+++ b/sftp/pom.xml
@@ -28,6 +28,11 @@
junit
test
+
+ org.hamcrest
+ hamcrest-library
+ test
+
com.jcraft
jsch
diff --git a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPFileSystemProvider.java b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPFileSystemProvider.java
index e8a4fed..a53dec8 100644
--- a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPFileSystemProvider.java
+++ b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPFileSystemProvider.java
@@ -121,12 +121,14 @@ public void createDirectory(Path dir, FileAttribute>... attrs) throws IOExcept
sftp.connect();
- SFTPPath sftpPath = (SFTPPath)dir;
- String dirString = sftpPath.getPathString();
- try {
- sftp.mkdir(dirString);
- } catch(SftpException e) {
- throw new IOException(dirString, e);
+ // Implementation might not support recursive creation of directories
+ for (String subPath : ((SFTPPath) dir).getParts()) {
+ try {
+ sftp.mkdir(subPath);
+ } catch(SftpException e) {
+ throw new IOException(subPath, e);
+ }
+
}
sftp.quit();
diff --git a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java
index 3517dcc..8f0d7a8 100644
--- a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java
+++ b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java
@@ -1,5 +1,7 @@
package no.maddin.niofs.sftp;
+import sun.nio.fs.UnixFileSystemProvider;
+
import java.io.File;
import java.io.IOException;
import java.net.URI;
@@ -7,36 +9,38 @@
import java.nio.file.*;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Queue;
-import java.util.StringTokenizer;
+import java.util.*;
/**
* A Path implementation for SFTP.
*/
public class SFTPPath implements Path {
- private static final String HOME_PREFIX = "/~/";
- private static final int HOME_PREFIX_LEN = HOME_PREFIX.length();
- private static final String DEFAULT_ROOT_PATH = "";
private static final String PATH_SEP = "/";
private final String path;
private final SFTPHost host;
+ private final java.util.List parts;
SFTPPath(SFTPHost sftpHost, String path) {
this.host = sftpHost;
+ this.path = path;
+ parts = splitParts(path);
+ }
+
+ private List splitParts(String path) {
+ String[] parts = path.split(PATH_SEP, -1);
+ return Arrays.asList(parts);
+ }
- // TODO split the path in ist components
- if (path == null || path.trim().isEmpty()) {
- this.path = DEFAULT_ROOT_PATH;
- } else {
- if (path.startsWith(HOME_PREFIX)) {
- this.path = path.substring(HOME_PREFIX_LEN);
- } else {
- this.path = path;
+ private String combineParts(int startIdx, int endIdx) {
+ StringBuilder sb = new StringBuilder();
+ for (String part : parts.subList(startIdx, endIdx)) {
+ if (sb.length() > 0) {
+ sb.append(PATH_SEP);
}
+ sb.append(part);
}
+ return sb.toString();
}
@Override
@@ -51,10 +55,10 @@ public boolean isAbsolute() {
@Override
public Path getRoot() {
- if (path.equals(DEFAULT_ROOT_PATH)) {
+ if (path == null) {
return this;
}
- return new SFTPPath(this.host, DEFAULT_ROOT_PATH);
+ return new SFTPPath(this.host, null);
}
@Override
@@ -64,12 +68,17 @@ public Path getFileName() {
@Override
public Path getParent() {
- throw new UnsupportedOperationException("Not Implemented");
+
+ if (path == null) {
+ return null;
+ }
+ return new SFTPPath(this.host, combineParts(0, getNameCount() - 1));
}
@Override
public int getNameCount() {
- throw new UnsupportedOperationException("Not Implemented");
+
+ return parts.size();
}
@Override
@@ -79,7 +88,7 @@ public Path getName(int index) {
@Override
public Path subpath(int beginIndex, int endIndex) {
- throw new UnsupportedOperationException("Not Implemented");
+ return new SFTPPath(beginIndex == 0 ? host : null, combineParts(0, endIndex));
}
@Override
@@ -141,11 +150,14 @@ public Path relativize(Path other) {
public URI toUri() {
try {
- String userInfo;
+ String userInfo = null;
if (host.getUserName() != null) {
- userInfo = host.getUserName() + ':' + host.getPassword();
- } else {
- userInfo = null;
+ StringBuilder uinfoSb = new StringBuilder();
+ uinfoSb.append(host.getUserName());
+ if (host.getPassword() != null) {
+ uinfoSb.append(':').append(host.getPassword());
+ }
+ userInfo = uinfoSb.toString();
}
return new URI("sftp", userInfo, host.getHost(), host.getPort(), this.path, null, null);
} catch (URISyntaxException e) {
@@ -191,4 +203,8 @@ public int compareTo(Path other) {
String getPathString() {
return this.path;
}
+
+ List getParts() {
+ return Collections.unmodifiableList(parts);
+ }
}
\ No newline at end of file
diff --git a/sftp/src/test/java/no/maddin/niofs/sftp/PartsTest.java b/sftp/src/test/java/no/maddin/niofs/sftp/PartsTest.java
new file mode 100644
index 0000000..1f0e8a1
--- /dev/null
+++ b/sftp/src/test/java/no/maddin/niofs/sftp/PartsTest.java
@@ -0,0 +1,56 @@
+package no.maddin.niofs.sftp;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests splitting Path parts.
+ */
+@RunWith(Parameterized.class)
+public class PartsTest {
+
+ private final String input;
+ private final List result;
+
+ @Parameterized.Parameters(name = "{index}: {0} {1}")
+ public static List