Skip to content
volt
GitHub

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_error

why a file operation failed

Variant
NOT_FOUNDno such file or directory
IOreading or writing failed
PERMISSIONnot allowed
EXISTSsomething is there already
NOT_EMPTYthe directory still has entries
NOT_A_DIRa directory was needed
IS_A_DIRa file was needed
BAD_PATHtoo long for the system, or a NUL byte in it

enum std::fs::seek_from

enum seek_from

where seek counts from

Variant
STARTthe start of the file
CURRENTthe current position
ENDthe end of the file

struct std::fs::file

struct file

An open file, read and written through a buffer (C's FILE). Closed when it's deleted.

Field
handle: void*
failed: bool = falsea write through write_str (std::write) failed; flush reports it

Methods

attach fn read(this: std::fs::file&, buf: u8[..]) -> std::fs::fs_error!usize

read 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!void

write data at the current position

attach fn write_str(this: std::fs::file&, s: str) -> void

write 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!u64

move 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!u64

the current position, in bytes from the start

attach fn flush(this: std::fs::file&) -> std::fs::fs_error!void

write out what's buffered; IO if that (or an earlier std::write into the file) failed

attach fn delete(this: std::fs::file&) -> void

closes the file (writing out what's buffered)

struct std::fs::file_lines

<A: std::mem::allocator> struct file_lines

walks a file's lines (see lines)

Field
f: file*
allocator: Awhere 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!void

replace the file's contents with data (creating it)

std::fs::append_file

fn append_file(path: str, data: str) -> fs_error!void

add data to the end of the file (creating it)

std::fs::exists

fn exists(path: str) -> bool

whether something is at path

std::fs::is_dir

fn is_dir(path: str) -> bool

whether path is a directory (or a link to one)

std::fs::is_file

fn is_file(path: str) -> bool

whether path is there and isn't a directory

std::fs::size

fn size(path: str) -> fs_error!u64

the 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!void

make directory path (its parent has to exist)

std::fs::create_dir_all

fn create_dir_all(path: str) -> fs_error!void

make 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!void

remove the file (or link) at path

std::fs::remove_dir

fn remove_dir(path: str) -> fs_error!void

remove directory path, which has to be empty

std::fs::remove_all

fn remove_all(path: str) -> fs_error!void

remove 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!void

move (or rename) from to to, replacing a file at to

std::fs::copy_file

fn copy_file(from: str, to: str) -> fs_error!void

copy the file at from to to (replacing it)

std::fs::open

fn open(path: str, mode: str = "r") -> fs_error!file

open 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