Package com.google.common.jimfs
Class FileSystemView
java.lang.Object
com.google.common.jimfs.FileSystemView
View of a file system with a specific working directory. As all file system operations need to
work when given either relative or absolute paths, this class contains the implementation of most
file system operations, with relative path operations resolving against the working directory.
A file system has one default view using the file system's working directory. Additional views
may be created for use in SecureDirectoryStream
instances, which each have a different
working directory they use.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final JimfsFileStore
private final Directory
private final JimfsPath
-
Constructor Summary
ConstructorsConstructorDescriptionFileSystemView
(JimfsFileStore store, Directory workingDirectory, JimfsPath workingDirectoryPath) Creates a new file system view. -
Method Summary
Modifier and TypeMethodDescriptionvoid
checkAccess
(JimfsPath path) Checks access to the file at the given path for the given modes.private void
checkDeletable
(File file, FileSystemView.DeleteMode mode, Path path) Checks that the given file can be deleted, throwing an exception if it can't.private void
checkEmpty
(Directory dir, Path pathForException) Checks that given directory is empty, throwingDirectoryNotEmptyException
if not.private void
checkMovable
(File file, JimfsPath path) private void
checkNotAncestor
(File source, Directory destParent, FileSystemView destView) Checks that source is not an ancestor of dest, throwing an exception if it is.void
copy
(JimfsPath source, FileSystemView destView, JimfsPath dest, Set<CopyOption> options, boolean move) Copies or moves the file at the given source path to the given dest path.createDirectory
(JimfsPath path, FileAttribute<?>... attrs) Creates a new directory at the given path.private File
createFile
(JimfsPath path, com.google.common.base.Supplier<? extends File> fileCreator, boolean failIfExists, FileAttribute<?>... attrs) Creates a new file at the given path if possible, using the given supplier to create the file.createSymbolicLink
(JimfsPath path, JimfsPath target, FileAttribute<?>... attrs) Creates a new symbolic link at the given path with the given target.private void
delete
(DirectoryEntry entry, FileSystemView.DeleteMode deleteMode, JimfsPath pathForException) Deletes the given directory entry from its parent directory.void
deleteFile
(JimfsPath path, FileSystemView.DeleteMode deleteMode) Deletes the file at the given absolute path.<V extends FileAttributeView>
@Nullable VgetFileAttributeView
(FileLookup lookup, Class<V> type) Returns a file attribute view using the given lookup callback.<V extends FileAttributeView>
@Nullable VgetFileAttributeView
(JimfsPath path, Class<V> type, Set<? super LinkOption> options) Returns a file attribute view for the given path in this view.getOrCreateRegularFile
(JimfsPath path, Set<OpenOption> options, FileAttribute<?>... attrs) Gets the regular file at the given path, creating it if it doesn't exist and the given options specify that it should be created.private RegularFile
getOrCreateRegularFileWithWriteLock
(JimfsPath path, Set<OpenOption> options, FileAttribute<?>[] attrs) Gets or creates a new regular file with a write lock (assuming the file does not exist).Returns the path of the working directory at the time this view was created.boolean
isSameFile
(JimfsPath path, FileSystemView view2, JimfsPath path2) Returns whether or not the two given paths locate the same file.private boolean
isSameFileSystem
(FileSystemView other) Returns whether or not this view and the given view belong to the same file system.void
link
(JimfsPath link, FileSystemView existingView, JimfsPath existing) Creates a hard link at the given link path to the regular file at the given path.private static void
Acquires both write locks in a way that attempts to avoid the possibility of deadlock.private void
lockSourceAndCopy
(File sourceFile, File copyFile) Locks source and copy files before copying content.private DirectoryEntry
lookUp
(JimfsPath path, Set<? super LinkOption> options) Looks up the file at the given path without locking.private @Nullable RegularFile
lookUpRegularFile
(JimfsPath path, Set<OpenOption> options) Looks up the regular file at the given path, throwing an exception if the file isn't a regular file.(package private) DirectoryEntry
lookUpWithLock
(JimfsPath path, Set<? super LinkOption> options) Attempt to look up the file at the given path.newDirectoryStream
(JimfsPath dir, DirectoryStream.Filter<? super Path> filter, Set<? super LinkOption> options, JimfsPath basePathForStream) Creates a new directory stream for the directory located by the given path.private FileTime
now()
private static RegularFile
open
(RegularFile file, Set<OpenOption> options) Opens the given regular file with the given options, truncating it if necessary and incrementing its open count.<A extends BasicFileAttributes>
AreadAttributes
(JimfsPath path, Class<A> type, Set<? super LinkOption> options) Reads attributes of the file located by the given path in this view as an object.readAttributes
(JimfsPath path, String attributes, Set<? super LinkOption> options) Reads attributes of the file located by the given path in this view as a map.readSymbolicLink
(JimfsPath path) Returns the target of the symbolic link at the given path.void
setAttribute
(JimfsPath path, String attribute, Object value, Set<? super LinkOption> options) Sets the given attribute to the given value on the file located by the given path in this view.Returns a snapshot mapping the names of each file in the directory at the given path to the last modified time of that file.com.google.common.collect.ImmutableSortedSet
<Name> Snapshots the entries of the working directory of this view.state()
Returns the file system state.toRealPath
(JimfsPath path, PathService pathService, Set<? super LinkOption> options) Gets the real path to the file located by the given path.private void
unlockSourceAndCopy
(File sourceFile, File copyFile) Unlocks source and copy files after copying content.
-
Field Details
-
store
-
workingDirectory
-
workingDirectoryPath
-
-
Constructor Details
-
FileSystemView
public FileSystemView(JimfsFileStore store, Directory workingDirectory, JimfsPath workingDirectoryPath) Creates a new file system view.
-
-
Method Details
-
isSameFileSystem
Returns whether or not this view and the given view belong to the same file system. -
state
Returns the file system state. -
now
-
getWorkingDirectoryPath
Returns the path of the working directory at the time this view was created. Does not reflect changes to the path caused by the directory being moved. -
lookUpWithLock
Attempt to look up the file at the given path.- Throws:
IOException
-
lookUp
Looks up the file at the given path without locking.- Throws:
IOException
-
newDirectoryStream
public DirectoryStream<Path> newDirectoryStream(JimfsPath dir, DirectoryStream.Filter<? super Path> filter, Set<? super LinkOption> options, JimfsPath basePathForStream) throws IOException Creates a new directory stream for the directory located by the given path. The givenbasePathForStream
is that base path that the returned stream will use. This will be the same asdir
except for streams created relative to another secure stream.- Throws:
IOException
-
snapshotWorkingDirectoryEntries
Snapshots the entries of the working directory of this view. -
snapshotModifiedTimes
public com.google.common.collect.ImmutableMap<Name,FileTime> snapshotModifiedTimes(JimfsPath path) throws IOException Returns a snapshot mapping the names of each file in the directory at the given path to the last modified time of that file.- Throws:
IOException
-
isSameFile
Returns whether or not the two given paths locate the same file. The second path is located using the given view rather than this file view.- Throws:
IOException
-
toRealPath
public JimfsPath toRealPath(JimfsPath path, PathService pathService, Set<? super LinkOption> options) throws IOException Gets the real path to the file located by the given path.- Throws:
IOException
-
createDirectory
@CanIgnoreReturnValue public Directory createDirectory(JimfsPath path, FileAttribute<?>... attrs) throws IOException Creates a new directory at the given path. The given attributes will be set on the new file if possible.- Throws:
IOException
-
createSymbolicLink
@CanIgnoreReturnValue public SymbolicLink createSymbolicLink(JimfsPath path, JimfsPath target, FileAttribute<?>... attrs) throws IOException Creates a new symbolic link at the given path with the given target. The given attributes will be set on the new file if possible.- Throws:
IOException
-
createFile
private File createFile(JimfsPath path, com.google.common.base.Supplier<? extends File> fileCreator, boolean failIfExists, FileAttribute<?>... attrs) throws IOException Creates a new file at the given path if possible, using the given supplier to create the file. Returns the new file. IfallowExisting
istrue
and a file already exists at the given path, returns that file. Otherwise, throwsFileAlreadyExistsException
.- Throws:
IOException
-
getOrCreateRegularFile
public RegularFile getOrCreateRegularFile(JimfsPath path, Set<OpenOption> options, FileAttribute<?>... attrs) throws IOException Gets the regular file at the given path, creating it if it doesn't exist and the given options specify that it should be created.- Throws:
IOException
-
lookUpRegularFile
private @Nullable RegularFile lookUpRegularFile(JimfsPath path, Set<OpenOption> options) throws IOException Looks up the regular file at the given path, throwing an exception if the file isn't a regular file. Returns null if the file did not exist.- Throws:
IOException
-
getOrCreateRegularFileWithWriteLock
private RegularFile getOrCreateRegularFileWithWriteLock(JimfsPath path, Set<OpenOption> options, FileAttribute<?>[] attrs) throws IOException Gets or creates a new regular file with a write lock (assuming the file does not exist).- Throws:
IOException
-
open
Opens the given regular file with the given options, truncating it if necessary and incrementing its open count. Returns the given file. -
readSymbolicLink
Returns the target of the symbolic link at the given path.- Throws:
IOException
-
checkAccess
Checks access to the file at the given path for the given modes. Since access controls are not implemented for this file system, this just checks that the file exists.- Throws:
IOException
-
link
public void link(JimfsPath link, FileSystemView existingView, JimfsPath existing) throws IOException Creates a hard link at the given link path to the regular file at the given path. The existing file must exist and must be a regular file. The given file system view must belong to the same file system as this view.- Throws:
IOException
-
deleteFile
Deletes the file at the given absolute path.- Throws:
IOException
-
delete
private void delete(DirectoryEntry entry, FileSystemView.DeleteMode deleteMode, JimfsPath pathForException) throws IOException Deletes the given directory entry from its parent directory.- Throws:
IOException
-
checkDeletable
private void checkDeletable(File file, FileSystemView.DeleteMode mode, Path path) throws IOException Checks that the given file can be deleted, throwing an exception if it can't.- Throws:
IOException
-
checkEmpty
Checks that given directory is empty, throwingDirectoryNotEmptyException
if not.- Throws:
FileSystemException
-
copy
public void copy(JimfsPath source, FileSystemView destView, JimfsPath dest, Set<CopyOption> options, boolean move) throws IOException Copies or moves the file at the given source path to the given dest path.- Throws:
IOException
-
checkMovable
- Throws:
FileSystemException
-
lockBoth
Acquires both write locks in a way that attempts to avoid the possibility of deadlock. Note that typically (when only one file system instance is involved), both locks will be the same lock and there will be no issue at all. -
checkNotAncestor
private void checkNotAncestor(File source, Directory destParent, FileSystemView destView) throws IOException Checks that source is not an ancestor of dest, throwing an exception if it is.- Throws:
IOException
-
lockSourceAndCopy
Locks source and copy files before copying content. Also marks the source file as opened so that its content won't be deleted until after the copy if it is deleted. -
unlockSourceAndCopy
Unlocks source and copy files after copying content. Also closes the source file so its content can be deleted if it was deleted. -
getFileAttributeView
public <V extends FileAttributeView> @Nullable V getFileAttributeView(FileLookup lookup, Class<V> type) Returns a file attribute view using the given lookup callback. -
getFileAttributeView
public <V extends FileAttributeView> @Nullable V getFileAttributeView(JimfsPath path, Class<V> type, Set<? super LinkOption> options) Returns a file attribute view for the given path in this view. -
readAttributes
public <A extends BasicFileAttributes> A readAttributes(JimfsPath path, Class<A> type, Set<? super LinkOption> options) throws IOException Reads attributes of the file located by the given path in this view as an object.- Throws:
IOException
-
readAttributes
public com.google.common.collect.ImmutableMap<String,Object> readAttributes(JimfsPath path, String attributes, Set<? super LinkOption> options) throws IOException Reads attributes of the file located by the given path in this view as a map.- Throws:
IOException
-
setAttribute
public void setAttribute(JimfsPath path, String attribute, Object value, Set<? super LinkOption> options) throws IOException Sets the given attribute to the given value on the file located by the given path in this view.- Throws:
IOException
-