The prelude's file effects are native: the runtime implements them itself,
without a shell or a helper program, so each can promise exactly what it
does. They are all LocalMachine effects except installFileText, which
runs over SSH. Each one appears in plans, is recorded in the run log, and is
answered from the log on replay: a replayed read reads nothing and a
replayed write writes nothing.
None of them is a sandbox or an atomic snapshot of a tree. They check what they can (the kind of each path component, links, identities before and after) and report a change they detect; a determined concurrent writer can still race them.
Where reads and writes are allowed
- The read-only policy (
captain run --read-only) admits every read and observation on this page and refuses every write. - A write lands only beneath a directory the host names, after every link in
the path is resolved:
captain run --allow-write DIR. A host that names none admits no writes at all, whatever else it allows.
Host policies covers both.
Text files by path
These four take a whole Path. They are the simple ones, and the ones to
reach for when a program edits text files.
| Effect | Gives | Notes |
|---|---|---|
readFileText path: p maxBytes: b |
FileText |
a regular file only, never through a link; valid UTF-8 only; refuses a file larger than b, and the runtime caps b at 16 MiB |
writeFileText path: p content: t replacing: d |
FileWritten |
replaces an existing regular file only if its content still has digest d, the digest its read returned; writes beside the file and renames into place, keeping its mode. VM 3 |
createFileText path: p content: t |
FileWritten |
makes a file that must not exist. VM 3 |
listDirectory path: p maxEntries: n |
List[DirEntry] |
one directory's entries sorted by name; fails rather than truncate past n (at most 100,000). VM 3 |
| Type | Members |
|---|---|
FileText |
path: Path, content: Text, size: i64, digest: Digest (SHA-256 of the bytes returned) |
FileWritten |
path: Path, size: i64, digest: Digest; the content is not repeated |
DirEntry |
name: Text, kind: Symbol (#file, #directory, #link or #other; links are reported, not followed), size: i64 (for a file) |
These effects fail the run when they cannot do what was asked; they do not
return a Result. In a plan, text longer than 160 bytes is shown as a preview
with its size and digest, so the plan stays readable and still binds to the
exact content.
The replacing: digest is what makes a read-edit-write safe: if anything
changed the file after the program read it, the write is refused instead of
overwriting that change.
module Example::Edit.
;; Replace one exact passage in a file. Refused if the passage is not there
;; exactly once, or if the file changed after it was read.
export bump path: Path from: Text to: Text -> Text [
file := readFileText path: path maxBytes: 1MiB.
((file content) replaceOnce: from with: to)
ifOk: [ :updated |
written := writeFileText path: path content: updated replacing: (file digest).
ret Text from: (written digest) ]
ifFail: [ :e | ret (e message) ].
].Files under a root
The remaining local effects name a file as a root: Path and a
relativePath: Text beneath it, and walk from the root one component at a
time through opened directories. The rules are shared:
rootis absolute and must exist. Links in the root itself are resolved once; the opened directory is then the root.relativePathis split on/. No component may be empty,.or.., or contain NUL or a backslash; an absolute relative path is refused. Effects that change things also limit it to 4096 bytes and 64 components.- A link anywhere below the root is refused (
observePathandremoveTreereport or remove a link at the leaf instead of following it). - Expected filesystem problems come back as
Failvalues with a stablekind, so a program can handle them. Refusal by the host, a malformed request, and an unsupported platform fail the effect instead.
Reading and digesting
| Effect | Gives | Notes |
|---|---|---|
observeFileDigest root: r relativePath: p maxBytes: b |
Result[FileDigest] |
SHA-256 without reading the content into the program; b at most 64 MiB. VM 5 |
observeFileSHA256 root: r relativePath: p maxBytes: b |
Result[FileDigest] |
the same, for files up to 1 GiB. VM 7 |
readFileOctets root: r relativePath: p maxBytes: b |
Result[FileOctets] |
the content as Octets, any bytes; b at most 16 MiB. VM 7 |
observePath root: r relativePath: p |
Result[PathState] |
kind, size, mode, identity and times; a missing path is #Missing, not a failure; an empty p observes the root. VM 7 |
| Type | Members |
|---|---|
FileDigest |
relativePath: Text, byteCount: i64, sha256: Digest, root: Path, rootIdentity: Text, fileIdentity: Text |
FileOctets |
the FileDigest members and content: Octets, all from the same read |
PathState |
root: Path, relativePath: Text, kind: Symbol (#File, #Directory, #Link, #Other or #Missing), size: i64, mode: i32, identity: Text, version: Text, accessedSeconds: i64, accessedNanos: i32, modifiedSeconds: i64, modifiedNanos: i32 |
At most maxBytes + 1 bytes are read: the extra byte proves the file is too
large, and no digest of a prefix is ever returned. Size, identity and times
are checked before and after the read, and a change fails with
#FileChanged. The identity texts are opaque stamps for diagnostics, not
portable identifiers. The failure kinds are #FileNotFound, #FileDenied,
#FileUnsupportedKind, #FilePathPolicy, #FileSymlink, #FileSizeLimit,
#FileChanged, #FileIo, #FileLimitConfig, #FileUnsupportedPlatform and
#FileRuntime.
Streams
For files too large to hold, read or hash them a piece at a time. VM 7.
| Effect | Gives |
|---|---|
openByteStream root: r relativePath: p encoding: e trailing: t maxInputBytes: i maxDecodedBytes: d |
Result[ByteStream] |
readByteStream stream: s offset: o maxBytes: b |
Result[ByteStreamChunk] |
hashByteStream stream: s offset: o byteCount: n |
Result[ByteStreamDigest] |
closeByteStream stream: s |
Result[ByteStreamEnd] |
encoding is #Raw or #Gzip. With #Raw, trailing must be #Reject.
With #Gzip, trailing says what may follow the last member: #Reject
(nothing but more gzip members), #ZeroPadding (also an all-zero tail) or
#IgnoreAfterZero (stop at the first zero byte after a member and count the
rest as ignored). Both input and decoded limits are at most 1 GiB; one chunk
is at most 16 MiB; an executor holds at most 16 open streams.
offset is the decoded offset the program has reached, and must be given
exactly: a stale offset is refused. A short hashed segment fails
(#StreamTruncated) rather than returning a prefix's digest. A stream is
valid only within one run, and a complete gzip check needs both an observed
end of file and a successful closeByteStream.
| Type | Members |
|---|---|
ByteStream |
token: Text, root: Path, relativePath: Text, sourceSize: i64, rootIdentity: Text, fileIdentity: Text |
ByteStreamChunk |
offset: i64, byteCount: i64, nextOffset: i64, eof: bool, content: Octets |
ByteStreamDigest |
offset: i64, byteCount: i64, nextOffset: i64, eof: bool, sha256: Digest |
ByteStreamEnd |
eof: bool, sourceBytes: i64, decodedBytes: i64, ignoredBytes: i64 |
Changing files
All of these need a write root and VM 7. The ones that create something
refuse to replace an existing entry (only movePath with #Replace or
#Exchange does); none follows a link, and none rolls back. When a
change has been made and a later check fails, the effect's outcome is
Unknown: the run stops and says the change may have happened and must be
inspected before a retry.
| Effect | Gives | Notes |
|---|---|---|
createFileOctets root: r relativePath: p content: o mode: m |
Result[FileCreation] |
a new file from bytes already in hand, at most 16 MiB; m (0 to 07777, as an integer) is the creation mode, subject to the umask; the bytes are read back and checked |
copyFile sourceRoot: r relativePath: p destinationRoot: d destination: q maxBytes: b |
Result[FileCopy] |
a new, independent copy (not a link) with the source's mode; refuses an existing destination; b at most 1 GiB |
linkFile root: r relativePath: p destination: q |
Result[PathMutation] |
a hard link at q to the regular file p; never replaces q |
movePath root: r relativePath: p destination: q mode: m |
Result[PathMutation] |
#Exclusive: rename only if q is absent; #Replace: rename a file over an absent or regular file; #Exchange: swap two existing entries of the same kind atomically. No copy across devices |
createDirectories root: r relativePath: p mode: m |
Result[DirectoryCreation] |
creates missing directories; existing ones keep their mode; an empty p ensures the root |
createTemporaryDirectory root: r prefix: t |
Result[DirectoryCreation] |
one new directory named t plus six random letters and digits, mode 0700 |
removeTree root: r relativePath: p maxEntries: n maxDepth: d |
Result[TreeRemoval] |
scans first, then removes; a missing path succeeds; links are removed, not followed; the root itself cannot be removed; at most 100,000 entries and 64 levels |
setPathMode root: r relativePath: p mode: m |
Result[PathState] |
changes only the mode bits |
setPathTimes root: r relativePath: p accessedSeconds: a modifiedSeconds: s |
Result[PathState] |
whole Unix seconds |
| Type | Members |
|---|---|
FileCreation |
root: Path, relativePath: Text, byteCount: i64, sha256: Digest, mode: i32, identity: Text, version: Text |
FileCopy |
sourceRoot: Path, relativePath: Text, destinationRoot: Path, destination: Text, byteCount: i64, sha256: Digest, sourceIdentity: Text, destinationIdentity: Text |
PathMutation |
root: Path, relativePath: Text, destination: Text, operation: Symbol, sourceIdentity: Text, priorDestinationIdentity: Text (empty if it was absent) |
DirectoryCreation |
root: Path, relativePath: Text, path: Path, createdEntries: i32, identity: Text |
TreeRemoval |
root: Path, relativePath: Text, removedEntries: i64, initiallyPresent: bool, identity: Text |
Their failure kinds include #FsExists, #FsNotFound, #FsKind,
#FsSymlink, #FsPathPolicy, #FsSameIdentity, #FsCrossDevice,
#FsDenied, #FsUnsupported and #FsConfig. #Replace is not
compare-and-swap: use #Exclusive when nothing may ever be overwritten.
createFileOctets and copyFile differ in when the bytes are taken.
copyFile reads the source path while copying; createFileOctets writes
bytes the program already holds, for example from readFileOctets, so what
is written is exactly what was checked even if the source changes meanwhile.
module Example::Publish.
;; Copy a file into a fresh staging directory beside its destination, then
;; move the copy into place. Nothing that already exists is replaced.
export publish source: Path name: Text into: Path -> Result[PathMutation] [
(createTemporaryDirectory root: into prefix: ".staging-")
ifOk: [ :staging |
(copyFile sourceRoot: source relativePath: name
destinationRoot: (staging path) destination: name maxBytes: 64MiB)
ifFail: [ :e | ret Fail { err: e } ].
ret movePath root: into
relativePath: ((staging relativePath) append: ("/" append: name))
destination: name
mode: #Exclusive ]
ifFail: [ :e | ret Fail { err: e } ].
].
export fingerprint root: Path name: Text -> Text [
(observeFileSHA256 root: root relativePath: name maxBytes: 1GiB)
ifOk: [ :d | ret Text from: (d sha256) ]
ifFail: [ :e | ret Text from: (e kind) ].
].Installing a file over SSH
installFileText path: Path content: Text expectedDigest: Digest
owner: Text mode: Text -> FileInstallinstallFileText is an SshHost effect, carried by the SSH transport. It
refuses content whose SHA-256 is not expectedDigest before sending
anything, streams the content over standard input (never in a command line),
stages it beside the destination, sets the owner ("root:root") and mode
("0644"), and renames it into place. If the
file already has that content, owner and mode it is left alone.
FileInstall has path: Path, digest: Digest and changed: bool.
System::RemoteFile wraps it as RemoteFile::installText
(System and tool modules).