FileSystemBackend Class

The storage of the bucket file systems (https://fs.spec.whatwg.org/#bucket-file-system) of the browsing contexts: the origin private file system of each origin (navigator.storage.getDirectory()), e.g. a directory, a database or InMemoryFileSystemBackend (see BrowsingContextOptions.FileSystem). The backend stores a tree of directories and files per origin; the library implements the API (the handles, the name validation, the locks of the files, the writable streams, which replace the content of a file when they are closed, and the access handles, which read and write a file in place).

public abstract class FileSystemBackend

Remarks

An entry is identified by the serialization of its origin (e.g. https://example.com) and by its path: the names of the directories from the root directory of the origin, then its own name (an empty path is the root directory, which always exists). The names are valid file names (https://fs.spec.whatwg.org/#valid-file-name): they are not empty, are not . nor .., and contain neither / nor \; they are compared ordinally.

The methods are called on the threads of the event loops of the windows and the workers that use the file system: an implementation must be thread-safe. The library guarantees that a file is not written while a writable stream replaces its content, and that a file with an open access handle is not replaced, moved nor removed. The exceptions reject the promises of the API (an IOException is reported as an InvalidStateError).

Constructors

NameDescription
FileSystemBackend()

FileSystemBackend()

protected FileSystemBackend()

No documentation is available.

Methods

NameDescription
CreateEntry(string, IReadOnlyList<string>, FileSystemHandleKind, long)Creates an empty file or an empty directory, and returns true; returns false when its parent directory does not exist or when an entry already has its name.
FlushFile(string, IReadOnlyList<string>)Transfers the cached modifications of a file to the storage device (FileSystemSyncAccessHandle.flush()). The default does nothing.
GetEntries(string, IReadOnlyList<string>)Returns the entries of a directory (in any order), or null when the directory does not exist.
GetEntryKind(string, IReadOnlyList<string>)Returns the kind of the entry at a path, or null when there is none.
GetFileInfo(string, IReadOnlyList<string>)Returns the size and the modification timestamp of a file, or null when the file does not exist.
GetUsage(string)Returns an estimation of the number of bytes used by the file system of an origin (navigator.storage.estimate()). The default is 0: the usage is not reported.
MoveFile(string, IReadOnlyList<string>, IReadOnlyList<string>)Moves a file to another path (another name, another directory, or both), replacing the file that has the new path when there is one, and returns true; returns false when the file or the new parent directory does not exist, or when the new path is the one of a directory.
ReadFile(string, IReadOnlyList<string>, long, Span<byte>)Reads bytes of a file from a position, and returns the number of bytes read (less than the length of the buffer at the end of the file; 0 when the file does not exist).
RemoveEntry(string, IReadOnlyList<string>)Removes a file, or a directory with all its entries, and returns true; returns false when the entry does not exist. The root directory is emptied, not removed.
ReplaceFileContent(string, IReadOnlyList<string>, ReadOnlyMemory<byte>, long)Replaces the content of a file atomically (a writable stream is closed), and returns true; returns false when the file does not exist.
SetFileSize(string, IReadOnlyList<string>, long, long)Sets the size of a file: the file is truncated, or grows with zeros.
WriteFile(string, IReadOnlyList<string>, long, ReadOnlySpan<byte>, long)Writes bytes into a file at a position (the access handles write in place): the file grows when the bytes end after its end, and the bytes between its end and a greater position are zeros.

CreateEntry(string, IReadOnlyList<string>, FileSystemHandleKind, long)

Creates an empty file or an empty directory, and returns true; returns false when its parent directory does not exist or when an entry already has its name.

public abstract bool CreateEntry(string origin, IReadOnlyList<string> path, FileSystemHandleKind kind, long modificationTime)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the new entry.
kind FileSystemHandleKind
The kind of the entry.
modificationTime long
The modification timestamp of a new file, in milliseconds since the Unix epoch.

Returns

bool

FlushFile(string, IReadOnlyList<string>)

FileSystemBackend.cs

Transfers the cached modifications of a file to the storage device (FileSystemSyncAccessHandle.flush()). The default does nothing.

public virtual void FlushFile(string origin, IReadOnlyList<string> path)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.

GetEntries(string, IReadOnlyList<string>)

Returns the entries of a directory (in any order), or null when the directory does not exist.

public abstract IReadOnlyList<FileSystemEntry>? GetEntries(string origin, IReadOnlyList<string> path)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the directory (empty for the root directory).

Returns

IReadOnlyList<FileSystemEntry>?

GetEntryKind(string, IReadOnlyList<string>)

Returns the kind of the entry at a path, or null when there is none.

public abstract FileSystemHandleKind? GetEntryKind(string origin, IReadOnlyList<string> path)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the entry (empty for the root directory).

Returns

FileSystemHandleKind?

GetFileInfo(string, IReadOnlyList<string>)

Returns the size and the modification timestamp of a file, or null when the file does not exist.

public abstract FileSystemFileInfo? GetFileInfo(string origin, IReadOnlyList<string> path)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.

Returns

FileSystemFileInfo?

GetUsage(string)

FileSystemBackend.cs

Returns an estimation of the number of bytes used by the file system of an origin (navigator.storage.estimate()). The default is 0: the usage is not reported.

public virtual long GetUsage(string origin)

Parameters

origin string
The serialization of the origin.

Returns

long

MoveFile(string, IReadOnlyList<string>, IReadOnlyList<string>)

Moves a file to another path (another name, another directory, or both), replacing the file that has the new path when there is one, and returns true; returns false when the file or the new parent directory does not exist, or when the new path is the one of a directory.

public abstract bool MoveFile(string origin, IReadOnlyList<string> path, IReadOnlyList<string> newPath)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.
newPath IReadOnlyList<string>
The new path of the file.

Returns

bool

ReadFile(string, IReadOnlyList<string>, long, Span<byte>)

Reads bytes of a file from a position, and returns the number of bytes read (less than the length of the buffer at the end of the file; 0 when the file does not exist).

public abstract int ReadFile(string origin, IReadOnlyList<string> path, long position, Span<byte> buffer)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.
position long
The position of the first byte to read.
buffer Span<byte>
The buffer to fill.

Returns

int

RemoveEntry(string, IReadOnlyList<string>)

Removes a file, or a directory with all its entries, and returns true; returns false when the entry does not exist. The root directory is emptied, not removed.

public abstract bool RemoveEntry(string origin, IReadOnlyList<string> path)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the entry.

Returns

bool

ReplaceFileContent(string, IReadOnlyList<string>, ReadOnlyMemory<byte>, long)

Replaces the content of a file atomically (a writable stream is closed), and returns true; returns false when the file does not exist.

public abstract bool ReplaceFileContent(string origin, IReadOnlyList<string> path, ReadOnlyMemory<byte> content, long modificationTime)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.
content ReadOnlyMemory<byte>
The new content.
modificationTime long
The new modification timestamp of the file, in milliseconds since the Unix epoch.

Returns

bool

SetFileSize(string, IReadOnlyList<string>, long, long)

Sets the size of a file: the file is truncated, or grows with zeros.

public abstract void SetFileSize(string origin, IReadOnlyList<string> path, long size, long modificationTime)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.
size long
The new size.
modificationTime long
The new modification timestamp of the file, in milliseconds since the Unix epoch.

WriteFile(string, IReadOnlyList<string>, long, ReadOnlySpan<byte>, long)

Writes bytes into a file at a position (the access handles write in place): the file grows when the bytes end after its end, and the bytes between its end and a greater position are zeros.

public abstract void WriteFile(string origin, IReadOnlyList<string> path, long position, ReadOnlySpan<byte> data, long modificationTime)

Parameters

origin string
The serialization of the origin.
path IReadOnlyList<string>
The path of the file.
position long
The position of the first byte to write.
data ReadOnlySpan<byte>
The bytes to write.
modificationTime long
The new modification timestamp of the file, in milliseconds since the Unix epoch.