@std/os/tree.ms#
Import each required symbol with import Name from "@std/os/tree.ms".
EmbeddedTree Type#
A read-only tree over files held in the program. Generated modules for [embed] in ms.toml build one with
embedded_tree: making it costs one map entry per file and one index entry per path, and read copies only the
file asked for.
type EmbeddedTree {
indexes Map[String, Int]
directories Set[String]
kids Map[String, List[String]]
load fn(Int) Bytes
}EmbeddedTree.directories#
Clean paths of every directory, including the root (the empty string).
directories Set[String]EmbeddedTree.indexes#
The number each file is loaded by, by clean path.
indexes Map[String, Int]EmbeddedTree.kids#
Internal state: clean paths directly under each directory, so listing is not a scan of the tree.
kids Map[String, List[String]]EmbeddedTree.load#
Returns the bytes of the file with that number.
load fn(Int) BytesEmbeddedTree.children#
Lists the files and directories directly under path, sorted by name.
fn children(path String, max_entries Int? = null) Result[List[DirectoryEntry], Error]EmbeddedTree.read#
Returns a copy of one file; the other files are not touched.
fn read(path String, max_bytes Int? = null) Result[Bytes, Error]FileTree Interface#
Read files and list directories through one interface: use host_tree(root) for disk
files or memory_tree() to supply test data without temporary files.
interface FileTree {
fn read(path String, max_bytes Int? = null) Result[Bytes, Error]
fn children(path String, max_entries Int? = null) Result[List[DirectoryEntry], Error]
}FileTree.children#
Lists a directory’s children, sorted by name. A missing path is ENOENT, a file
is EINVAL, and more than max_entries children (when given) is EOVERFLOW.
fn children(path String, max_entries Int? = null) Result[List[DirectoryEntry], Error]FileTree.read#
Reads a whole file; more than max_bytes bytes returns EOVERFLOW. A missing
path is ENOENT.
fn read(path String, max_bytes Int? = null) Result[Bytes, Error]HostTree Type#
The host filesystem below root, read through the FileTree interface. A path
cannot climb out of root, and a symbolic link that leads outside it is EINVAL;
links that stay inside are followed.
type HostTree {
root String
}HostTree.root#
Directory that is the tree’s root, in the host’s spelling.
root StringHostTree.children#
Lists a directory below the root from the host disk, sorted by name.
fn children(path String, max_entries Int? = null) Result[List[DirectoryEntry], Error]HostTree.confine#
Checks that full, once its symbolic links are resolved, is inside the root. A path
that does not resolve is left to the read or listing, which reports why.
fn confine(full String) Result[Void, Error]HostTree.read#
Reads a file below the root from the host disk; failures are the host’s Error.
fn read(path String, max_bytes Int? = null) Result[Bytes, Error]MemoryTree Type#
A file tree held in memory: build it with write_file and create_directory, change
it with remove, and read it through FileTree.
type MemoryTree {
files Map[String, Bytes]
directories Set[String]
kids Map[String, Set[String]] = Map[String, Set[String]]()
}MemoryTree.directories#
Clean paths of directories; the root is the empty string and always present.
directories Set[String]MemoryTree.files#
File contents by clean path.
files Map[String, Bytes]MemoryTree.kids#
Internal state: clean paths of the entries directly under each directory, so listing is not a scan of the tree.
kids Map[String, Set[String]] = Map[String, Set[String]]()MemoryTree.children#
Lists the files and directories directly under path, sorted by name.
fn children(path String, max_entries Int? = null) Result[List[DirectoryEntry], Error]MemoryTree.create_directory#
Creates path and any missing parent directories. An existing directory is fine; an
existing file is EEXIST, and a file where a parent directory should be is EINVAL.
var fn create_directory(path String) Result[Void, Error]MemoryTree.exists#
True when path names a file or directory.
fn exists(path String) BoolMemoryTree.link#
Records path as a child of its parent directory.
var fn link(path String) VoidMemoryTree.read#
Reads a file held in this tree.
fn read(path String, max_bytes Int? = null) Result[Bytes, Error]MemoryTree.remove#
Removes a file or an empty directory; recursive removes a directory with
everything under it. A missing path is ENOENT, a non-empty directory without
recursive is ENOTEMPTY, and the root cannot be removed.
var fn remove(path String, recursive Bool = false) Result[Void, Error]MemoryTree.unlink#
Forgets path as a child of its parent directory.
var fn unlink(path String) VoidMemoryTree.write_file#
Writes a file, replacing any earlier content and creating missing parent directories.
A path that is a directory, or that has a file for an ancestor, is EINVAL; the root
cannot be written.
var fn write_file(path String, data Bytes) Result[Void, Error]WalkEntry Type#
One file or directory found by walk.
type WalkEntry {
path String
directory Bool
}WalkEntry.directory#
True when the entry is a directory (its children follow it).
directory BoolWalkEntry.path#
Path relative to the walked root, /-separated.
path Stringembedded_tree Function#
Returns a read-only tree over file numbers by clean path, the clean paths of every directory (the root, the empty string, included) and the function that returns the bytes of a file number.
fn embedded_tree(move indexes Map[String, Int], move directories Set[String], move load fn(Int) Bytes) EmbeddedTree { ... }host_tree Function#
Returns a tree over the directory root of the host filesystem.
fn host_tree(root String) HostTree { ... }memory_tree Function#
Returns an empty in-memory tree holding only its root directory.
fn memory_tree() MemoryTree { ... }msstd_embed_bytes Function#
Bytes held in the program’s read-only data. [embed] modules call this with the Base64 text of a file as a
string literal; the compiler decodes the text when it builds the program, so the running program holds the raw bytes
and a read decodes nothing. The argument must be a string literal.
fn msstd_embed_bytes(encoded String) Bytes { ... }walk Function#
Lists everything below root depth first, a directory before its children and siblings sorted by name.
Symbolic links on a host tree appear as non-directory entries and are not followed. max_entries bounds the
number of entries found, returning EOVERFLOW rather than a partial list.
fn walk(tree FileTree, root String = "", max_entries Int? = null) Result[List[WalkEntry], Error] { ... }