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​(JimfsPath 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, 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, 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​(JimfsPath path, java.nio.file.attribute.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, 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​(JimfsPath path, JimfsPath 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, 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 java.nio.file.attribute.FileAttributeView>
      @Nullable 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>
      @Nullable V
      getFileAttributeView​(JimfsPath 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​(JimfsPath 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​(JimfsPath 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).
      JimfsPath getWorkingDirectoryPath()
      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 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​(JimfsPath path, java.util.Set<? super java.nio.file.LinkOption> options)
      Looks up the file at the given path without locking.
      private @Nullable RegularFile lookUpRegularFile​(JimfsPath 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​(JimfsPath 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​(JimfsPath dir, java.nio.file.DirectoryStream.Filter<? super java.nio.file.Path> filter, java.util.Set<? super java.nio.file.LinkOption> options, JimfsPath 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​(JimfsPath 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.
      com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Object> readAttributes​(JimfsPath 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.
      JimfsPath readSymbolicLink​(JimfsPath path)
      Returns the target of the symbolic link at the given path.
      void setAttribute​(JimfsPath 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.
      com.google.common.collect.ImmutableMap<Name,​java.nio.file.attribute.FileTime> snapshotModifiedTimes​(JimfsPath 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.
      com.google.common.collect.ImmutableSortedSet<Name> snapshotWorkingDirectoryEntries()
      Snapshots the entries of the working directory of this view.
      FileSystemState state()
      Returns the file system state.
      JimfsPath toRealPath​(JimfsPath 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
    • Field Detail

      • workingDirectory

        private final Directory workingDirectory
      • workingDirectoryPath

        private final JimfsPath workingDirectoryPath
    • 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 JimfsPath 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​(JimfsPath 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​(JimfsPath 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​(JimfsPath dir,
                                                                                    java.nio.file.DirectoryStream.Filter<? super java.nio.file.Path> filter,
                                                                                    java.util.Set<? super java.nio.file.LinkOption> options,
                                                                                    JimfsPath 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 com.google.common.collect.ImmutableSortedSet<Name> snapshotWorkingDirectoryEntries()
        Snapshots the entries of the working directory of this view.
      • snapshotModifiedTimes

        public com.google.common.collect.ImmutableMap<Name,​java.nio.file.attribute.FileTime> snapshotModifiedTimes​(JimfsPath 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​(JimfsPath path,
                                  FileSystemView view2,
                                  JimfsPath 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 JimfsPath toRealPath​(JimfsPath 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

        @CanIgnoreReturnValue
        public Directory createDirectory​(JimfsPath 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

        @CanIgnoreReturnValue
        public SymbolicLink createSymbolicLink​(JimfsPath path,
                                               JimfsPath 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​(JimfsPath path,
                                com.google.common.base.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​(JimfsPath 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 @Nullable RegularFile lookUpRegularFile​(JimfsPath 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​(JimfsPath 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 JimfsPath readSymbolicLink​(JimfsPath path)
                                   throws java.io.IOException
        Returns the target of the symbolic link at the given path.
        Throws:
        java.io.IOException
      • checkAccess

        public void checkAccess​(JimfsPath 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​(JimfsPath link,
                         FileSystemView existingView,
                         JimfsPath 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​(JimfsPath path,
                               FileSystemView.DeleteMode deleteMode)
                        throws java.io.IOException
        Deletes the file at the given absolute path.
        Throws:
        java.io.IOException
      • delete

        private void delete​(DirectoryEntry entry,
                            FileSystemView.DeleteMode deleteMode,
                            JimfsPath pathForException)
                     throws java.io.IOException
        Deletes the given directory entry from its parent directory.
        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​(JimfsPath source,
                         FileSystemView destView,
                         JimfsPath 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,
                                  JimfsPath 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> @Nullable 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> @Nullable V getFileAttributeView​(JimfsPath 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​(JimfsPath 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 com.google.common.collect.ImmutableMap<java.lang.String,​java.lang.Object> readAttributes​(JimfsPath 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​(JimfsPath 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