diff options
author | Colin Decker <cgdecker@google.com> | 2013-10-09 14:21:36 -0400 |
---|---|---|
committer | Colin Decker <cgdecker@google.com> | 2013-10-09 14:21:36 -0400 |
commit | 81c21f4152ff23a0548da1e35bb31750a3154ad6 (patch) | |
tree | 2290ecb26e8cc3d9473374e5dc3eeb318fd6843d | |
parent | 823c40e6eab921b3bc44cf56221139696defbd63 (diff) | |
download | jimfs-81c21f4152ff23a0548da1e35bb31750a3154ad6.tar.gz |
Add special lookup handling for root dirs.
-rw-r--r-- | jimfs/src/main/java/com/google/jimfs/internal/FileTree.java | 53 | ||||
-rw-r--r-- | jimfs/src/main/java/com/google/jimfs/internal/JimfsPath.java | 26 |
2 files changed, 26 insertions, 53 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 a5d8825..044cbcf 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/FileTree.java @@ -38,6 +38,8 @@ final class FileTree { private static final int MAX_SYMBOLIC_LINK_DEPTH = 10; + private static final ImmutableList<Name> EMPTY_PATH_NAMES = ImmutableList.of(Name.SELF); + /** * Special directory linking root names to root directories. */ @@ -80,19 +82,7 @@ final class FileTree { checkNotNull(path); checkNotNull(options); - File base; - Iterable<Name> names = path.path(); - if (path.isAbsolute()) { - base = superRoot; - } else { - base = workingDirectory; - if (isEmpty(path)) { - // empty path is equivalent to "." in a lookup - names = ImmutableList.of(Name.SELF); - } - } - - DirectoryEntry result = lookup(base, names, options, 0); + DirectoryEntry result = lookup(workingDirectory, path, options, 0); if (result == null) { // an intermediate file in the path did not exist or was not a directory throw new NoSuchFileException(path.toString()); @@ -102,17 +92,35 @@ final class FileTree { private DirectoryEntry lookup( File dir, JimfsPath path, LinkOptions options, int linkDepth) throws IOException { - Iterable<Name> names = path.path(); + ImmutableList<Name> names = path.names(); + if (path.isAbsolute()) { - dir = superRoot; - } else if (isEmpty(path)) { - // empty path is equivalent to "." in a lookup - names = ImmutableList.of(Name.SELF); + // 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); + } else if (names.isEmpty()) { + // root found, no more names to look up + return entry; + } else { + // root found, more names to look up; set dir to the root directory for the path + dir = entry.file(); + } + } else if (isEmpty(names)) { + // set names to the canonical list of names for an empty path (singleton list of ".") + names = EMPTY_PATH_NAMES; } return lookup(dir, names, options, linkDepth); } + private boolean isEmpty(ImmutableList<Name> names) { + return names.isEmpty() || names.size() == 1 && names.get(0).toString().equals(""); + } + /** * Looks up the given names against the given base file. If the file does not exist ({@code dir} * is null) or is not a directory, the lookup fails. @@ -213,13 +221,4 @@ final class FileTree { return null; } - - /** - * Returns true if path has no root component (is not absolute) and either has no name components - * or only has a single name component, the empty string. - */ - private static boolean isEmpty(JimfsPath path) { - return !path.isAbsolute() && (path.getNameCount() == 0 - || path.getNameCount() == 1 && path.getName(0).toString().equals("")); - } } diff --git a/jimfs/src/main/java/com/google/jimfs/internal/JimfsPath.java b/jimfs/src/main/java/com/google/jimfs/internal/JimfsPath.java index 7a3d2fb..5f63ae8 100644 --- a/jimfs/src/main/java/com/google/jimfs/internal/JimfsPath.java +++ b/jimfs/src/main/java/com/google/jimfs/internal/JimfsPath.java @@ -112,32 +112,6 @@ final class JimfsPath implements Path, FileContent { } /** - * Returns a view of this path as an immutable iterable of name objects (including the root). - */ - public Iterable<Name> path() { - return new Iterable<Name>() { - @Override - public Iterator<Name> iterator() { - return new AbstractIterator<Name>() { - private Iterator<Name> nameIterator; - - @Override - protected Name computeNext() { - if (nameIterator == null) { - nameIterator = names.iterator(); - if (root != null) { - return root; - } - } - - return nameIterator.hasNext() ? nameIterator.next() : endOfData(); - } - }; - } - }; - } - - /** * Returns whether or not this is the empty path, with no root and a single, empty string, name. */ public boolean isEmptyPath() { |