Skip to content

std.fs - ZZ

Comprehensive non-blocking filesystem. All fallible ops return Result<_, str> with unified fs:<op>:<code>: <path> diagnostics (identical on VM/AOT). Predicates return bool. Open handles are Opaque("file") and dispatch file.* methods. Pluggable providers: Os / Mem / Tar / Embed.

import std.fs

std.bytes holds arbitrary binary data safely — no UTF-8 validation, no lossy decoding. Produced by std.fs.read_bytes, fs.read_chunk_bytes, fs.read_bytes_at, and bytes.finish. Use for file uploads, ciphertext, binary protocols, and any payload that isn’t valid UTF-8.

bytes is immutable (b[i] = x is an error). Slices (b[1:4]) share the underlying store with zero copies.

import std.bytes
import std.fs
match fs.read_bytes("data.bin") {
.ok(b) => {
println(len(b)) // or b.len()
println(b[0]) // u8 as int (negatives wrap)
println(b[1:4]) // zero-copy slice -> bytes
println(typeof(b)) // bytes
for x in b { println(x) } // iterate ints
}
.err(e) => println(e),
}
// Build binary incrementally
buf := bytes.builder()
buf = bytes.push_byte(buf, 0x48) // 'H'
buf = bytes.push_byte(buf, 0x65) // 'e'
match bytes.finish(buf) {
.ok(b) => println(b), // "He"
.err(e) => println(e),
}
  • Slices are zero-copy (share the store).
  • bytes.builder + push_byte + finish is O(n); never concatenate binary in a loop.
  • read_bytes caches content in the process-wide FS cache (see std.fs).
  • Immutable — safe to share across tasks without locking.
import std.bytes
import std.crypto
import std.encoding
// Hash a binary file
match fs.read_bytes("payload.bin") {
.ok(b) => {
println(crypto.sha256_bytes(b)) // hex digest over raw bytes
}
.err(e) => println(e),
}
// Base64 round-trip preserving binary
match encoding.base64_decode_bytes("aGk=") {
.ok(raw) => println(bytes.to_ints(raw)[0]), // first byte as int
.err(e) => println(e),
}

std.fs covers file read/write, directory ops, streaming handles, path lexing, and pluggable FS providers (Os / Mem / Tar / Embed). Reads share a process-wide stat-validated content cache. All paths use / separators internally (backslash accepted, ./.. resolved).

import std.fs
match fs.read("data.txt") {
.ok(content) => println(content),
.err(e) => println("Error: {e}"),
}
match fs.write("out.txt", "Hello World") {
.ok(_) => println("Written"),
.err(e) => println("Error: {e}"),
}
if fs.exists("config.toml") {
println("config found")
}

read_bytes, read_chunk_bytes, read_bytes_at return bytes: one contiguous buffer (~1 byte RSS per byte), slices share zero-copy, prints like [104, 105], immutable (b[i] = x errors), JSON as int arrays.

match fs.read_bytes("data.bin") {
.ok(b) => {
println(len(b)) // or b.len()
println(b[0]) // u8 as int
println(b[1:4]) // zero-copy slice -> bytes
for x in b { println(x) }
}
.err(e) => println(e),
}

read_chunk is text (lossy on arbitrary bytes); use read_chunk_bytes for binary.

import std.fs
func main() -> Result<int, str> {
match fs.open("log.txt", "a") {
.ok(f) => {
fs.write_chunk(f, "line\n")?
fs.flush(f)?
fs.close(f)?
}
.err(e) => println(e),
}
return .ok(0)
}

fs.read_chunk(f, n) (text, "" at EOF), fs.read_chunk_bytes(f, n) (bytes, empty at EOF), fs.write_chunk(f, data), fs.seek(f, pos), fs.flush(f), fs.close(f).

Function Description
fs.normalize(path) Lexical normalize (./.., roots, separators)
fs.join(a, b) Join + normalize (b wins when absolute)
fs.basename(path) Final segment
fs.dirname(path) Directory part
fs.is_absolute(path) Rooted check (/x, C:\x, \\unc\…)
fs.extension(path) Extension without dot
match fs.memfs() {
.ok(m) => {
fs.write_at(m, "a/b.txt", "hello")
match fs.read_to_string_at(m, "a/b.txt") {
.ok(c) => println(c),
.err(e) => println(e),
}
}
.err(e) => println(e),
}
  • fs.osfs() — real OS filesystem.
  • fs.memfs() — thread-safe in-memory tree (tests, caches).
  • fs.tarfs(path) — read-only plain .tar view (regular files + dirs; symlinks/devices skipped).
  • fs.embedfs() — read-only --embed assets (empty unless CLI got --embed <dir>).

*_at ops: read_to_string_at, read_bytes_at, write_at, append_at, exists_at, is_file_at, is_dir_at, read_dir_at, mkdir_all_at, remove_file_at.

  • fs.scan_counts(path, markers, bstart, bend, nested, whole) — fused read + NUL-sniff + str.classify → [lines, code, comments, blanks, binary].
  • fs.is_generated(path, markers) — minified/generated-content heuristic.
Terminal window
zz build --embed ./assets main.zz
zz run --embed ./assets main.zz

File handles dispatch method calls:

import std.fs
func main() -> Result<int, str> {
f := fs.open("log.txt", "a")?
f.write_chunk("line\n")?
f.flush()?
f.close()?
return .ok(0)
}
  • All fallible ops return fs:<op>:<code>: <path> diagnostics — identical on VM/AOT.
  • Reads are cached (stat-validated); writes/removals invalidate eagerly; outside writes detected by len/mtime.
  • read_chunk is lossy on arbitrary bytes — use read_chunk_bytes for binary.
  • bytes are immutable — b[i] = x is an error.
  • fs.rename was removed — use fs.move.
import std.fs
// Atomic-ish write: write to temp, then move
func write_atomic(path: str, content: str) -> Result<unit, str> {
tmp := "{path}.tmp"
return match fs.write(tmp, content) {
.ok(_) => fs.move(tmp, path),
.err(e) => .err(e),
}
}
func main() -> Result<int, str> {
// In-memory test fs
match fs.memfs() {
.ok(mem) => {
fs.write_at(mem, "config.toml", "key = 'val'")?
println(fs.read_to_string_at(mem, "config.toml")?)
}
.err(e) => println(e),
}
return .ok(0)
}

std.path resolves, joins, and inspects paths without touching the disk. Separator style follows std.env.os() ("windows" → \, else /). Use for building paths before passing them to std.fs.

52 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.fs.append append(path: str, content: str) -> Result<unit, str> Append
std.fs.append_at append_at(fsys: zzfs, path: str, content: str) -> Result<unit, str> Provider append
std.fs.basename basename(path: str) -> str Final segment
std.fs.close close(f: file) -> Result<unit, str> Close handle
std.fs.copy copy(src: str, dst: str) -> Result<unit, str> Copy
std.fs.dirname dirname(path: str) -> str Directory part
std.fs.embedfs embedfs() -> Result<zzfs, str> Embed provider
std.fs.exists exists(path: str) -> bool Existence
std.fs.exists_at exists_at(fsys: zzfs, path: str) -> bool Provider exists
std.fs.extension extension(path: str) -> str Extension without dot
std.fs.flush flush(f: file) -> Result<unit, str> Flush handle
std.fs.is_absolute is_absolute(path: str) -> bool Rooted check
std.fs.is_dir is_dir(path: str) -> bool Dir predicate
std.fs.is_dir_at is_dir_at(fsys: zzfs, path: str) -> bool Provider is_dir
std.fs.is_file is_file(path: str) -> bool File predicate
std.fs.is_file_at is_file_at(fsys: zzfs, path: str) -> bool Provider is_file
std.fs.is_generated is_generated(path: str, markers: [str]) -> Result<bool, str> Generated heuristic
std.fs.join join(a: str, b: str) -> str Join + normalize
std.fs.memfs memfs() -> Result<zzfs, str> Memory provider
std.fs.mkdir mkdir(path: str) -> Result<unit, str> Create dir
std.fs.mkdir_all mkdir_all(path: str) -> Result<unit, str> Create dirs recursively
std.fs.mkdir_all_at mkdir_all_at(fsys: zzfs, path: str) -> Result<unit, str> Provider mkdir_all
std.fs.move move(src: str, dst: str) -> Result<unit, str> Move/rename
std.fs.normalize normalize(path: str) -> str Lexical normalize
std.fs.open open(path: str, mode: str) -> Result<file, str> Streaming handle (r/w/a)
std.fs.osfs osfs() -> Result<zzfs, str> OS provider
std.fs.read read(path: str) -> Result<str, str> Read file contents
std.fs.read_bytes read_bytes(path: str) -> Result<bytes, str> Raw bytes
std.fs.read_bytes_at read_bytes_at(fsys: zzfs, path: str) -> Result<bytes, str> Provider read bytes
std.fs.read_chunk read_chunk(f: file, n: int) -> Result<str, str> Text chunk
std.fs.read_chunk_bytes read_chunk_bytes(f: file, n: int) -> Result<bytes, str> Binary chunk
std.fs.read_dir read_dir(path: str) -> Result<[str], str> Sorted basenames
std.fs.read_dir_at read_dir_at(fsys: zzfs, path: str) -> Result<[str], str> Provider read_dir
std.fs.read_to_string_at read_to_string_at(fsys: zzfs, path: str) -> Result<str, str> Provider read
std.fs.remove remove(path: str) -> Result<unit, str> Delete file
std.fs.remove_dir_all remove_dir_all(path: str) -> Result<unit, str> Recursive remove
std.fs.remove_file_at remove_file_at(fsys: zzfs, path: str) -> Result<unit, str> Provider remove file
std.fs.scan_counts scan_counts(path: str, markers: [str], bstart: str, bend: str, nested: bool, whole: bool) -> Result<[int], str> Fused classify
std.fs.seek seek(f: file, pos: int) -> Result<int, str> Seek position
std.fs.stat stat(path: str) -> Result<{str: str}, str> Metadata dict
std.fs.tarfs tarfs(path: str) -> Result<zzfs, str> Tar provider
std.fs.walk_dir walk_dir(path: str) -> Result<[str], str> Recursive list
std.fs.write write(path: str, content: str) -> Result<unit, str> Write file
std.fs.write_at write_at(fsys: zzfs, path: str, content: str) -> Result<unit, str> Provider write
std.fs.write_chunk write_chunk(f: file, data: str) -> Result<int, str> Write to handle
File.open open(path: str, mode: str) -> Result<file, str> Streaming handle (r/w/a)
file.close close(f: file) -> Result<unit, str> —
file.flush flush(f: file) -> Result<unit, str> —
file.read_chunk read_chunk(f: file, n: int) -> Result<str, str> —
file.read_chunk_bytes read_chunk_bytes(f: file, n: int) -> Result<bytes, str> —
file.seek seek(f: file, pos: int) -> Result<int, str> —
file.write_chunk write_chunk(f: file, data: str) -> Result<int, str> —

Full index: API reference · Standard library