module std::fssource
The file system: directories, paths and what is at them.
File contents are std::io's (File, read_file, write_file); this module
lists, makes, removes and renames, reads metadata, and maps files into
memory (MappedFile). Paths are UTF-8
bytes with '/' between parts, which every system takes; on Windows '\'
too (PathStyle). The externs are the runtime's (alloyc/src/support/
system.c, system_windows.c), which the interpreter reaches
as well.
KindMetadatametadataexistsis_fileis_directorylist_directorymake_directorymake_directoriesremove_directoryremove_filerenamecopy_fileMappedFileMappedFile::openviewis_mappeddisposeKeptMappedFileKeptMappedFile::openviewis_mappeddata_directorycache_directoryPathStylenative_stylejoinparentfile_namestemextensionjoin_inparent_infile_name_instem_inextension_in
type Kindsource
exp type Kind = enum
Variants
FileDirectoryOthera device, a socket, a pipe
type Metadatasource
exp type Metadata = struct
Fields
exp kind: Kindexp size: u64in bytes
exp modified: i64the last change, in nanoseconds since 1970
fn metadatasource
exp fn metadata(path: &[u8]) -> Option<Metadata>
What is at the path, following links, or None when nothing is.
fn existssource
exp fn exists(path: &[u8]) -> bool
fn is_filesource
exp fn is_file(path: &[u8]) -> bool
fn is_directorysource
exp fn is_directory(path: &[u8]) -> bool
fn list_directorysource
exp fn list_directory(path: &[u8]) -> Option<Vector<String>>
The names in the directory, '.' and .. left out, sorted bytewise; or
None when it cannot be read.
fn make_directorysource
exp fn make_directory(path: &[u8]) -> bool
Makes the directory, whose parent must exist; false when it cannot.
fn make_directoriessource
exp fn make_directories(path: &[u8]) -> bool
Makes the directory and any missing above it; true when it exists afterwards.
fn remove_directorysource
exp fn remove_directory(path: &[u8]) -> bool
Removes the directory, which must be empty; false when it cannot.
fn remove_filesource
exp fn remove_file(path: &[u8]) -> bool
fn renamesource
exp fn rename(from: &[u8], target: &[u8]) -> bool
Moves the file or directory, replacing a file at the target; false when it cannot (across file systems among other cases).
fn copy_filesource
exp fn copy_file(from: &[u8], target: &[u8]) -> bool
Copies the file's bytes to the target, replacing it; false when either cannot be opened or a write fails.
type MappedFilesource
exp type MappedFile : Disposable = struct
A file's bytes mapped read-only into the program's memory: the system
reads its pages from the disk as they are first touched and may drop
them again when short of memory, so a large file read in a few places,
such as a font, costs only what is read. Where nothing is mapped (on
the web, under WASI, in alloyc run, for an empty file or one that is
not regular) it is read whole, as read_file does. Disposing it
unmaps it, after which no view of it may be read. The file must not
shrink while it is mapped.
import std::io;
write_file(&"mapped.txt", &"mapped bytes");
var file = if (MappedFile::open(&"mapped.txt") is ::Some |move f|) move f else {
panic "the file was written";
};
expect_equal(&file.view(), &"mapped bytes");
file.dispose();
remove_file(&"mapped.txt");
Functions
fn MappedFile::opensource
exp fn MappedFile::open(path: &[u8]) -> Option<MappedFile>
The file at the path mapped, or read whole where it cannot be; None when it cannot be read.
fn viewsource
exp fn view(self file: &MappedFile) -> &[u8]
The file's bytes.
fn is_mappedsource
exp fn is_mapped(self file: &MappedFile) -> bool
Whether the system maps the file, false where it was read whole.
fn disposesource
exp fn dispose(self file: &var MappedFile)
Unmaps the file.
type KeptMappedFilesource
exp type KeptMappedFile = struct
A file mapped as MappedFile is, for a holder that cannot be disposed:
it stays mapped until the program ends, as a library loaded does, so
its views never dangle. For files a program reads as long as it runs.
Functions
fn KeptMappedFile::opensource
exp fn KeptMappedFile::open(path: &[u8]) -> Option<KeptMappedFile>
As MappedFile::open.
fn viewsource
exp fn view(self file: &KeptMappedFile) -> &[u8]
fn is_mappedsource
exp fn is_mapped(self file: &KeptMappedFile) -> bool
fn data_directorysource
exp fn data_directory(app: &[u8]) -> Option<String>
The directory the app keeps its data in, the user's directory for apps'
data joined with the app's name (the user's directory itself for ""):
$XDG_DATA_HOME/<app> on Linux, ~/.local/share/<app> when that is
not set to an absolute path, %APPDATA%\<app> on Windows,
~/Library/Application Support/<app> on macOS. Under WASI it is the
same as Linux's when the runtime gives the variable and the directory
is preopened; None there otherwise, and on the web, which keeps data in
the browser (std::storage). It may not exist yet.
fn cache_directorysource
exp fn cache_directory(app: &[u8]) -> Option<String>
The directory the app keeps what it can make again in, which the
system may clear: $XDG_CACHE_HOME/<app> (else ~/.cache/<app>),
%LOCALAPPDATA%\<app>, ~/Library/Caches/<app>; as data_directory
otherwise.
type PathStylesource
exp type PathStyle = enum
How paths are written: Linux's, '/' between parts and a leading '/' for
the root, or Windows', '\' or '/' between parts and a root that may hold
a drive ("C:\", "C:" alone relative to that drive's directory) or a
share ("\\server\share\"). The functions below follow the target's
style; the _in ones take it, for paths of another system.
Variants
UnixWindows
fn native_stylesource
exp fn native_style() -> PathStyle
The style of the target the program is built for.
fn joinsource
exp fn join(base: &[u8], part: &[u8]) -> String
The two joined with one separator between them; the part alone when it has a root of its own, the base alone when the part is empty.
fn parentsource
exp fn parent(path: &[u8]) -> &[u8]
The path up to its last part: "a/b" for "a/b/c", "/" for "/c", "" for "c", the root for the root. Trailing separators are not a part.
fn file_namesource
exp fn file_name(path: &[u8]) -> &[u8]
The last part: "c.txt" for "a/b/c.txt", "" for a root.
fn stemsource
exp fn stem(path: &[u8]) -> &[u8]
The last part without its extension: "c" for "a/c.txt", ".profile" for ".profile".
fn extensionsource
exp fn extension(path: &[u8]) -> &[u8]
The extension without its dot: "txt" for "a/c.txt", "" for "a/c" and ".profile".
fn join_insource
exp fn join_in(style: PathStyle, base: &[u8], part: &[u8]) -> String
fn parent_insource
exp fn parent_in(style: PathStyle, path: &[u8]) -> &[u8]
fn file_name_insource
exp fn file_name_in(style: PathStyle, path: &[u8]) -> &[u8]
fn stem_insource
exp fn stem_in(style: PathStyle, path: &[u8]) -> &[u8]
fn extension_insource
exp fn extension_in(style: PathStyle, path: &[u8]) -> &[u8]