Read and write files, inspect directories and manage temporary paths with explicit bounds and checked failures.
Import
import stdlib/filesystem as filesystem
For an async function, select the async module instead:
import stdlib/filesystem/async as filesystem
Await effectful operations. In the signatures below, an ordinary effectful function becomes async under the async import; parameter and result types stay the same. Pure functions and value methods remain ordinary calls. The two imports share their value and error types.
metadata()
metadata(path: Path): Result[FileMetadata, FilesystemError]
Parameters
path: Path— entry to inspect.
Returns: Result[FileMetadata,FilesystemError].
Inspects the final directory entry without following a final symlink. Intermediate symlinks and trailing separators use host path resolution. Size is the host byte-size field, not a recursive directory total.
Errors: Returns FilesystemError; see the error categories below.
read()
read(path: Path, limit: Nat): Result[Bytes, FilesystemError]
Parameters
path: Path— regular file to read.limit: Nat— whole-file byte limit, at most 128 MiB.
Returns: Result[Bytes,FilesystemError].
Follows symlinks and preserves bytes. Exceeding the limit fails without returning partial data. Special files are rejected; use stream APIs for streams.
Errors: Returns FilesystemError; see the error categories below.
write()
write(path: Path, data: Bytes): Result[Unit, FilesystemError]
Parameters
path: Path— file to create or truncate.data: Bytes— complete contents, at most 128 MiB.
Returns: Result[Unit,FilesystemError].
Follows symlinks. May leave partial contents on failure. It is neither atomic replacement nor a durability guarantee.
Errors: Returns FilesystemError; see the error categories below.
read_chunk()
read_chunk(file: Path, offset: Nat, limit: Nat): Result[Bytes,FilesystemError]
Parameters
file: Path— regular file to read without following a final symlink.offset: Nat— byte offset, at most 256 MiB.limit: Nat— maximum bytes for this chunk, at most 65,536.
Returns: Result[Bytes,FilesystemError].
Reads a bounded portion of a regular file. With a positive limit, empty bytes indicate no remaining data at the offset. This does not establish directory confinement.
Errors: Returns FilesystemError; see the error categories below.
write_atomic()
write_atomic(file: Path, data: Bytes): Result[Unit,FilesystemError]
Parameters
file: Path— destination file.data: Bytes— complete replacement contents, at most 32 MiB.
Returns: Result[Unit,FilesystemError].
Uses atomic replacement so readers do not observe an incrementally written destination. Atomic visibility does not promise persistence after a crash.
Errors: Returns FilesystemError; see the error categories below.
list_directory()
list_directory(path: Path): Result[[Path], FilesystemError]
Parameters
path: Path— directory to enumerate.
Returns: Result[[Path],FilesystemError], immediate relative child names.
Excludes . and ... Orders names lexicographically by unsigned native bytes. Join each name to its parent before accessing it. Fails without a partial list beyond 65,536 entries or 16 MiB of names. Enumeration is not a snapshot against concurrent changes.
Errors: Returns FilesystemError; see the error categories below.
create_directory()
create_directory(path: Path): Result[Unit, FilesystemError]
Parameters
path: Path— single directory to create.
Returns: Result[Unit,FilesystemError].
Creates one level; it does not create missing ancestors. Fails if the entry already exists. Permissions are subject to the host umask.
Errors: Returns FilesystemError; see the error categories below.
remove_file()
remove_file(path: Path): Result[Unit, FilesystemError]
Parameters
path: Path— file or symbolic link to unlink.
Returns: Result[Unit,FilesystemError].
Removes that entry. Does not recursively remove directories.
Errors: Returns FilesystemError; see the error categories below.
remove_directory()
remove_directory(path: Path): Result[Unit, FilesystemError]
Parameters
path: Path— empty directory to remove.
Returns: Result[Unit,FilesystemError].
Refuses a nonempty directory. It does not recursively delete contents.
Errors: Returns FilesystemError; see the error categories below.
create_temp_file()
create_temp_file(parent: Path): Result[Path,FilesystemError]
Parameters
parent: Path— existing parent directory.
Returns: Result[Path,FilesystemError], the already-created file.
Atomically chooses a unique name and creates a file with permissions 0600 subject to umask. The caller owns cleanup; this is not merely a name reservation.
Errors: Returns FilesystemError; see the error categories below.
create_temp_directory()
create_temp_directory(parent: Path): Result[Path,FilesystemError]
Parameters
parent: Path— existing parent directory.
Returns: Result[Path,FilesystemError], the already-created directory.
Atomically chooses a unique name and creates a directory with permissions 0700 subject to umask. The caller must remove its contents and directory.
Errors: Returns FilesystemError; see the error categories below.
directories_overlap()
directories_overlap(left: Path, right: Path): Result[Bool,FilesystemError]
Parameters
left: Path— first directory.right: Path— second directory.
Returns: Result[Bool,FilesystemError].
Checks physical directory overlap rather than comparing path spellings. The result describes that operation, not a permanent guarantee against later changes.
Errors: Returns FilesystemError; see the error categories below.
Paths, cancellation and ownership
Construct paths using the path module. Ordinary paths do not confine access: relative paths, .. and symlinks follow host rules. Use confined reads when access must remain under a root. Async operations run on bounded workers. Cancelling a wait does not undo an already-started filesystem side effect. Temporary paths do not gain automatic cleanup.
FileMetadata
record FileMetadata { kind: FileKind, size: Nat }
enum FileKind { RegularFile, Directory, SymbolicLink, OtherFile }
FilesystemError
record FilesystemError { operation: FilesystemOperation, kind: FilesystemErrorKind, native: HostError }
operation records the requested operation and its paths/bounds. Missing entries, permissions, existing destinations, wrong entry types and exceeded limits have distinct categories. Unknown host codes remain OtherFailure. Use problem.kind for control flow and render for a human-readable explanation. The native field preserves the original failure. Host-backed native errors contain operation: Str, code: Str and native_code: Nat; a zero native number means no host error number was supplied.
enum FilesystemErrorKind {
NotFound,
PermissionDenied,
AlreadyExists,
NotDirectory,
IsDirectory,
NotRegularFile,
DirectoryNotEmpty,
SymlinkRejected,
OutputLimitExceeded,
LimitExceeded,
InvalidArgument,
InvalidPath,
InvalidParent,
InvalidStage,
StageBusy,
ParentChanged,
StageChanged,
DestinationConflict,
PublicationFailed,
CleanupFailed,
StageCreationFailed,
StageOpenFailed,
FilesystemFailure,
IOFailure,
ResourceLimit,
OutOfMemory,
Cancelled,
UnsupportedHost,
OtherFailure
}
FilesystemError.render()
pure render(): Str
Parameters
None.
Returns: Str, a human-readable explanation of this error, including the available operation and failure details.
Pure. Use the structured error fields for control flow; the wording is diagnostic text and must not be parsed.
Example and compatibility
The temporary-note program preserves both the primary failure and a cleanup failure. temp_file and temp_directory remain compatibility names for the canonical creation operations.
Complete example: async temporary note
import stdlib/encoding/utf8 as utf8
import stdlib/filesystem/async as files
import stdlib/path as paths
import stdlib/console/async as console
import stdlib/host/async as host
async write_and_read(file: Path): Result[Str,Str] {
match await files.write(file, "remember the milk".encode_utf8()) {
Error(problem) => Error(problem.render()),
Ok(written) => match await files.read(file, 1024) {
Error(problem) => Error(problem.render()),
Ok(data) => match utf8.decode(data) {
Error(decode_error) => Error("The note is not valid UTF-8."), Ok(text) => Ok(text)
}
}
}
}
async temporary_note(): Result[Str,Str] {
match await files.create_temp_file(paths.current()) {
Error(problem) => Error(problem.render()),
Ok(file) => {
result = await write_and_read(file)
// Always attempt cleanup, including when writing or reading failed.
match await files.remove_file(file) {
Ok(removed) => result,
Error(cleanup) => match result {
Ok(text) => Error(cleanup.render()),
Error(original) => Error(original + "\nCleanup also failed: " + cleanup.render())
}
}
}
}
}
async main(): Unit {
match await temporary_note() {
Error(message) => {
reported = await console.write_error_line(message)
await host.exit(1)
},
Ok(text) => match await console.write_line(text) {
Error(write_error) => await host.exit(1), Ok(displayed) => ()
}
}
}Implementation: view source on GitHub.