WasiHostFileSystem Class

A file system whose files are the files of a directory of the disk, which the module cannot leave.

public sealed class WasiHostFileSystem : WasiFileSystem

Remarks

WASI resolves the paths of the module before calling the file system: it follows the symbolic links itself, and rejects the paths and the links that leave the directory, the absolute paths and the absolute links. This file system also rejects the names that are not portable on Windows (\, :, *, ?, ", <, >, |, the control characters, the reserved names such as CON or NUL, and the names that end with a dot or a space) on Windows, and checks that the full path of each file is inside the directory.

The checks and the operations are not atomic: a process of the host that replaces a directory of the file system with a link between the check and the operation could make the module access the target of the link. Do not give the module a directory that untrusted processes can change.

The hard links are supported on Windows (NTFS, ReFS), Linux, macOS and FreeBSD: a hard link never follows a symbolic link, so that the module cannot link a file outside of the directory. The serial numbers of the files (which tell the hard links of a file) and their numbers of links come from the platform on Windows, Linux and macOS.

Inheritance
WasiFileSystem WasiHostFileSystem

Constructors

NameDescription
WasiHostFileSystem(string, bool)Initializes a new instance of the WasiHostFileSystem class.

WasiHostFileSystem(string, bool)

WasiHostFileSystem.cs

Initializes a new instance of the WasiHostFileSystem class.

public WasiHostFileSystem(string rootDirectory, bool isReadOnly = false)

Parameters

rootDirectory string
The directory of the files. It must exist.
isReadOnly bool
Whether the module can only read the files.

Exceptions

DirectoryNotFoundException
The directory does not exist.

Properties

NameDescription
IsReadOnlyGets whether the module can only read the file system: the operations that change it return WasiErrno.ReadOnlyFileSystem.
RootDirectoryGets the full path of the directory of the files.

Gets whether the module can only read the file system: the operations that change it return WasiErrno.ReadOnlyFileSystem.

public override bool IsReadOnly { get; }

Property value

bool

Gets the full path of the directory of the files.

public string RootDirectory { get; }

Property value

string

Methods

NameDescription
CreateDirectory(string)Creates a directory.
CreateHardLink(string, string)Creates a hard link to a file. The default implementation throws a WasiException whose code is WasiErrno.NotSupported.
CreateSymbolicLink(string, string)Creates a symbolic link. The default implementation throws a WasiException whose code is WasiErrno.NotSupported.
DeleteDirectory(string)Deletes a directory.
DeleteFile(string)Deletes a file or a symbolic link (not its target).
EnumerateDirectory(string)Returns the entries of a directory, without . and ...
GetStat(string)Returns the status of a file, of a directory or of a symbolic link (not of its target), or null when it does not exist.
Move(string, string)Moves a file, a directory or a symbolic link.
OpenFile(string, FileMode, FileAccess)Opens a regular file. The stream of a regular file must be seekable.
ReadSymbolicLink(string)Returns the target of a symbolic link, with / separators. A target that starts with / is absolute: WASI does not follow it.
Replace(string, string)Moves a file or a symbolic link over an existing file or symbolic link, which it replaces (a rename of POSIX). The default implementation deletes the destination, then moves the source: override it to replace the destination in one operation, so that a move that fails does not lose the destination.
SetTimes(string, DateTimeOffset?, DateTimeOffset?)Changes the times of a file or of a directory.

CreateDirectory(string)

WasiHostFileSystem.cs

Creates a directory.

public override void CreateDirectory(string path)

Parameters

path string
The path of the directory, which does not exist. Its parent is an existing directory.

CreateHardLink(string, string)

WasiHostFileSystem.cs

Creates a hard link to a file. The default implementation throws a WasiException whose code is WasiErrno.NotSupported.

public override void CreateHardLink(string path, string existingPath)

Parameters

path string
The path of the link, which does not exist. Its parent is an existing directory.
existingPath string
The path of an existing regular file.

CreateSymbolicLink(string, string)

WasiHostFileSystem.cs

Creates a symbolic link. The default implementation throws a WasiException whose code is WasiErrno.NotSupported.

public override void CreateSymbolicLink(string path, string target)

Parameters

path string
The path of the link, which does not exist. Its parent is an existing directory.
target string
The target of the link, relative to the directory of the link, with / separators.

DeleteDirectory(string)

WasiHostFileSystem.cs

Deletes a directory.

public override void DeleteDirectory(string path)

Parameters

path string
The path of an existing empty directory, which is not the root.

DeleteFile(string)

WasiHostFileSystem.cs

Deletes a file or a symbolic link (not its target).

public override void DeleteFile(string path)

Parameters

path string
The path of an existing file or symbolic link.

EnumerateDirectory(string)

WasiHostFileSystem.cs

Returns the entries of a directory, without . and ...

public override IEnumerable<WasiDirectoryEntry> EnumerateDirectory(string path)

Parameters

path string
The path of an existing directory.

Returns

IEnumerable<WasiDirectoryEntry>

GetStat(string)

WasiHostFileSystem.cs

Returns the status of a file, of a directory or of a symbolic link (not of its target), or null when it does not exist.

public override WasiFileStat? GetStat(string path)

Parameters

path string
The path of the file.

Returns

WasiFileStat?

Move(string, string)

WasiHostFileSystem.cs

Moves a file, a directory or a symbolic link.

public override void Move(string sourcePath, string destinationPath)

Parameters

sourcePath string
The path of the existing file, which is not the root.
destinationPath string
The new path of the file, which does not exist. Its parent is an existing directory, which is not inside sourcePath.

OpenFile(string, FileMode, FileAccess)

WasiHostFileSystem.cs

Opens a regular file. The stream of a regular file must be seekable.

public override Stream OpenFile(string path, FileMode mode, FileAccess access)

Parameters

path string
The path of the file. Its parent is an existing directory.
mode FileMode
How the file is opened, or created.
access FileAccess
The access to the file.

Returns

Stream

ReadSymbolicLink(string)

WasiHostFileSystem.cs

Returns the target of a symbolic link, with / separators. A target that starts with / is absolute: WASI does not follow it.

public override string ReadSymbolicLink(string path)

Parameters

path string
The path of the symbolic link.

Returns

string

Replace(string, string)

WasiHostFileSystem.cs

Moves a file or a symbolic link over an existing file or symbolic link, which it replaces (a rename of POSIX). The default implementation deletes the destination, then moves the source: override it to replace the destination in one operation, so that a move that fails does not lose the destination.

public override void Replace(string sourcePath, string destinationPath)

Parameters

sourcePath string
The path of the existing file or symbolic link, which is not the root.
destinationPath string
The path of the existing file or symbolic link to replace.

SetTimes(string, DateTimeOffset?, DateTimeOffset?)

WasiHostFileSystem.cs

Changes the times of a file or of a directory.

public override void SetTimes(string path, DateTimeOffset? lastAccessTime, DateTimeOffset? lastWriteTime)

Parameters

path string
The path of an existing file or directory.
lastAccessTime DateTimeOffset?
The new time of the last access, or null to keep it.
lastWriteTime DateTimeOffset?
The new time of the last write, or null to keep it.