aboutsummaryrefslogtreecommitdiff
path: root/jimfs
diff options
context:
space:
mode:
authorColin Decker <cgdecker@google.com>2013-10-09 14:55:21 -0400
committerColin Decker <cgdecker@google.com>2013-10-09 14:55:21 -0400
commita78183e6610ccba9a85eb29f6e52cf348b2b70e4 (patch)
tree2e9ef8a01f1cd7993d7373fb2b5b0a7871826f75 /jimfs
parent81c21f4152ff23a0548da1e35bb31750a3154ad6 (diff)
downloadjimfs-a78183e6610ccba9a85eb29f6e52cf348b2b70e4.tar.gz
Fix lookup of a root directory that doesn't exist to always throw NoSuchFileException.
This prevents creation of a new root directory (or file!) using Files.createDirectory etc. in file systems that support multiple roots.
Diffstat (limited to 'jimfs')
-rw-r--r--jimfs/src/main/java/com/google/jimfs/internal/FileTree.java7
-rw-r--r--jimfs/src/test/java/com/google/jimfs/JimfsWindowsLikeIntegrationTest.java57
-rw-r--r--jimfs/src/test/java/com/google/jimfs/internal/FileTreeTest.java25
3 files changed, 80 insertions, 9 deletions
diff --git a/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java b/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java
index 044cbcf..d3eaa55 100644
--- a/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java
+++ b/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java
@@ -98,10 +98,9 @@ final class FileTree {
// lookup the root directory
DirectoryEntry entry = superRoot.asDirectoryTable().get(path.root());
if (entry == null) {
- // root not found
- return !names.isEmpty()
- ? null
- : new DirectoryEntry(superRoot, path.root(), null);
+ // root not found; always return null as no real parent directory exists
+ // this prevents new roots from being created in file systems supporting multiple roots
+ return null;
} else if (names.isEmpty()) {
// root found, no more names to look up
return entry;
diff --git a/jimfs/src/test/java/com/google/jimfs/JimfsWindowsLikeIntegrationTest.java b/jimfs/src/test/java/com/google/jimfs/JimfsWindowsLikeIntegrationTest.java
index e09e641..362e873 100644
--- a/jimfs/src/test/java/com/google/jimfs/JimfsWindowsLikeIntegrationTest.java
+++ b/jimfs/src/test/java/com/google/jimfs/JimfsWindowsLikeIntegrationTest.java
@@ -274,4 +274,61 @@ public class JimfsWindowsLikeIntegrationTest extends AbstractJimfsIntegrationTes
} catch (UnsupportedOperationException expected) {
}
}
+
+ @Test
+ public void testCreateFileOrDirectory_forRootPath_fails() throws IOException {
+ try {
+ Files.createDirectory(path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+
+ try {
+ Files.createFile(path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+
+ try {
+ Files.createSymbolicLink(path("Z:\\"), path("foo"));
+ fail();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void testCopyFile_toRootPath_fails() throws IOException {
+ Files.createFile(path("foo"));
+ Files.createDirectory(path("bar"));
+
+ try {
+ Files.copy(path("foo"), path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+
+ try {
+ Files.copy(path("bar"), path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
+ public void testMoveFile_toRootPath_fails() throws IOException {
+ Files.createFile(path("foo"));
+ Files.createDirectory(path("bar"));
+
+ try {
+ Files.move(path("foo"), path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+
+ try {
+ Files.move(path("bar"), path("Z:\\"));
+ fail();
+ } catch (IOException expected) {
+ }
+ }
}
diff --git a/jimfs/src/test/java/com/google/jimfs/internal/FileTreeTest.java b/jimfs/src/test/java/com/google/jimfs/internal/FileTreeTest.java
index e390021..4e66e7d 100644
--- a/jimfs/src/test/java/com/google/jimfs/internal/FileTreeTest.java
+++ b/jimfs/src/test/java/com/google/jimfs/internal/FileTreeTest.java
@@ -69,8 +69,8 @@ public class FileTreeTest {
*/
/**
- * This path service is for unix-like paths, with the exception that it recognizes $ as a root in
- * addition to /, allowing for two roots.
+ * This path service is for unix-like paths, with the exception that it recognizes $ and ! as
+ * roots in addition to /, allowing for up to three roots.
*/
private final PathService pathService = fakePathService(
new PathType(CaseSensitivity.CASE_SENSITIVE, true, '/') {
@@ -82,7 +82,7 @@ public class FileTreeTest {
@Override
public ParseResult parsePath(String path) {
String root = null;
- if (path.startsWith("/") || path.startsWith("$")) {
+ if (path.matches("^[/$!].*")) {
root = path.substring(0, 1);
path = path.substring(1);
}
@@ -103,8 +103,8 @@ public class FileTreeTest {
@Override
public ParseResult parseUriPath(String uriPath) {
- checkArgument(uriPath.startsWith("//") || uriPath.startsWith("/$"),
- "uriPath (%s) must start with // or /$");
+ checkArgument(uriPath.matches("^/[/$!].*"),
+ "uriPath (%s) must start with // or /$ or /!");
return parsePath(uriPath.substring(1)); // skip leading /
}
});
@@ -163,6 +163,21 @@ public class FileTreeTest {
}
@Test
+ public void testLookup_nonExistentRoot() throws IOException {
+ try {
+ lookup("!");
+ fail();
+ } catch (NoSuchFileException expected) {
+ }
+
+ try {
+ lookup("!a");
+ fail();
+ } catch (NoSuchFileException expected) {
+ }
+ }
+
+ @Test
public void testLookup_absolute() throws IOException {
assertExists(lookup("/work"), "/", "work");
assertExists(lookup("/work/one/two/three"), "two", "three");