std.fs
std.fs: files, and the standard input, output and error streams.
OpenError
pub error OpenError { NotFound, AccessDenied, OpenFailed }
ReadError
pub error ReadError { ReadFailed, OutOfMemory }
WriteError
pub error WriteError { WriteFailed }
ReadFileError
pub error ReadFileError = OpenError || ReadError
PathError
pub error PathError { NotFound, AccessDenied, NotADirectory, Exists, NotEmpty, PathFailed }
ReadDirError
pub error ReadDirError = PathError || AllocError
ReadLinkError
pub error ReadLinkError = PathError || AllocError
Kind
pub enum Kind { File, Dir, Symlink, Other }
What a path names: a regular file, a directory, a symbolic link, or anything else (a device, a socket, a pipe; also an entry that the file system does not describe).
Stat
pub struct Stat {
size: u64
mtime: i64
kind: Kind
}
What `stat` knows about a path: the size in bytes, the modification time in nanoseconds since 1970-01-01 UTC, and the kind.
DirEntry
pub struct DirEntry {
name: []u8
kind: Kind
}
An entry of a directory: its name (without the directory) and kind.
DirEntries
pub struct DirEntries {
items: []mut DirEntry
store: []mut DirEntry
names: []mut u8
alloc: Allocator
pub fn deinit(self: Self)
}
The entries of a directory, from `readDir`. The names are valid until `deinit`.
pub fn deinit(self: Self)-
Free the entries and their names.
File
pub struct File {
fd: i32
pub fn readAll(self: Self, alloc: Allocator) ReadError![]mut u8
pub fn write(self: Self, bytes: []u8) WriteError!void
pub fn read(self: Self, buf: []mut u8) ReadError!usize
pub fn reader(self: Self, alloc: Allocator) Reader
pub fn close(self: Self)
}
An open file: a file descriptor.
pub fn readAll(self: Self, alloc: Allocator) ReadError![]mut u8-
Read the rest of the file into new memory from `alloc` (free it with `alloc.free`). An empty file gives the empty slice.
pub fn write(self: Self, bytes: []u8) WriteError!void-
Write all of `bytes`. Output of `@print` is flushed first, so the two stay in order.
pub fn read(self: Self, buf: []mut u8) ReadError!usize-
Read at most `buf.len` bytes into `buf`: the number read, 0 at the end of the file. Waits for input on a terminal or a pipe.
pub fn reader(self: Self, alloc: Allocator) Reader-
A buffered reader of this file, for `readLine`; its buffer comes from `alloc` (call `deinit`).
pub fn close(self: Self)-
Closes the file.
Reader
pub struct Reader {
file: File
alloc: Allocator
buf: []mut u8 = []mut u8{}
start: usize = 0
end: usize = 0
eof: bool = false
pub fn readLine(self: *mut Self) ReadError!?[]u8
pub fn deinit(self: *mut Self)
}
Reads a file line by line through a buffer that grows to the longest line: `var r = std.fs.stdin().reader(alloc)`, then `while try r.readLine() |line| { ... }`.
pub fn readLine(self: *mut Self) ReadError!?[]u8-
The next line, without its `\n` (or `\r\n`); null at the end of the file (a last line without `\n` is a line too). It reads only as much as it needs, so it answers each line of an interactive input as it comes. The slice is valid until the next `readLine`.
pub fn deinit(self: *mut Self)-
Free the buffer.
open
pub fn open(path: []u8) OpenError!File
Open the file at `path` for reading.
create
pub fn create(path: []u8) OpenError!File
Create the file at `path` for writing, or empty it if it exists.
exists
pub fn exists(path: []u8) bool
Is there a file or directory at `path`?
mtime
pub fn mtime(path: []u8) ?i64
The modification time of the file or directory at `path`, in nanoseconds since 1970-01-01 UTC; null if it cannot be read.
stat
pub fn stat(path: []u8) PathError!Stat
Size, modification time and kind of the file at `path`. This `stat` does not follow a symbolic link: its own kind is `Symlink`, and its size is the length of its target. `statFollow` follows links.
statFollow
pub fn statFollow(path: []u8) PathError!Stat
Like `stat`, but follows symbolic links: the result describes what the link leads to, never a `Symlink`. A link to nothing is `NotFound`.
readDir
pub fn readDir(alloc: Allocator, path: []u8) ReadDirError!DirEntries
The entries of the directory at `path`, in no particular order, without `.` and `..`. Free the result with `deinit`. A symbolic link is a `Symlink`. Where the file system does not give an entry's kind, it is found as `stat` finds it; it is `Other` only for a socket, pipe or device, or if that fails.
mkdir
pub fn mkdir(path: []u8) PathError!void
Create the directory at `path`; its parent must exist.
remove
pub fn remove(path: []u8) PathError!void
Delete the file, symbolic link or empty directory at `path`.
rename
pub fn rename(from: []u8, to: []u8) PathError!void
Rename or move `from` to `to`; an existing `to` is replaced (a file by a file, an empty directory by a directory).
symlink
pub fn symlink(target: []u8, link: []u8) PathError!void
Create a symbolic link at `link` that holds `target` (as given; it is not looked at). An existing `link` is `Exists`; a path of 4096 bytes or more is `PathFailed`.
readLink
pub fn readLink(alloc: Allocator, path: []u8) ReadLinkError![]mut u8
The target a symbolic link holds, as stored (not resolved), in new memory from `alloc` (free it with `alloc.free`). A path that is not a link is `PathFailed`; a target of 4096 bytes or more is `PathFailed`.
readFile
pub fn readFile(alloc: Allocator, path: []u8) ReadFileError![]mut u8
Read the whole file at `path` into new memory from `alloc` (free it with `alloc.free`).
stdin
pub fn stdin() File
Standard input (file descriptor 0).
stdout
pub fn stdout() File
Standard output (file descriptor 1).
stderr
pub fn stderr() File
Standard error (file descriptor 2).