std::fs
Files and directories: whole files, file streams, listing, walking, creating and removing.
Source: std/fs.volt
Types
error std::fs::fs_error
error fs_errorwhy a file operation failed
| Variant | |
|---|---|
NOT_FOUND | no such file or directory |
IO | reading or writing failed |
PERMISSION | not allowed |
EXISTS | something is there already |
NOT_EMPTY | the directory still has entries |
NOT_A_DIR | a directory was needed |
IS_A_DIR | a file was needed |
BAD_PATH | too long for the system, or a NUL byte in it |
enum std::fs::seek_from
enum seek_fromwhere seek counts from
| Variant | |
|---|---|
START | the start of the file |
CURRENT | the current position |
END | the end of the file |
struct std::fs::file
struct fileAn open file, read and written through a buffer (C's FILE). Closed when it's deleted.
| Field | |
|---|---|
handle: void* | |
failed: bool = false | a write through write_str (std::write) failed; flush reports it |
Methods
attach fn read(this: std::fs::file&, buf: u8[..]) -> std::fs::fs_error!usizeread up to buf.len bytes into buf: how many (0 at the end of the file)
<A: std::mem::allocator = std::mem::default_allocator> attach fn read_all(this: std::fs::file&, allocator: A = {}) -> std::fs::fs_error!std::string<A>the rest of the file
<A: std::mem::allocator = std::mem::default_allocator> attach fn read_line(this: std::fs::file&, allocator: A = {}) -> std::fs::fs_error!(std::string<A>?)the next line, without its "\n" (or "\r\n"); null at the end of the file. Any length, NUL bytes too
<A: std::mem::allocator = std::mem::default_allocator> attach fn lines(this: std::fs::file&, allocator: A = {}) -> std::fs::file_lines<A>for (line) in f.lines(): each line as read_line gives it, until the end (or a read error)
attach fn write(this: std::fs::file&, data: str) -> std::fs::fs_error!voidwrite data at the current position
attach fn write_str(this: std::fs::file&, s: str) -> voidwrite s: this makes a file a writer, for std::write. A failure shows at the next flush
attach fn seek(this: std::fs::file&, offset: i64, from: std::fs::seek_from = std::fs::seek_from::START) -> std::fs::fs_error!u64move to offset bytes from the start (or the current position, or the end): the new position
attach fn position(this: std::fs::file&) -> std::fs::fs_error!u64the current position, in bytes from the start
attach fn flush(this: std::fs::file&) -> std::fs::fs_error!voidwrite out what's buffered; IO if that (or an earlier std::write into the file) failed
attach fn delete(this: std::fs::file&) -> voidcloses the file (writing out what's buffered)
struct std::fs::file_lines
<A: std::mem::allocator> struct file_lineswalks a file's lines (see lines)
| Field | |
|---|---|
f: file* | |
allocator: A | where each line goes |
Methods
<A: std::mem::allocator> attach fn next(this: std::fs::file_lines<A>&) -> std::string<A>?the next line
Functions
std::fs::read_file
<A: std::mem::allocator = std::mem::default_allocator> fn read_file(path: str, allocator: A = {}) -> fs_error!std::string<A>the whole file
std::fs::write_file
fn write_file(path: str, data: str) -> fs_error!voidreplace the file's contents with data (creating it)
std::fs::append_file
fn append_file(path: str, data: str) -> fs_error!voidadd data to the end of the file (creating it)
std::fs::exists
fn exists(path: str) -> boolwhether something is at path
std::fs::is_dir
fn is_dir(path: str) -> boolwhether path is a directory (or a link to one)
std::fs::is_file
fn is_file(path: str) -> boolwhether path is there and isn't a directory
std::fs::size
fn size(path: str) -> fs_error!u64the size of the file at path, in bytes
std::fs::list_dir
<A: std::mem::allocator = std::mem::default_allocator> fn list_dir(path: str, allocator: A = {}) -> fs_error!std::vec<std::string<A>, A>the names in directory path (not "." or ".."), sorted
std::fs::walk
<A: std::mem::allocator = std::mem::default_allocator> fn walk(path: str, allocator: A = {}) -> fs_error!std::vec<std::string<A>, A>every file under directory path, in its subdirectories too (not the directories themselves), as paths starting with path, sorted; links to directories aren't followed
std::fs::create_dir
fn create_dir(path: str) -> fs_error!voidmake directory path (its parent has to exist)
std::fs::create_dir_all
fn create_dir_all(path: str) -> fs_error!voidmake directory path and any of its parents that are missing; fine if it's there already
std::fs::remove_file
fn remove_file(path: str) -> fs_error!voidremove the file (or link) at path
std::fs::remove_dir
fn remove_dir(path: str) -> fs_error!voidremove directory path, which has to be empty
std::fs::remove_all
fn remove_all(path: str) -> fs_error!voidremove path: a file, or a directory with everything in it. A link is removed, not followed
std::fs::rename
fn rename(from: str, to: str) -> fs_error!voidmove (or rename) from to to, replacing a file at to
std::fs::copy_file
fn copy_file(from: str, to: str) -> fs_error!voidcopy the file at from to to (replacing it)
std::fs::open
fn open(path: str, mode: str = "r") -> fs_error!fileopen the file at path. mode: "r" reads; "w" writes, creating or emptying it; "a" appends, creating it; "r+" reads and writes; "w+" empties it, then both; "a+" reads and appends