Skip to content

Commit d65394b

Browse files
author
=
committed
fix(p2-shim/fs): correct read/write semantics
1 parent bb56a3e commit d65394b

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

packages/preview2-shim/lib/nodejs/filesystem.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class Descriptor {
8181

8282
static _createPreopen(hostPreopen) {
8383
const descriptor = new Descriptor();
84+
descriptor.#mode = {
85+
read: true,
86+
write: true,
87+
mutateDirectory: true,
88+
};
8489
descriptor.#hostPreopen = hostPreopen.endsWith("/")
8590
? hostPreopen.slice(0, -1) || "/"
8691
: hostPreopen;
@@ -353,6 +358,25 @@ class Descriptor {
353358
if (preopenEntries.length === 0) {
354359
throw "access";
355360
}
361+
// https://github.com/WebAssembly/WASI/blob/b7ee9febdcc0652aef5aeaf80dc329d240eb84e8/proposals/filesystem/wit/types.wit#L532-L534
362+
// > If `flags` contains `descriptor-flags::mutate-directory`, and the base
363+
// > descriptor doesn't have `descriptor-flags::mutate-directory` set,
364+
// > `open-at` fails with `error-code::read-only`.
365+
if (descriptorFlags.mutateDirectory && !this.#mode?.mutateDirectory) {
366+
throw "read-only";
367+
}
368+
// https://github.com/WebAssembly/WASI/blob/b7ee9febdcc0652aef5aeaf80dc329d240eb84e8/proposals/filesystem/wit/types.wit#L536-L539
369+
// > If `flags` contains `write` or `mutate-directory`, or `open-flags`
370+
// > contains `truncate` or `create`, and the base descriptor doesn't have
371+
// > `descriptor-flags::mutate-directory` set, `open-at` fails with
372+
// > `error-code::read-only`.
373+
if(
374+
!this.#mode?.mutateDirectory
375+
&&
376+
(descriptorFlags.write || descriptorFlags.mutateDirectory || openFlags.truncate || openFlags.create)
377+
) {
378+
throw "read-only";
379+
}
356380
const fullPath = this.#getFullPath(path, pathFlags.symlinkFollow);
357381
let fsOpenFlags = 0x0;
358382
if (openFlags.create) {
@@ -383,7 +407,7 @@ class Descriptor {
383407
if (!pathFlags.symlinkFollow) {
384408
fsOpenFlags |= constants.O_NOFOLLOW;
385409
}
386-
if (descriptorFlags.requestedWriteSync || descriptorFlags.mutateDirectory) {
410+
if (descriptorFlags.requestedWriteSync) {
387411
throw "unsupported";
388412
}
389413
// Currently throw to match Wasmtime

0 commit comments

Comments
 (0)