@pgxsinkit/pgwasm 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +111 -0
- package/README.md +56 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +21 -0
- package/dist/build/index.js.map +9 -0
- package/dist/build/seam.d.ts +174 -0
- package/dist/build/seam.d.ts.map +1 -0
- package/dist/chunk-0w227vw2.js +233 -0
- package/dist/chunk-0w227vw2.js.map +11 -0
- package/dist/chunk-74gcrk47.js +563 -0
- package/dist/chunk-74gcrk47.js.map +14 -0
- package/dist/chunk-at1py5s5.js +373 -0
- package/dist/chunk-at1py5s5.js.map +12 -0
- package/dist/chunk-cpsnmzrf.js +126 -0
- package/dist/chunk-cpsnmzrf.js.map +10 -0
- package/dist/chunk-d82gg1hx.js +306 -0
- package/dist/chunk-d82gg1hx.js.map +10 -0
- package/dist/chunk-gjf102c9.js +1005 -0
- package/dist/chunk-gjf102c9.js.map +16 -0
- package/dist/chunk-kb3rsr7n.js +19 -0
- package/dist/chunk-kb3rsr7n.js.map +9 -0
- package/dist/chunk-tdvrvgt5.js +83 -0
- package/dist/chunk-tdvrvgt5.js.map +11 -0
- package/dist/chunk-z0c40tja.js +50 -0
- package/dist/chunk-z0c40tja.js.map +10 -0
- package/dist/core/data-dir-archive.d.ts +17 -0
- package/dist/core/data-dir-archive.d.ts.map +1 -0
- package/dist/core/internals.d.ts +59 -0
- package/dist/core/internals.d.ts.map +1 -0
- package/dist/core/marker.d.ts +35 -0
- package/dist/core/marker.d.ts.map +1 -0
- package/dist/core/mutex.d.ts +9 -0
- package/dist/core/mutex.d.ts.map +1 -0
- package/dist/core/names.d.ts +11 -0
- package/dist/core/names.d.ts.map +1 -0
- package/dist/core/parse.d.ts +11 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/core/pgwasm.d.ts +53 -0
- package/dist/core/pgwasm.d.ts.map +1 -0
- package/dist/core/storage.d.ts +14 -0
- package/dist/core/storage.d.ts.map +1 -0
- package/dist/create.d.ts +10 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/drizzle/codecs.d.ts +9 -0
- package/dist/drizzle/codecs.d.ts.map +1 -0
- package/dist/drizzle/driver.d.ts +50 -0
- package/dist/drizzle/driver.d.ts.map +1 -0
- package/dist/drizzle/index.d.ts +4 -0
- package/dist/drizzle/index.d.ts.map +1 -0
- package/dist/drizzle/index.js +261 -0
- package/dist/drizzle/index.js.map +12 -0
- package/dist/drizzle/session.d.ts +39 -0
- package/dist/drizzle/session.d.ts.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fs/base-filesystem.d.ts +118 -0
- package/dist/fs/base-filesystem.d.ts.map +1 -0
- package/dist/fs/index.d.ts +2 -0
- package/dist/fs/index.d.ts.map +1 -0
- package/dist/fs/index.js +11 -0
- package/dist/fs/index.js.map +9 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +9 -0
- package/dist/interface.d.ts +165 -0
- package/dist/interface.d.ts.map +1 -0
- package/dist/live/debounce-mutex.d.ts +6 -0
- package/dist/live/debounce-mutex.d.ts.map +1 -0
- package/dist/live/format-query.d.ts +8 -0
- package/dist/live/format-query.d.ts.map +1 -0
- package/dist/live/index.d.ts +9 -0
- package/dist/live/index.d.ts.map +1 -0
- package/dist/live/index.js +601 -0
- package/dist/live/index.js.map +12 -0
- package/dist/live/interface.d.ts +86 -0
- package/dist/live/interface.d.ts.map +1 -0
- package/dist/opfs/broker/client.d.ts +163 -0
- package/dist/opfs/broker/client.d.ts.map +1 -0
- package/dist/opfs/broker/protocol.d.ts +326 -0
- package/dist/opfs/broker/protocol.d.ts.map +1 -0
- package/dist/opfs/broker/server.d.ts +84 -0
- package/dist/opfs/broker/server.d.ts.map +1 -0
- package/dist/opfs/core/checksum.d.ts +3 -0
- package/dist/opfs/core/checksum.d.ts.map +1 -0
- package/dist/opfs/core/codec.d.ts +73 -0
- package/dist/opfs/core/codec.d.ts.map +1 -0
- package/dist/opfs/core/errors.d.ts +80 -0
- package/dist/opfs/core/errors.d.ts.map +1 -0
- package/dist/opfs/core/limits.d.ts +35 -0
- package/dist/opfs/core/limits.d.ts.map +1 -0
- package/dist/opfs/core/memory-port.d.ts +51 -0
- package/dist/opfs/core/memory-port.d.ts.map +1 -0
- package/dist/opfs/core/metadata-store.d.ts +19 -0
- package/dist/opfs/core/metadata-store.d.ts.map +1 -0
- package/dist/opfs/core/mounted-vfs.d.ts +119 -0
- package/dist/opfs/core/mounted-vfs.d.ts.map +1 -0
- package/dist/opfs/core/path.d.ts +14 -0
- package/dist/opfs/core/path.d.ts.map +1 -0
- package/dist/opfs/core/port.d.ts +30 -0
- package/dist/opfs/core/port.d.ts.map +1 -0
- package/dist/opfs/core/repacked-vfs.d.ts +138 -0
- package/dist/opfs/core/repacked-vfs.d.ts.map +1 -0
- package/dist/opfs/core/state-machine.d.ts +296 -0
- package/dist/opfs/core/state-machine.d.ts.map +1 -0
- package/dist/opfs/create.d.ts +50 -0
- package/dist/opfs/create.d.ts.map +1 -0
- package/dist/opfs/file-port.d.ts +14 -0
- package/dist/opfs/file-port.d.ts.map +1 -0
- package/dist/opfs/index.d.ts +37 -0
- package/dist/opfs/index.d.ts.map +1 -0
- package/dist/opfs/index.js +6671 -0
- package/dist/opfs/index.js.map +28 -0
- package/dist/opfs/opfs-port.d.ts +34 -0
- package/dist/opfs/opfs-port.d.ts.map +1 -0
- package/dist/opfs/opfs-repacked-fs.d.ts +55 -0
- package/dist/opfs/opfs-repacked-fs.d.ts.map +1 -0
- package/dist/opfs/wasi/preview1.d.ts +228 -0
- package/dist/opfs/wasi/preview1.d.ts.map +1 -0
- package/dist/protocol/index.d.ts +15 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +23 -0
- package/dist/protocol/index.js.map +10 -0
- package/dist/protocol/wire/buffer-reader.d.ts +13 -0
- package/dist/protocol/wire/buffer-reader.d.ts.map +1 -0
- package/dist/protocol/wire/buffer-writer.d.ts +12 -0
- package/dist/protocol/wire/buffer-writer.d.ts.map +1 -0
- package/dist/protocol/wire/index.d.ts +5 -0
- package/dist/protocol/wire/index.d.ts.map +1 -0
- package/dist/protocol/wire/messages.d.ts +194 -0
- package/dist/protocol/wire/messages.d.ts.map +1 -0
- package/dist/protocol/wire/parser.d.ts +12 -0
- package/dist/protocol/wire/parser.d.ts.map +1 -0
- package/dist/protocol/wire/serializer.d.ts +61 -0
- package/dist/protocol/wire/serializer.d.ts.map +1 -0
- package/dist/protocol/wire/string-utils.d.ts +6 -0
- package/dist/protocol/wire/string-utils.d.ts.map +1 -0
- package/dist/protocol/wire/types.d.ts +7 -0
- package/dist/protocol/wire/types.d.ts.map +1 -0
- package/dist/tar/gzip.d.ts +7 -0
- package/dist/tar/gzip.d.ts.map +1 -0
- package/dist/tar/tar.d.ts +39 -0
- package/dist/tar/tar.d.ts.map +1 -0
- package/dist/templating.d.ts +42 -0
- package/dist/templating.d.ts.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
- package/skills/opfs/SKILL.md +138 -0
- package/src/build/index.ts +23 -0
- package/src/build/seam.ts +179 -0
- package/src/core/data-dir-archive.ts +68 -0
- package/src/core/internals.ts +87 -0
- package/src/core/marker.ts +94 -0
- package/src/core/mutex.ts +16 -0
- package/src/core/names.ts +26 -0
- package/src/core/parse.ts +79 -0
- package/src/core/pgwasm.ts +876 -0
- package/src/core/storage.ts +70 -0
- package/src/create.ts +134 -0
- package/src/drizzle/codecs.ts +114 -0
- package/src/drizzle/driver.ts +141 -0
- package/src/drizzle/index.ts +15 -0
- package/src/drizzle/session.ts +133 -0
- package/src/errors.ts +201 -0
- package/src/fs/base-filesystem.ts +149 -0
- package/src/fs/index.ts +7 -0
- package/src/index.ts +44 -0
- package/src/interface.ts +186 -0
- package/src/live/debounce-mutex.ts +45 -0
- package/src/live/format-query.ts +47 -0
- package/src/live/index.ts +707 -0
- package/src/live/interface.ts +109 -0
- package/src/opfs/broker/client.ts +482 -0
- package/src/opfs/broker/protocol.ts +624 -0
- package/src/opfs/broker/server.ts +522 -0
- package/src/opfs/core/checksum.ts +85 -0
- package/src/opfs/core/codec.ts +1051 -0
- package/src/opfs/core/errors.ts +134 -0
- package/src/opfs/core/limits.ts +68 -0
- package/src/opfs/core/memory-port.ts +459 -0
- package/src/opfs/core/metadata-store.ts +512 -0
- package/src/opfs/core/mounted-vfs.ts +407 -0
- package/src/opfs/core/path.ts +82 -0
- package/src/opfs/core/port.ts +114 -0
- package/src/opfs/core/repacked-vfs.ts +1464 -0
- package/src/opfs/core/state-machine.ts +1699 -0
- package/src/opfs/create.ts +125 -0
- package/src/opfs/file-port.ts +183 -0
- package/src/opfs/index.ts +113 -0
- package/src/opfs/opfs-port.ts +90 -0
- package/src/opfs/opfs-repacked-fs.ts +210 -0
- package/src/opfs/wasi/preview1.ts +1016 -0
- package/src/protocol/index.ts +24 -0
- package/src/protocol/wire/buffer-reader.ts +60 -0
- package/src/protocol/wire/buffer-writer.ts +93 -0
- package/src/protocol/wire/index.ts +15 -0
- package/src/protocol/wire/messages.ts +332 -0
- package/src/protocol/wire/parser.ts +373 -0
- package/src/protocol/wire/serializer.ts +233 -0
- package/src/protocol/wire/string-utils.ts +18 -0
- package/src/protocol/wire/types.ts +12 -0
- package/src/tar/gzip.ts +36 -0
- package/src/tar/tar.ts +253 -0
- package/src/templating.ts +103 -0
- package/src/types.ts +283 -0
|
@@ -0,0 +1,1016 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A WASI preview1 FILESYSTEM adapter over `RepackedSyncClient`.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this file exists
|
|
5
|
+
*
|
|
6
|
+
* The broker gives a futex-parked thread synchronous access to ONE repacked store. A wasm engine does
|
|
7
|
+
* not speak that API — it speaks `wasi_snapshot_preview1`, thirty-odd i32-returning imports over its
|
|
8
|
+
* own linear memory. This module is the seam: every WASI file call a guest makes is translated into a
|
|
9
|
+
* broker request, and the errno the broker already speaks (the protocol's numbers ARE WASI preview1
|
|
10
|
+
* errnos) is handed straight back. Nothing here knows about PGlite, OPFS, or any particular engine —
|
|
11
|
+
* it needs a client, a way to reach the guest's memory, and nothing else.
|
|
12
|
+
*
|
|
13
|
+
* ## What it owns and what it refuses to touch
|
|
14
|
+
*
|
|
15
|
+
* The adapter owns fd `preopenFd` (3 by default — the "/" preopen) and every fd at or above `fdBase`
|
|
16
|
+
* (4 by default). It NEVER touches fds 0–2: a host keeps its own stdin/stdout/stderr, and
|
|
17
|
+
* {@link WasiPreview1Fs.compose} builds the merged import object that routes each call to whichever
|
|
18
|
+
* side owns the fd. `owns()` is public so a host can make that decision itself.
|
|
19
|
+
*
|
|
20
|
+
* ## The fd table
|
|
21
|
+
*
|
|
22
|
+
* One entry per open descriptor, keyed by the WASI fd the guest sees:
|
|
23
|
+
*
|
|
24
|
+
* { fd, path, isDir, clientFd, offset, fdflags, rightsBase, rightsInheriting, readable, writable }
|
|
25
|
+
*
|
|
26
|
+
* - `path` is the normalized absolute path the fd was opened with. It is kept for the whole life of
|
|
27
|
+
* the descriptor because the store resizes by PATH, not by descriptor: `fd_filestat_set_size` and
|
|
28
|
+
* `fd_allocate` resolve through it.
|
|
29
|
+
* - `clientFd` is the broker's descriptor for a FILE. A DIRECTORY has none: the store cannot open a
|
|
30
|
+
* directory at all (its `open` answers `EISDIR`), so a directory fd is adapter-local — a remembered
|
|
31
|
+
* path plus a listing snapshot — and is still usable for `fd_readdir`, `fd_filestat_get`, `fd_sync`
|
|
32
|
+
* and as a dirfd for every path operation.
|
|
33
|
+
* - `offset` is tracked HERE, not in the store. Every read and write the adapter issues carries an
|
|
34
|
+
* explicit position (the store's `pread`/`pwrite` form), so the store's own cursor is never used and
|
|
35
|
+
* `fd_seek`/`fd_tell` are exact even though several threads share one store.
|
|
36
|
+
*
|
|
37
|
+
* ## The mapping, decision by decision
|
|
38
|
+
*
|
|
39
|
+
* - **rights → access.** `RIGHTS_FD_READ` grants read, `RIGHTS_FD_WRITE` grants write; a request that
|
|
40
|
+
* asks for neither is read-only. That access is enforced by the BROKER (per its own fd) and again
|
|
41
|
+
* here, because a POSIX `open(O_RDONLY|O_CREAT)` has to open the store descriptor wider than the
|
|
42
|
+
* guest asked for — see below.
|
|
43
|
+
* - **oflags → POSIX bits.** `CREAT`→`O_CREAT`, `EXCL`→`O_EXCL`, `TRUNC`→`O_TRUNC`, and the access
|
|
44
|
+
* mode from the rights. `planOpen` (server side) rejects `O_CREAT`/`O_TRUNC` without write access,
|
|
45
|
+
* so a create/truncate request always adds write to the POSIX access mode while the adapter keeps
|
|
46
|
+
* the NARROWER access the rights asked for and rejects a later `fd_write` with `ENOTCAPABLE`.
|
|
47
|
+
* - **`O_DIRECTORY` and directories.** With `DIRECTORY` set the adapter never calls `open` (that would
|
|
48
|
+
* create a file); it stats and builds a directory fd. Without it, `open` is attempted and an
|
|
49
|
+
* `EISDIR` answer is recognised as "this is a directory" and turned into a directory fd too — which
|
|
50
|
+
* costs nothing on the common file path.
|
|
51
|
+
* - **`O_APPEND` is emulated here, never passed to the broker.** An append write resolves end-of-file
|
|
52
|
+
* with `fstat` and then `pwrite`s there, so the adapter's own offset stays exact and
|
|
53
|
+
* `fd_fdstat_set_flags` can turn `APPEND` on and off on a descriptor that was not opened with it.
|
|
54
|
+
* - **`fd_seek`.** `SET`/`CUR` are pure arithmetic on the adapter's offset; `END` resolves the size
|
|
55
|
+
* with `fstat` on the broker fd (never `size(path)`, which would race a rename).
|
|
56
|
+
* - **`fd_readdir` cookies.** A cookie is an index into a listing SNAPSHOT taken on cookie 0, exactly
|
|
57
|
+
* as a POSIX `readdir` may. The snapshot is built out of `readdirPage` calls, and it exists for a
|
|
58
|
+
* protocol reason as much as a semantic one: the broker treats a cursor past the end of the listing
|
|
59
|
+
* as a protocol violation and DETACHES the client, so a stale cookie from a directory that shrank
|
|
60
|
+
* must never reach it. A cookie past the snapshot's end reports zero bytes used — end of directory.
|
|
61
|
+
* - **truncate by path.** `fd_filestat_set_size` and `fd_allocate` call `truncate(path, size)` with
|
|
62
|
+
* the descriptor's remembered path. The store has no hard links, so nothing is lost.
|
|
63
|
+
* - **`fd_sync`/`fd_datasync`.** Both map to the broker's `fsync`, whose durability is STORE-WIDE: on
|
|
64
|
+
* success every byte written through the broker by ANY client before the call returned is
|
|
65
|
+
* recoverable. That is stronger than `fd_sync` promises, never weaker.
|
|
66
|
+
* - **symlinks.** The store HAS them, and `SYMLINK_FOLLOW` therefore means something everywhere it
|
|
67
|
+
* appears: `path_filestat_get` picks `stat` or `lstat`, and a `path_open` without it sends
|
|
68
|
+
* `O_NOFOLLOW` so a final component that IS a link answers `ELOOP` instead of opening its target.
|
|
69
|
+
* `path_unlink_file` removes the LINK, never the target — that is the core's `unlink`, not
|
|
70
|
+
* something added here. `path_symlink` writes a link and `path_readlink` reads one back, both
|
|
71
|
+
* verbatim: the store takes ABSOLUTE targets only (see `validateSymlinkTarget`), so a relative
|
|
72
|
+
* target is `EINVAL` rather than being silently reinterpreted, and an absolute one is a STORE
|
|
73
|
+
* path — which is the same thing as a guest path whenever the preopen is the store root, the
|
|
74
|
+
* default and the only arrangement a datadir uses. `path_link` remains `ENOTSUP`: the store has
|
|
75
|
+
* no hard links.
|
|
76
|
+
* - **`fd_advise`** is a no-op success. **`fd_filestat_set_times`/`path_filestat_set_times`** answer
|
|
77
|
+
* `ENOTSUP`: the broker exposes no `utimes` opcode, and inventing an adapter-local timestamp would
|
|
78
|
+
* make two threads on ONE store disagree about a file's mtime, which is exactly what this whole
|
|
79
|
+
* arrangement exists to prevent.
|
|
80
|
+
*
|
|
81
|
+
* ## Failure discipline
|
|
82
|
+
*
|
|
83
|
+
* Every exported function is wrapped so that a JS exception can never leave it. A throw out of a WASI
|
|
84
|
+
* import unwinds through the guest's nounwind frames and surfaces as a bare `RuntimeError:
|
|
85
|
+
* unreachable` with no attribution at all; instead the wrapper reports `EIO` and hands the cause to
|
|
86
|
+
* `onError` with the call name. A transport failure (the coordinator died, the client was detached) is
|
|
87
|
+
* therefore an `EIO` the guest can act on rather than an abort it cannot.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
import type { RepackedSyncClient } from "../broker/client";
|
|
91
|
+
import { O_CREAT, O_EXCL, O_NOFOLLOW, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY } from "../broker/protocol";
|
|
92
|
+
import type { BrokerStat } from "../broker/protocol";
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* WASI preview1 errno numbers. A superset of the store's own `FS_ERRNO` (whose values already ARE
|
|
96
|
+
* these numbers); the extra codes are ones a filesystem ADAPTER has to answer and the store core
|
|
97
|
+
* never produces, so they live here rather than widening the core's error vocabulary.
|
|
98
|
+
*/
|
|
99
|
+
export const WASI_ERRNO = {
|
|
100
|
+
SUCCESS: 0,
|
|
101
|
+
ACCES: 2,
|
|
102
|
+
BADF: 8,
|
|
103
|
+
EXIST: 20,
|
|
104
|
+
INVAL: 28,
|
|
105
|
+
IO: 29,
|
|
106
|
+
ISDIR: 31,
|
|
107
|
+
LOOP: 32,
|
|
108
|
+
NOENT: 44,
|
|
109
|
+
NOSYS: 52,
|
|
110
|
+
NOTDIR: 54,
|
|
111
|
+
NOTEMPTY: 55,
|
|
112
|
+
NOTSUP: 58,
|
|
113
|
+
OVERFLOW: 61,
|
|
114
|
+
PERM: 63,
|
|
115
|
+
SPIPE: 70,
|
|
116
|
+
NOTCAPABLE: 76,
|
|
117
|
+
} as const;
|
|
118
|
+
|
|
119
|
+
/** WASI preview1 filetypes. */
|
|
120
|
+
export const WASI_FILETYPE = {
|
|
121
|
+
UNKNOWN: 0,
|
|
122
|
+
BLOCK_DEVICE: 1,
|
|
123
|
+
CHARACTER_DEVICE: 2,
|
|
124
|
+
DIRECTORY: 3,
|
|
125
|
+
REGULAR_FILE: 4,
|
|
126
|
+
SOCKET_DGRAM: 5,
|
|
127
|
+
SOCKET_STREAM: 6,
|
|
128
|
+
SYMBOLIC_LINK: 7,
|
|
129
|
+
} as const;
|
|
130
|
+
|
|
131
|
+
/** `path_open` oflags. */
|
|
132
|
+
export const OFLAGS_CREAT = 1;
|
|
133
|
+
export const OFLAGS_DIRECTORY = 2;
|
|
134
|
+
export const OFLAGS_EXCL = 4;
|
|
135
|
+
export const OFLAGS_TRUNC = 8;
|
|
136
|
+
|
|
137
|
+
/** `fdflags`, on `path_open` and `fd_fdstat_set_flags`. */
|
|
138
|
+
export const FDFLAGS_APPEND = 1;
|
|
139
|
+
export const FDFLAGS_DSYNC = 2;
|
|
140
|
+
export const FDFLAGS_NONBLOCK = 4;
|
|
141
|
+
export const FDFLAGS_RSYNC = 8;
|
|
142
|
+
export const FDFLAGS_SYNC = 16;
|
|
143
|
+
|
|
144
|
+
/** `lookupflags`, on every path operation that could follow a symlink. */
|
|
145
|
+
export const LOOKUPFLAGS_SYMLINK_FOLLOW = 1;
|
|
146
|
+
|
|
147
|
+
/** The two rights the adapter reads; the rest are carried through untouched. */
|
|
148
|
+
export const RIGHTS_FD_READ = 1n << 1n;
|
|
149
|
+
export const RIGHTS_FD_WRITE = 1n << 6n;
|
|
150
|
+
/** What a preopen advertises when the host asked for nothing narrower. */
|
|
151
|
+
export const RIGHTS_ALL = 0xffff_ffff_ffff_ffffn;
|
|
152
|
+
|
|
153
|
+
/** `fd_seek` whence values. */
|
|
154
|
+
export const WHENCE_SET = 0;
|
|
155
|
+
export const WHENCE_CUR = 1;
|
|
156
|
+
export const WHENCE_END = 2;
|
|
157
|
+
|
|
158
|
+
/** `filestat` is 64 bytes; `dirent` is 24 bytes plus the name. */
|
|
159
|
+
const FILESTAT_BYTES = 64;
|
|
160
|
+
const DIRENT_HEADER_BYTES = 24;
|
|
161
|
+
/** What `stat` reports for a directory — the store has no directory size, and `0` confuses callers. */
|
|
162
|
+
const DIRECTORY_SIZE = 4096n;
|
|
163
|
+
const NS_PER_MS = 1_000_000n;
|
|
164
|
+
|
|
165
|
+
/** One open descriptor the adapter owns. */
|
|
166
|
+
interface AdapterFd {
|
|
167
|
+
readonly fd: number;
|
|
168
|
+
/** The normalized absolute path this fd was opened with; the store resizes by path only. */
|
|
169
|
+
path: string;
|
|
170
|
+
readonly isDir: boolean;
|
|
171
|
+
/** The broker's descriptor — `undefined` for a directory, which the store cannot open. */
|
|
172
|
+
readonly clientFd: number | undefined;
|
|
173
|
+
/** The adapter's own cursor. Every transfer carries an explicit position, so this is authoritative. */
|
|
174
|
+
offset: bigint;
|
|
175
|
+
fdflags: number;
|
|
176
|
+
rightsBase: bigint;
|
|
177
|
+
rightsInheriting: bigint;
|
|
178
|
+
readonly readable: boolean;
|
|
179
|
+
readonly writable: boolean;
|
|
180
|
+
/** The listing snapshot a `fd_readdir` cookie indexes into, taken on cookie 0. */
|
|
181
|
+
listing: DirentSnapshot | undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
interface DirentSnapshot {
|
|
185
|
+
readonly names: readonly string[];
|
|
186
|
+
/** Lazily filled per emitted entry: a `d_type` costs one `lstat`, so only emitted names pay it. */
|
|
187
|
+
readonly filetypes: Map<string, number>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface WasiPreview1FsOptions {
|
|
191
|
+
/** The synchronous broker client this adapter turns WASI calls into. */
|
|
192
|
+
readonly client: RepackedSyncClient;
|
|
193
|
+
/**
|
|
194
|
+
* The guest's linear memory, resolved on EVERY call. A shared wasm memory grows underneath the host
|
|
195
|
+
* and any cached `Uint8Array`/`DataView` goes stale (or detaches outright, for a non-shared one), so
|
|
196
|
+
* nothing here holds a view across a call.
|
|
197
|
+
*/
|
|
198
|
+
readonly memory: () => ArrayBuffer | SharedArrayBuffer;
|
|
199
|
+
/** The fd the guest sees the preopened directory as. Defaults to 3, the WASI convention. */
|
|
200
|
+
readonly preopenFd?: number;
|
|
201
|
+
/** The directory that preopen names. Defaults to the store root. */
|
|
202
|
+
readonly preopenPath?: string;
|
|
203
|
+
/** The first fd the adapter hands out, and the bottom of the range it claims. Defaults to 4. */
|
|
204
|
+
readonly fdBase?: number;
|
|
205
|
+
/** The adapter's clock, in milliseconds. Defaults to the wall clock; used for diagnostics. */
|
|
206
|
+
readonly now?: () => bigint;
|
|
207
|
+
/** Where a JS exception that escaped a WASI call is reported. Defaults to `console.error`. */
|
|
208
|
+
readonly onError?: (call: string, cause: unknown) => void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The `wasi_snapshot_preview1` filesystem surface, with the ABI's exact signatures. */
|
|
212
|
+
export interface WasiPreview1FsFunctions {
|
|
213
|
+
fd_prestat_get(fd: number, resultPtr: number): number;
|
|
214
|
+
fd_prestat_dir_name(fd: number, pathPtr: number, pathLen: number): number;
|
|
215
|
+
fd_close(fd: number): number;
|
|
216
|
+
fd_read(fd: number, iovsPtr: number, iovsLen: number, nreadPtr: number): number;
|
|
217
|
+
fd_pread(fd: number, iovsPtr: number, iovsLen: number, offset: bigint, nreadPtr: number): number;
|
|
218
|
+
fd_write(fd: number, iovsPtr: number, iovsLen: number, nwrittenPtr: number): number;
|
|
219
|
+
fd_pwrite(fd: number, iovsPtr: number, iovsLen: number, offset: bigint, nwrittenPtr: number): number;
|
|
220
|
+
fd_seek(fd: number, offset: bigint, whence: number, resultPtr: number): number;
|
|
221
|
+
fd_tell(fd: number, resultPtr: number): number;
|
|
222
|
+
fd_fdstat_get(fd: number, resultPtr: number): number;
|
|
223
|
+
fd_fdstat_set_flags(fd: number, fdflags: number): number;
|
|
224
|
+
fd_fdstat_set_rights(fd: number, rightsBase: bigint, rightsInheriting: bigint): number;
|
|
225
|
+
fd_filestat_get(fd: number, resultPtr: number): number;
|
|
226
|
+
fd_filestat_set_size(fd: number, size: bigint): number;
|
|
227
|
+
fd_filestat_set_times(fd: number, atim: bigint, mtim: bigint, fstflags: number): number;
|
|
228
|
+
fd_readdir(fd: number, bufPtr: number, bufLen: number, cookie: bigint, bufusedPtr: number): number;
|
|
229
|
+
fd_sync(fd: number): number;
|
|
230
|
+
fd_datasync(fd: number): number;
|
|
231
|
+
fd_allocate(fd: number, offset: bigint, length: bigint): number;
|
|
232
|
+
fd_advise(fd: number, offset: bigint, length: bigint, advice: number): number;
|
|
233
|
+
path_open(
|
|
234
|
+
dirfd: number,
|
|
235
|
+
dirflags: number,
|
|
236
|
+
pathPtr: number,
|
|
237
|
+
pathLen: number,
|
|
238
|
+
oflags: number,
|
|
239
|
+
rightsBase: bigint,
|
|
240
|
+
rightsInheriting: bigint,
|
|
241
|
+
fdflags: number,
|
|
242
|
+
resultPtr: number,
|
|
243
|
+
): number;
|
|
244
|
+
path_filestat_get(dirfd: number, flags: number, pathPtr: number, pathLen: number, resultPtr: number): number;
|
|
245
|
+
path_filestat_set_times(
|
|
246
|
+
dirfd: number,
|
|
247
|
+
flags: number,
|
|
248
|
+
pathPtr: number,
|
|
249
|
+
pathLen: number,
|
|
250
|
+
atim: bigint,
|
|
251
|
+
mtim: bigint,
|
|
252
|
+
fstflags: number,
|
|
253
|
+
): number;
|
|
254
|
+
path_create_directory(dirfd: number, pathPtr: number, pathLen: number): number;
|
|
255
|
+
path_remove_directory(dirfd: number, pathPtr: number, pathLen: number): number;
|
|
256
|
+
path_unlink_file(dirfd: number, pathPtr: number, pathLen: number): number;
|
|
257
|
+
path_rename(dirfd: number, oldPtr: number, oldLen: number, newDirfd: number, newPtr: number, newLen: number): number;
|
|
258
|
+
path_readlink(
|
|
259
|
+
dirfd: number,
|
|
260
|
+
pathPtr: number,
|
|
261
|
+
pathLen: number,
|
|
262
|
+
bufPtr: number,
|
|
263
|
+
bufLen: number,
|
|
264
|
+
bufusedPtr: number,
|
|
265
|
+
): number;
|
|
266
|
+
path_symlink(oldPtr: number, oldLen: number, dirfd: number, newPtr: number, newLen: number): number;
|
|
267
|
+
path_link(
|
|
268
|
+
oldDirfd: number,
|
|
269
|
+
oldFlags: number,
|
|
270
|
+
oldPtr: number,
|
|
271
|
+
oldLen: number,
|
|
272
|
+
newDirfd: number,
|
|
273
|
+
newPtr: number,
|
|
274
|
+
newLen: number,
|
|
275
|
+
): number;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** The adapter: the WASI surface plus the three composition/lifecycle helpers a host needs. */
|
|
279
|
+
export interface WasiPreview1Fs extends WasiPreview1FsFunctions {
|
|
280
|
+
/** Whether this adapter answers for `fd`: the preopen, or anything at or above `fdBase`. */
|
|
281
|
+
owns(fd: number): boolean;
|
|
282
|
+
/** How many descriptors the adapter currently holds, preopen excluded. */
|
|
283
|
+
openFdCount(): number;
|
|
284
|
+
/**
|
|
285
|
+
* A merged `wasi_snapshot_preview1` object: every filesystem call goes to the adapter when the fd
|
|
286
|
+
* (or the dirfd, for a path operation) is adapter-owned and to `base` otherwise. Everything in
|
|
287
|
+
* `base` that is not a filesystem call — `args_get`, `clock_time_get`, `poll_oneoff`, `proc_exit`,
|
|
288
|
+
* `random_get`, `sched_yield`, the socket stubs — is carried through untouched, as are `base`'s own
|
|
289
|
+
* fd 0/1/2 handlers.
|
|
290
|
+
*/
|
|
291
|
+
compose(base: Readonly<Record<string, unknown>>): Record<string, unknown>;
|
|
292
|
+
/**
|
|
293
|
+
* Close every descriptor, returning how many were released. The call a thread makes on its way out:
|
|
294
|
+
* without it the coordinator holds the thread's store descriptors until the whole channel detaches.
|
|
295
|
+
*/
|
|
296
|
+
closeAll(): number;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Every filesystem import name, mapped to the argument index carrying the fd or dirfd. */
|
|
300
|
+
const FD_ARGUMENT_INDEX: Readonly<Record<keyof WasiPreview1FsFunctions, number>> = {
|
|
301
|
+
fd_prestat_get: 0,
|
|
302
|
+
fd_prestat_dir_name: 0,
|
|
303
|
+
fd_close: 0,
|
|
304
|
+
fd_read: 0,
|
|
305
|
+
fd_pread: 0,
|
|
306
|
+
fd_write: 0,
|
|
307
|
+
fd_pwrite: 0,
|
|
308
|
+
fd_seek: 0,
|
|
309
|
+
fd_tell: 0,
|
|
310
|
+
fd_fdstat_get: 0,
|
|
311
|
+
fd_fdstat_set_flags: 0,
|
|
312
|
+
fd_fdstat_set_rights: 0,
|
|
313
|
+
fd_filestat_get: 0,
|
|
314
|
+
fd_filestat_set_size: 0,
|
|
315
|
+
fd_filestat_set_times: 0,
|
|
316
|
+
fd_readdir: 0,
|
|
317
|
+
fd_sync: 0,
|
|
318
|
+
fd_datasync: 0,
|
|
319
|
+
fd_allocate: 0,
|
|
320
|
+
fd_advise: 0,
|
|
321
|
+
path_open: 0,
|
|
322
|
+
path_filestat_get: 0,
|
|
323
|
+
path_filestat_set_times: 0,
|
|
324
|
+
path_create_directory: 0,
|
|
325
|
+
path_remove_directory: 0,
|
|
326
|
+
path_unlink_file: 0,
|
|
327
|
+
path_rename: 0,
|
|
328
|
+
path_readlink: 0,
|
|
329
|
+
// The only call whose directory argument is not first: `path_symlink(old_path, old_len, fd, …)`.
|
|
330
|
+
path_symlink: 2,
|
|
331
|
+
path_link: 0,
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const textEncoder = new TextEncoder();
|
|
335
|
+
const textDecoder = new TextDecoder("utf-8", { fatal: false });
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Canonicalize a guest path the way the store demands: absolute, no `.`/`..`, no empty or repeated
|
|
339
|
+
* separator, no trailing slash. Deliberately lenient in the same places a WASI host has to be —
|
|
340
|
+
* wasi-libc hands preopen-RELATIVE paths, but an absolute one is accepted too, and a trailing NUL from
|
|
341
|
+
* a fixed-size buffer is stripped rather than rejected.
|
|
342
|
+
*/
|
|
343
|
+
export function normalizeWasiPath(path: string): string {
|
|
344
|
+
let value = path.replace(/\0+$/u, "");
|
|
345
|
+
if (value === "" || value === ".") return "/";
|
|
346
|
+
if (!value.startsWith("/")) value = `/${value}`;
|
|
347
|
+
const parts: string[] = [];
|
|
348
|
+
for (const segment of value.split("/")) {
|
|
349
|
+
if (segment === "" || segment === ".") continue;
|
|
350
|
+
if (segment === "..") {
|
|
351
|
+
parts.pop();
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
parts.push(segment);
|
|
355
|
+
}
|
|
356
|
+
return `/${parts.join("/")}`;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* A stable synthetic inode number for a path. The store has no inode numbers on the wire, and a WASI
|
|
361
|
+
* `filestat`/`dirent` must carry one; a 64-bit FNV-1a of the path is stable for as long as the path
|
|
362
|
+
* is, never zero, and never collides in practice within one datadir.
|
|
363
|
+
*/
|
|
364
|
+
function inodeOf(path: string): bigint {
|
|
365
|
+
let hash = 0xcbf2_9ce4_8422_2325n;
|
|
366
|
+
const bytes = textEncoder.encode(path);
|
|
367
|
+
for (const byte of bytes) {
|
|
368
|
+
hash = BigInt.asUintN(64, (hash ^ BigInt(byte)) * 0x1000_0000_01b3n);
|
|
369
|
+
}
|
|
370
|
+
return hash === 0n ? 1n : hash;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function toBigInt(value: bigint | number): bigint {
|
|
374
|
+
return typeof value === "bigint" ? value : BigInt(Math.trunc(value));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function createWasiPreview1Fs(options: WasiPreview1FsOptions): WasiPreview1Fs {
|
|
378
|
+
const client = options.client;
|
|
379
|
+
const preopenFd = options.preopenFd ?? 3;
|
|
380
|
+
const preopenPath = normalizeWasiPath(options.preopenPath ?? "/");
|
|
381
|
+
const fdBase = options.fdBase ?? preopenFd + 1;
|
|
382
|
+
const now = options.now ?? (() => BigInt(Date.now()));
|
|
383
|
+
const onError =
|
|
384
|
+
options.onError ??
|
|
385
|
+
((call: string, cause: unknown) => {
|
|
386
|
+
const detail = cause instanceof Error ? (cause.stack ?? `${cause.name}: ${cause.message}`) : String(cause);
|
|
387
|
+
console.error(`[wasi-preview1 ${call} @${now()}] ${detail}`);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
if (!Number.isSafeInteger(preopenFd) || preopenFd < 3) {
|
|
391
|
+
throw new RangeError("a WASI preopen fd must be a safe integer of at least 3 (0-2 are stdio)");
|
|
392
|
+
}
|
|
393
|
+
if (!Number.isSafeInteger(fdBase) || fdBase <= preopenFd) {
|
|
394
|
+
throw new RangeError("the WASI adapter fd base must be a safe integer above the preopen fd");
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const table = new Map<number, AdapterFd>();
|
|
398
|
+
let nextFd = fdBase;
|
|
399
|
+
|
|
400
|
+
const preopenEntry = (): AdapterFd => ({
|
|
401
|
+
fd: preopenFd,
|
|
402
|
+
path: preopenPath,
|
|
403
|
+
isDir: true,
|
|
404
|
+
clientFd: undefined,
|
|
405
|
+
offset: 0n,
|
|
406
|
+
fdflags: 0,
|
|
407
|
+
rightsBase: RIGHTS_ALL,
|
|
408
|
+
rightsInheriting: RIGHTS_ALL,
|
|
409
|
+
readable: true,
|
|
410
|
+
writable: false,
|
|
411
|
+
listing: undefined,
|
|
412
|
+
});
|
|
413
|
+
table.set(preopenFd, preopenEntry());
|
|
414
|
+
|
|
415
|
+
// ---- memory access -------------------------------------------------------
|
|
416
|
+
// Re-derived per call, never cached: the guest memory is shared and grows underneath us.
|
|
417
|
+
const bytes = (): Uint8Array => new Uint8Array(options.memory());
|
|
418
|
+
const view = (): DataView => new DataView(options.memory());
|
|
419
|
+
|
|
420
|
+
function readGuestString(ptr: number, length: number): string {
|
|
421
|
+
// `.slice()`, not `.subarray()`: TextDecoder refuses a SharedArrayBuffer-backed view in Chrome
|
|
422
|
+
// (the [AllowShared] rule). Paths are short, so the copy costs nothing.
|
|
423
|
+
return textDecoder.decode(bytes().slice(ptr, ptr + length));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function* iovecs(ptr: number, count: number): Generator<{ ptr: number; len: number }> {
|
|
427
|
+
const data = view();
|
|
428
|
+
for (let index = 0; index < count; index += 1) {
|
|
429
|
+
const base = ptr + index * 8;
|
|
430
|
+
yield { ptr: data.getUint32(base, true), len: data.getUint32(base + 4, true) };
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ---- fd table ------------------------------------------------------------
|
|
435
|
+
function owns(fd: number): boolean {
|
|
436
|
+
return fd === preopenFd || (Number.isSafeInteger(fd) && fd >= fdBase);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function entryOf(fd: number): AdapterFd | undefined {
|
|
440
|
+
return table.get(fd);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function allocate(entry: Omit<AdapterFd, "fd">): number {
|
|
444
|
+
const fd = nextFd;
|
|
445
|
+
nextFd += 1;
|
|
446
|
+
table.set(fd, { ...entry, fd });
|
|
447
|
+
return fd;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function resolve(dirfd: number, ptr: number, length: number): string | undefined {
|
|
451
|
+
const dir = entryOf(dirfd);
|
|
452
|
+
if (dir === undefined || !dir.isDir) return undefined;
|
|
453
|
+
const relative = readGuestString(ptr, length);
|
|
454
|
+
if (relative.startsWith("/")) return normalizeWasiPath(relative);
|
|
455
|
+
return normalizeWasiPath(dir.path === "/" ? `/${relative}` : `${dir.path}/${relative}`);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// ---- stat ----------------------------------------------------------------
|
|
459
|
+
function filetypeOf(kind: BrokerStat["kind"]): number {
|
|
460
|
+
if (kind === "directory") return WASI_FILETYPE.DIRECTORY;
|
|
461
|
+
return kind === "symlink" ? WASI_FILETYPE.SYMBOLIC_LINK : WASI_FILETYPE.REGULAR_FILE;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function writeFilestat(ptr: number, path: string, stat: BrokerStat): void {
|
|
465
|
+
// Zero first: the struct has padding the guest is entitled to see as zero, and a reused stack
|
|
466
|
+
// slot would otherwise hand it whatever the last call left there.
|
|
467
|
+
bytes().fill(0, ptr, ptr + FILESTAT_BYTES);
|
|
468
|
+
const data = view();
|
|
469
|
+
const isDirectory = stat.kind === "directory";
|
|
470
|
+
const times = [stat.atimeMs, stat.mtimeMs, stat.ctimeMs].map((ms) => ms * NS_PER_MS);
|
|
471
|
+
data.setBigUint64(ptr + 0, 1n, true); // dev
|
|
472
|
+
data.setBigUint64(ptr + 8, inodeOf(path), true); // ino
|
|
473
|
+
data.setUint8(ptr + 16, filetypeOf(stat.kind));
|
|
474
|
+
data.setBigUint64(ptr + 24, 1n, true); // nlink
|
|
475
|
+
// A link's size is its target's byte length, exactly as POSIX `lstat` reports it.
|
|
476
|
+
data.setBigUint64(ptr + 32, isDirectory ? DIRECTORY_SIZE : stat.size, true);
|
|
477
|
+
data.setBigUint64(ptr + 40, times[0]!, true); // atim
|
|
478
|
+
data.setBigUint64(ptr + 48, times[1]!, true); // mtim
|
|
479
|
+
data.setBigUint64(ptr + 56, times[2]!, true); // ctim
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** The size of a descriptor's file, from the broker fd (never the path — a rename must not race it). */
|
|
483
|
+
function sizeOf(entry: AdapterFd): { errno: number; size: bigint } {
|
|
484
|
+
if (entry.clientFd === undefined) return { errno: 0, size: DIRECTORY_SIZE };
|
|
485
|
+
const stat = client.fstat(entry.clientFd);
|
|
486
|
+
if (stat.errno !== 0 || stat.stat === undefined) return { errno: stat.errno || WASI_ERRNO.IO, size: 0n };
|
|
487
|
+
return { errno: 0, size: stat.stat.size };
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// ---- the WASI surface ----------------------------------------------------
|
|
491
|
+
const fs: WasiPreview1FsFunctions = {
|
|
492
|
+
fd_prestat_get(fd, resultPtr) {
|
|
493
|
+
if (fd !== preopenFd) return WASI_ERRNO.BADF;
|
|
494
|
+
const data = view();
|
|
495
|
+
data.setUint8(resultPtr, 0); // preopentype::dir
|
|
496
|
+
data.setUint32(resultPtr + 4, textEncoder.encode(preopenPath).byteLength, true);
|
|
497
|
+
return WASI_ERRNO.SUCCESS;
|
|
498
|
+
},
|
|
499
|
+
|
|
500
|
+
fd_prestat_dir_name(fd, pathPtr, pathLen) {
|
|
501
|
+
if (fd !== preopenFd) return WASI_ERRNO.BADF;
|
|
502
|
+
const encoded = textEncoder.encode(preopenPath);
|
|
503
|
+
if (pathLen < encoded.byteLength) return WASI_ERRNO.INVAL;
|
|
504
|
+
bytes().set(encoded, pathPtr);
|
|
505
|
+
return WASI_ERRNO.SUCCESS;
|
|
506
|
+
},
|
|
507
|
+
|
|
508
|
+
fd_close(fd) {
|
|
509
|
+
if (fd === preopenFd) return WASI_ERRNO.SUCCESS; // the preopen outlives every guest close
|
|
510
|
+
const entry = entryOf(fd);
|
|
511
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
512
|
+
table.delete(fd);
|
|
513
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.SUCCESS;
|
|
514
|
+
return client.close(entry.clientFd).errno;
|
|
515
|
+
},
|
|
516
|
+
|
|
517
|
+
fd_read(fd, iovsPtr, iovsLen, nreadPtr) {
|
|
518
|
+
const entry = entryOf(fd);
|
|
519
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
520
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
521
|
+
if (!entry.readable) return WASI_ERRNO.NOTCAPABLE;
|
|
522
|
+
let total = 0;
|
|
523
|
+
let errno: number = WASI_ERRNO.SUCCESS;
|
|
524
|
+
for (const { ptr, len } of iovecs(iovsPtr, iovsLen)) {
|
|
525
|
+
if (len === 0) continue;
|
|
526
|
+
const chunk = client.read(entry.clientFd, len, entry.offset + BigInt(total));
|
|
527
|
+
if (chunk.count > 0) bytes().set(chunk.bytes, ptr);
|
|
528
|
+
total += chunk.count;
|
|
529
|
+
if (chunk.errno !== 0) {
|
|
530
|
+
errno = chunk.errno;
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
if (chunk.count < len) break; // end of file
|
|
534
|
+
}
|
|
535
|
+
entry.offset += BigInt(total);
|
|
536
|
+
view().setUint32(nreadPtr, total, true);
|
|
537
|
+
// A rejection part-way through still reports what transferred, exactly as a short read does.
|
|
538
|
+
return total > 0 ? WASI_ERRNO.SUCCESS : errno;
|
|
539
|
+
},
|
|
540
|
+
|
|
541
|
+
fd_pread(fd, iovsPtr, iovsLen, offset, nreadPtr) {
|
|
542
|
+
const entry = entryOf(fd);
|
|
543
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
544
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
545
|
+
if (!entry.readable) return WASI_ERRNO.NOTCAPABLE;
|
|
546
|
+
let at = toBigInt(offset);
|
|
547
|
+
let total = 0;
|
|
548
|
+
let errno: number = WASI_ERRNO.SUCCESS;
|
|
549
|
+
for (const { ptr, len } of iovecs(iovsPtr, iovsLen)) {
|
|
550
|
+
if (len === 0) continue;
|
|
551
|
+
const chunk = client.read(entry.clientFd, len, at);
|
|
552
|
+
if (chunk.count > 0) bytes().set(chunk.bytes, ptr);
|
|
553
|
+
at += BigInt(chunk.count);
|
|
554
|
+
total += chunk.count;
|
|
555
|
+
if (chunk.errno !== 0) {
|
|
556
|
+
errno = chunk.errno;
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
if (chunk.count < len) break;
|
|
560
|
+
}
|
|
561
|
+
view().setUint32(nreadPtr, total, true);
|
|
562
|
+
return total > 0 ? WASI_ERRNO.SUCCESS : errno;
|
|
563
|
+
},
|
|
564
|
+
|
|
565
|
+
fd_write(fd, iovsPtr, iovsLen, nwrittenPtr) {
|
|
566
|
+
const entry = entryOf(fd);
|
|
567
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
568
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
569
|
+
if (!entry.writable) return WASI_ERRNO.NOTCAPABLE;
|
|
570
|
+
// O_APPEND is emulated here: resolve end-of-file once, then write at explicit positions.
|
|
571
|
+
if ((entry.fdflags & FDFLAGS_APPEND) !== 0) {
|
|
572
|
+
const end = sizeOf(entry);
|
|
573
|
+
if (end.errno !== 0) return end.errno;
|
|
574
|
+
entry.offset = end.size;
|
|
575
|
+
}
|
|
576
|
+
let total = 0;
|
|
577
|
+
let errno: number = WASI_ERRNO.SUCCESS;
|
|
578
|
+
for (const { ptr, len } of iovecs(iovsPtr, iovsLen)) {
|
|
579
|
+
if (len === 0) continue;
|
|
580
|
+
const source = bytes().subarray(ptr, ptr + len);
|
|
581
|
+
const written = client.write(entry.clientFd, source, entry.offset + BigInt(total));
|
|
582
|
+
total += written.count;
|
|
583
|
+
if (written.errno !== 0) {
|
|
584
|
+
errno = written.errno;
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
if (written.count < len) break; // the store admitted less than asked
|
|
588
|
+
}
|
|
589
|
+
entry.offset += BigInt(total);
|
|
590
|
+
view().setUint32(nwrittenPtr, total, true);
|
|
591
|
+
return total > 0 ? WASI_ERRNO.SUCCESS : errno;
|
|
592
|
+
},
|
|
593
|
+
|
|
594
|
+
fd_pwrite(fd, iovsPtr, iovsLen, offset, nwrittenPtr) {
|
|
595
|
+
const entry = entryOf(fd);
|
|
596
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
597
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
598
|
+
if (!entry.writable) return WASI_ERRNO.NOTCAPABLE;
|
|
599
|
+
let at = toBigInt(offset);
|
|
600
|
+
let total = 0;
|
|
601
|
+
let errno: number = WASI_ERRNO.SUCCESS;
|
|
602
|
+
for (const { ptr, len } of iovecs(iovsPtr, iovsLen)) {
|
|
603
|
+
if (len === 0) continue;
|
|
604
|
+
const written = client.write(entry.clientFd, bytes().subarray(ptr, ptr + len), at);
|
|
605
|
+
at += BigInt(written.count);
|
|
606
|
+
total += written.count;
|
|
607
|
+
if (written.errno !== 0) {
|
|
608
|
+
errno = written.errno;
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
if (written.count < len) break;
|
|
612
|
+
}
|
|
613
|
+
view().setUint32(nwrittenPtr, total, true);
|
|
614
|
+
return total > 0 ? WASI_ERRNO.SUCCESS : errno;
|
|
615
|
+
},
|
|
616
|
+
|
|
617
|
+
fd_seek(fd, offset, whence, resultPtr) {
|
|
618
|
+
const entry = entryOf(fd);
|
|
619
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
620
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.BADF; // a directory has no seekable offset
|
|
621
|
+
const delta = toBigInt(offset);
|
|
622
|
+
let target: bigint;
|
|
623
|
+
if (whence === WHENCE_SET) target = delta;
|
|
624
|
+
else if (whence === WHENCE_CUR) target = entry.offset + delta;
|
|
625
|
+
else if (whence === WHENCE_END) {
|
|
626
|
+
const end = sizeOf(entry);
|
|
627
|
+
if (end.errno !== 0) return end.errno;
|
|
628
|
+
target = end.size + delta;
|
|
629
|
+
} else return WASI_ERRNO.INVAL;
|
|
630
|
+
if (target < 0n) return WASI_ERRNO.INVAL;
|
|
631
|
+
entry.offset = target;
|
|
632
|
+
view().setBigUint64(resultPtr, target, true);
|
|
633
|
+
return WASI_ERRNO.SUCCESS;
|
|
634
|
+
},
|
|
635
|
+
|
|
636
|
+
fd_tell(fd, resultPtr) {
|
|
637
|
+
const entry = entryOf(fd);
|
|
638
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
639
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.BADF;
|
|
640
|
+
view().setBigUint64(resultPtr, entry.offset, true);
|
|
641
|
+
return WASI_ERRNO.SUCCESS;
|
|
642
|
+
},
|
|
643
|
+
|
|
644
|
+
fd_fdstat_get(fd, resultPtr) {
|
|
645
|
+
const entry = entryOf(fd);
|
|
646
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
647
|
+
const data = view();
|
|
648
|
+
data.setUint8(resultPtr, entry.isDir ? WASI_FILETYPE.DIRECTORY : WASI_FILETYPE.REGULAR_FILE);
|
|
649
|
+
data.setUint8(resultPtr + 1, 0);
|
|
650
|
+
data.setUint16(resultPtr + 2, entry.fdflags, true);
|
|
651
|
+
data.setUint32(resultPtr + 4, 0, true);
|
|
652
|
+
data.setBigUint64(resultPtr + 8, entry.rightsBase, true);
|
|
653
|
+
data.setBigUint64(resultPtr + 16, entry.rightsInheriting, true);
|
|
654
|
+
return WASI_ERRNO.SUCCESS;
|
|
655
|
+
},
|
|
656
|
+
|
|
657
|
+
fd_fdstat_set_flags(fd, fdflags) {
|
|
658
|
+
const entry = entryOf(fd);
|
|
659
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
660
|
+
// APPEND is the adapter's own emulation, so it can be turned on and off freely; the rest of the
|
|
661
|
+
// sync bits are accepted and ignored (the store's only durability primitive is `fsync`, which is
|
|
662
|
+
// already store-wide and stronger than DSYNC/RSYNC/SYNC promise). NONBLOCK is meaningless here:
|
|
663
|
+
// every broker call blocks by construction.
|
|
664
|
+
entry.fdflags = fdflags & (FDFLAGS_APPEND | FDFLAGS_DSYNC | FDFLAGS_NONBLOCK | FDFLAGS_RSYNC | FDFLAGS_SYNC);
|
|
665
|
+
return WASI_ERRNO.SUCCESS;
|
|
666
|
+
},
|
|
667
|
+
|
|
668
|
+
fd_fdstat_set_rights(fd, rightsBase, rightsInheriting) {
|
|
669
|
+
const entry = entryOf(fd);
|
|
670
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
671
|
+
// Rights may only ever be narrowed.
|
|
672
|
+
const base = toBigInt(rightsBase);
|
|
673
|
+
const inheriting = toBigInt(rightsInheriting);
|
|
674
|
+
if ((base & ~entry.rightsBase) !== 0n || (inheriting & ~entry.rightsInheriting) !== 0n) {
|
|
675
|
+
return WASI_ERRNO.NOTCAPABLE;
|
|
676
|
+
}
|
|
677
|
+
entry.rightsBase = base;
|
|
678
|
+
entry.rightsInheriting = inheriting;
|
|
679
|
+
return WASI_ERRNO.SUCCESS;
|
|
680
|
+
},
|
|
681
|
+
|
|
682
|
+
fd_filestat_get(fd, resultPtr) {
|
|
683
|
+
const entry = entryOf(fd);
|
|
684
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
685
|
+
const stat = entry.clientFd === undefined ? client.stat(entry.path) : client.fstat(entry.clientFd);
|
|
686
|
+
if (stat.errno !== 0 || stat.stat === undefined) return stat.errno || WASI_ERRNO.IO;
|
|
687
|
+
writeFilestat(resultPtr, entry.path, stat.stat);
|
|
688
|
+
return WASI_ERRNO.SUCCESS;
|
|
689
|
+
},
|
|
690
|
+
|
|
691
|
+
fd_filestat_set_size(fd, size) {
|
|
692
|
+
const entry = entryOf(fd);
|
|
693
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
694
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
695
|
+
if (!entry.writable) return WASI_ERRNO.NOTCAPABLE;
|
|
696
|
+
// The store resizes by PATH only, which is why every fd remembers the path it was opened with.
|
|
697
|
+
return client.truncate(entry.path, toBigInt(size)).errno;
|
|
698
|
+
},
|
|
699
|
+
|
|
700
|
+
fd_filestat_set_times(fd, _atim, _mtim, fstflags) {
|
|
701
|
+
const entry = entryOf(fd);
|
|
702
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
703
|
+
if ((fstflags & ~0b1111) !== 0) return WASI_ERRNO.INVAL;
|
|
704
|
+
if (fstflags === 0) return WASI_ERRNO.SUCCESS;
|
|
705
|
+
// The broker exposes no `utimes`. Answering ENOTSUP is the honest answer: a timestamp kept in
|
|
706
|
+
// this adapter alone would make two threads on ONE store disagree about a file's mtime.
|
|
707
|
+
return WASI_ERRNO.NOTSUP;
|
|
708
|
+
},
|
|
709
|
+
|
|
710
|
+
fd_readdir(fd, bufPtr, bufLen, cookie, bufusedPtr) {
|
|
711
|
+
const entry = entryOf(fd);
|
|
712
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
713
|
+
if (!entry.isDir) return WASI_ERRNO.NOTDIR;
|
|
714
|
+
const start = Number(toBigInt(cookie));
|
|
715
|
+
if (!Number.isSafeInteger(start) || start < 0) return WASI_ERRNO.INVAL;
|
|
716
|
+
// Snapshot on cookie 0 so pagination is stable across guest mutations — and so a stale cookie
|
|
717
|
+
// can never be forwarded to the broker, which treats a cursor past the listing as a protocol
|
|
718
|
+
// violation and detaches the client.
|
|
719
|
+
if (start === 0 || entry.listing === undefined) {
|
|
720
|
+
const listed = client.readdir(entry.path);
|
|
721
|
+
if (listed.errno !== 0) return listed.errno;
|
|
722
|
+
entry.listing = { names: listed.entries, filetypes: new Map() };
|
|
723
|
+
}
|
|
724
|
+
const listing = entry.listing;
|
|
725
|
+
const data = view();
|
|
726
|
+
const memory = bytes();
|
|
727
|
+
let used = 0;
|
|
728
|
+
for (let index = start; index < listing.names.length; index += 1) {
|
|
729
|
+
const name = listing.names[index]!;
|
|
730
|
+
const nameBytes = textEncoder.encode(name);
|
|
731
|
+
if (used + DIRENT_HEADER_BYTES >= bufLen) {
|
|
732
|
+
// No room for another header: report the buffer full so the caller grows it and retries.
|
|
733
|
+
used = bufLen;
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
let filetype = listing.filetypes.get(name);
|
|
737
|
+
if (filetype === undefined) {
|
|
738
|
+
// `lstat`, so a link is reported as one: a guest that walks a directory to decide what to
|
|
739
|
+
// recurse into must see the link rather than whatever it points at.
|
|
740
|
+
const child = client.lstat(entry.path === "/" ? `/${name}` : `${entry.path}/${name}`);
|
|
741
|
+
filetype =
|
|
742
|
+
child.errno !== 0 || child.stat === undefined ? WASI_FILETYPE.UNKNOWN : filetypeOf(child.stat.kind);
|
|
743
|
+
listing.filetypes.set(name, filetype);
|
|
744
|
+
}
|
|
745
|
+
const at = bufPtr + used;
|
|
746
|
+
data.setBigUint64(at, BigInt(index + 1), true); // d_next
|
|
747
|
+
data.setBigUint64(at + 8, inodeOf(entry.path === "/" ? `/${name}` : `${entry.path}/${name}`), true); // d_ino
|
|
748
|
+
data.setUint32(at + 16, nameBytes.byteLength, true); // d_namlen
|
|
749
|
+
data.setUint8(at + 20, filetype);
|
|
750
|
+
data.setUint8(at + 21, 0);
|
|
751
|
+
data.setUint16(at + 22, 0, true);
|
|
752
|
+
const room = bufLen - used - DIRENT_HEADER_BYTES;
|
|
753
|
+
const copied = Math.min(nameBytes.byteLength, room);
|
|
754
|
+
memory.set(nameBytes.subarray(0, copied), at + DIRENT_HEADER_BYTES);
|
|
755
|
+
used += DIRENT_HEADER_BYTES + copied;
|
|
756
|
+
if (copied < nameBytes.byteLength) break; // truncated name: the caller grows the buffer
|
|
757
|
+
}
|
|
758
|
+
data.setUint32(bufusedPtr, used, true);
|
|
759
|
+
return WASI_ERRNO.SUCCESS;
|
|
760
|
+
},
|
|
761
|
+
|
|
762
|
+
fd_sync(fd) {
|
|
763
|
+
const entry = entryOf(fd);
|
|
764
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
765
|
+
// Store-wide by construction: on success every byte any client wrote before the call is durable.
|
|
766
|
+
return entry.clientFd === undefined ? client.fsync().errno : client.fsync(entry.clientFd).errno;
|
|
767
|
+
},
|
|
768
|
+
|
|
769
|
+
fd_datasync(fd) {
|
|
770
|
+
return fs.fd_sync(fd);
|
|
771
|
+
},
|
|
772
|
+
|
|
773
|
+
fd_allocate(fd, offset, length) {
|
|
774
|
+
const entry = entryOf(fd);
|
|
775
|
+
if (entry === undefined) return WASI_ERRNO.BADF;
|
|
776
|
+
if (entry.clientFd === undefined) return WASI_ERRNO.ISDIR;
|
|
777
|
+
if (!entry.writable) return WASI_ERRNO.NOTCAPABLE;
|
|
778
|
+
const end = toBigInt(offset) + toBigInt(length);
|
|
779
|
+
if (end < 0n) return WASI_ERRNO.INVAL;
|
|
780
|
+
const current = sizeOf(entry);
|
|
781
|
+
if (current.errno !== 0) return current.errno;
|
|
782
|
+
// The store has no space RESERVATION primitive, so this extends by truncation instead. That
|
|
783
|
+
// satisfies everything `posix_fallocate` observably promises — the file is at least that big,
|
|
784
|
+
// existing bytes are untouched, the gap reads as zeros — but it does not pre-commit capacity, so
|
|
785
|
+
// a later write into the range can still fail on a full store.
|
|
786
|
+
if (end <= current.size) return WASI_ERRNO.SUCCESS;
|
|
787
|
+
return client.truncate(entry.path, end).errno;
|
|
788
|
+
},
|
|
789
|
+
|
|
790
|
+
fd_advise(fd, _offset, _length, _advice) {
|
|
791
|
+
return entryOf(fd) === undefined ? WASI_ERRNO.BADF : WASI_ERRNO.SUCCESS;
|
|
792
|
+
},
|
|
793
|
+
|
|
794
|
+
path_open(dirfd, dirflags, pathPtr, pathLen, oflags, rightsBase, rightsInheriting, fdflags, resultPtr) {
|
|
795
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
796
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
797
|
+
const followLinks = (dirflags & LOOKUPFLAGS_SYMLINK_FOLLOW) !== 0;
|
|
798
|
+
const base = toBigInt(rightsBase);
|
|
799
|
+
const inheriting = toBigInt(rightsInheriting);
|
|
800
|
+
const wantsRead = (base & RIGHTS_FD_READ) !== 0n;
|
|
801
|
+
const wantsWrite = (base & RIGHTS_FD_WRITE) !== 0n;
|
|
802
|
+
const create = (oflags & OFLAGS_CREAT) !== 0;
|
|
803
|
+
const exclusive = (oflags & OFLAGS_EXCL) !== 0;
|
|
804
|
+
const truncate = (oflags & OFLAGS_TRUNC) !== 0;
|
|
805
|
+
const directory = (oflags & OFLAGS_DIRECTORY) !== 0;
|
|
806
|
+
// A request that asks for neither right is a plain read-only open.
|
|
807
|
+
const readable = wantsRead || !wantsWrite;
|
|
808
|
+
const writable = wantsWrite;
|
|
809
|
+
|
|
810
|
+
const openDirectory = (): number => {
|
|
811
|
+
const stat = followLinks ? client.stat(path) : client.lstat(path);
|
|
812
|
+
if (stat.errno !== 0 || stat.stat === undefined) return stat.errno || WASI_ERRNO.IO;
|
|
813
|
+
// Without SYMLINK_FOLLOW a final component that IS a link must not be opened through.
|
|
814
|
+
if (stat.stat.kind === "symlink") return WASI_ERRNO.LOOP;
|
|
815
|
+
if (stat.stat.kind !== "directory") return WASI_ERRNO.NOTDIR;
|
|
816
|
+
if (create && exclusive) return WASI_ERRNO.EXIST;
|
|
817
|
+
const fd = allocate({
|
|
818
|
+
path,
|
|
819
|
+
isDir: true,
|
|
820
|
+
clientFd: undefined,
|
|
821
|
+
offset: 0n,
|
|
822
|
+
fdflags: 0,
|
|
823
|
+
rightsBase: base === 0n ? RIGHTS_ALL : base,
|
|
824
|
+
rightsInheriting: inheriting === 0n ? RIGHTS_ALL : inheriting,
|
|
825
|
+
readable: true,
|
|
826
|
+
writable: false,
|
|
827
|
+
listing: undefined,
|
|
828
|
+
});
|
|
829
|
+
view().setUint32(resultPtr, fd, true);
|
|
830
|
+
return WASI_ERRNO.SUCCESS;
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
// With O_DIRECTORY the path must never be created as a file, so `open` is not attempted at all.
|
|
834
|
+
if (directory) return openDirectory();
|
|
835
|
+
|
|
836
|
+
// `planOpen` refuses O_CREAT/O_TRUNC without write access, but POSIX `open(O_RDONLY|O_CREAT)` is
|
|
837
|
+
// perfectly legal — so the STORE descriptor is opened wide enough to satisfy the plan while the
|
|
838
|
+
// adapter keeps the narrower access the rights asked for and rejects a later write itself.
|
|
839
|
+
const posixWrite = writable || create || truncate;
|
|
840
|
+
const posixRead = readable || !posixWrite;
|
|
841
|
+
let flags = posixRead && posixWrite ? O_RDWR : posixWrite ? O_WRONLY : O_RDONLY;
|
|
842
|
+
if (create) flags |= O_CREAT;
|
|
843
|
+
if (exclusive) flags |= O_EXCL;
|
|
844
|
+
if (truncate) flags |= O_TRUNC;
|
|
845
|
+
// The broker follows a final link by default; the absent lookup flag is what turns that off.
|
|
846
|
+
if (!followLinks) flags |= O_NOFOLLOW;
|
|
847
|
+
// O_APPEND is never sent: it is emulated on this side so the adapter's offset stays exact and
|
|
848
|
+
// `fd_fdstat_set_flags` can add or remove APPEND on an already-open descriptor.
|
|
849
|
+
|
|
850
|
+
const opened = client.open(path, flags);
|
|
851
|
+
if (opened.errno !== 0) {
|
|
852
|
+
// The store cannot open a directory at all — it answers EISDIR — so that answer is how a
|
|
853
|
+
// directory is recognised on the common path, at no cost to a file open.
|
|
854
|
+
if (opened.errno === WASI_ERRNO.ISDIR) return create && exclusive ? WASI_ERRNO.EXIST : openDirectory();
|
|
855
|
+
return opened.errno;
|
|
856
|
+
}
|
|
857
|
+
const fd = allocate({
|
|
858
|
+
path,
|
|
859
|
+
isDir: false,
|
|
860
|
+
clientFd: opened.fd,
|
|
861
|
+
offset: 0n,
|
|
862
|
+
fdflags: fdflags & (FDFLAGS_APPEND | FDFLAGS_DSYNC | FDFLAGS_NONBLOCK | FDFLAGS_RSYNC | FDFLAGS_SYNC),
|
|
863
|
+
rightsBase: base === 0n ? RIGHTS_ALL : base,
|
|
864
|
+
rightsInheriting: inheriting === 0n ? RIGHTS_ALL : inheriting,
|
|
865
|
+
readable,
|
|
866
|
+
writable,
|
|
867
|
+
listing: undefined,
|
|
868
|
+
});
|
|
869
|
+
view().setUint32(resultPtr, fd, true);
|
|
870
|
+
return WASI_ERRNO.SUCCESS;
|
|
871
|
+
},
|
|
872
|
+
|
|
873
|
+
path_filestat_get(dirfd, flags, pathPtr, pathLen, resultPtr) {
|
|
874
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
875
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
876
|
+
// The split that makes `std::fs::symlink_metadata` differ from `std::fs::metadata`: without
|
|
877
|
+
// the flag the LINK is reported, with it the thing it points at.
|
|
878
|
+
const follow = (flags & LOOKUPFLAGS_SYMLINK_FOLLOW) !== 0;
|
|
879
|
+
const stat = follow ? client.stat(path) : client.lstat(path);
|
|
880
|
+
if (stat.errno !== 0 || stat.stat === undefined) return stat.errno || WASI_ERRNO.IO;
|
|
881
|
+
writeFilestat(resultPtr, path, stat.stat);
|
|
882
|
+
return WASI_ERRNO.SUCCESS;
|
|
883
|
+
},
|
|
884
|
+
|
|
885
|
+
path_filestat_set_times(dirfd, _flags, pathPtr, pathLen, _atim, _mtim, fstflags) {
|
|
886
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
887
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
888
|
+
if ((fstflags & ~0b1111) !== 0) return WASI_ERRNO.INVAL;
|
|
889
|
+
const stat = client.lstat(path);
|
|
890
|
+
if (stat.errno !== 0) return stat.errno;
|
|
891
|
+
if (fstflags === 0) return WASI_ERRNO.SUCCESS;
|
|
892
|
+
return WASI_ERRNO.NOTSUP; // see fd_filestat_set_times
|
|
893
|
+
},
|
|
894
|
+
|
|
895
|
+
path_create_directory(dirfd, pathPtr, pathLen) {
|
|
896
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
897
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
898
|
+
return client.mkdir(path).errno;
|
|
899
|
+
},
|
|
900
|
+
|
|
901
|
+
path_remove_directory(dirfd, pathPtr, pathLen) {
|
|
902
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
903
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
904
|
+
return client.rmdir(path).errno;
|
|
905
|
+
},
|
|
906
|
+
|
|
907
|
+
path_unlink_file(dirfd, pathPtr, pathLen) {
|
|
908
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
909
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
910
|
+
return client.unlink(path).errno;
|
|
911
|
+
},
|
|
912
|
+
|
|
913
|
+
path_rename(dirfd, oldPtr, oldLen, newDirfd, newPtr, newLen) {
|
|
914
|
+
const oldPath = resolve(dirfd, oldPtr, oldLen);
|
|
915
|
+
const newPath = resolve(newDirfd, newPtr, newLen);
|
|
916
|
+
if (oldPath === undefined || newPath === undefined) return WASI_ERRNO.BADF;
|
|
917
|
+
const result = client.rename(oldPath, newPath);
|
|
918
|
+
if (result.errno !== 0) return result.errno;
|
|
919
|
+
// Every descriptor still open on the old path must follow it, or a later `fd_filestat_set_size`
|
|
920
|
+
// would resize whatever now occupies the source name.
|
|
921
|
+
const prefix = oldPath === "/" ? "/" : `${oldPath}/`;
|
|
922
|
+
for (const entry of table.values()) {
|
|
923
|
+
if (entry.path === oldPath) entry.path = newPath;
|
|
924
|
+
else if (entry.path.startsWith(prefix)) entry.path = newPath + entry.path.slice(oldPath.length);
|
|
925
|
+
}
|
|
926
|
+
return WASI_ERRNO.SUCCESS;
|
|
927
|
+
},
|
|
928
|
+
|
|
929
|
+
path_readlink(dirfd, pathPtr, pathLen, bufPtr, bufLen, bufusedPtr) {
|
|
930
|
+
const path = resolve(dirfd, pathPtr, pathLen);
|
|
931
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
932
|
+
// EINVAL for a path that is not a link, ENOENT for one that is not there — both come from the
|
|
933
|
+
// store, whose errnos already ARE these numbers.
|
|
934
|
+
const link = client.readlink(path);
|
|
935
|
+
if (link.errno !== 0 || link.target === undefined) return link.errno || WASI_ERRNO.IO;
|
|
936
|
+
const encoded = textEncoder.encode(link.target);
|
|
937
|
+
// preview1 writes no terminator and truncates silently; `bufused === buflen` is how a caller
|
|
938
|
+
// learns its buffer was too small, so a short buffer must NOT be an error here.
|
|
939
|
+
const copied = Math.min(encoded.byteLength, bufLen);
|
|
940
|
+
if (copied > 0) bytes().set(encoded.subarray(0, copied), bufPtr);
|
|
941
|
+
view().setUint32(bufusedPtr, copied, true);
|
|
942
|
+
return WASI_ERRNO.SUCCESS;
|
|
943
|
+
},
|
|
944
|
+
|
|
945
|
+
path_symlink(oldPtr, oldLen, dirfd, newPtr, newLen) {
|
|
946
|
+
const path = resolve(dirfd, newPtr, newLen);
|
|
947
|
+
if (path === undefined) return WASI_ERRNO.BADF;
|
|
948
|
+
// The TARGET is the link's contents, not a path to walk: it is read verbatim (bar a trailing
|
|
949
|
+
// NUL from a fixed-size guest buffer) and stored as given. The store takes absolute targets
|
|
950
|
+
// only and answers EINVAL for anything else.
|
|
951
|
+
const target = readGuestString(oldPtr, oldLen).replace(/\0+$/u, "");
|
|
952
|
+
return client.symlink(target, path).errno;
|
|
953
|
+
},
|
|
954
|
+
|
|
955
|
+
path_link(oldDirfd, _oldFlags, _oldPtr, _oldLen, newDirfd, _newPtr, _newLen) {
|
|
956
|
+
if (entryOf(oldDirfd)?.isDir !== true || entryOf(newDirfd)?.isDir !== true) return WASI_ERRNO.BADF;
|
|
957
|
+
return WASI_ERRNO.NOTSUP; // the store has no hard links
|
|
958
|
+
},
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
// ---- failure containment -------------------------------------------------
|
|
962
|
+
// A JS exception thrown out of a WASI import unwinds through the guest's nounwind frames and
|
|
963
|
+
// surfaces as a bare `RuntimeError: unreachable` with nothing attached. Every call is therefore
|
|
964
|
+
// wrapped: the guest sees EIO and the host sees the stack.
|
|
965
|
+
const guarded: Record<string, unknown> = {};
|
|
966
|
+
for (const name of Object.keys(fs) as (keyof WasiPreview1FsFunctions)[]) {
|
|
967
|
+
const inner = fs[name] as (...args: unknown[]) => number;
|
|
968
|
+
guarded[name] = (...args: unknown[]): number => {
|
|
969
|
+
try {
|
|
970
|
+
return inner(...args);
|
|
971
|
+
} catch (cause) {
|
|
972
|
+
onError(name, cause);
|
|
973
|
+
return WASI_ERRNO.IO;
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
const adapter = guarded as unknown as WasiPreview1Fs;
|
|
979
|
+
|
|
980
|
+
adapter.owns = owns;
|
|
981
|
+
|
|
982
|
+
adapter.openFdCount = (): number => table.size - (table.has(preopenFd) ? 1 : 0);
|
|
983
|
+
|
|
984
|
+
adapter.closeAll = (): number => {
|
|
985
|
+
let released = 0;
|
|
986
|
+
for (const entry of table.values()) {
|
|
987
|
+
if (entry.clientFd === undefined) continue;
|
|
988
|
+
client.close(entry.clientFd);
|
|
989
|
+
released += 1;
|
|
990
|
+
}
|
|
991
|
+
table.clear();
|
|
992
|
+
table.set(preopenFd, preopenEntry());
|
|
993
|
+
nextFd = fdBase;
|
|
994
|
+
return released;
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
adapter.compose = (base: Readonly<Record<string, unknown>>): Record<string, unknown> => {
|
|
998
|
+
const merged: Record<string, unknown> = { ...base };
|
|
999
|
+
for (const [name, index] of Object.entries(FD_ARGUMENT_INDEX)) {
|
|
1000
|
+
const mine = guarded[name] as (...args: unknown[]) => number;
|
|
1001
|
+
const theirs = base[name];
|
|
1002
|
+
if (typeof theirs !== "function") {
|
|
1003
|
+
// The base host does not implement this call at all (`fd_tell`, `fd_allocate` and friends are
|
|
1004
|
+
// commonly absent): the adapter answers for its own fds and reports EBADF for anything else,
|
|
1005
|
+
// which is exactly what an absent implementation would have to say.
|
|
1006
|
+
merged[name] = (...args: unknown[]): number => (owns(Number(args[index])) ? mine(...args) : WASI_ERRNO.BADF);
|
|
1007
|
+
continue;
|
|
1008
|
+
}
|
|
1009
|
+
const fallback = theirs as (...args: unknown[]) => number;
|
|
1010
|
+
merged[name] = (...args: unknown[]): number => (owns(Number(args[index])) ? mine(...args) : fallback(...args));
|
|
1011
|
+
}
|
|
1012
|
+
return merged;
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
return adapter;
|
|
1016
|
+
}
|