Class FileSystemView


  • final class FileSystemView
    extends java.lang.Object
    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 
      Modifier and Type Class Description
      static class  FileSystemView.DeleteMode
      Mode for deleting.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkAccess​(ZeroFsPath path)
      Checks access to the file at the given path for the given modes.
      private void checkDeletable​(File file, FileSystemView.DeleteMode mode, java.nio.file.Path path)
      Checks that the given file can be deleted, throwing an exception if it can't.
      private void checkEmpty​(Directory dir, java.nio.file.Path pathForException)
      Checks that given directory is empty, throwing DirectoryNotEmptyException if not.
      private void checkMovable​(File file, ZeroFsPath 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​(ZeroFsPath source, FileSystemView destView, ZeroFsPath dest, java.util.Set<java.nio.file.CopyOption> options, boolean move)
      Copies or moves the file at the given source path to the given dest path.
      Directory createDirectory​(ZeroFsPath path, java.nio.file.attribute.FileAttribute<?>... attrs)
      Creates a new directory at the given path.
      private File createFile​(ZeroFsPath path, java.util.function.Supplier<? extends File> fileCreator, boolean failIfExists, java.nio.file.attribute.FileAttribute<?>... attrs)
      Creates a new file at the given path if possible, using the given supplier to create the file.
      SymbolicLink createSymbolicLink​(ZeroFsPath path, ZeroFsPath target, java.nio.file.attribute.FileAttribute<?>... attrs)
      Creates a new symbolic link at the given path with the given target.
      private void delete​(DirectoryEntry entry, FileSystemView.DeleteMode deleteMode, ZeroFsPath pathForException)
      Deletes the given directory entry from its parent directory.
      void deleteFile​(ZeroFsPath path, FileSystemView.DeleteMode deleteMode)
      Deletes the file at the given absolute path.
      <V extends java.nio.file.attribute.FileAttributeView>
      V
      getFileAttributeView​(FileLookup lookup, java.lang.Class<V> type)
      Returns a file attribute view using the given lookup callback.
      <V extends java.nio.file.attribute.FileAttributeView>
      V
      getFileAttributeView​(ZeroFsPath path, java.lang.Class<V> type, java.util.Set<? super java.nio.file.LinkOption> options)
      Returns a file attribute view for the given path in this view.
      RegularFile getOrCreateRegularFile​(ZeroFsPath path, java.util.Set<java.nio.file.OpenOption> options, java.nio.file.attribute.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​(ZeroFsPath path, java.util.Set<java.nio.file.OpenOption> options, java.nio.file.attribute.FileAttribute<?>[] attrs)
      Gets or creates a new regular file with a write lock (assuming the file does not exist).
      ZeroFsPath getWorkingDirectoryPath()
      Returns the path of the working directory at the time this view was created.
      boolean isSameFile​(ZeroFsPath path, FileSystemView view2, ZeroFsPath 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​(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 void lockBoth​(java.util.concurrent.locks.Lock sourceWriteLock, java.util.concurrent.locks.Lock destWriteLock)
      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​(ZeroFsPath path, java.util.Set<? super java.nio.file.LinkOption> options)
      Looks up the file at the given path without locking.
      private RegularFile lookUpRegularFile​(ZeroFsPath path, java.util.Set<java.nio.file.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​(ZeroFsPath path, java.util.Set<? super java.nio.file.LinkOption> options)
      Attempt to look up the file at the given path.
      java.nio.file.DirectoryStream<java.nio.file.Path> newDirectoryStream​(ZeroFsPath dir, java.nio.file.DirectoryStream.Filter<? super java.nio.file.Path> filter, java.util.Set<? super java.nio.file.LinkOption> options, ZeroFsPath basePathForStream)
      Creates a new directory stream for the directory located by the given path.
      private java.nio.file.attribute.FileTime now()  
      private static RegularFile open​(RegularFile file, java.util.Set<java.nio.file.OpenOption> options)
      Opens the given regular file with the given options, truncating it if necessary and incrementing its open count.
      <A extends java.nio.file.attribute.BasicFileAttributes>
      A
      readAttributes​(ZeroFsPath path, java.lang.Class<A> type, java.util.Set<? super java.nio.file.LinkOption> options)
      Reads attributes of the file located by the given path in this view as an object.
      java.util.Map<java.lang.String,​java.lang.Object> readAttributes​(ZeroFsPath path, java.lang.String attributes, java.util.Set<? super java.nio.file.LinkOption> options)
      Reads attributes of the file located by the given path in this view as a map.
      ZeroFsPath readSymbolicLink​(ZeroFsPath path)
      Returns the target of the symbolic link at the given path.
      void setAttribute​(ZeroFsPath path, java.lang.String attribute, java.lang.Object value, java.util.Set<? super java.nio.file.LinkOption> options)
      Sets the given attribute to the given value on the file located by the given path in this view.
      java.util.Map<Name,​java.nio.file.attribute.FileTime> snapshotModifiedTimes​(ZeroFsPath path)
      Returns a snapshot mapping the names of each file in the directory at the given path to the last modified time of that file.
      java.util.SortedSet<Name> snapshotWorkingDirectoryEntries()
      Snapshots the entries of the working directory of this view.
      FileSystemState state()
      Returns the file system state.
      ZeroFsPath toRealPath​(ZeroFsPath path, PathService pathService, java.util.Set<? super java.nio.file.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • isSameFileSystem

        private boolean isSameFileSystem​(FileSystemView other)
        Returns whether or not this view and the given view belong to the same file system.
      • now

        private java.nio.file.attribute.FileTime now()
      • getWorkingDirectoryPath

        public ZeroFsPath 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

        DirectoryEntry lookUpWithLock​(ZeroFsPath path,
                                      java.util.Set<? super java.nio.file.LinkOption> options)
                               throws java.io.IOException
        Attempt to look up the file at the given path.
        Throws:
        java.io.IOException
      • lookUp

        private DirectoryEntry lookUp​(ZeroFsPath path,
                                      java.util.Set<? super java.nio.file.LinkOption> options)
                               throws java.io.IOException
        Looks up the file at the given path without locking.
        Throws:
        java.io.IOException
      • newDirectoryStream

        public java.nio.file.DirectoryStream<java.nio.file.Path> newDirectoryStream​(ZeroFsPath dir,
                                                                                    java.nio.file.DirectoryStream.Filter<? super java.nio.file.Path> filter,
                                                                                    java.util.Set<? super java.nio.file.LinkOption> options,
                                                                                    ZeroFsPath basePathForStream)
                                                                             throws java.io.IOException
        Creates a new directory stream for the directory located by the given path. The given basePathForStream is that base path that the returned stream will use. This will be the same as dir except for streams created relative to another secure stream.
        Throws:
        java.io.IOException
      • snapshotWorkingDirectoryEntries

        public java.util.SortedSet<Name> snapshotWorkingDirectoryEntries()
        Snapshots the entries of the working directory of this view.
      • snapshotModifiedTimes

        public java.util.Map<Name,​java.nio.file.attribute.FileTime> snapshotModifiedTimes​(ZeroFsPath path)
                                                                                         throws java.io.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:
        java.io.IOException
      • isSameFile

        public boolean isSameFile​(ZeroFsPath path,
                                  FileSystemView view2,
                                  ZeroFsPath path2)
                           throws java.io.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:
        java.io.IOException
      • toRealPath

        public ZeroFsPath toRealPath​(ZeroFsPath path,
                                     PathService pathService,
                                     java.util.Set<? super java.nio.file.LinkOption> options)
                              throws java.io.IOException
        Gets the real path to the file located by the given path.
        Throws:
        java.io.IOException
      • createDirectory

        public Directory createDirectory​(ZeroFsPath path,
                                         java.nio.file.attribute.FileAttribute<?>... attrs)
                                  throws java.io.IOException
        Creates a new directory at the given path. The given attributes will be set on the new file if possible.
        Throws:
        java.io.IOException
      • createSymbolicLink

        public SymbolicLink createSymbolicLink​(ZeroFsPath path,
                                               ZeroFsPath target,
                                               java.nio.file.attribute.FileAttribute<?>... attrs)
                                        throws java.io.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:
        java.io.IOException
      • createFile

        private File createFile​(ZeroFsPath path,
                                java.util.function.Supplier<? extends File> fileCreator,
                                boolean failIfExists,
                                java.nio.file.attribute.FileAttribute<?>... attrs)
                         throws java.io.IOException
        Creates a new file at the given path if possible, using the given supplier to create the file. Returns the new file. If allowExisting is true and a file already exists at the given path, returns that file. Otherwise, throws FileAlreadyExistsException.
        Throws:
        java.io.IOException
      • getOrCreateRegularFile

        public RegularFile getOrCreateRegularFile​(ZeroFsPath path,
                                                  java.util.Set<java.nio.file.OpenOption> options,
                                                  java.nio.file.attribute.FileAttribute<?>... attrs)
                                           throws java.io.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:
        java.io.IOException
      • lookUpRegularFile

        private RegularFile lookUpRegularFile​(ZeroFsPath path,
                                              java.util.Set<java.nio.file.OpenOption> options)
                                       throws java.io.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:
        java.io.IOException
      • getOrCreateRegularFileWithWriteLock

        private RegularFile getOrCreateRegularFileWithWriteLock​(ZeroFsPath path,
                                                                java.util.Set<java.nio.file.OpenOption> options,
                                                                java.nio.file.attribute.FileAttribute<?>[] attrs)
                                                         throws java.io.IOException
        Gets or creates a new regular file with a write lock (assuming the file does not exist).
        Throws:
        java.io.IOException
      • open

        private static RegularFile open​(RegularFile file,
                                        java.util.Set<java.nio.file.OpenOption> options)
        Opens the given regular file with the given options, truncating it if necessary and incrementing its open count. Returns the given file.
      • readSymbolicLink

        public ZeroFsPath readSymbolicLink​(ZeroFsPath path)
                                    throws java.io.IOException
        Returns the target of the symbolic link at the given path.
        Throws:
        java.io.IOException
      • checkAccess

        public void checkAccess​(ZeroFsPath path)
                         throws java.io.IOException
        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:
        java.io.IOException
      • link

        public void link​(ZeroFsPath link,
                         FileSystemView existingView,
                         ZeroFsPath existing)
                  throws java.io.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:
        java.io.IOException
      • deleteFile

        public void deleteFile​(ZeroFsPath path,
                               FileSystemView.DeleteMode deleteMode)
                        throws java.io.IOException
        Deletes the file at the given absolute path.
        Throws:
        java.io.IOException
      • checkDeletable

        private void checkDeletable​(File file,
                                    FileSystemView.DeleteMode mode,
                                    java.nio.file.Path path)
                             throws java.io.IOException
        Checks that the given file can be deleted, throwing an exception if it can't.
        Throws:
        java.io.IOException
      • checkEmpty

        private void checkEmpty​(Directory dir,
                                java.nio.file.Path pathForException)
                         throws java.nio.file.FileSystemException
        Checks that given directory is empty, throwing DirectoryNotEmptyException if not.
        Throws:
        java.nio.file.FileSystemException
      • copy

        public void copy​(ZeroFsPath source,
                         FileSystemView destView,
                         ZeroFsPath dest,
                         java.util.Set<java.nio.file.CopyOption> options,
                         boolean move)
                  throws java.io.IOException
        Copies or moves the file at the given source path to the given dest path.
        Throws:
        java.io.IOException
      • checkMovable

        private void checkMovable​(File file,
                                  ZeroFsPath path)
                           throws java.nio.file.FileSystemException
        Throws:
        java.nio.file.FileSystemException
      • lockBoth

        private static void lockBoth​(java.util.concurrent.locks.Lock sourceWriteLock,
                                     java.util.concurrent.locks.Lock destWriteLock)
        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 java.io.IOException
        Checks that source is not an ancestor of dest, throwing an exception if it is.
        Throws:
        java.io.IOException
      • lockSourceAndCopy

        private void lockSourceAndCopy​(File sourceFile,
                                       File copyFile)
        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

        private void unlockSourceAndCopy​(File sourceFile,
                                         File copyFile)
        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 java.nio.file.attribute.FileAttributeView> V getFileAttributeView​(FileLookup lookup,
                                                                                            java.lang.Class<V> type)
        Returns a file attribute view using the given lookup callback.
      • getFileAttributeView

        public <V extends java.nio.file.attribute.FileAttributeView> V getFileAttributeView​(ZeroFsPath path,
                                                                                            java.lang.Class<V> type,
                                                                                            java.util.Set<? super java.nio.file.LinkOption> options)
        Returns a file attribute view for the given path in this view.
      • readAttributes

        public <A extends java.nio.file.attribute.BasicFileAttributes> A readAttributes​(ZeroFsPath path,
                                                                                        java.lang.Class<A> type,
                                                                                        java.util.Set<? super java.nio.file.LinkOption> options)
                                                                                 throws java.io.IOException
        Reads attributes of the file located by the given path in this view as an object.
        Throws:
        java.io.IOException
      • readAttributes

        public java.util.Map<java.lang.String,​java.lang.Object> readAttributes​(ZeroFsPath path,
                                                                                     java.lang.String attributes,
                                                                                     java.util.Set<? super java.nio.file.LinkOption> options)
                                                                              throws java.io.IOException
        Reads attributes of the file located by the given path in this view as a map.
        Throws:
        java.io.IOException
      • setAttribute

        public void setAttribute​(ZeroFsPath path,
                                 java.lang.String attribute,
                                 java.lang.Object value,
                                 java.util.Set<? super java.nio.file.LinkOption> options)
                          throws java.io.IOException
        Sets the given attribute to the given value on the file located by the given path in this view.
        Throws:
        java.io.IOException