wasi_fs (wasm v0.16.0)
View SourceFile access for WASI, over whichever backend is available.
Call backend/0 if you need to know which one you got, because the weaker one
carries a documented race. There are two implementations of the same six
operations:
- native, via
wasi_file_nif, which walks each path component withopenat(..., O_NOFOLLOW)so a name is never resolved twice and no symlink is ever followed. This closes the time-of-check-to-time-of-use window. - fallback, via
filepluswasi_path, which refuses every escape it can detect lexically and throughfilelib:safe_relative_path/2, but resolves the path and opens it as two separate steps.
The backend is chosen once, at open time, and recorded in the handle, so a descriptor is always used with the backend that produced it.
A native handle is safe to share between processes. Each one carries its own lock, so a close cannot land between another operation testing the descriptor and using it; without that, closing while a read was in flight returned the descriptor number to the kernel, the next open anywhere in the VM was handed it back, and the read in flight read whatever that now was.
You do not choose between them. wasi_preview1 asks for a path beneath a
preopen and gets a handle back; which mechanism enforced the boundary is this
module's business.
Where things are
| you want | look at |
|---|---|
| opening a path below a preopen | open/3, preopen/1 |
| reading and writing at an offset | pread/3, pwrite/3, pwrite_all/3 |
| reserving space in a file | allocate/3 |
| making a file or a directory durable | sync/2, sync_dir/2; sync/1 is a stream flush |
| the stat shapes, by path and by descriptor | stat/2, stat/3, stat_fd/1 |
| reading a directory | list_dir/2 |
| the other path operations | mkdir/2, unlink/2, rmdir/2, symlink/3, rename/4, link/4 |
| which backend a build got | backend/0 |
Rights are not here. wasi_preview1 owns the descriptor table and the
rights mask; this module owns the two backends and the lock that makes a native
handle safe to share. The path sandbox is wasi_path, and a descriptor with
the right to read may still be refused a path that escapes its preopen.
Summary
Types
An open file, tagged with the backend that opened it.
A directory that guest paths are resolved beneath.
Functions
Reserve Len bytes of disk from Offset, so that a later write in that range
cannot fail for want of space, and grow the file to Offset + Len when it is
shorter. It never shrinks.
Which backend is in use. fallback still refuses every detectable escape.
Duplicate a handle into an independent one, closed on its own.
Release a root. A preopen holds one for the life of the instance.
List the directory a root names, rather than one beneath it.
Open Guest beneath the preopened host directory Root.
Open directory Guest beneath Root as a new root.
Open a preopen root, once, for every path that will be resolved under it.
Write all of Data at Offset, or answer the error that stopped it.
One bounded fd_readdir batch on the descriptor's own directory, native only.
Set a name's access and modification times, in nanoseconds.
As set_times/4, following the final component when asked.
As set_times/4, for an open descriptor rather than a name.
Stat a name without following it, which is what a link's own stat is.
Everything the WASI filestat holds, for an open descriptor.
Create a link holding Target verbatim. The target is never resolved.
Flush to disk as fsync(2) does, answering ok for anything with nothing to
flush, such as a pipe. A regular file the filesystem cannot sync answers the
error. This is what a stream flush asks for; a guest's own sync is sync/2.
Make a file durable, as the guest asked: all for its data and metadata,
which is fd_sync and sync, or data for what reading it back needs,
which is fd_datasync and sync-data.
Make a directory durable: the names created, renamed or removed in it, which
a database that writes a journal or replaces a file needs to survive a power
loss. Kind is as for sync/2.
Cut the file to Len bytes, or extend it with zeroes.
Types
-nominal handle() :: {native, term()} | {fallback, file:io_device(), string()}.
An open file, tagged with the backend that opened it.
A directory that guest paths are resolved beneath.
On the native backend it is the directory itself, opened once, and every guest path is resolved relative to that descriptor. Naming the root by path on each open would leave it to be resolved again every time, so replacing or renaming it between two opens would silently move the sandbox; anchoring means operations continue against the directory that was opened, and a swapped child is what gets refused.
Functions
-spec allocate(handle(), non_neg_integer(), non_neg_integer()) -> ok | {error, non_neg_integer()}.
Reserve Len bytes of disk from Offset, so that a later write in that range
cannot fail for want of space, and grow the file to Offset + Len when it is
shorter. It never shrinks.
Both backends ask the system to reserve the space: the native one through
posix_fallocate(3), the fallback through file:allocate/3. On macOS only
what lies past the end of the file can be reserved, so a hole already inside
it stays a hole. A system or filesystem that cannot reserve space answers
ENOTSUP, rather than a sparse file that looks reserved and is not.
-spec backend() -> native | fallback.
Which backend is in use. fallback still refuses every detectable escape.
-spec close(handle()) -> ok.
-spec dup(handle()) -> {ok, handle()} | {error, non_neg_integer()}.
Duplicate a handle into an independent one, closed on its own.
Native only: it duplicates the open descriptor, so the copy owns its own fd and
reads positionally without touching the descriptor's offset, and closing either
does not affect the other. The fallback cannot duplicate an io_device, and
reopening by pathname would resolve the name a second time and could follow a
symlink swapped in since the descriptor was opened, so dup is refused there and
the caller reads eagerly instead.
-spec forget(root()) -> ok.
Release a root. A preopen holds one for the life of the instance.
-spec link(root(), binary(), root(), binary()) -> ok | {error, non_neg_integer()}.
-spec list(root()) -> {ok, [binary()]} | {error, non_neg_integer()}.
List the directory a root names, rather than one beneath it.
What fd_readdir needs: the descriptor already names the directory, and on the
native backend it is the directory, so there is no name to resolve. . and
.. are filtered out here so both backends answer the same thing; the caller
puts them back, because a reader that counts entries expects them first.
-spec list_dir(file:filename_all(), binary()) -> {ok, [binary()]} | {error, non_neg_integer()}.
-spec mkdir(root(), binary()) -> ok | {error, non_neg_integer()}.
-spec open(root(), binary(), [read | write | create | truncate | exclusive]) -> {ok, handle()} | {error, non_neg_integer()}.
Open Guest beneath the preopened host directory Root.
Returns a WASI errno on failure, already mapped, so callers do not have to know which backend produced it.
-spec open_dir_at(root(), binary()) -> {ok, root()} | {error, non_neg_integer()}.
Open directory Guest beneath Root as a new root.
A guest that opens a directory (rather than being granted a preopen) then does
path operations under it. The native backend uses the opened directory fd itself
as the root -- openat beneath a directory fd is exactly what a root does. The
fallback resolves the name through the sandbox and adopts the host path, the same
shape preopen/1 produces. forget/1 releases either.
-spec pread(handle(), non_neg_integer(), non_neg_integer()) -> {ok, binary()} | eof | {error, non_neg_integer()}.
-spec preopen(file:filename_all()) -> {ok, root()} | {error, non_neg_integer()}.
Open a preopen root, once, for every path that will be resolved under it.
With the require_native_fs application setting on, the fallback backend
grants nothing: this answers ENOTCAPABLE, so the guest gets no file system
rather than one whose paths are checked and opened in two steps.
-spec pwrite(handle(), non_neg_integer(), binary()) -> {ok, non_neg_integer()} | {error, non_neg_integer()}.
-spec pwrite_all(handle(), non_neg_integer(), binary()) -> ok | {error, non_neg_integer()}.
Write all of Data at Offset, or answer the error that stopped it.
pwrite/3 may write less than it is given, as pwrite(2) may, and the
native backend writes at most 1 MiB per call so that one call cannot hold a
dirty scheduler for long. A caller that can hand the guest a count, such as
fd_write, passes the short count on. A stream cannot, so a file stream
writes through this, and the rest of a long write is not dropped.
-spec readdir(root(), non_neg_integer(), non_neg_integer()) -> {ok, binary()} | {error, non_neg_integer()}.
One bounded fd_readdir batch on the descriptor's own directory, native only.
Returns the WASI wire bytes for the batch starting at Cookie (0 to start),
filling at most BufLen bytes plus one straddling entry. The fallback backend
cannot stream a directory with a bounded buffer, so its listing is handled by
the caller.
-spec readlink(root(), binary()) -> {ok, binary()} | {error, non_neg_integer()}.
-spec rename(root(), binary(), root(), binary()) -> ok | {error, non_neg_integer()}.
-spec rmdir(root(), binary()) -> ok | {error, non_neg_integer()}.
-spec set_times(root(), binary(), non_neg_integer() | omit, non_neg_integer() | omit) -> ok | {error, non_neg_integer()}.
Set a name's access and modification times, in nanoseconds.
omit for either leaves that stamp alone, which is what the WASI fstflags
bits ask for when they name only one of the two. The final component is not
followed, so this sets the times on a symlink itself rather than on its target,
which is what path_filestat_set_times without a follow flag means.
-spec set_times(root(), binary(), non_neg_integer() | omit, non_neg_integer() | omit, follow | nofollow) -> ok | {error, non_neg_integer()}.
As set_times/4, following the final component when asked.
path_filestat_set_times takes the same lookup flag path_filestat_get does,
and means the same thing by it: without it the symlink's own times are set, with
it the target's.
The fallback follows either way. file:write_file_info/3 has no lstat
counterpart, so on that backend a symlink's own times cannot be set at all,
which is the same class of limitation as its path resolution.
-spec set_times_fd(handle(), non_neg_integer() | omit, non_neg_integer() | omit) -> ok | {error, non_neg_integer()}.
As set_times/4, for an open descriptor rather than a name.
-spec size(handle()) -> {ok, non_neg_integer()} | {error, non_neg_integer()}.
-spec stat(root(), binary()) -> {ok, map()} | {error, non_neg_integer()}.
Stat a name without following it, which is what a link's own stat is.
-spec stat(root(), binary(), follow | nofollow) -> {ok, map()} | {error, non_neg_integer()}.
As stat/2, following the final component when asked.
path_filestat_get takes a lookup flag saying whether it is asking about a
symlink or about what it points at, and the two answers differ in every field.
Ignoring it reported the link for both, which upstream catches three ways:
path_exists, symlink_filestat and fd_filestat_set.
Only the final component is a choice. Everything before it is followed either way, because that is what a path means.
-spec stat_fd(handle()) -> {ok, map()} | {error, non_neg_integer()}.
Everything the WASI filestat holds, for an open descriptor.
The same map stat/2 answers for a name. fd_filestat_get used to report only
the size and the descriptor's own filetype and zero for the rest, which is what
made two files in a directory share an inode.
The fallback has no fstat, so it stats the path the descriptor was opened
from. That is a second resolution and therefore a window, which is the whole
reason the native backend exists; it is the fallback's existing bargain and not
a new one.
-spec symlink(root(), binary(), binary()) -> ok | {error, non_neg_integer()}.
Create a link holding Target verbatim. The target is never resolved.
-spec sync(handle()) -> ok | {error, non_neg_integer()}.
Flush to disk as fsync(2) does, answering ok for anything with nothing to
flush, such as a pipe. A regular file the filesystem cannot sync answers the
error. This is what a stream flush asks for; a guest's own sync is sync/2.
-spec sync(handle(), all | data) -> ok | {error, non_neg_integer()}.
Make a file durable, as the guest asked: all for its data and metadata,
which is fd_sync and sync, or data for what reading it back needs,
which is fd_datasync and sync-data.
On macOS the native backend flushes the drive's cache as well
(F_FULLFSYNC), because fsync(2) there does not, and a guest asking for
durability has no other call to make. The fallback can only do what
file:sync/1 and file:datasync/1 do, which on macOS orders the writes
(F_BARRIERFSYNC) without flushing that cache.
-spec sync_dir(root(), all | data) -> ok | {error, non_neg_integer()}.
Make a directory durable: the names created, renamed or removed in it, which
a database that writes a journal or replaces a file needs to survive a power
loss. Kind is as for sync/2.
The native backend syncs the directory it holds open. The fallback cannot:
Erlang does not open a directory, so it answers ENOTSUP rather than
ok for a sync it did not make.
-spec truncate(handle(), non_neg_integer()) -> ok | {error, non_neg_integer()}.
Cut the file to Len bytes, or extend it with zeroes.
-spec unlink(root(), binary()) -> ok | {error, non_neg_integer()}.