Skip to content

fs

Read and write files, list folders, and make temporary folders. Paths are plain Str values; the path module builds and takes them apart.

A script’s main file may call the free functions (fs.read, fs.write and the rest) directly. Every other module receives an fs.Files permission instead, made by fs.files() in the main file, and calls the same operations as methods on it. Every operation that can fail returns ! fs.IoError, which holds the path and a message.

import fs
let dir = try fs.temp_dir()
let file = dir.join("notes.txt")
try fs.write(&file, "first\n")
try fs.append(&file, "second\n")
print(try fs.read(&file))
first
second
import fs
let text = fs.read("/no/such/file.txt") catch {
fs.IoError(e) => {
print(f"could not read {e.path}")
""
}
}
print(text.len())
could not read /no/such/file.txt
0

read_dir gives names, not full paths, sorted. walk gives full paths of everything below a folder.

import fs
let dir = try fs.temp_dir()
try fs.create_dir_all(&dir.join("src/lib"))
try fs.write(&dir.join("src/main.brass"), "print(1)")
try fs.write(&dir.join("src/lib/util.brass"), "")
for name in try fs.read_dir(&dir.join("src")) {
print(name)
}
let base = dir.path()
for p in try fs.walk(&base) {
print(p.slice(base.len() + 1, p.len()))
}
lib
main.brass
src
src/lib
src/lib/util.brass
src/main.brass

Code outside the main file takes &fs.Files. within limits it to one folder.

import fs
fn save(files: &fs.Files, p: &Str) ! fs.IoError {
try files.write(p, "saved")
}
let dir = try fs.temp_dir()
let files = fs.files().within(&dir.path())
try save(&files, &dir.join("ok.txt"))
save(&files, "/etc/brass-test.txt") catch {
fs.IoError(e) => print(e.message)
}
outside the folder this file access is limited to
import fs
pub fn read(path: &Str) -> Str ! IoError
pub fn write(path: &Str, content: &Str) ! IoError
pub fn exists(path: &Str) -> bool
pub fn create_dir_all(path: &Str) ! IoError
pub fn remove(path: &Str) ! IoError

Removes a file or an empty folder. Use remove_dir_all for a folder with contents.

pub fn append(path: &Str, content: &Str) ! IoError

Adds to the end of the file, creating it if needed.

pub fn is_dir(path: &Str) -> bool
pub fn read_dir(path: &Str) -> Vec<Str> ! IoError

The names in a directory (not full paths), sorted, without . and ...

Names only, sorted. Use path.join(dir, name) or TempDir.join to get a full path.

pub fn rename(from: &Str, to: &Str) ! IoError

Replaces to if it exists. Both paths must be on the same file system; use copy and remove to move across file systems.

pub fn copy(from: &Str, to: &Str) ! IoError

Copies a file’s bytes and permission bits, replacing to if it exists.

pub fn metadata(path: &Str) -> Metadata ! IoError

Size, kind, modification time and permissions; follows links to what they point to.

Follows symbolic links. Use link_metadata to learn whether a path is itself a link.

import fs
let dir = try fs.temp_dir()
try fs.write(&dir.join("a.txt"), "hello")
let m = try fs.metadata(&dir.join("a.txt"))
print(f"{m.size} {m.is_file()} {m.is_folder()}")
5 true false
pub fn link_metadata(path: &Str) -> Metadata ! IoError

Like metadata, but a link is reported as a link instead of followed.

pub fn walk(dir: &Str) -> Vec<Str> ! IoError

Every path below dir, folders before what they hold, sorted; links are listed, not followed.

pub fn remove_dir_all(dir: &Str) ! IoError

Removes a folder and everything in it; links inside are removed, never followed.

pub fn temp_dir() -> TempDir ! IoError

A new folder in the system’s temporary folder ($TMPDIR or /tmp).

The folder and everything in it are removed when the TempDir value is dropped, usually at the end of the block that made it. Keep the TempDir alive as long as you use the folder: storing only dir.path() and dropping dir deletes the folder.

pub fn files() -> Files

This program’s own file access; the compiler allows this call only in the main file.

pub struct IoError {
pub path: Str
pub message: Str
}
pub enum Kind {
File
Folder
Link
Other
}
pub struct Metadata {
pub kind: Kind
pub size: i64
pub modified: time.DateTime
pub permissions: i64
}

What metadata reports about one path; permissions holds the Unix bits, such as 0o644.

pub fn is_file(&self) -> bool
pub fn is_folder(&self) -> bool
pub fn is_link(&self) -> bool
pub struct TempDir {
}

A new private folder that is removed, with everything in it, when the value is dropped.

It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn path(&self) -> Str
pub fn join(&self, name: &Str) -> Str

The path of name inside the folder.

fn drop(&mut self)
pub struct Files {
}

Permission to use files outside the main file; within narrows it to one folder.

It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn within(&self, dir: &Str) -> Files

The same access, limited to dir and what is below it; neither .. nor a link can lead out.

Paths are made absolute (relative ones against the current folder) and .. is resolved before the check, so dir/../x cannot climb out. Symbolic links are resolved too, so a link inside dir that points elsewhere is refused, while one pointing to another place inside dir works. Narrowing an already limited Files to a folder outside it gives access to nothing.

pub fn allows(&self, p: &Str) -> bool
pub fn read(&self, p: &Str) -> Str ! IoError
pub fn write(&self, p: &Str, content: &Str) ! IoError
pub fn append(&self, p: &Str, content: &Str) ! IoError
pub fn exists(&self, p: &Str) -> bool
pub fn is_dir(&self, p: &Str) -> bool
pub fn read_dir(&self, p: &Str) -> Vec<Str> ! IoError
pub fn create_dir_all(&self, p: &Str) ! IoError
pub fn remove(&self, p: &Str) ! IoError
pub fn rename(&self, from: &Str, to: &Str) ! IoError
pub fn copy(&self, from: &Str, to: &Str) ! IoError
pub fn metadata(&self, p: &Str) -> Metadata ! IoError
pub fn link_metadata(&self, p: &Str) -> Metadata ! IoError
pub fn walk(&self, p: &Str) -> Vec<Str> ! IoError
pub fn remove_dir_all(&self, p: &Str) ! IoError
pub fn temp_dir(&self) -> TempDir ! IoError

Inside the permitted folder when access is limited, else in the system’s temporary folder.