Standardbibliothek
Antra::FS
import Antra::FSFilesystem operations for Antra.
FS is the path-oriented companion to Antra::IO and Antra::IO::File.
Use it for existence checks, directory creation, copying, moving, removing,
and simple permission changes.
Return style:
- checks like
exists(...),is_file(...),is_dir(...)returnbool - mutating operations return
%Ok[undef]or%Err[%Error{...}] temp_file(...)/temp_dir(...)return%Ok[path]or%Err[%Error{...}]
Überblick#
Fuer pfadbasierte Dateisystem-Operationen, die nicht an offene Handles gebunden sind.
Die erste kleine API ist bewusst knapp gehalten:
FS.exists(...) -> boolFS.is_file(...) -> boolFS.is_dir(...) -> boolFS.mkdir(...) -> %Ok[undef] | %Err[%Error{...}]FS.mkdir_p(...) -> %Ok[undef] | %Err[%Error{...}]FS.rm(...) -> %Ok[undef] | %Err[%Error{...}]FS.rmdir(...) -> %Ok[undef] | %Err[%Error{...}]FS.cp(...) -> %Ok[undef] | %Err[%Error{...}]FS.mv(...) -> %Ok[undef] | %Err[%Error{...}]FS.chmod(...) -> %Ok[undef] | %Err[%Error{...}]— Modus als Zahl, am besten oktal:FS.chmod(path, 0o755)FS.list(dir) -> %Ok[[name, ...]] | %Err[...]— Einträge sortiert, ohne./..FS.copy_tree(source, target) -> %Ok[undef] | %Err[...]— Verzeichnis rekursiv kopierenFS.remove_tree(path) -> %Ok[undef] | %Err[...]— rekursiv löschen; fehlt der Pfad, ist das kein Fehler
Bei Fehlern kannst du also im Result-Kanal direkt err.message lesen. Wenn du
an einer Boundary hart scheitern willst, nimm explizit Result.unwrap!(FS.rm(path))
statt separater FS.rm!-Wrapper.
Fuer temporaere Dateien und Verzeichnisse:
FS.temp_file([prefix: = "antra", suffix: = "", dir: = undef]) -> %Ok[path] | %Err[%Error{...}]FS.temp_dir([prefix: = "antra", dir: = undef]) -> %Ok[path] | %Err[%Error{...}]FS.with_temp_file([prefix:, suffix:, dir:], f) -> Ergebnis von f | %Err[%Error{...}]FS.temp_base() -> String—TMPDIR,TEMPoderTMP, sonst/tmp
temp_file legt <dir>/<prefix>-<zufall><suffix> exklusiv an (nie ueber
einen schon vorhandenen Pfad hinweg) und mit Modus 0600, temp_dir mit
0700; dir: ist standardmaessig FS.temp_base(). Aufraeumen ist Sache des
Aufrufers — ausser bei with_temp_file: der Callback bekommt den Pfad und muss
%Ok[...] oder %Err[...] liefern; danach ist die Datei weg, auch wenn der
Callback wirft (die Exception geht weiter) oder sie selbst verschoben hat.
import Antra::FS
import Antra::IO
val r = FS.with_temp_file([suffix: ".txt"]) |path| -> {
IO.spit(path, "zwischenstand").!
%Ok[IO.slurp(path).!]
}
println(r) # %Ok['zwischenstand']Die Trennung ist absichtlich so gedacht:
Antra::IOfuer bequeme inhaltliche Datei-I/O wieslurp(...)undspit(...)Antra::IO::Filefuer offene Handles und inkrementelles Lesen/SchreibenAntra::FSfuer Operationen auf Pfaden und Verzeichnissen
Funktionen#
chmodcopy_treecpexistsis_diris_filelistmkdirmkdir_pmvremove_treermrmdirtemp_basetemp_dirtemp_filewith_temp_file
fn chmod(path :- String, mode :- Number) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Changes POSIX permissions using a numeric mode such as 0o755 or
decimal 493.
fn copy_tree(source, target)Returns %Ok[undef] or %Err[%Error{...}].
Copies the directory source with everything below it to target
(created if missing; existing files are overwritten).
fn cp(source :- String, target :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
In v1, copies exactly one regular file.
fn exists(path :- String) :- BoolReturns bool.
true if the path exists, otherwise false.
fn is_dir(path :- String) :- BoolReturns bool.
true if the path is a directory, otherwise false.
fn is_file(path :- String) :- BoolReturns bool.
true if the path is a regular file, otherwise false.
fn list(path :- String) :- Result[[String], Any]Returns %Ok[[name, ...]] or %Err[%Error{...}].
The entries of directory path (files and directories, without . and
..), sorted by name.
fn mkdir(path :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Creates exactly one directory and fails if parent directories are missing or the path already exists.
fn mkdir_p(path :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Creates a directory including any missing parent directories.
fn mv(source :- String, target :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Moves or renames a path.
fn remove_tree(path)Returns %Ok[undef] or %Err[%Error{...}].
Removes the directory path with everything below it; a missing path
is not an error.
fn rm(path :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Removes exactly one regular file.
fn rmdir(path :- String) :- Result[Undef, Any]Returns %Ok[undef] or %Err[%Error{...}].
Removes exactly one empty directory.
fn temp_base() :- StringReturns String.
The platform's temp directory: TMPDIR, TEMP or TMP, otherwise
/tmp.
fn temp_dir([prefix: = "antra", dir: = undef])Returns %Ok[path] or %Err[%Error{...}].
Creates a new, empty directory <dir>/<prefix>-<random> with mode
0700. dir: defaults to temp_base().
fn temp_file([prefix: = "antra", suffix: = "", dir: = undef])Returns %Ok[path] or %Err[%Error{...}].
Creates a new, empty file <dir>/<prefix>-<random><suffix> exclusively
(it never reuses an existing path) with mode 0600. dir: defaults to
temp_base(). The caller removes the file; with_temp_file(...) does
that automatically.
fn with_temp_file(f)
fn with_temp_file([
prefix: = "antra",
suffix: = "",
dir: = undef,
], f)Returns the callback's %Ok[...] / %Err[...], or the %Err of
temp_file(...).
Creates a temp file like temp_file(opts), calls f(path) and removes
the file afterwards — also when f throws (the exception is rethrown)
or has already moved or removed it.
