std.fs
sync files and folders; paths over 1023 bytes fail with path_too_long
FsError
Section titled “FsError”pub FsError :: errors { not_found, create_failed, read_failed, write_failed, seek_failed, delete_failed, path_too_long, rename_failed,}not_found: missing path (or access denied)create_failed: create/append failedread_failed: a read returned an errorwrite_failed: a write returned an errorseek_failed: a seek returned an errordelete_failed: delete failed (or folder not empty)path_too_long: path + NUL over 1024 bytesrename_failed: rename could not move the file
pub File :: struct { handle: i64,}open OS file; a Reader and a Writer
is_open
Section titled “is_open”pub is_open :: fn(self: *File) -> boolpub close :: fn(self: *File)close the handle; idempotent
pub open :: fn(path: []const u8) -> File ! FsErroropen for reading; not_found if it can’t be
create
Section titled “create”pub create :: fn(path: []const u8) -> File ! FsErrorcreate for writing; truncates an existing file
append
Section titled “append”pub append :: fn(path: []const u8) -> File ! FsErroropen for appending; creates it if missing
exists
Section titled “exists”pub exists :: fn(path: []const u8) -> boolfile or dir exists; never fails
is_dir
Section titled “is_dir”pub is_dir :: fn(path: []const u8) -> booldelete
Section titled “delete”pub delete :: fn(path: []const u8) -> void ! FsErrorremove a file; delete_failed if it can’t
make_dir
Section titled “make_dir”pub make_dir :: fn(path: []const u8) -> void ! FsErrormake folder path (ok if it exists); create_failed if no parent or a file is there
make_dirs
Section titled “make_dirs”pub make_dirs :: fn(path: []const u8) -> void ! FsErrormake path and any missing parents (/ or \ separators)
remove_dir
Section titled “remove_dir”pub remove_dir :: fn(path: []const u8) -> void ! FsErrorremove empty folder path; delete_failed otherwise
read_all
Section titled “read_all”pub read_all :: fn(path: []const u8, a: *Arena) -> []u8 ! FsErrorwhole file into a new buffer in a
DirIter
Section titled “DirIter”pub DirIter :: struct { handle: i64, namebuf: [1024]u8, namelen: usize,}dir iteration; names include “.” and “..”, valid only until the next dir_next
open_dir
Section titled “open_dir”pub open_dir :: fn(path: []const u8) -> DirIter ! FsErrordir_next
Section titled “dir_next”pub dir_next :: fn(self: *DirIter) -> ?[]const u8next entry name, or null at the end
dir_is_dir
Section titled “dir_is_dir”pub dir_is_dir :: fn(self: *DirIter) -> boollast entry is a dir
dir_size
Section titled “dir_size”pub dir_size :: fn(self: *DirIter) -> u64last entry’s size in bytes; always 0 on linux (stat the path)
dir_is_reparse
Section titled “dir_is_reparse”pub dir_is_reparse :: fn(self: *DirIter) -> boollast entry is a symlink/reparse point; skip these to avoid cycles
dir_close
Section titled “dir_close”pub dir_close :: fn(self: *DirIter)pub Info :: struct { size: u64, modified_ns: i64, is_dir: bool,}result of info
size: bytes (0 for a folder on Linux and others)modified_ns: last write, ns since the unix epoch
pub info :: fn(path: []const u8) -> Info ! FsErrorsize, mtime, kind of path; not_found if nothing is there
rename
Section titled “rename”pub rename :: fn(from: []const u8, to: []const u8) -> void ! FsErrorrename/move; replaces a file at to; rename_failed (missing, dir target, cross-volume)
write_file
Section titled “write_file”pub write_file :: fn(path: []const u8, data: []const u8) -> void ! FsErrorwrite data to path, created or truncated
copy_file
Section titled “copy_file”pub copy_file :: fn(from: []const u8, to: []const u8) -> u64 ! FsErrorcopy a file, to created or truncated; returns bytes copied
pub Entry :: struct { path: []const u8, name: []const u8, is_dir: bool, depth: usize,}a walk entry
path: root + names below it, joined by “/”name: the last part ofpathdepth: 1 for an entry of the root folder itself
Walker
Section titled “Walker”pub Walker :: struct { region: *Arena, dirs: Vec(DirIter), paths: Vec([]const u8), failed: bool,}recursive walk, folders before their contents; links listed but not followed
dirs: the open folders, the innermost lastpaths: their paths
Walker.next
Section titled “Walker.next”pub next :: fn(self: *Self) -> ?EntryWalker.close
Section titled “Walker.close”pub close :: fn(self: *Self)close what’s still open if the loop stops early
pub walk :: fn(a: *Arena, root: []const u8) -> Walker ! FsErrorrecursive walk; paths live in a
pub cwd :: fn(a: *Arena) -> []const u8 ! FsErrorcurrent folder, absolute, in a; not_found if removed or path too long
set_cwd
Section titled “set_cwd”pub set_cwd :: fn(path: []const u8) -> void ! FsErrorset the current folder, process-wide (all threads); not_found if not a folder
absolute
Section titled “absolute”pub absolute :: fn(a: *Arena, p: []const u8) -> []const u8 ! FsErrorabsolute p in a via path::normalize; only cwd is read, so .. is lexical
temp_dir
Section titled “temp_dir”pub temp_dir :: fn(a: *Arena) -> []const u8temp folder, no trailing sep (windows: TMP, TEMP, USERPROFILE; else TMPDIR or /tmp)
home_dir
Section titled “home_dir”pub home_dir :: fn(a: *Arena) -> ?[]const u8USERPROFILE on windows, HOME elsewhere; null if unset
TempFile
Section titled “TempFile”pub TempFile :: struct { file: File, path: []const u8,}from create_temp: the file, open to write, and its path
create_temp
Section titled “create_temp”pub create_temp :: fn(a: *Arena, dir: []const u8, pattern: []const u8) -> TempFile ! FsErrornew exclusive file in dir (“” = temp_dir()); last * of pattern -> 16 hex digits
caller closes and deletes it; owner-only on linux; create_failed if no name worked
make_temp_dir
Section titled “make_temp_dir”pub make_temp_dir :: fn(a: *Arena, dir: []const u8, pattern: []const u8) -> []const u8 ! FsErrornew temp folder, named like create_temp; path in a; create_failed if none worked
remove_tree
Section titled “remove_tree”pub remove_tree :: fn(path: []const u8) -> void ! FsErrordelete a file or a whole folder tree; links are removed, not followed not_found if missing; delete_failed (open, read-only) after deleting what it could