Class FileSystemView

java.lang.Object
com.google.common.jimfs.FileSystemView

final class FileSystemView extends 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.

  • Field Details

    • store

      private final JimfsFileStore store
    • workingDirectory

      private final Directory workingDirectory
    • workingDirectoryPath

      private final JimfsPath workingDirectoryPath
  • Constructor Details

  • Method Details

    • isSameFileSystem

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

      public FileSystemState state()
      Returns the file system state.
    • now

      private 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, Set<? super LinkOption> options) throws IOException
      Attempt to look up the file at the given path.
      Throws:
      IOException
    • lookUp

      private DirectoryEntry lookUp(JimfsPath path, Set<? super LinkOption> options) throws IOException
      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 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:
      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,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

      public boolean isSameFile(JimfsPath path, FileSystemView view2, JimfsPath 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 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. If allowExisting is true and a file already exists at the given path, returns that file. Otherwise, throws FileAlreadyExistsException.
      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

      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. Returns the given file.
    • readSymbolicLink

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

      public void checkAccess(JimfsPath path) throws 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:
      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

      public void deleteFile(JimfsPath path, FileSystemView.DeleteMode deleteMode) throws IOException
      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

      private void checkEmpty(Directory dir, Path pathForException) throws FileSystemException
      Checks that given directory is empty, throwing DirectoryNotEmptyException 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

      private void checkMovable(File file, JimfsPath path) throws FileSystemException
      Throws:
      FileSystemException
    • lockBoth

      private static void lockBoth(Lock sourceWriteLock, 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 IOException
      Checks that source is not an ancestor of dest, throwing an exception if it is.
      Throws:
      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 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