WasiInMemoryFileSystem Class

A file system in memory: files, directories, symbolic links and hard links that the module reads and writes, and that the host prepares (WasiInMemoryFileSystem.AddFile, WasiInMemoryFileSystem.AddDirectory, WasiInMemoryFileSystem.AddSymbolicLink) and reads (WasiInMemoryFileSystem.ReadAllBytes). It does not access the disk, so it works on every platform, including the browser.

public sealed class WasiInMemoryFileSystem : WasiFileSystem

Remarks

The paths of the methods of the host are relative to the root, with / or \ separators. The file system is not thread-safe.

Inheritance
WasiFileSystem WasiInMemoryFileSystem

Constructors

NameDescription
WasiInMemoryFileSystem(bool)Initializes a new instance of the WasiInMemoryFileSystem class, which is empty.

WasiInMemoryFileSystem(bool)

WasiInMemoryFileSystem.cs

Initializes a new instance of the WasiInMemoryFileSystem class, which is empty.

public WasiInMemoryFileSystem(bool isReadOnly = false)

Parameters

isReadOnly bool
Whether the module can only read the file system. The host can still change it.

Properties

NameDescription
IsReadOnlyGets whether the module can only read the file system: the operations that change it return WasiErrno.ReadOnlyFileSystem.
MaxFileSizeGets the maximum size of a file in bytes: a write or a truncation beyond fails with WasiErrno.FileTooLarge. The default and the upper limit is the maximum length of a .NET array.
TimeProviderGets the clock of the times of the files. The default is TimeProvider.System.

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 maximum size of a file in bytes: a write or a truncation beyond fails with WasiErrno.FileTooLarge. The default and the upper limit is the maximum length of a .NET array.

public long MaxFileSize { get; init; }

Property value

long

Gets the clock of the times of the files. The default is TimeProvider.System.

public TimeProvider TimeProvider { get; init; }

Property value

TimeProvider

Methods

NameDescription
AddDirectory(string)Adds a directory and its parents.
AddFile(string, ReadOnlySpan<byte>)Adds a file, or replaces the content of a file, and creates its parent directories.
AddSymbolicLink(string, string)Adds a symbolic link, and creates its parent directories.
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 ...
Exists(string)Returns whether a file, a directory or a symbolic link exists.
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.
ReadAllBytes(string)Returns the content of a file.
ReadSymbolicLink(string)Returns the target of a symbolic link, with / separators. A target that starts with / is absolute: WASI does not follow it.
SetTimes(string, DateTimeOffset?, DateTimeOffset?)Changes the times of a file or of a directory.

AddDirectory(string)

WasiInMemoryFileSystem.cs

Adds a directory and its parents.

public void AddDirectory(string path)

Parameters

path string
The path of the directory.

Exceptions

ArgumentException
The path is not valid, or an entry of the path is not a directory.

AddFile(string, ReadOnlySpan<byte>)

WasiInMemoryFileSystem.cs

Adds a file, or replaces the content of a file, and creates its parent directories.

public void AddFile(string path, ReadOnlySpan<byte> content)

Parameters

path string
The path of the file.
content ReadOnlySpan<byte>
The content of the file.

Exceptions

ArgumentException
The path is not valid, or an entry of the path is not a directory.

AddSymbolicLink(string, string)

WasiInMemoryFileSystem.cs

Adds a symbolic link, and creates its parent directories.

public void AddSymbolicLink(string path, string target)

Parameters

path string
The path of the link.
target string
The target of the link, relative to the directory of the link, with / separators.

Exceptions

ArgumentException
The path is not valid or exists, or an entry of the path is not a directory.

CreateDirectory(string)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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>

Returns whether a file, a directory or a symbolic link exists.

public bool Exists(string path)

Parameters

path string
The path of the entry. The symbolic links are not followed.

Returns

bool

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)

WasiInMemoryFileSystem.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)

WasiInMemoryFileSystem.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

ReadAllBytes(string)

WasiInMemoryFileSystem.cs

Returns the content of a file.

public byte[] ReadAllBytes(string path)

Parameters

path string
The path of the file. The symbolic links are not followed.

Returns

byte[]

Exceptions

FileNotFoundException
The file does not exist, or is not a regular file.

ReadSymbolicLink(string)

WasiInMemoryFileSystem.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

SetTimes(string, DateTimeOffset?, DateTimeOffset?)

WasiInMemoryFileSystem.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.