Class JimfsFileSystem
- java.lang.Object
-
- java.nio.file.FileSystem
-
- com.google.common.jimfs.JimfsFileSystem
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
final class JimfsFileSystem extends java.nio.file.FileSystem
FileSystem
implementation for Jimfs. Most behavior for the file system is implemented by its default file system view.Overview of file system design
JimfsFileSystem
instances are created byJimfsFileSystems
using a user-providedConfiguration
. The configuration is used to create the various classes that implement the file system with the correct settings and to create the file system root directories and working directory. The file system is then used to create thePath
objects that all file system operations use.Once created, the primary entry points to the file system are
JimfsFileSystemProvider
, which handles calls to methods inFiles
, andJimfsSecureDirectoryStream
, which provides methods that are similar to those of the file system provider but which treat relative paths as relative to the stream's directory rather than the file system's working directory.The implementation of the methods on both of those classes is handled by the
FileSystemView
class, which acts as a view of the file system with a specific working directory. The file system provider uses the file system's default view, while each secure directory stream uses a view specific to that stream.File system views make use of the file system's singleton
JimfsFileStore
which handles file creation, storage and attributes. The file store delegates to several other classes to handle each of these:FileFactory
handles creation of new file objects.HeapDisk
handles allocation of blocks toRegularFile
instances.FileTree
stores the root of the file hierarchy and handles file lookup.AttributeService
handles file attributes, using a set ofAttributeProvider
implementations to handle each supported file attribute view.
Paths
The implementation ofPath
for the file system isJimfsPath
. Paths are created by aPathService
with help from the file system's configuredPathType
.Paths are made up of
Name
objects, which also serve as the file names in directories. A name has two forms:- The display form is used in
Path
fortoString()
. It is also used for determining the equality and sort order ofPath
objects for most file systems. - The canonical form is used for equality of two
Name
objects. This affects the notion of name equality in the file system itself for file lookup. A file system may be configured to use the canonical form of the name for path equality (a Windows-like file system configuration does this, as the real Windows file system implementation uses case-insensitive equality for its path objects.
The canonical form of a name is created by applying a series of normalizations to the original string. These normalization may be either a Unicode normalization (e.g. NFD) or case folding normalization for case-insensitivity. Normalizations may also be applied to the display form of a name, but this is currently only done for a Mac OS X type configuration.
Files
All files in the file system are an instance ofFile
. A file object contains both the file's attributes and content.There are three types of files:
Directory
- contains a table linking file names to directory entries.RegularFile
- an in-memory store for raw bytes.SymbolicLink
- contains a path.
JimfsFileChannel
,JimfsInputStream
andJimfsOutputStream
implement the standard channel/stream APIs for regular files.JimfsSecureDirectoryStream
handles reading the entries of a directory. The secure directory stream additionally contains aFileSystemView
with its directory as the working directory, allowing for operations relative to the actual directory file rather than just the path to the file. This allows the operations to continue to work as expected even if the directory is moved.A directory can be watched for changes using the
WatchService
implementation,PollingWatchService
.Regular files
RegularFile
makes use of a singletonHeapDisk
. A disk is a resizable factory and cache for fixed size blocks of memory. These blocks are allocated to files as needed and returned to the disk when a file is deleted or truncated. When cached free blocks are available, those blocks are allocated to files first. If more blocks are needed, they are created.Linking
When a file is mapped to a file name in a directory table, it is linked. Each type of file has different rules governing how it is linked.- Directory - A directory has two or more links to it. The first is the link from its parent directory to it. This link is the name of the directory. The second is the self link (".") which links the directory to itself. The directory may also have any number of additional parent links ("..") from child directories back to it.
- Regular file - A regular file has one link from its parent directory by default. However, regular files are also allowed to have any number of additional user-created hard links, from the same directory with different names and/or from other directories with any names.
- Symbolic link - A symbolic link can only have one link, from its parent directory.
Thread safety
All file system operations should be safe in a multithreaded environment. The file hierarchy itself is protected by a file system level read-write lock. This ensures safety of all modifications to directory tables as well as atomicity of operations like file moves. Regular files are each protected by a read-write lock which is obtained for each read or write operation. File attributes are protected by synchronization on the file object itself.
-
-
Field Summary
Fields Modifier and Type Field Description private @Nullable java.util.concurrent.ExecutorService
defaultThreadPool
private FileSystemView
defaultView
private JimfsFileStore
fileStore
private PathService
pathService
private JimfsFileSystemProvider
provider
private java.net.URI
uri
private java.nio.file.attribute.UserPrincipalLookupService
userLookupService
private WatchServiceConfiguration
watchServiceConfig
-
Constructor Summary
Constructors Constructor Description JimfsFileSystem(JimfsFileSystemProvider provider, java.net.URI uri, JimfsFileStore fileStore, PathService pathService, FileSystemView defaultView, WatchServiceConfiguration watchServiceConfig)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.util.concurrent.ExecutorService
getDefaultThreadPool()
Returns a default thread pool to use for asynchronous file channels when users do not provide an executor themselves.FileSystemView
getDefaultView()
Returns the default view for this file system.JimfsFileStore
getFileStore()
Returns the file store for this file system.com.google.common.collect.ImmutableSet<java.nio.file.FileStore>
getFileStores()
JimfsPath
getPath(java.lang.String first, java.lang.String... more)
java.nio.file.PathMatcher
getPathMatcher(java.lang.String syntaxAndPattern)
(package private) PathService
getPathService()
Returns the path service for this file system.com.google.common.collect.ImmutableSortedSet<java.nio.file.Path>
getRootDirectories()
java.lang.String
getSeparator()
java.net.URI
getUri()
Returns the URI for this file system.java.nio.file.attribute.UserPrincipalLookupService
getUserPrincipalLookupService()
JimfsPath
getWorkingDirectory()
Returns the working directory path for this file system.boolean
isOpen()
boolean
isReadOnly()
Returnsfalse
; currently, cannot create a read-only file system.java.nio.file.WatchService
newWatchService()
JimfsFileSystemProvider
provider()
com.google.common.collect.ImmutableSet<java.lang.String>
supportedFileAttributeViews()
JimfsPath
toPath(java.net.URI uri)
Converts the given URI into a path in this file system.java.net.URI
toUri(JimfsPath path)
Gets the URI of the given path in this file system.
-
-
-
Field Detail
-
provider
private final JimfsFileSystemProvider provider
-
uri
private final java.net.URI uri
-
fileStore
private final JimfsFileStore fileStore
-
pathService
private final PathService pathService
-
userLookupService
private final java.nio.file.attribute.UserPrincipalLookupService userLookupService
-
defaultView
private final FileSystemView defaultView
-
watchServiceConfig
private final WatchServiceConfiguration watchServiceConfig
-
defaultThreadPool
private @Nullable java.util.concurrent.ExecutorService defaultThreadPool
-
-
Constructor Detail
-
JimfsFileSystem
JimfsFileSystem(JimfsFileSystemProvider provider, java.net.URI uri, JimfsFileStore fileStore, PathService pathService, FileSystemView defaultView, WatchServiceConfiguration watchServiceConfig)
-
-
Method Detail
-
provider
public JimfsFileSystemProvider provider()
- Specified by:
provider
in classjava.nio.file.FileSystem
-
getUri
public java.net.URI getUri()
Returns the URI for this file system.
-
getDefaultView
public FileSystemView getDefaultView()
Returns the default view for this file system.
-
getSeparator
public java.lang.String getSeparator()
- Specified by:
getSeparator
in classjava.nio.file.FileSystem
-
getRootDirectories
public com.google.common.collect.ImmutableSortedSet<java.nio.file.Path> getRootDirectories()
- Specified by:
getRootDirectories
in classjava.nio.file.FileSystem
-
getWorkingDirectory
public JimfsPath getWorkingDirectory()
Returns the working directory path for this file system.
-
getPathService
PathService getPathService()
Returns the path service for this file system.
-
getFileStore
public JimfsFileStore getFileStore()
Returns the file store for this file system.
-
getFileStores
public com.google.common.collect.ImmutableSet<java.nio.file.FileStore> getFileStores()
- Specified by:
getFileStores
in classjava.nio.file.FileSystem
-
supportedFileAttributeViews
public com.google.common.collect.ImmutableSet<java.lang.String> supportedFileAttributeViews()
- Specified by:
supportedFileAttributeViews
in classjava.nio.file.FileSystem
-
getPath
public JimfsPath getPath(java.lang.String first, java.lang.String... more)
- Specified by:
getPath
in classjava.nio.file.FileSystem
-
toUri
public java.net.URI toUri(JimfsPath path)
Gets the URI of the given path in this file system.
-
toPath
public JimfsPath toPath(java.net.URI uri)
Converts the given URI into a path in this file system.
-
getPathMatcher
public java.nio.file.PathMatcher getPathMatcher(java.lang.String syntaxAndPattern)
- Specified by:
getPathMatcher
in classjava.nio.file.FileSystem
-
getUserPrincipalLookupService
public java.nio.file.attribute.UserPrincipalLookupService getUserPrincipalLookupService()
- Specified by:
getUserPrincipalLookupService
in classjava.nio.file.FileSystem
-
newWatchService
public java.nio.file.WatchService newWatchService() throws java.io.IOException
- Specified by:
newWatchService
in classjava.nio.file.FileSystem
- Throws:
java.io.IOException
-
getDefaultThreadPool
public java.util.concurrent.ExecutorService getDefaultThreadPool()
Returns a default thread pool to use for asynchronous file channels when users do not provide an executor themselves. (This is required by the spec of newAsynchronousFileChannel in FileSystemProvider.)
-
isReadOnly
public boolean isReadOnly()
Returnsfalse
; currently, cannot create a read-only file system.- Specified by:
isReadOnly
in classjava.nio.file.FileSystem
- Returns:
false
, always
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in classjava.nio.file.FileSystem
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.nio.file.FileSystem
- Throws:
java.io.IOException
-
-