Package io.roastedroot.zerofs
Class FileSystemView
java.lang.Object
io.roastedroot.zerofs.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 ZeroFsFileStoreprivate final Directoryprivate final ZeroFsPath -
Constructor Summary
ConstructorsConstructorDescriptionFileSystemView(ZeroFsFileStore store, Directory workingDirectory, ZeroFsPath workingDirectoryPath) Creates a new file system view. -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckAccess(ZeroFsPath path) Checks access to the file at the given path for the given modes.private voidcheckDeletable(File file, FileSystemView.DeleteMode mode, Path path) Checks that the given file can be deleted, throwing an exception if it can't.private voidcheckEmpty(Directory dir, Path pathForException) Checks that given directory is empty, throwingDirectoryNotEmptyExceptionif not.private voidcheckMovable(File file, ZeroFsPath path) private voidcheckNotAncestor(File source, Directory destParent, FileSystemView destView) Checks that source is not an ancestor of dest, throwing an exception if it is.voidcopy(ZeroFsPath source, FileSystemView destView, ZeroFsPath dest, Set<CopyOption> options, boolean move) Copies or moves the file at the given source path to the given dest path.createDirectory(ZeroFsPath path, FileAttribute<?>... attrs) Creates a new directory at the given path.private FilecreateFile(ZeroFsPath path, 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(ZeroFsPath path, ZeroFsPath target, FileAttribute<?>... attrs) Creates a new symbolic link at the given path with the given target.private voiddelete(DirectoryEntry entry, FileSystemView.DeleteMode deleteMode, ZeroFsPath pathForException) Deletes the given directory entry from its parent directory.voiddeleteFile(ZeroFsPath path, FileSystemView.DeleteMode deleteMode) Deletes the file at the given absolute path.<V extends FileAttributeView>
VgetFileAttributeView(FileLookup lookup, Class<V> type) Returns a file attribute view using the given lookup callback.<V extends FileAttributeView>
VgetFileAttributeView(ZeroFsPath path, Class<V> type, Set<? super LinkOption> options) Returns a file attribute view for the given path in this view.getOrCreateRegularFile(ZeroFsPath 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 RegularFilegetOrCreateRegularFileWithWriteLock(ZeroFsPath 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.booleanisSameFile(ZeroFsPath path, FileSystemView view2, ZeroFsPath path2) Returns whether or not the two given paths locate the same file.private booleanisSameFileSystem(FileSystemView other) Returns whether or not this view and the given view belong to the same file system.voidlink(ZeroFsPath link, FileSystemView existingView, ZeroFsPath existing) Creates a hard link at the given link path to the regular file at the given path.private static voidAcquires both write locks in a way that attempts to avoid the possibility of deadlock.private voidlockSourceAndCopy(File sourceFile, File copyFile) Locks source and copy files before copying content.private DirectoryEntrylookUp(ZeroFsPath path, Set<? super LinkOption> options) Looks up the file at the given path without locking.private RegularFilelookUpRegularFile(ZeroFsPath 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) DirectoryEntrylookUpWithLock(ZeroFsPath path, Set<? super LinkOption> options) Attempt to look up the file at the given path.newDirectoryStream(ZeroFsPath dir, DirectoryStream.Filter<? super Path> filter, Set<? super LinkOption> options, ZeroFsPath basePathForStream) Creates a new directory stream for the directory located by the given path.private FileTimenow()private static RegularFileopen(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(ZeroFsPath 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(ZeroFsPath path, String attributes, Set<? super LinkOption> options) Reads attributes of the file located by the given path in this view as a map.readSymbolicLink(ZeroFsPath path) Returns the target of the symbolic link at the given path.voidsetAttribute(ZeroFsPath 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.Snapshots the entries of the working directory of this view.state()Returns the file system state.toRealPath(ZeroFsPath path, PathService pathService, Set<? super LinkOption> options) Gets the real path to the file located by the given path.private voidunlockSourceAndCopy(File sourceFile, File copyFile) Unlocks source and copy files after copying content.
-
Field Details
-
store
-
workingDirectory
-
workingDirectoryPath
-
-
Constructor Details
-
FileSystemView
public FileSystemView(ZeroFsFileStore store, Directory workingDirectory, ZeroFsPath 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(ZeroFsPath dir, DirectoryStream.Filter<? super Path> filter, Set<? super LinkOption> options, ZeroFsPath basePathForStream) throws IOException Creates a new directory stream for the directory located by the given path. The givenbasePathForStreamis that base path that the returned stream will use. This will be the same asdirexcept for streams created relative to another secure stream.- Throws:
IOException
-
snapshotWorkingDirectoryEntries
Snapshots the entries of the working directory of this view. -
snapshotModifiedTimes
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
public boolean isSameFile(ZeroFsPath path, FileSystemView view2, ZeroFsPath path2) throws IOException 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 ZeroFsPath toRealPath(ZeroFsPath path, PathService pathService, Set<? super LinkOption> options) throws IOException Gets the real path to the file located by the given path.- Throws:
IOException
-
createDirectory
Creates a new directory at the given path. The given attributes will be set on the new file if possible.- Throws:
IOException
-
createSymbolicLink
public SymbolicLink createSymbolicLink(ZeroFsPath path, ZeroFsPath 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(ZeroFsPath path, 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. IfallowExistingistrueand a file already exists at the given path, returns that file. Otherwise, throwsFileAlreadyExistsException.- Throws:
IOException
-
getOrCreateRegularFile
public RegularFile getOrCreateRegularFile(ZeroFsPath 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
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(ZeroFsPath 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(ZeroFsPath link, FileSystemView existingView, ZeroFsPath 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, ZeroFsPath 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, throwingDirectoryNotEmptyExceptionif not.- Throws:
FileSystemException
-
copy
public void copy(ZeroFsPath source, FileSystemView destView, ZeroFsPath 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
Returns a file attribute view using the given lookup callback. -
getFileAttributeView
public <V extends FileAttributeView> V getFileAttributeView(ZeroFsPath 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(ZeroFsPath 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 Map<String,Object> readAttributes(ZeroFsPath 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(ZeroFsPath 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
-