21 Filesystem Functions

The fs/ namespace reads and changes the host filesystem. Embedded hosts enable filesystem access for each evaluation or call. When access is off, operations that access the filesystem produce #err <fs denied>. Lexical path operations need only the stdlib plugin and do not require the filesystem grant. A function does not retain filesystem access from the evaluation that created it. The fs/ names are reserved. The stardust command enables access when the io plugin is explicitly selected. Byte I/O uses Bytes values. Streaming I/O, pipes, devices, and changes to the process working directory are outside this namespace.

The namespace specifies Linux, macOS, FreeBSD, and Windows behavior. Behavior that differs between them is stated with each function, and an operation a host cannot perform produces #err <fs unsupported>.

Calls that read or change the filesystem run in evaluation order. Each call observes the filesystem at the time it runs.

These functions are not a filesystem sandbox. Lexical path operations can change which file a path containing symbolic links reaches, so they do not replace fs/real-path or an access-control check.

21.1. Common contracts

21.1.1. Paths

A path is a nonempty string in the host platform's path syntax. There is no separate Path or File value. Use "." for the current directory. null, an empty string, and a string that contains a NUL character produce #err <invalid value>. A non-string path produces #err <mismatch type>. A host filename that cannot be represented losslessly as a Dust string produces an error rather than a lossy path.

The namespace does not expand ~, environment variables, or wildcard patterns.

21.1.2. Arguments and options

Required arguments have fixed positions, and an options map comes last. An omitted or null options argument means {}. An unknown option, an option of the wrong type, or a capability that the host does not support produces an error. Boolean options take true or false only, not other truthy values, and can use tag syntax:

(fs/write "app.log" "message\n" {<append>})
(fs/copy "source" "destination" {<replace-existing>})

Arguments are validated before the filesystem changes.

21.1.3. Links and file kinds

Whole-file readers, writers, and fs/copy operate on regular files and follow symbolic links. Exclusive creation treats any existing entry as a collision, including a dangling symbolic link. Deletion and recursive traversal do not follow symbolic links. Intermediate symbolic links in a supplied path use normal host path resolution.

21.1.4. Failures

A filesystem failure returns an #err value tagged fs and one kind:

Tags Raised when
<fs missing> The entry or a parent component does not exist
<fs exists> Exclusive creation found an existing entry
<fs denied> Filesystem access is off, or the host refused access
<fs not-directory> A directory was required
<fs directory> A non-directory was required
<fs not-empty> A directory to remove has entries
<fs loop> Symbolic-link resolution found a cycle
<fs same-file> A copy source and destination are the same file
<fs replaced> A temporary root was replaced before cleanup
<fs unsupported> The host cannot perform the requested operation or setting
<fs io> Any other host failure

The error map carries the operation name, the relevant paths, and the OS error code as diagnostics. A permission failure is denied, never missing.

Filesystem calls respect the evaluation's resource and cancellation limits. Cancellation can be delayed until an operation in progress completes. Directory traversal goes to any depth. fs/walk and fs/remove-all keep one open descriptor for each directory on the current path, so the host's open-file limit bounds the depth; past it they fail with <fs io>. Only fs/mkdir-all creates parent directories. Writes and copies are not transactions: an I/O failure can leave partial output, and success does not promise durability after a crash.

21.2. Paths

Path functions are lexical and do not read the filesystem, except fs/real-path. fs/abs and fs/rel read the process working directory.

Extension functions examine only the final name component. A leading dot alone does not start an extension:

Path fs/ext fs/strip-ext
"archive.tar.gz" "gz" "archive.tar"
".config.json" "json" ".config"
".env" null ".env"
"notes." "" "notes"
".." null ".."

A filename stem is (fs/strip-ext (fs/file-name path)).

21.2.1. fs/join

Function

(fs/join part) → string
(fs/join parent child more*) → string

Returns a path formed from the supplied parts. An absolute child replaces the path before it. Does not resolve symbolic links or collapse . and .. components.

21.2.2. fs/parent

Function

(fs/parent path) → string or null

Returns the lexical parent of path, or null if there is none. Ignores trailing separators except those that form a root.

21.2.3. fs/file-name

Function

(fs/file-name path) → string or null

Returns the final name component of path, or null for a root. Ignores trailing separators.

21.2.4. fs/ext

Function

(fs/ext path) → string or null

Returns the final filename extension without its dot, or null if there is none. Neither . nor .. has an extension. A trailing dot gives the empty string.

21.2.5. fs/strip-ext

Function

(fs/strip-ext path) → string

Returns path with its final filename extension and dot removed. Returns path unchanged when it has no extension.

21.2.6. fs/normalize

Function

(fs/normalize path) → string

Returns a lexically normalized path. Removes redundant separators and . components, and reduces .. components that have a preceding name. Keeps unresolved leading .. components in a relative path. Returns "." when a relative path reduces to the current directory. Does not resolve symbolic links.

21.2.7. fs/abs

Function

(fs/abs path) → string

Returns an absolute path, using the process working directory when path is relative. The target need not exist. Does not resolve symbolic links or normalize dot components.

21.2.8. fs/real-path

Function

(fs/real-path path) → string

Returns an absolute path with all symbolic links resolved. The target must exist. A broken link produces #err <fs missing>, and a link cycle produces #err <fs loop>.

21.2.9. fs/rel

Function

(fs/rel path) → string
(fs/rel base path) → string

Returns a lexical relative path from base to path. base defaults to the process working directory. Both inputs are made absolute against the same working directory and normalized. Returns "." when the normalized paths are equal. Paths under different host roots, such as two drives, produce #err <invalid value>. Does not resolve symbolic links or require the paths to exist.

21.3. Files

Write options are append, create-new, and permissions. append and create-new default to false. append affects only this write, not a persistent file attribute. create-new fails if the path already exists, even when append is also true. When calls compete to create the same path, only one can succeed. permissions takes a permission map and applies only when the call creates the file.

Text I/O uses UTF-8. For other encodings, read Bytes with fs/read and decode them. Content that is neither a string nor Bytes produces #err <mismatch type>.

(fs/write "secret.key" data {<create-new> permissions {user <r w>}})

21.3.1. fs/read

Function

(fs/read path) → Bytes
(fs/read path opts) → Bytes or string

Returns the entire file as Bytes. An empty file returns #b64 "". With {encoding <utf8>}, returns a string and rejects invalid UTF-8 with #err <invalid value>. Text reads retain line endings and a leading byte-order mark.

21.3.2. fs/write

Function

(fs/write path content) → string
(fs/write path content opts) → string

Writes content and returns path. A string is encoded as UTF-8; Bytes are written unchanged. Creates the file if absent and truncates an existing file unless append is true. Adds no newline or byte-order mark.

21.3.3. fs/create-file

Function

(fs/create-file path) → string
(fs/create-file path opts) → string

Exclusively creates an empty regular file and returns path. Any existing entry produces #err <fs exists>. The only option is permissions.

21.3.4. fs/copy

Function

(fs/copy source destination) → string
(fs/copy source destination opts) → string

Copies the contents of source to the exact path destination and returns destination. Options are replace-existing, which defaults to false, and permissions. An existing destination produces #err <fs exists> unless replace-existing is true. With replacement, a destination symbolic link is followed to its target, as the write functions do. When source and destination are the same file, including through a hard or symbolic link, the copy produces #err <fs same-file> before it truncates anything. Does not recurse, copy metadata, or add a filename when destination is a directory.

21.4. Directories

Directory creation takes only the permissions option. Listing functions form child paths under the supplied path, keeping it relative or absolute. They sort siblings by Dust string ordering and include hidden entries. A listing error returns the error, never a partial list.

21.4.1. fs/mkdir

Function

(fs/mkdir path) → string
(fs/mkdir path opts) → string

Creates one directory and returns path. The parent must exist. An existing entry produces #err <fs exists>.

21.4.2. fs/mkdir-all

Function

(fs/mkdir-all path) → string
(fs/mkdir-all path opts) → string

Creates missing parent directories and the directory at path, and returns path. An existing directory is accepted, including through a symbolic link. A conflicting non-directory entry produces #err <fs not-directory>. permissions applies only to the directories that the call creates.

21.4.3. fs/read-dir

Function

(fs/read-dir dir) → list

Returns a list of the immediate child paths of dir. Excludes the . and .. entries. An empty directory returns [].

21.4.4. fs/walk

Function

(fs/walk root) → list

Returns a depth-first list of root and its descendants. Each directory comes before its children. Includes symbolic links but never descends through one, including when the root has trailing separators. A file or symbolic-link root returns a one-element list. A missing root or a traversal failure produces an error. There are no visitor callbacks, glob patterns, or link-following option.

21.5. Inspection

The predicates return false for a missing entry and an error for any other failure.

fs/stat and fs/lstat report the kind of entry as a type member, a tag-only map with one tag:

Tag Entry
file Regular file
dir Directory
symlink Symbolic link, reported only by fs/lstat
block-device Block device
char-device Character device
named-pipe Named pipe (FIFO)
socket Unix domain socket
irregular Any other kind that the host reports
(get (fs/lstat "/dev/null") "type")
; <char-device>

21.5.1. fs/exists?

Function

(fs/exists? path) → boolean

Returns true if path resolves to an existing entry. Returns false if the target or a parent component is absent. Follows symbolic links, so a dangling link returns false.

21.5.2. fs/file?

Function

(fs/file? path) → boolean

Returns true if path resolves to a regular file, following symbolic links.

21.5.3. fs/dir?

Function

(fs/dir? path) → boolean

Returns true if path resolves to a directory, following symbolic links.

21.5.5. fs/stat

Function

(fs/stat path) → map

Returns metadata for the entry that path reaches, following symbolic links. A missing target or other failure produces an error.

The result always has the same members:

Member Value
type File type tag map with exactly one tag
size Host-reported byte length of a regular file, otherwise null
modified, accessed, changed, created #utc timestamp, or null when unavailable
permissions Permission map, or null when unsupported

changed is the metadata-change time, not the creation time. A missing creation time stays null and is not taken from another timestamp. Host timestamps are rounded down to the 100 ns tick. type describes the entry found. Changing the returned map does not change the filesystem.

(fs/stat "run.sh")
; {accessed #utc "2026-11-05T09:12:44.1234567Z"
;  changed #utc "2026-11-01T17:03:10Z"
;  created null
;  modified #utc "2026-11-01T17:03:10Z"
;  permissions {group <r x> other <r x> user <r w x>}
;  size 412
;  type <file>}

21.5.6. fs/lstat

Function

(fs/lstat path) → map

Returns the same metadata as fs/stat for the final entry at path, ignoring trailing separators and without following that entry's symbolic link.

21.6. Permissions

A permission map gives the POSIX permission bits as boolean tags. The members user, group, and other are each a tag-only map of the access bits r, w, and x. The special bits are the top-level tags setuid, setgid, and sticky. On a directory, x permits searching the directory.

Permission map POSIX mode Access
{user <r w>} 0600 User read and write
{group <r> other <r> user <r w>} 0644 User read and write, group and other read
{user <r w x>} 0700 User read, write, and execute
{group <r x> other <r x> user <r w x>} 0755 User all, group and other read and execute
{<setuid> group <r x> other <r x> user <r w x>} 4755 Set-user-ID with the 0755 access bits
{<sticky> group <r w x> other <r w x> user <r w x>} 1777 Shared directory, such as /tmp
{} 0000 No bits

A permission map is the complete mask, not a patch. An omitted class, bit, or special tag is false, so {} requests zero bits. A bit can also be written false explicitly, as in {user <r !w>}. An unknown member, a class that is not a map of booleans, a number, a string, and null produce an error when setting permissions. A host that cannot represent a requested bit produces #err <fs unsupported>.

Returned permission maps contain only true bits and omit a class with no bits. This keeps two results with the same mode equal, and lets a program compare a result with a written map:

(= (get (fs/stat "secret.key") "permissions") {user <r w>})

Permission maps do not describe file types, ACLs, or other platform attributes. File types are the type member of fs/stat. Host file attributes, such as append-only or immutable flags, are not part of this namespace.

A creation permissions option uses the same map. On POSIX hosts, creation filters it through the process umask. When the option is omitted, a file is requested with {group <r w> other <r w> user <r w>} and a directory with {group <r w x> other <r w x> user <r w x>}. Windows has no POSIX permission bits. There, fs/stat returns null permissions, creation uses the OS defaults, and an explicit permissions option or fs/chmod produces #err <fs unsupported>.

21.6.1. fs/chmod

Function

(fs/chmod path permissions) → string

Sets the permission bits of path to the permission map and returns path. Follows symbolic links and does not apply the umask. Windows produces #err <fs unsupported>.

(fs/chmod "script.sh" {group <r x> other <r x> user <r w x>})
(fs/create-file "private.txt" {permissions {user <r w>}})

21.7. Deletion and symbolic links

Deletion does not follow the final symbolic link, and ignores trailing separators, so "link/" removes the link. Deletion never descends through a symbolic link, including one substituted during traversal.

21.7.1. fs/remove

Function

(fs/remove path) → null

Deletes a file, symbolic link, or empty directory and returns null. A missing entry produces #err <fs missing>, and a nonempty directory produces #err <fs not-empty>.

21.7.2. fs/remove-if-exists

Function

(fs/remove-if-exists path) → boolean

Deletes a file, symbolic link, or empty directory. Returns true if it deleted the entry, or false if the entry was absent. A nonempty directory or other failure produces an error.

21.7.3. fs/remove-all

Function

(fs/remove-all path) → null

Deletes a file, symbolic link, or directory tree and returns null. An absent path is accepted. Does not follow symbolic links or change permissions to force deletion. A host root, such as / or a drive root, produces #err <invalid value>, also when the path reaches it through a link, as link/. does for a link to /. A failure can leave part of the tree deleted.

21.8. Temporary resources

Temporary-file options are dir, prefix, suffix, and permissions. Temporary-directory options omit suffix. dir must already exist and defaults to the host temporary directory. prefix defaults to "dust-" and suffix to "". dir, prefix, and suffix must be strings, and a prefix or suffix must not contain a path separator or NUL character. Creation is exclusive even when a candidate name collides.

Temporary resources are private by default. On POSIX hosts, a file is requested with {user <r w>} and a directory with {user <r w x>}, filtered through the umask. On Windows, the resource inherits the access control of dir, and an explicit permissions option produces #err <fs unsupported>. An explicit permissions option replaces the requested default.

The functions do not arrange deletion at process exit.

21.8.1. fs/create-temp

Function

(fs/create-temp) → string
(fs/create-temp opts) → string

Exclusively creates an empty file with a unique name and returns its absolute path.

21.8.2. fs/mkdir-temp

Function

(fs/mkdir-temp) → string
(fs/mkdir-temp opts) → string

Exclusively creates a directory with a unique name and returns its absolute path.

21.8.3. fs/with-temp-dir

Function

(fs/with-temp-dir f) → value
(fs/with-temp-dir f opts) → value

Creates a temporary directory, calls f with its absolute path, and deletes the tree afterwards. Takes the same options as fs/mkdir-temp. It is an ordinary function, not a binding form.

Cleanup runs after f returns, including when f returns an error value, and on cancellation. Abrupt process termination can leave the directory in place. Cleanup never follows symbolic links. Returns the result of f after successful cleanup. A cleanup failure after a successful call produces an error. When both f and cleanup fail, the result is the error from f, with the cleanup failure attached as diagnostics. If the temporary root was replaced, cleanup produces #err <fs replaced> and does not delete the replacement.

(fs/with-temp-dir
  (fn [dir]
    (fs/write (fs/join dir "input.txt") "hello")
    (fs/read (fs/join dir "input.txt") {encoding <utf8>})))
; "hello"