/** * Returns a single filename path with the given name. */ public JimfsPath createFileName(Name name) { return createPath(null, ImmutableList.of(name)); }
/** * Returns a relative path with the given names. */ public JimfsPath createRelativePath(Iterable<Name> names) { return createPath(null, ImmutableList.copyOf(names)); }
/** * Returns a root path with the given name. */ public JimfsPath createRoot(Name root) { return createPath(checkNotNull(root), ImmutableList.<Name>of()); }
@Override public JimfsPath getParent() { if (names.isEmpty() || (names.size() == 1 && root == null)) { return null; } return pathService.createPath(root, names.subList(0, names.size() - 1)); }
@Override public JimfsPath resolve(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } if (isEmptyPath() || otherPath.isAbsolute()) { return otherPath; } if (otherPath.isEmptyPath()) { return this; } return pathService.createPath( root, ImmutableList.<Name>builder() .addAll(names) .addAll(otherPath.names) .build()); }
@Override public JimfsPath normalize() { if (isNormal()) { return this; } Deque<Name> newNames = new ArrayDeque<>(); for (Name name : names) { if (name.equals(Name.PARENT)) { Name lastName = newNames.peekLast(); if (lastName != null && !lastName.equals(Name.PARENT)) { newNames.removeLast(); } else if (!isAbsolute()) { // if there's a root and we have an extra ".." that would go up above the root, ignore it newNames.add(name); } } else if (!name.equals(Name.SELF)) { newNames.add(name); } } return Iterables.elementsEqual(newNames, names) ? this : pathService.createPath(root, newNames); }
/** * Gets the {@linkplain Path#toRealPath(LinkOption...) real path} to the file located by the * given path. */ public JimfsPath toRealPath( JimfsPath path, PathService pathService, Set<? super LinkOption> options) throws IOException { checkNotNull(path); checkNotNull(options); store.readLock().lock(); try { DirectoryEntry entry = lookUp(path, options).requireExists(path); List<Name> names = new ArrayList<>(); names.add(entry.name()); while (!entry.file().isRootDirectory()) { entry = entry.directory().entryInParent(); names.add(entry.name()); } // names are ordered last to first in the list, so get the reverse view List<Name> reversed = Lists.reverse(names); Name root = reversed.remove(0); return pathService.createPath(root, reversed); } finally { store.readLock().unlock(); } }
private JimfsPath toPath(ParseResult parsed) { Name root = parsed.root() == null ? null : name(parsed.root()); Iterable<Name> names = names(parsed.names()); return createPath(root, names); }
/** * Returns a relative path with the given names. */ public JimfsPath createRelativePath(Iterable<Name> names) { return createPath(null, ImmutableList.copyOf(names)); }
/** * Returns a single filename path with the given name. */ public JimfsPath createFileName(Name name) { return createPath(null, ImmutableList.of(name)); }
/** * Returns a root path with the given name. */ public JimfsPath createRoot(Name root) { return createPath(checkNotNull(root), ImmutableList.<Name>of()); }
@Override public JimfsPath getParent() { if (names.isEmpty() || names.size() == 1 && root == null) { return null; } return pathService.createPath(root, names.subList(0, names.size() - 1)); }
@Override public JimfsPath normalize() { if (isNormal()) { return this; } Deque<Name> newNames = new ArrayDeque<>(); for (Name name : names) { if (name.equals(Name.PARENT)) { Name lastName = newNames.peekLast(); if (lastName != null && !lastName.equals(Name.PARENT)) { newNames.removeLast(); } else if (!isAbsolute()) { // if there's a root and we have an extra ".." that would go up above the root, ignore it newNames.add(name); } } else if (!name.equals(Name.SELF)) { newNames.add(name); } } return newNames.equals(names) ? this : pathService.createPath(root, newNames); }
@Override public JimfsPath resolve(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } if (isEmptyPath() || otherPath.isAbsolute()) { return otherPath; } if (otherPath.isEmptyPath()) { return this; } return pathService.createPath( root, ImmutableList.<Name>builder() .addAll(names) .addAll(otherPath.names) .build()); }
/** * Gets the {@linkplain Path#toRealPath(LinkOption...) real path} to the file located by the * given path. */ public JimfsPath toRealPath( JimfsPath path, PathService pathService, Set<? super LinkOption> options) throws IOException { checkNotNull(path); checkNotNull(options); store.readLock().lock(); try { DirectoryEntry entry = lookUp(path, options).requireExists(path); List<Name> names = new ArrayList<>(); names.add(entry.name()); while (!entry.file().isRootDirectory()) { entry = entry.directory().entryInParent(); names.add(entry.name()); } // names are ordered last to first in the list, so get the reverse view List<Name> reversed = Lists.reverse(names); Name root = reversed.remove(0); return pathService.createPath(root, reversed); } finally { store.readLock().unlock(); } }
private JimfsPath toPath(ParseResult parsed) { Name root = parsed.root() == null ? null : name(parsed.root()); Iterable<Name> names = names(parsed.names()); return createPath(root, names); }