radlang — File System (fs)
This document covers the fs namespace: reading and writing files, inspecting
paths, listing directories, and querying file metadata.
For language fundamentals see the Overview.
Overview
fs is a compiler-intrinsic namespace. The name is reserved and dispatched
directly to the runtime — it needs no import:
fn main() {
if sys.blockOn(fs.writeText("hello.txt", "hi there")) {
string? contents = sys.blockOn(fs.readText("hello.txt"))
sys.output(contents ?? "(unreadable)")
}
}Content reads and writes are asynchronous and return futures. Use await inside
an async fn or sys.blockOn from synchronous code. The *Sync variants keep
the blocking behavior and return their result directly. Write, append, mkdir,
and delete report success rather than throwing. readText returns a
nullable string so a missing or unreadable file is distinguishable from an
empty one.
Reading files
Text
fs.readText(path: string) : Future<string?>
fs.readTextSync(path: string) : string?Reads an entire file as text. Returns null if the file cannot be read
(missing, permission denied, etc.), so use ?? or a null check.
fn main() {
string? data = sys.blockOn(fs.readText("notes.txt"))
if data == null {
sys.error("could not read notes.txt")
return
}
sys.output(data)
} Bytes
fs.readBytes(path: string) : Future<byte[]>
fs.readBytesSync(path: string) : byte[]Reads a file as a list of raw bytes — use this for binary data or when you need
byte-level access. A missing or unreadable file gives an empty list, not an
error (use fs.exists or fs.stat to tell it from an empty file).
fn main() {
byte[] bytes = sys.blockOn(fs.readBytes("image.png"))
sys.output("read " + bytes.length().toString() + " bytes")
} Large binary files and model weights
fs.mapBytesSync(path: string) : byte[]
fs.readBytesAtSync(path: string, offset: long, count: long) : byte[]mapBytesSync maps a regular file into virtual memory. Pages are loaded on
access, so opening a large model does not first copy the whole file into the
arena. The result is a normal byte[] with private copy-on-write storage:
modifying a byte does not change the file. Empty files produce an empty list.
The mapping follows arena ownership, survives return compaction, and is unmapped
when the last value that holds it is released. Do not truncate or modify the
underlying file while it is mapped.
A mapped byte[] is never copied byte for byte by the runtime. Where another
list would be copied because it outlives the scope that built it — stored in a
namespace or global variable, a list or a struct from an async fn, a callback
(syn.after, onMessage, …) or a closure, or returned through a Future —
the stored list shares the mapped pages and keeps the mapping alive instead, so
mapping a model inside a callback or coroutine and keeping it costs no more
memory than doing it in main. Because the pages are shared, a byte written
through one of those lists is visible through the others (the file itself is
never changed). Explicit copies (slice, +, json) still copy.
readBytesAtSync reads at most count bytes starting at a 64-bit byte offset.
It returns a shorter list at EOF, an empty list at/beyond EOF or for zero count,
and throws for negative offsets/counts or I/O errors. Process chunks in a
using fn to reclaim each chunk’s storage before reading the next one.
Both APIs throw if the path cannot be opened as a supported regular file.
byte[] weights = fs.mapBytesSync("model.gguf")
long totalBytes = weights.length64()
long offset = 0
byte[] header = fs.readBytesAtSync("model.gguf", offset, 4096)All lists expose .length64() (or .length64) as a long; use long indices
and offsets for large buffers. The existing .length() / .length stays an
int and throws above int.max instead of silently wrapping. Explicit casts
to int can still narrow a value; keep model sizes as long throughout.
Arena reservations are fast-path capacity, not hard allocation ceilings. Allocations that do not fit automatically use separately owned storage while keeping existing pointers stable. No arena-size setting is required. Ordinary whole-file reads still need enough memory for their buffers and copies; address-space and OS resource limits still apply.
Writing files
fs.writeText(path: string, text: string) : Future<bool>
fs.writeTextSync(path: string, text: string) : bool
fs.writeBytes(path: string, data: byte[]) : Future<bool>
fs.writeBytesSync(path: string, data: byte[]) : bool
fs.appendText(path: string, text: string) : Future<bool>
fs.appendTextSync(path: string, text: string) : bool
fs.appendBytes(path: string, data: byte[]) : Future<bool>
fs.appendBytesSync(path: string, data: byte[]) : bool
fs.writeBytesAt(path: string, offset: long, data: byte[]) : Future<bool> // overwrite in place
fs.writeBytesAtSync(path: string, offset: long, data: byte[]) : boolEach returns true on success, false on failure (for example when the parent
directory does not exist: none of these create directories). write* truncates
and replaces any existing file; append* adds to the end (creating the file if
needed). writeBytesAt* overwrites the bytes starting at offset and leaves the
rest of the file alone.
fn main() {
sys.blockOn(fs.writeText("log.txt", "first line\n"))
sys.blockOn(fs.appendText("log.txt", "second line\n"))
byte[] header = [0x89, 0x50, 0x4E, 0x47]
sys.blockOn(fs.writeBytes("out.bin", header))
}For blocking code, use writeTextSync, writeBytesSync, appendTextSync,
and appendBytesSync instead.
Paths
fs.basename(path) : string // final component
fs.dirname(path) : string // parent directory
fs.extension(path) : string // file extension
fs.resolve(path) : string // absolute, canonical path
fs.join(a, b) : string // join two path segmentsextension includes the dot and returns "" when there is none. join drops
the duplicate separator (fs.join("src/", "/main.rad") is src/main.rad).
resolve follows symlinks and removes . and ..; for a path that does not
exist it returns the path unchanged.
fn main() {
sys.output(fs.basename("/usr/local/bin/rad")) // rad
sys.output(fs.dirname("/usr/local/bin/rad")) // /usr/local/bin
sys.output(fs.extension("archive.tar.gz")) // .gz
sys.output(fs.join("src", "main.rad")) // src/main.rad
sys.output(fs.resolve("./config.toml")) // /abs/path/config.toml
}Directories
fs.mkdir(path: string) : bool // create a directory (and any parents)
fs.delete(path: string) : Future<bool> // async remove
fs.deleteSync(path: string) : bool // blocking remove
fs.listDir(path: string) : string[] // entry names only
fs.listDirFull(path: string) : string[] // full paths of entries
fs.walk(path: string) : Future<string[]> // recursive paths
fs.walkSync(path: string) : string[]fn main() {
fs.mkdir("build")
string[] names = fs.listDir(".") // ["main.rad", "config.toml", ...]
for (string name in names) {
sys.output(name)
}
string[] full = fs.listDirFull("src") // ["src/main.rad", "src/lib.rad", ...]
sys.output(full.length().toString() + " entries")
}mkdir creates missing parent directories and returns true when the
directory already exists. delete removes one file or one empty directory: a
non-empty directory, or a path that does not exist, returns false. listDir
and listDirFull return an empty list for a path that cannot be read.
walk recursively returns the paths below a directory, directories included
(["build/z.txt", "build/sub", "build/sub/w.txt"]). walkSync performs the
same traversal on the calling thread.
Directory metadata
type DirEntry {
string path
FileInfo info
}
fs.readDirInfo(path: string) : Future<DirEntry[]>
fs.readDirInfoSync(path: string) : DirEntry[]These return immediate children (not recursive), with each child’s full path
and metadata. The asynchronous form performs directory enumeration and
stat calls on the worker pool.
File operations
fs.rename(from: string, to: string) : Future<bool>
fs.renameSync(from: string, to: string) : bool
fs.copy(from: string, to: string) : Future<bool>
fs.copySync(from: string, to: string) : bool
fs.tempFile() : Future<string>
fs.tempFileSync() : string
fs.tempDir() : Future<string>
fs.tempDirSync() : stringrename and copy report success or failure. copy copies the contents of one
file (a directory source fails) and overwrites the destination. The temporary
path helpers create a unique empty file or directory under /tmp (named
radlang-XXXXXX) and return its path; remove it with fs.delete when done.
Watching a path
fs.watch(path: string, fn(string changedPath): void callback): voidRegisters a callback that runs on the microtask queue after the watched path’s existence, size, or modification time changes. Watching is asynchronous and does not block the RadLang main thread. Callbacks should be short-lived; an active watch uses one shared async worker while it waits for a change.
File metadata
fs.exists(path: string) : bool
fs.stat(path: string) : FileInfofs.exists is a quick existence check. fs.stat returns a FileInfo struct
with full metadata:
| Field | Type | Meaning |
|---|---|---|
exists |
bool | Whether the path exists |
isFile |
bool | Is a regular file |
isDir |
bool | Is a directory |
size |
int | Size in bytes (32-bit, truncated for files of 2 GiB or more; use readBytesAtSync or mapBytesSync and length64() for those) |
modified |
long | Last-modified time, epoch milliseconds (whole-second resolution) |
created |
long | Creation time, epoch milliseconds (the birth time on macOS, the inode change time on Linux) |
permissions |
int | POSIX permission bits (0644 is 420) |
fn main() {
if fs.exists("main.rad") {
FileInfo info = fs.stat("main.rad")
sys.output("is file: " + info.isFile.toString())
sys.output("size: " + info.size.toString() + " bytes")
sys.output("modified: " + info.modified.toString())
}
}Note. When a path does not exist,
fs.statstill returns aFileInfowithexists = false,size = -1, times of 0 and flags false, rather than failing — checkinfo.existsfirst.
Quick reference
| Call | Returns | Purpose |
|---|---|---|
fs.readText(p) |
Future<string?> | Async text read (null on failure) |
fs.readTextSync(p) |
string? | Blocking text read |
fs.readBytes(p) |
Future<byte[]> | Async byte read |
fs.readBytesSync(p) |
byte[] | Blocking byte read |
fs.mapBytesSync(p) |
byte[] | Private file mapping, pages loaded on access |
fs.readBytesAtSync(p, offset, count) |
byte[] | Read a range using 64-bit offsets/counts |
fs.writeText(p, s) / writeBytes(p, b) |
Future<bool> | Async overwrite |
fs.writeTextSync(p, s) / writeBytesSync(p, b) |
bool | Blocking overwrite |
fs.appendText(p, s) / appendBytes(p, b) |
Future<bool> | Async append |
fs.appendTextSync(p, s) / appendBytesSync(p, b) |
bool | Blocking append |
fs.exists(p) |
bool | Does the path exist |
fs.stat(p) |
FileInfo | File metadata |
fs.basename(p) / dirname(p) / extension(p) |
string | Path components |
fs.resolve(p) |
string | Absolute canonical path |
fs.join(a, b) |
string | Join path segments |
fs.listDir(p) |
string[] | Directory entry names |
fs.listDirFull(p) |
string[] | Directory entries as full paths |
fs.walk(p) / walkSync(p) |
Future<string[]> / string[] | Recursive directory paths |
fs.readDirInfo(p) / readDirInfoSync(p) |
Future<DirEntry[]> / DirEntry[] | Immediate entries with metadata |
fs.rename(a, b) / renameSync(a, b) |
Future<bool> / bool | Rename a path |
fs.copy(a, b) / copySync(a, b) |
Future<bool> / bool | Copy a file |
fs.tempFile() / tempFileSync() |
Future<string> / string | Create a temporary file |
fs.tempDir() / tempDirSync() |
Future<string> / string | Create a temporary directory |
fs.watch(p, callback) |
void | Invoke callback after a path change |
fs.mkdir(p) |
bool | Create a directory |
fs.delete(p) |
Future<bool> | Async remove a file or directory |
fs.deleteSync(p) |
bool | Blocking remove a file or directory |
Picking the right tool
- Text config/logs →
fs.readText/fs.writeText/fs.appendText. - Binary files →
fs.readBytes/fs.writeBytes. - Just checking presence →
fs.exists; need size/times →fs.stat. - Manipulating path strings →
basename/dirname/extension/join/resolve. - Walking a folder →
fs.listDir(names) orfs.listDirFull(paths). - Recursive traversal →
fs.walk; immediate entries plus metadata →fs.readDirInfo. - File moves/copies →
fs.rename/fs.copy; use*Synconly when blocking is acceptable.