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.
Read and write a file
Section titled “Read and write a file”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))firstsecondHandle a missing file
Section titled “Handle a missing file”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.txt0List a folder
Section titled “List a folder”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()))}libmain.brasssrcsrc/libsrc/lib/util.brasssrc/main.brassPass file access to other code
Section titled “Pass file access to other code”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 toReference
Section titled “Reference”import fsFunctions
Section titled “Functions”pub fn read(path: &Str) -> Str ! IoErrorpub fn write(path: &Str, content: &Str) ! IoErrorexists
Section titled “exists”pub fn exists(path: &Str) -> boolcreate_dir_all
Section titled “create_dir_all”pub fn create_dir_all(path: &Str) ! IoErrorremove
Section titled “remove”pub fn remove(path: &Str) ! IoErrorRemoves a file or an empty folder. Use remove_dir_all for a folder with contents.
append
Section titled “append”pub fn append(path: &Str, content: &Str) ! IoErrorAdds to the end of the file, creating it if needed.
is_dir
Section titled “is_dir”pub fn is_dir(path: &Str) -> boolread_dir
Section titled “read_dir”pub fn read_dir(path: &Str) -> Vec<Str> ! IoErrorThe 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.
rename
Section titled “rename”pub fn rename(from: &Str, to: &Str) ! IoErrorReplaces 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) ! IoErrorCopies a file’s bytes and permission bits, replacing to if it exists.
metadata
Section titled “metadata”pub fn metadata(path: &Str) -> Metadata ! IoErrorSize, 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 falselink_metadata
Section titled “link_metadata”pub fn link_metadata(path: &Str) -> Metadata ! IoErrorLike metadata, but a link is reported as a link instead of followed.
pub fn walk(dir: &Str) -> Vec<Str> ! IoErrorEvery path below dir, folders before what they hold, sorted; links are listed, not followed.
remove_dir_all
Section titled “remove_dir_all”pub fn remove_dir_all(dir: &Str) ! IoErrorRemoves a folder and everything in it; links inside are removed, never followed.
temp_dir
Section titled “temp_dir”pub fn temp_dir() -> TempDir ! IoErrorA 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() -> FilesThis program’s own file access; the compiler allows this call only in the main file.
IoError
Section titled “IoError”pub struct IoError { pub path: Str pub message: Str}pub enum Kind { File Folder Link Other}Metadata
Section titled “Metadata”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.
Metadata.is_file
Section titled “Metadata.is_file”pub fn is_file(&self) -> boolMetadata.is_folder
Section titled “Metadata.is_folder”pub fn is_folder(&self) -> boolMetadata.is_link
Section titled “Metadata.is_link”pub fn is_link(&self) -> boolTempDir
Section titled “TempDir”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.
TempDir.path
Section titled “TempDir.path”pub fn path(&self) -> StrTempDir.join
Section titled “TempDir.join”pub fn join(&self, name: &Str) -> StrThe path of name inside the folder.
TempDir.drop
Section titled “TempDir.drop”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.
Files.within
Section titled “Files.within”pub fn within(&self, dir: &Str) -> FilesThe 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.
Files.allows
Section titled “Files.allows”pub fn allows(&self, p: &Str) -> boolFiles.read
Section titled “Files.read”pub fn read(&self, p: &Str) -> Str ! IoErrorFiles.write
Section titled “Files.write”pub fn write(&self, p: &Str, content: &Str) ! IoErrorFiles.append
Section titled “Files.append”pub fn append(&self, p: &Str, content: &Str) ! IoErrorFiles.exists
Section titled “Files.exists”pub fn exists(&self, p: &Str) -> boolFiles.is_dir
Section titled “Files.is_dir”pub fn is_dir(&self, p: &Str) -> boolFiles.read_dir
Section titled “Files.read_dir”pub fn read_dir(&self, p: &Str) -> Vec<Str> ! IoErrorFiles.create_dir_all
Section titled “Files.create_dir_all”pub fn create_dir_all(&self, p: &Str) ! IoErrorFiles.remove
Section titled “Files.remove”pub fn remove(&self, p: &Str) ! IoErrorFiles.rename
Section titled “Files.rename”pub fn rename(&self, from: &Str, to: &Str) ! IoErrorFiles.copy
Section titled “Files.copy”pub fn copy(&self, from: &Str, to: &Str) ! IoErrorFiles.metadata
Section titled “Files.metadata”pub fn metadata(&self, p: &Str) -> Metadata ! IoErrorFiles.link_metadata
Section titled “Files.link_metadata”pub fn link_metadata(&self, p: &Str) -> Metadata ! IoErrorFiles.walk
Section titled “Files.walk”pub fn walk(&self, p: &Str) -> Vec<Str> ! IoErrorFiles.remove_dir_all
Section titled “Files.remove_dir_all”pub fn remove_dir_all(&self, p: &Str) ! IoErrorFiles.temp_dir
Section titled “Files.temp_dir”pub fn temp_dir(&self) -> TempDir ! IoErrorInside the permitted folder when access is limited, else in the system’s temporary folder.