public interface FileSystem
Truffle
files.Modifier and Type | Method and Description |
---|---|
static FileSystem |
allowLanguageHomeAccess(FileSystem fileSystem)
Decorates the given
fileSystem by an implementation that forwards access to files in
the language home to the default file system. |
void |
checkAccess(Path path,
Set<? extends AccessMode> modes,
LinkOption... linkOptions)
Checks existence and accessibility of a file.
|
default void |
copy(Path source,
Path target,
CopyOption... options)
Copies source file to target file.
|
void |
createDirectory(Path dir,
FileAttribute<?>... attrs)
Creates a directory.
|
default void |
createLink(Path link,
Path existing)
Creates a new link for an existing file.
|
default void |
createSymbolicLink(Path link,
Path target,
FileAttribute<?>... attrs)
Creates a new symbolic link.
|
void |
delete(Path path)
Deletes a file.
|
default Charset |
getEncoding(Path path)
Returns an file encoding for given path.
|
default String |
getMimeType(Path path)
Returns a MIME type for given path.
|
default String |
getPathSeparator()
Returns the path separator used to separate filenames in a path list.
|
default String |
getSeparator()
Returns the name separator used to separate names in a path string.
|
default Path |
getTempDirectory()
Returns the default temporary directory.
|
default boolean |
isSameFile(Path path1,
Path path2,
LinkOption... options)
Tests if the given paths refer to the same physical file.
|
default void |
move(Path source,
Path target,
CopyOption... options)
Moves (renames) source file to target file.
|
SeekableByteChannel |
newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
Opens or creates a file returning a
SeekableByteChannel to access the file content. |
static FileSystem |
newDefaultFileSystem()
Creates a
FileSystem implementation based on the host Java NIO. |
DirectoryStream<Path> |
newDirectoryStream(Path dir,
DirectoryStream.Filter<? super Path> filter)
Returns directory entries.
|
static FileSystem |
newFileSystem(FileSystem fileSystem)
Creates a
FileSystem implementation based on the given Java NIO filesystem. |
static FileSystem |
newReadOnlyFileSystem(FileSystem fileSystem)
Decorates the given
fileSystem by an implementation that makes the passed
fileSystem read-only by forbidding all write operations. |
Path |
parsePath(String path)
Parses a path from a
String . |
Path |
parsePath(URI uri)
Parses a path from an
URI . |
Map<String,Object> |
readAttributes(Path path,
String attributes,
LinkOption... options)
Reads a file's attributes as a bulk operation.
|
default Path |
readSymbolicLink(Path link)
Reads the target of the symbolic link.
|
default void |
setAttribute(Path path,
String attribute,
Object value,
LinkOption... options)
Sets a file's attribute.
|
default void |
setCurrentWorkingDirectory(Path currentWorkingDirectory)
Sets the current working directory.
|
Path |
toAbsolutePath(Path path)
Resolves given path to an absolute path.
|
Path |
toRealPath(Path path,
LinkOption... linkOptions)
Returns the real (canonical) path of an existing file.
|
Path parsePath(URI uri)
URI
.uri
- the URI
to be converted to Path
Path
representing given URI
UnsupportedOperationException
- when URI
scheme is not supportedIllegalArgumentException
- if preconditions on the uri
do not hold. The format
of the URI is FileSystem
specific.Path parsePath(String path)
path
- the string path to be converted to Path
Path
UnsupportedOperationException
- when the FileSystem
supports only URI
IllegalArgumentException
- if the path
string cannot be converted to a
Path
void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException
path
- the path to the file to checkmodes
- the access modes to check, possibly empty to check existence only.linkOptions
- options determining how the symbolic links should be handledNoSuchFileException
- if the file denoted by the path does not existIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationvoid createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException
dir
- the directory to createattrs
- the optional attributes to set atomically when creating the directoryFileAlreadyExistsException
- if a file on given path already existsIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be
set atomicallySecurityException
- if this FileSystem
denied the operationvoid delete(Path path) throws IOException
path
- the path to the file to deleteNoSuchFileException
- if a file on given path does not existDirectoryNotEmptyException
- if the path denotes a non empty directoryIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationSeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException
SeekableByteChannel
to access the file content.path
- the path to the file to openoptions
- the options specifying how the file should be openedattrs
- the optional attributes to set atomically when creating the new fileSeekableByteChannel
FileAlreadyExistsException
- if StandardOpenOption.CREATE_NEW
option is set and
a file already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be
set atomicallyIllegalArgumentException
- in case of invalid options combinationSecurityException
- if this FileSystem
denied the operationDirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException
dir
- the path to the directory to iterate entries forfilter
- the filterDirectoryStream
NotDirectoryException
- when given path does not denote a directoryIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationPath toAbsolutePath(Path path)
path
- the path to resolve, may be a non normalized pathPath
SecurityException
- if this FileSystem
denied the operationPath toRealPath(Path path, LinkOption... linkOptions) throws IOException
path
- the path to resolve, may be a non normalized pathlinkOptions
- options determining how the symbolic links should be handledIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationMap<String,Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException
path
- the path to file to read attributes forattributes
- the attributes to read. The attributes
parameter has the form:
[view-name:]attribute-list
. The optional view-name
corresponds to
AttributeView.name()
and determines the set of attributes, the default
value is "basic"
. The attribute-list
is a comma separated list of
attributes. If the attribute-list
contains '*'
then all the
attributes from given view are read.options
- the options determining how the symbolic links should be handledMap
containing the file attributes. The map's keys are attribute names,
map's values are the attribute values. The map may contain a subset of required
attributes in case when the FileSystem
does not support some of the required
attributes.UnsupportedOperationException
- if the attribute view is not supported. At least the
"basic"
attribute view has to be supported by the file system.IllegalArgumentException
- is the attribute-list
is empty or contains an
unknown attributeIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException
path
- the path to file to set an attribute toattribute
- the attribute to set. The attribute
parameter has the form:
[view-name:]attribute-name
. The optional view-name
corresponds to
AttributeView.name()
and determines the set of attributes, the default
value is "basic"
. The attribute-name
is a name of an attribute.value
- the attribute valueoptions
- the options determining how the symbolic links should be handledClassCastException
- if value
is not of the expected type or value
is a
Collection
containing element of a non expected typeUnsupportedOperationException
- if the attribute view is not supported.IllegalArgumentException
- is the attribute-name
is an unknown attribute or
value
has an inappropriate valueIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void copy(Path source, Path target, CopyOption... options) throws IOException
source
- the path to file to copytarget
- the path to the target fileoptions
- the options specifying how the copy should be performed, see
StandardCopyOption
UnsupportedOperationException
- if options
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and the options
don't contain StandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if the options
contain
StandardCopyOption.REPLACE_EXISTING
but the target
is a non empty
directoryIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void move(Path source, Path target, CopyOption... options) throws IOException
source
- the path to file to movetarget
- the path to the target fileoptions
- the options specifying how the move should be performed, see
StandardCopyOption
UnsupportedOperationException
- if options
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and the options
don't contain StandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if the options
contain
StandardCopyOption.REPLACE_EXISTING
but the target
is a non empty
directoryAtomicMoveNotSupportedException
- if the options
contain
StandardCopyOption.ATOMIC_MOVE
but file cannot be moved atomicallyIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void createLink(Path link, Path existing) throws IOException
link
- the path to link to createexisting
- the path to existing fileUnsupportedOperationException
- if links are not supported by file systemFileAlreadyExistsException
- if a file on given link path already existsIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException
link
- the path to symbolic link to createtarget
- the target path of the symbolic linkattrs
- the optional attributes to set atomically when creating the new symbolic linkUnsupportedOperationException
- if symbolic links are not supported by file systemFileAlreadyExistsException
- if a file on given link path already existsIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault Path readSymbolicLink(Path link) throws IOException
link
- the path to symbolic link to readPath
representing the symbolic link targetUnsupportedOperationException
- if symbolic links are not supported by file systemNotLinkException
- if the link
does not denote a symbolic linkIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationdefault void setCurrentWorkingDirectory(Path currentWorkingDirectory)
FileSystem
operations.currentWorkingDirectory
- the new current working directoryUnsupportedOperationException
- if setting of the current working directory is not
supportedIllegalArgumentException
- if the currentWorkingDirectory
is not a valid
current working directorySecurityException
- if currentWorkingDirectory
is not readabledefault String getSeparator()
toString()
method.default String getPathSeparator()
':'
. On Windows it's ';'
.default String getMimeType(Path path)
filesystem
implementations which can provide MIME types in an efficient way.path
- the file to find a MIME type fornull
if the MIME type is not recognized or the
filesystem
does not support MIME type detectiondefault Charset getEncoding(Path path)
filesystem
implementations which can provide file encodings in an efficient way.path
- the file to find an file encoding fornull
if the file encoding is not detected or the
filesystem
does not support file encoding detectiondefault Path getTempDirectory()
default boolean isSameFile(Path path1, Path path2, LinkOption... options) throws IOException
true
without checking if the file exists. Otherwise, this
method converts the paths into canonical representations and tests the canonical paths for
equality. The FileSystem
may re-implement the method with a more efficient test. When
re-implemented the method must have the same security privileges as the
toAbsolutePath
and toRealPath
.path1
- the path to the filepath2
- the other pathoptions
- the options determining how the symbolic links should be handledtrue
if the given paths refer to the same physical fileIOException
- in case of IO errorSecurityException
- if this FileSystem
denied the operationstatic FileSystem newDefaultFileSystem()
FileSystem
implementation based on the host Java NIO. The returned instance
can be used as a delegate by a decorating FileSystem
.
For an untrusted code execution, access to the host filesystem should be prevented either by
using IOAccess.NONE
or an FileSystem.newFileSystem(java.nio.file.FileSystem)
in-memory
filesystem}. For more details on executing untrusted code, see the
Polyglot Sandboxing
Security Guide.
The following example shows a FileSystem
logging filesystem operations.
class TracingFileSystem implements FileSystem { private static final Logger LOGGER = Logger.getLogger(TracingFileSystem.class.getName()); private final FileSystem delegate; TracingFileSystem() { this.delegate = FileSystem.newDefaultFileSystem(); } @Override public Path parsePath(String path) { return delegate.parsePath(path); } @Override public Path parsePath(URI uri) { return delegate.parsePath(uri); } @Override public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { boolean success = false; try { SeekableByteChannel result = delegate.newByteChannel(path, options, attrs); success = true; return result; } finally { trace("newByteChannel", path, success); } } ... private void trace(String operation, Path path, boolean success) { LOGGER.log(Level.FINE, "The {0} request for the path {1} {2}.",new Object[] { operation, path, success ? "was successful" : "failed" }); } }
IOAccess.Builder.fileSystem(FileSystem)
,
Context.Builder.allowIO(IOAccess)
static FileSystem allowLanguageHomeAccess(FileSystem fileSystem)
fileSystem
by an implementation that forwards access to files in
the language home to the default file system. The method is intended to be used by custom
filesystem implementations with non default storage to allow guest languages to access files
in the languages homes. As the returned filesystem uses a default file system to access files
in the language home, the fileSystem
has to use the same Path
type,
separator
and path separator
as the
default filesystem
.IllegalArgumentException
- when the fileSystem
does not use the same
Path
type or has a different separator
or
path separator
as the default file system
.static FileSystem newReadOnlyFileSystem(FileSystem fileSystem)
fileSystem
by an implementation that makes the passed
fileSystem
read-only by forbidding all write operations. This method can be used to
make an existing file system, such as the default filesystem
,
read-only.static FileSystem newFileSystem(FileSystem fileSystem)
FileSystem
implementation based on the given Java NIO filesystem. The
returned FileSystem
delegates all operations to fileSystem
's
provider
.
The following example shows how to configure Context
so that languages read files
from a prepared zip file.
Path zipFile = Paths.get("filesystem.zip"); try (java.nio.file.FileSystem nioFs = FileSystems.newFileSystem(zipFile)) { IOAccess ioAccess = IOAccess.newBuilder().fileSystem(FileSystem.newFileSystem(nioFs)).build(); try (Context ctx = Context.newBuilder().allowIO(ioAccess).build()) { Value result = ctx.eval("js", "load('scripts/app.sh'); execute()"); } }
IOAccess