Package org.graalvm.polyglot.io
Interface FileSystem
public interface FileSystem
Service-provider for
Truffle
files.- Since:
- 19.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic FileSystem
allowInternalResourceAccess
(FileSystem fileSystem) Decorates the givenfileSystem
by an implementation that forwards access to the internal resources to the default file system.static FileSystem
allowLanguageHomeAccess
(FileSystem fileSystem) Deprecated.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
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
Returns the path separator used to separate filenames in a path list.default String
Returns the name separator used to separate names in a path string.default Path
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.newByteChannel
(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) Opens or creates a file returning aSeekableByteChannel
to access the file content.static FileSystem
Creates aFileSystem
implementation based on the host Java NIO.newDirectoryStream
(Path dir, DirectoryStream.Filter<? super Path> filter) Returns directory entries.static FileSystem
newFileSystem
(FileSystem fileSystem) Creates aFileSystem
implementation based on the given Java NIO filesystem.static FileSystem
newReadOnlyFileSystem
(FileSystem fileSystem) Decorates the givenfileSystem
by an implementation that makes the passedfileSystem
read-only by forbidding all write operations.Parses a path from aString
.Parses a path from anURI
.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.toAbsolutePath
(Path path) Resolves given path to an absolute path.toRealPath
(Path path, LinkOption... linkOptions) Returns the real (canonical) path of an existing file.
-
Method Details
-
parsePath
Parses a path from anURI
.- Parameters:
uri
- theURI
to be converted toPath
- Returns:
- the
Path
representing givenURI
- Throws:
UnsupportedOperationException
- whenURI
scheme is not supportedIllegalArgumentException
- if preconditions on theuri
do not hold. The format of the URI isFileSystem
specific.- Since:
- 19.0
-
parsePath
- Parameters:
path
- the string path to be converted toPath
- Returns:
- the
Path
- Throws:
UnsupportedOperationException
- when theFileSystem
supports onlyURI
IllegalArgumentException
- if thepath
string cannot be converted to aPath
- Since:
- 19.0
-
checkAccess
void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException Checks existence and accessibility of a file.- Parameters:
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 handled- Throws:
NoSuchFileException
- if the file denoted by the path does not existIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
createDirectory
Creates a directory.- Parameters:
dir
- the directory to createattrs
- the optional attributes to set atomically when creating the directory- Throws:
FileAlreadyExistsException
- 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 thisFileSystem
denied the operation- Since:
- 19.0
-
delete
Deletes a file.- Parameters:
path
- the path to the file to delete- Throws:
NoSuchFileException
- if a file on given path does not existDirectoryNotEmptyException
- if the path denotes a non empty directoryIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
newByteChannel
SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException Opens or creates a file returning aSeekableByteChannel
to access the file content.- Parameters:
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 file- Returns:
- the created
SeekableByteChannel
- Throws:
FileAlreadyExistsException
- ifStandardOpenOption.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 thisFileSystem
denied the operation- Since:
- 19.0
-
newDirectoryStream
DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException Returns directory entries.- Parameters:
dir
- the path to the directory to iterate entries forfilter
- the filter- Returns:
- the new
DirectoryStream
- Throws:
NotDirectoryException
- when given path does not denote a directoryIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
toAbsolutePath
Resolves given path to an absolute path.- Parameters:
path
- the path to resolve, may be a non normalized path- Returns:
- an absolute
Path
- Throws:
SecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
toRealPath
Returns the real (canonical) path of an existing file.- Parameters:
path
- the path to resolve, may be a non normalized pathlinkOptions
- options determining how the symbolic links should be handled- Returns:
- an absolute canonical path
- Throws:
IOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
readAttributes
Map<String,Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException Reads a file's attributes as a bulk operation.- Parameters:
path
- the path to file to read attributes forattributes
- the attributes to read. Theattributes
parameter has the form:[view-name:]attribute-list
. The optionalview-name
corresponds toAttributeView.name()
and determines the set of attributes, the default value is"basic"
. Theattribute-list
is a comma separated list of attributes. If theattribute-list
contains'*'
then all the attributes from given view are read.options
- the options determining how the symbolic links should be handled- Returns:
- the
Map
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 theFileSystem
does not support some of the required attributes. - Throws:
UnsupportedOperationException
- if the attribute view is not supported. At least the"basic"
attribute view has to be supported by the file system.IllegalArgumentException
- is theattribute-list
is empty or contains an unknown attributeIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
setAttribute
default void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException Sets a file's attribute.- Parameters:
path
- the path to file to set an attribute toattribute
- the attribute to set. Theattribute
parameter has the form:[view-name:]attribute-name
. The optionalview-name
corresponds toAttributeView.name()
and determines the set of attributes, the default value is"basic"
. Theattribute-name
is a name of an attribute.value
- the attribute valueoptions
- the options determining how the symbolic links should be handled- Throws:
ClassCastException
- ifvalue
is not of the expected type orvalue
is aCollection
containing element of a non expected typeUnsupportedOperationException
- if the attribute view is not supported.IllegalArgumentException
- is theattribute-name
is an unknown attribute orvalue
has an inappropriate valueIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
copy
Copies source file to target file.- Parameters:
source
- the path to file to copytarget
- the path to the target fileoptions
- the options specifying how the copy should be performed, seeStandardCopyOption
- Throws:
UnsupportedOperationException
- ifoptions
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and theoptions
don't containStandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if theoptions
containStandardCopyOption.REPLACE_EXISTING
but thetarget
is a non empty directoryIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
move
Moves (renames) source file to target file.- Parameters:
source
- the path to file to movetarget
- the path to the target fileoptions
- the options specifying how the move should be performed, seeStandardCopyOption
- Throws:
UnsupportedOperationException
- ifoptions
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and theoptions
don't containStandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if theoptions
containStandardCopyOption.REPLACE_EXISTING
but thetarget
is a non empty directoryAtomicMoveNotSupportedException
- if theoptions
containStandardCopyOption.ATOMIC_MOVE
but file cannot be moved atomicallyIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
createLink
Creates a new link for an existing file.- Parameters:
link
- the path to link to createexisting
- the path to existing file- Throws:
UnsupportedOperationException
- if links are not supported by file systemFileAlreadyExistsException
- if a file on given link path already existsIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
createSymbolicLink
default void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException Creates a new symbolic link.- Parameters:
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 link- Throws:
UnsupportedOperationException
- if symbolic links are not supported by file systemFileAlreadyExistsException
- if a file on given link path already existsIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
readSymbolicLink
Reads the target of the symbolic link.- Parameters:
link
- the path to symbolic link to read- Returns:
- the
Path
representing the symbolic link target - Throws:
UnsupportedOperationException
- if symbolic links are not supported by file systemNotLinkException
- if thelink
does not denote a symbolic linkIOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 19.0
-
setCurrentWorkingDirectory
Sets the current working directory. The current working directory is used to resolve non absolute paths inFileSystem
operations.- Parameters:
currentWorkingDirectory
- the new current working directory- Throws:
UnsupportedOperationException
- if setting of the current working directory is not supportedIllegalArgumentException
- if thecurrentWorkingDirectory
is not a valid current working directorySecurityException
- ifcurrentWorkingDirectory
is not readable- Since:
- 19.0
-
getSeparator
Returns the name separator used to separate names in a path string. The separator is used when creating path strings by invoking thetoString()
method.- Returns:
- the name separator
- Since:
- 19.0
-
getPathSeparator
Returns the path separator used to separate filenames in a path list. On UNIX the path separator is':'
. On Windows it's';'
.- Returns:
- the path separator
- Since:
- 19.1.0
-
getMimeType
Returns a MIME type for given path. An optional operation forfilesystem
implementations which can provide MIME types in an efficient way.- Parameters:
path
- the file to find a MIME type for- Returns:
- the MIME type or
null
if the MIME type is not recognized or thefilesystem
does not support MIME type detection - Since:
- 19.0
-
getEncoding
Returns an file encoding for given path. An optional operation forfilesystem
implementations which can provide file encodings in an efficient way.- Parameters:
path
- the file to find an file encoding for- Returns:
- the file encoding or
null
if the file encoding is not detected or thefilesystem
does not support file encoding detection - Since:
- 19.0
-
getTempDirectory
Returns the default temporary directory.- Since:
- 19.3.0
-
isSameFile
Tests if the given paths refer to the same physical file. The default implementation firstly converts the paths into absolute paths. If the absolute paths are equal it returnstrue
without checking if the file exists. Otherwise, this method converts the paths into canonical representations and tests the canonical paths for equality. TheFileSystem
may re-implement the method with a more efficient test. When re-implemented the method must have the same security privileges as thetoAbsolutePath
andtoRealPath
.- Parameters:
path1
- the path to the filepath2
- the other pathoptions
- the options determining how the symbolic links should be handled- Returns:
true
if the given paths refer to the same physical file- Throws:
IOException
- in case of IO errorSecurityException
- if thisFileSystem
denied the operation- Since:
- 20.2.0
-
newDefaultFileSystem
Creates aFileSystem
implementation based on the host Java NIO. The returned instance can be used as a delegate by a decoratingFileSystem
.For an untrusted code execution, access to the host filesystem should be prevented either by using
IOAccess.NONE
or annewFileSystem(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" }); } }
- Since:
- 20.2.0
- See Also:
-
allowLanguageHomeAccess
Deprecated.Decorates the givenfileSystem
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, thefileSystem
has to use the samePath
type,separator
andpath separator
as thedefault filesystem
.- Throws:
IllegalArgumentException
- when thefileSystem
does not use the samePath
type or has a differentseparator
orpath separator
as thedefault file system
.- Since:
- 22.2
-
allowInternalResourceAccess
Decorates the givenfileSystem
by an implementation that forwards access to the internal resources 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 internal resources. As the returned filesystem uses a default file system to access internal resources, thefileSystem
has to use the samePath
type,separator
andpath separator
as thedefault filesystem
.- Throws:
IllegalArgumentException
- when thefileSystem
does not use the samePath
type or has a differentseparator
orpath separator
as thedefault file system
.- Since:
- 24.0
- See Also:
-
newReadOnlyFileSystem
Decorates the givenfileSystem
by an implementation that makes the passedfileSystem
read-only by forbidding all write operations. This method can be used to make an existing file system, such as thedefault filesystem
, read-only.- Since:
- 22.2
-
newFileSystem
Creates aFileSystem
implementation based on the given Java NIO filesystem. The returnedFileSystem
delegates all operations tofileSystem
'sprovider
.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()"); } }
- Since:
- 23.0
- See Also:
-
allowInternalResourceAccess(FileSystem)
}.