Manuscript Documentation
v2026.12.0

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

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

Manuscript
directories Set[String]

EmbeddedTree.indexes#

The number each file is loaded by, by clean path.

Manuscript
indexes Map[String, Int]

EmbeddedTree.kids#

Internal state: clean paths directly under each directory, so listing is not a scan of the tree.

Manuscript
kids Map[String, List[String]]

EmbeddedTree.load#

Returns the bytes of the file with that number.

Manuscript
load fn(Int) Bytes

EmbeddedTree.children#

Lists the files and directories directly under path, sorted by name.

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

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

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

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

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

Manuscript
type HostTree {
    root String
}

HostTree.root#

Directory that is the tree’s root, in the host’s spelling.

Manuscript
root String

HostTree.children#

Lists a directory below the root from the host disk, sorted by name.

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

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

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

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

Manuscript
directories Set[String]

MemoryTree.files#

File contents by clean path.

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

Manuscript
kids Map[String, Set[String]] = Map[String, Set[String]]()

MemoryTree.children#

Lists the files and directories directly under path, sorted by name.

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

Manuscript
var fn create_directory(path String) Result[Void, Error]

MemoryTree.exists#

True when path names a file or directory.

Manuscript
fn exists(path String) Bool

Records path as a child of its parent directory.

Manuscript
var fn link(path String) Void

MemoryTree.read#

Reads a file held in this tree.

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

Manuscript
var fn remove(path String, recursive Bool = false) Result[Void, Error]

Forgets path as a child of its parent directory.

Manuscript
var fn unlink(path String) Void

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

Manuscript
var fn write_file(path String, data Bytes) Result[Void, Error]

WalkEntry Type#

One file or directory found by walk.

Manuscript
type WalkEntry {
    path String
    directory Bool
}

WalkEntry.directory#

True when the entry is a directory (its children follow it).

Manuscript
directory Bool

WalkEntry.path#

Path relative to the walked root, /-separated.

Manuscript
path String

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

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

Manuscript
fn host_tree(root String) HostTree { ... }

memory_tree Function#

Returns an empty in-memory tree holding only its root directory.

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

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

Manuscript
fn walk(tree FileTree, root String = "", max_entries Int? = null) Result[List[WalkEntry], Error] { ... }