module use "std/fs.nib"
Io.Fs
Reading and writing files, as two interfaces — Io.Fs.Read looks, Io.Fs.Write changes — both answered by Io.Disk.
Io.Fs splits file access in two so a signature can say what a function may do to the disk, not only that it touches it. Io.Fs.Read reads files, checks whether a path exists and lists directories; Io.Fs.Write replaces and appends. A function that only reads declares ! Io.Fs.Read, and a wire that grants only the reader is refused before the run if anything under its entry writes.
Io.Disk answers both. Binding it twice in one wire — Io.Fs.Read = Io.Disk, Io.Fs.Write = Io.Disk — is one disk granted two ways:
use "std/fs.nib"
module Log where
func record :: line: String -> String ! Io.Fs.Read, Io.Fs.Write raises Err.FileNotFound =
let _ = @Io.Fs.Write.append("/tmp/nib-log.txt", line ++ "\n");
@Io.Fs.Read.read("/tmp/nib-log.txt")
func main :: -> String ! Io.Fs.Read, Io.Fs.Write raises Err.FileNotFound =
Log.record("started")
wire W runs Log.main where Io.Fs.Read = Io.Disk, Io.Fs.Write = Io.Disk
Every failure the filesystem reports — a missing file, a missing directory, a permission refusal — arrives as Err.FileNotFound, with the path in the message.
Testing without a disk
Because a module names only the interfaces, a test binds an instance of its own. This one keeps its files in a Map:
use "std/fs.nib"
effector Memory is Io.Fs.Read, Io.Fs.Write where
unkeyed var files :: Map<String, String> = {};
read(path) = if Map.has(files, path) then Map.get_or(files, path, "") else raise Err.FileNotFound(path)
exists(path) = Map.has(files, path)
list(path) = Map.fold(files, #[], { acc, name, body -> Vec.push(acc, name) })
write(path, body) = files := Map.set(files, path, body); ()
append(path, body) = files := Map.set(files, path, Map.get_or(files, path, "") ++ body); ()
flush() = ()
module Log where
func main :: -> String ! Io.Fs.Read, Io.Fs.Write raises Err.FileNotFound =
let _ = @Io.Fs.Write.append("log.txt", "one\n");
let _ = @Io.Fs.Write.append("log.txt", "two\n");
@Io.Fs.Read.read("log.txt")
wire Test runs Log.main where Io.Fs.Read = Memory as disk, Io.Fs.Write = disk
one
two
as disk names the instance so both bindings share it; without it each interface would get a separate, empty Memory.
Interfaces
interface Read where
read(path: String) -> String raises FileNotFound;
exists(path: String) -> Bool;
list(path: String) -> Vec<String> raises FileNotFound
Looking at the disk without changing it.
read(path)answers the file's whole contents. RaisesErr.FileNotFoundwhen there is no file atpath.exists(path)answers whether anything is atpath. It never raises: "no" is an ordinary answer.list(path)answers the names in the directory atpath, sorted, without.and... RaisesErr.FileNotFoundwhen there is no such directory.
Io.File is the older one-operation version of this interface, for programs that only ever call read.
use "std/fs.nib"
module Dir where
func describe :: dir: String -> String ! Io.Fs.Read raises Err.FileNotFound =
if @Io.Fs.Read.exists(dir) then
let names = @Io.Fs.Read.list(dir);
"{Vec.size(names)} entries in {dir}"
else "{dir} is not there"
wire W runs Dir.describe where Io.Fs.Read = Io.Disk
interface Write where
write(path: String, body: String) -> () raises FileNotFound;
append(path: String, body: String) -> () raises FileNotFound;
flush() -> ()
Changing the disk.
write(path, body)replaces the file's contents withbody.append(path, body)addsbodyto the end of the file.flush()asks for everything written so far to be visible to other processes. WithIo.Diskthat is already true whenwriteorappendreturns, soflushcosts nothing; calling it keeps a program correct against an instance that buffers. It does not promise the bytes survive a power cut.
write and append create the file when it is missing, but never its directory: a missing parent raises Err.FileNotFound rather than leaving a file somewhere unexpected.
use "std/fs.nib"
module Save where
func main :: -> () ! Io.Fs.Write raises Err.FileNotFound =
let _ = @Io.Fs.Write.write("/tmp/nib-save.txt", "first draft\n");
@Io.Fs.Write.flush()
wire W runs Save.main where Io.Fs.Write = Io.Disk
Effectors
effector Io.Disk is Io.File, Io.Fs.Read, Io.Fs.Write -- installed by nibc
read :: String -> String raises Err.FileNotFound
exists :: String -> Bool
list :: String -> Vec<String> raises Err.FileNotFound
write :: String, String -> () raises Err.FileNotFound
append :: String, String -> () raises Err.FileNotFound
flush :: -> ()
The machine's filesystem, installed by nibc. One instance answers all three interfaces; which operations a program may reach is decided by which of them the wire binds it to.
A write lands whole or not at all: the new contents are written beside the file and then renamed over it, so a reader never sees half a file. Writing through a symbolic link updates the file the link points to and leaves the link in place. Because of the rename, write needs permission to create files in the destination's directory, not just to change the file.
use "std/io.nib"
use "std/fs.nib"
module Config where
func load :: -> String ! Io.File raises Err.FileNotFound =
@Io.File.read("/etc/hosts")
wire W runs Config.load where Io.File = Io.Disk