@waffle-os/runtime 0.0.155 → 0.0.156
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/core/filesystem.ts +8 -0
- package/core/wasi.ts +52 -1
- package/dist/core/filesystem.js +1 -0
- package/dist/core/wasi.js +49 -2
- package/dist/fs/backend.js +22 -1
- package/dist/fs/client.js +5 -1
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/memory.js +17 -1
- package/dist/fs/multitab-backend.js +2 -1
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +19 -1
- package/dist/fs/service.js +2 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +21 -1
- package/fs/client.ts +5 -0
- package/fs/cold-read-cache.ts +38 -0
- package/fs/memory.ts +18 -0
- package/fs/multitab-backend.ts +2 -1
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +32 -1
- package/fs/service.ts +1 -0
- package/package.json +1 -1
- package/tooling/program/program-transformer.json +1 -1
package/core/filesystem.ts
CHANGED
|
@@ -11,6 +11,10 @@ export class FilesystemError extends Error {
|
|
|
11
11
|
|
|
12
12
|
/** Opaque filesystem-owned identity. Open handles remain valid across rename and unlink. */
|
|
13
13
|
export type FileHandle = number;
|
|
14
|
+
/** What `FileSystem.probe` looks for: a regular file or a directory. */
|
|
15
|
+
export type ProbeKind = "file" | "directory";
|
|
16
|
+
/** Most pathnames one `probe` call looks up. */
|
|
17
|
+
export const PROBE_PATH_LIMIT = 64;
|
|
14
18
|
export interface FilePermissions {
|
|
15
19
|
mode: number;
|
|
16
20
|
modeSupported: number;
|
|
@@ -111,6 +115,10 @@ export interface FileSystem {
|
|
|
111
115
|
* absolute path from the root; otherwise relative to `directory` ("." for itself). Positive
|
|
112
116
|
* WASI errno on failure. */
|
|
113
117
|
realpath(directory: FileHandle, path: string, rooted?: boolean): string | number;
|
|
118
|
+
/** The index of the first of `paths` that names a `kind` node, each looked up from `directory`
|
|
119
|
+
* with every link followed (a stat with the follow flag), or -1 when none does. A path that
|
|
120
|
+
* fails to resolve does not match. A bad `directory` throws a FilesystemError. */
|
|
121
|
+
probe(directory: FileHandle, paths: readonly string[], kind: ProbeKind, rooted?: boolean): number;
|
|
114
122
|
symlink(path: ResolvedPath, target: string): number;
|
|
115
123
|
readlink(handle: FileHandle): string;
|
|
116
124
|
stat(handle: FileHandle): FileStat;
|
package/core/wasi.ts
CHANGED
|
@@ -41,10 +41,12 @@ import { withFileOffset } from "./open-file.js";
|
|
|
41
41
|
import {
|
|
42
42
|
FilesystemError,
|
|
43
43
|
fileTimeNow,
|
|
44
|
+
PROBE_PATH_LIMIT,
|
|
44
45
|
type FileHandle,
|
|
45
46
|
type FileSystem,
|
|
46
47
|
type FileStat,
|
|
47
48
|
type FileTime,
|
|
49
|
+
type ProbeKind,
|
|
48
50
|
type ResolvedPath,
|
|
49
51
|
} from "./filesystem.js";
|
|
50
52
|
import {
|
|
@@ -161,7 +163,7 @@ export interface WasiOptions {
|
|
|
161
163
|
// replaces globalThis.performance and crypto with the runtime's polyfills, which call back into
|
|
162
164
|
// bun.wasm, which calls clock_time_get / random_get here.
|
|
163
165
|
const encoder = new TextEncoder();
|
|
164
|
-
const decoder = new TextDecoder();
|
|
166
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
165
167
|
const fatalDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
166
168
|
const perfNow = performance.now.bind(performance);
|
|
167
169
|
const timeOrigin = performance.timeOrigin;
|
|
@@ -198,6 +200,30 @@ function guestPathname(bytes: Uint8Array, ptr: number, len: number): string | un
|
|
|
198
200
|
}
|
|
199
201
|
}
|
|
200
202
|
|
|
203
|
+
/** `runtime_fs.path_probe` kinds: 1 a regular file, 2 a directory. */
|
|
204
|
+
const PROBE_KINDS: readonly (ProbeKind | undefined)[] = [undefined, "file", "directory"];
|
|
205
|
+
|
|
206
|
+
/** A path_probe list: one to PROBE_PATH_LIMIT pathnames, each ended by a NUL, exact UTF-8 bytes
|
|
207
|
+
* (a leading U+FEFF kept). Undefined for malformed UTF-8, a missing final NUL, an empty list or
|
|
208
|
+
* too many pathnames. */
|
|
209
|
+
function probePathnames(bytes: Uint8Array, ptr: number, len: number): string[] | undefined {
|
|
210
|
+
if (len === 0 || bytes[ptr + len - 1] !== 0) return undefined;
|
|
211
|
+
const end = ptr + len;
|
|
212
|
+
const paths: string[] = [];
|
|
213
|
+
let start = ptr;
|
|
214
|
+
for (let at = ptr; at < end; at++) {
|
|
215
|
+
if (bytes[at] !== 0) continue;
|
|
216
|
+
if (paths.length === PROBE_PATH_LIMIT) return undefined;
|
|
217
|
+
try {
|
|
218
|
+
paths.push(decodeBytes(bytes, start, at - start, fatalDecoder));
|
|
219
|
+
} catch {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
start = at + 1;
|
|
223
|
+
}
|
|
224
|
+
return paths;
|
|
225
|
+
}
|
|
226
|
+
|
|
201
227
|
function writeUtf8Prefix(target: string, output: Uint8Array): number {
|
|
202
228
|
if (output.length === 0) return 0;
|
|
203
229
|
// Browser encodeInto rejects resizable backing buffers, including fixed-length views.
|
|
@@ -495,6 +521,7 @@ export class Wasi {
|
|
|
495
521
|
const filesystem: Record<string, (...args: never[]) => number> = {
|
|
496
522
|
path_permissions_get: this.pathPermissions.bind(this),
|
|
497
523
|
path_realpath: this.pathRealpath.bind(this),
|
|
524
|
+
path_probe: this.pathProbe.bind(this),
|
|
498
525
|
watch_open: this.watchOpen.bind(this),
|
|
499
526
|
watch_ready: () => (this.watches?.ready() ? 1 : 0),
|
|
500
527
|
watch_read: this.watchRead.bind(this),
|
|
@@ -773,6 +800,30 @@ export class Wasi {
|
|
|
773
800
|
return SUCCESS;
|
|
774
801
|
}
|
|
775
802
|
|
|
803
|
+
/** runtime_fs.path_probe: the index of the first NUL-terminated pathname in `ptr..ptr+len`
|
|
804
|
+
* that names a regular file (`kind` 1) or a directory (`kind` 2), each looked up from `fd` as
|
|
805
|
+
* path_filestat_get with the follow flag does, in one filesystem request. Writes the index, or
|
|
806
|
+
* 0xffffffff when none matches, to the u32 at `found`. See docs/host-abi.md. */
|
|
807
|
+
private pathProbe(fd: number, ptr: number, len: number, kind: number, found: number): number {
|
|
808
|
+
ptr >>>= 0;
|
|
809
|
+
len >>>= 0;
|
|
810
|
+
found >>>= 0;
|
|
811
|
+
const bytes = this.bytes();
|
|
812
|
+
if (ptr + len > bytes.length || found + 4 > bytes.length) return EFAULT;
|
|
813
|
+
const paths = probePathnames(bytes, ptr, len);
|
|
814
|
+
if (paths === undefined) return EINVAL;
|
|
815
|
+
const wanted = PROBE_KINDS[kind >>> 0];
|
|
816
|
+
if (wanted === undefined) return EINVAL;
|
|
817
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
818
|
+
if (!entry) return EBADF;
|
|
819
|
+
if (entry.resource.kind !== "directory") return ENOTDIR;
|
|
820
|
+
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET)) return ENOTCAPABLE;
|
|
821
|
+
const index = this.opts.fs.probe(entry.resource.handle, paths, wanted, this.rooted);
|
|
822
|
+
// Reacquire after the service round trip; another thread may have grown shared memory.
|
|
823
|
+
this.view().setUint32(found, index < 0 ? 0xffffffff : index, true);
|
|
824
|
+
return SUCCESS;
|
|
825
|
+
}
|
|
826
|
+
|
|
776
827
|
private pathPermissions(
|
|
777
828
|
fd: number,
|
|
778
829
|
ptr: number,
|
package/dist/core/filesystem.js
CHANGED
package/dist/core/wasi.js
CHANGED
|
@@ -37,7 +37,8 @@ import { controlLoad, controlWait } from "../proc/control.js";
|
|
|
37
37
|
import { withFileOffset } from "./open-file.js";
|
|
38
38
|
import {
|
|
39
39
|
FilesystemError,
|
|
40
|
-
fileTimeNow
|
|
40
|
+
fileTimeNow,
|
|
41
|
+
PROBE_PATH_LIMIT
|
|
41
42
|
} from "./filesystem.js";
|
|
42
43
|
import {
|
|
43
44
|
DescriptorTable,
|
|
@@ -102,7 +103,7 @@ export class WasiExit extends ExecutionUnwind {
|
|
|
102
103
|
}
|
|
103
104
|
}
|
|
104
105
|
const encoder = new TextEncoder;
|
|
105
|
-
const decoder = new TextDecoder;
|
|
106
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
106
107
|
const fatalDecoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
107
108
|
const perfNow = performance.now.bind(performance);
|
|
108
109
|
const timeOrigin = performance.timeOrigin;
|
|
@@ -126,6 +127,27 @@ function guestPathname(bytes, ptr, len) {
|
|
|
126
127
|
return;
|
|
127
128
|
}
|
|
128
129
|
}
|
|
130
|
+
const PROBE_KINDS = [undefined, "file", "directory"];
|
|
131
|
+
function probePathnames(bytes, ptr, len) {
|
|
132
|
+
if (len === 0 || bytes[ptr + len - 1] !== 0)
|
|
133
|
+
return;
|
|
134
|
+
const end = ptr + len;
|
|
135
|
+
const paths = [];
|
|
136
|
+
let start = ptr;
|
|
137
|
+
for (let at = ptr;at < end; at++) {
|
|
138
|
+
if (bytes[at] !== 0)
|
|
139
|
+
continue;
|
|
140
|
+
if (paths.length === PROBE_PATH_LIMIT)
|
|
141
|
+
return;
|
|
142
|
+
try {
|
|
143
|
+
paths.push(decodeBytes(bytes, start, at - start, fatalDecoder));
|
|
144
|
+
} catch {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
start = at + 1;
|
|
148
|
+
}
|
|
149
|
+
return paths;
|
|
150
|
+
}
|
|
129
151
|
function writeUtf8Prefix(target, output) {
|
|
130
152
|
if (output.length === 0)
|
|
131
153
|
return 0;
|
|
@@ -347,6 +369,7 @@ export class Wasi {
|
|
|
347
369
|
const filesystem = {
|
|
348
370
|
path_permissions_get: this.pathPermissions.bind(this),
|
|
349
371
|
path_realpath: this.pathRealpath.bind(this),
|
|
372
|
+
path_probe: this.pathProbe.bind(this),
|
|
350
373
|
watch_open: this.watchOpen.bind(this),
|
|
351
374
|
watch_ready: () => this.watches?.ready() ? 1 : 0,
|
|
352
375
|
watch_read: this.watchRead.bind(this),
|
|
@@ -591,6 +614,30 @@ export class Wasi {
|
|
|
591
614
|
this.memory.range(buf, encoded.length).set(encoded);
|
|
592
615
|
return SUCCESS;
|
|
593
616
|
}
|
|
617
|
+
pathProbe(fd, ptr, len, kind, found) {
|
|
618
|
+
ptr >>>= 0;
|
|
619
|
+
len >>>= 0;
|
|
620
|
+
found >>>= 0;
|
|
621
|
+
const bytes = this.bytes();
|
|
622
|
+
if (ptr + len > bytes.length || found + 4 > bytes.length)
|
|
623
|
+
return EFAULT;
|
|
624
|
+
const paths = probePathnames(bytes, ptr, len);
|
|
625
|
+
if (paths === undefined)
|
|
626
|
+
return EINVAL;
|
|
627
|
+
const wanted = PROBE_KINDS[kind >>> 0];
|
|
628
|
+
if (wanted === undefined)
|
|
629
|
+
return EINVAL;
|
|
630
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
631
|
+
if (!entry)
|
|
632
|
+
return EBADF;
|
|
633
|
+
if (entry.resource.kind !== "directory")
|
|
634
|
+
return ENOTDIR;
|
|
635
|
+
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
|
|
636
|
+
return ENOTCAPABLE;
|
|
637
|
+
const index = this.opts.fs.probe(entry.resource.handle, paths, wanted, this.rooted);
|
|
638
|
+
this.view().setUint32(found, index < 0 ? 4294967295 : index, true);
|
|
639
|
+
return SUCCESS;
|
|
640
|
+
}
|
|
594
641
|
pathPermissions(fd, ptr, len, flags, out) {
|
|
595
642
|
if (flags !== 0)
|
|
596
643
|
return ENOTSUP;
|
package/dist/fs/backend.js
CHANGED
|
@@ -6,10 +6,28 @@ import {
|
|
|
6
6
|
mutationValue
|
|
7
7
|
} from "./publication.js";
|
|
8
8
|
import { IdentityAllocator } from "./file-identity.js";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
fileTimeMs,
|
|
11
|
+
fileTimeNow,
|
|
12
|
+
msFileTime,
|
|
13
|
+
PROBE_PATH_LIMIT
|
|
14
|
+
} from "../core/filesystem.js";
|
|
10
15
|
import { MemFS } from "./memory.js";
|
|
11
16
|
import { FilesystemError } from "./protocol.js";
|
|
12
17
|
import { SharedFilesystemState } from "./shared.js";
|
|
18
|
+
function probePaths(value) {
|
|
19
|
+
if (!Array.isArray(value) || value.length > PROBE_PATH_LIMIT)
|
|
20
|
+
throw new FilesystemError(28);
|
|
21
|
+
for (const path of value)
|
|
22
|
+
if (typeof path !== "string")
|
|
23
|
+
throw new FilesystemError(28);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function probeKind(value) {
|
|
27
|
+
if (value !== "file" && value !== "directory")
|
|
28
|
+
throw new FilesystemError(28);
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
13
31
|
function sameTimes(left, right) {
|
|
14
32
|
return left !== undefined && left.atime === right.atime && left.mtime === right.mtime && left.ctime === right.ctime;
|
|
15
33
|
}
|
|
@@ -273,6 +291,9 @@ export class MemoryBackend {
|
|
|
273
291
|
case "realpath":
|
|
274
292
|
this.check(a, "directory");
|
|
275
293
|
return this.fs.realpath(a, b, c === true);
|
|
294
|
+
case "probe":
|
|
295
|
+
this.check(a, "directory");
|
|
296
|
+
return this.fs.probe(a, probePaths(b), probeKind(c), args[3] === true);
|
|
276
297
|
case "readlink":
|
|
277
298
|
this.check(a);
|
|
278
299
|
return this.fs.readlink(a);
|
package/dist/fs/client.js
CHANGED
|
@@ -153,7 +153,8 @@ const PLAIN_REPLIES = new SetConstructor([
|
|
|
153
153
|
"setTimes",
|
|
154
154
|
"setMode",
|
|
155
155
|
"read",
|
|
156
|
-
"readlink"
|
|
156
|
+
"readlink",
|
|
157
|
+
"probe"
|
|
157
158
|
]);
|
|
158
159
|
|
|
159
160
|
export class FilesystemCompletion {
|
|
@@ -2105,6 +2106,9 @@ export class FilesystemClient {
|
|
|
2105
2106
|
return errno(error);
|
|
2106
2107
|
}
|
|
2107
2108
|
}
|
|
2109
|
+
probe(directory, paths, kind, rooted = false) {
|
|
2110
|
+
return this.call("probe", [directory, paths, kind, rooted]);
|
|
2111
|
+
}
|
|
2108
2112
|
readlink(handle) {
|
|
2109
2113
|
this.retain(handle);
|
|
2110
2114
|
try {
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import { PAGE } from "./shared-capacity.js";
|
|
2
2
|
const COLD_READ_CACHE_BYTES = 32 * 1024 * 1024;
|
|
3
3
|
export const COLD_PAGE_OVERHEAD = 256;
|
|
4
|
+
const READAHEAD_PAGES = 16;
|
|
5
|
+
export const READAHEAD_BYTES = READAHEAD_PAGES * PAGE;
|
|
6
|
+
const STREAMS = 64;
|
|
4
7
|
|
|
5
8
|
export class ColdReadCache {
|
|
6
9
|
limit;
|
|
7
10
|
pages = new Map;
|
|
11
|
+
ends = new Map;
|
|
8
12
|
bytes = 0;
|
|
9
13
|
constructor(limit = COLD_READ_CACHE_BYTES) {
|
|
10
14
|
this.limit = limit;
|
|
11
15
|
}
|
|
16
|
+
readWindow(key, offset, count, size) {
|
|
17
|
+
if (this.ends.get(key) !== offset)
|
|
18
|
+
return { offset, length: count };
|
|
19
|
+
const windowPages = Math.min(READAHEAD_PAGES, Math.floor(this.limit / 2 / (PAGE + COLD_PAGE_OVERHEAD)));
|
|
20
|
+
if (windowPages < 1)
|
|
21
|
+
return { offset, length: count };
|
|
22
|
+
const start = Math.floor(offset / PAGE) * PAGE;
|
|
23
|
+
const end = Math.min(size, start + windowPages * PAGE);
|
|
24
|
+
if (end < offset + count)
|
|
25
|
+
return { offset, length: count };
|
|
26
|
+
return { offset: start, length: end - start };
|
|
27
|
+
}
|
|
28
|
+
noteRead(key, end) {
|
|
29
|
+
this.ends.delete(key);
|
|
30
|
+
if (this.ends.size >= STREAMS)
|
|
31
|
+
this.ends.delete(this.ends.keys().next().value);
|
|
32
|
+
this.ends.set(key, end);
|
|
33
|
+
}
|
|
12
34
|
read(key, offset, length) {
|
|
13
35
|
const first = Math.floor(offset / PAGE);
|
|
14
36
|
const last = Math.floor((offset + length - 1) / PAGE);
|
|
@@ -46,6 +68,7 @@ export class ColdReadCache {
|
|
|
46
68
|
}
|
|
47
69
|
clear() {
|
|
48
70
|
this.pages.clear();
|
|
71
|
+
this.ends.clear();
|
|
49
72
|
this.bytes = 0;
|
|
50
73
|
}
|
|
51
74
|
remove(name) {
|
package/dist/fs/memory.js
CHANGED
|
@@ -3,7 +3,8 @@ import { utf8ByteLength } from "./utf8-size.js";
|
|
|
3
3
|
import {
|
|
4
4
|
permissionsFor,
|
|
5
5
|
FilesystemError,
|
|
6
|
-
fileTimeNow
|
|
6
|
+
fileTimeNow,
|
|
7
|
+
PROBE_PATH_LIMIT
|
|
7
8
|
} from "../core/filesystem.js";
|
|
8
9
|
import {
|
|
9
10
|
dirname,
|
|
@@ -476,6 +477,21 @@ export class MemFS {
|
|
|
476
477
|
return this.pathOf(resolved.node);
|
|
477
478
|
return joinBelow(this.pathOf(resolved.parent), "/" + resolved.name);
|
|
478
479
|
}
|
|
480
|
+
probe(directory, paths, kind, rooted = false) {
|
|
481
|
+
if (paths.length > PROBE_PATH_LIMIT)
|
|
482
|
+
throw new FilesystemError(28);
|
|
483
|
+
const start = this.directory(directory);
|
|
484
|
+
const wanted = kind === "directory" ? "dir" : "file";
|
|
485
|
+
for (let index = 0;index < paths.length; index++) {
|
|
486
|
+
const path = paths[index];
|
|
487
|
+
if (path.startsWith("/"))
|
|
488
|
+
continue;
|
|
489
|
+
const resolved = this.walkNodes(start, path, false, true, undefined, rooted);
|
|
490
|
+
if (typeof resolved !== "number" && resolved.node?.kind === wanted)
|
|
491
|
+
return index;
|
|
492
|
+
}
|
|
493
|
+
return -1;
|
|
494
|
+
}
|
|
479
495
|
pathOf(dir) {
|
|
480
496
|
const names = [];
|
|
481
497
|
for (let at = dir;at !== this.root; ) {
|
|
@@ -5342,7 +5342,8 @@ export class MultiTabBackend {
|
|
|
5342
5342
|
case "readdir":
|
|
5343
5343
|
case "readdirPage":
|
|
5344
5344
|
case "resolve":
|
|
5345
|
-
case "realpath":
|
|
5345
|
+
case "realpath":
|
|
5346
|
+
case "probe": {
|
|
5346
5347
|
const remote = mapHandle(args[0]);
|
|
5347
5348
|
if (remote === undefined)
|
|
5348
5349
|
return;
|
package/dist/fs/persistent.js
CHANGED
|
@@ -322,17 +322,35 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
322
322
|
const cached = this.coldReads.read(key, offset, count);
|
|
323
323
|
if (cached) {
|
|
324
324
|
this.storage.checkPayload(key, size);
|
|
325
|
+
this.coldReads.noteRead(key, offset + count);
|
|
325
326
|
return this.cachedDescriptorRange(cached, cancelled);
|
|
326
327
|
}
|
|
328
|
+
const window = this.coldReads.readWindow(key, offset, count, size);
|
|
327
329
|
const backing = this.coldBacking(node, key, undefined, cancelled);
|
|
330
|
+
this.coldReads.noteRead(key, offset + count);
|
|
328
331
|
const drained = Promise.withResolvers();
|
|
329
332
|
this.ownWork(drained.promise);
|
|
330
|
-
|
|
333
|
+
const reply = this.readDescriptorWindow(backing, offset, count, window, cancelled);
|
|
334
|
+
return reply.finally(() => {
|
|
331
335
|
const release = Promise.resolve().then(() => backing.retention.release());
|
|
332
336
|
release.then(drained.resolve, drained.reject);
|
|
333
337
|
return release;
|
|
334
338
|
});
|
|
335
339
|
}
|
|
340
|
+
async readDescriptorWindow(backing, offset, count, window, cancelled) {
|
|
341
|
+
if (window.offset === offset && window.length === count)
|
|
342
|
+
return this.readDescriptorRange(backing, offset, count, cancelled);
|
|
343
|
+
try {
|
|
344
|
+
const bytes = await this.readDescriptorRange(backing, window.offset, window.length, cancelled);
|
|
345
|
+
return bytes.slice(offset - window.offset, offset - window.offset + count);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (cancelled() || this.stopped)
|
|
348
|
+
throw new FilesystemError(11);
|
|
349
|
+
if (error instanceof FilesystemError && error.errno === 11)
|
|
350
|
+
throw error;
|
|
351
|
+
return this.readDescriptorRange(backing, offset, count, cancelled);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
336
354
|
async readDescriptorRange(backing, offset, length, cancelled) {
|
|
337
355
|
if (cancelled() || this.stopped)
|
|
338
356
|
throw new FilesystemError(11);
|
package/dist/fs/service.js
CHANGED
|
@@ -776,7 +776,8 @@ export class FilesystemService {
|
|
|
776
776
|
"readdir",
|
|
777
777
|
"readdirPage",
|
|
778
778
|
"resolve",
|
|
779
|
-
"realpath"
|
|
779
|
+
"realpath",
|
|
780
|
+
"probe"
|
|
780
781
|
].includes(operation))
|
|
781
782
|
this.check(session, a, held);
|
|
782
783
|
if (["create", "symlink", "link", "removePath", "rename"].includes(operation)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commit":"
|
|
1
|
+
{"commit":"be2e58bebc094d5185f24f45ed2b7be27d985f30","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
|
package/fs/backend.ts
CHANGED
|
@@ -16,7 +16,14 @@ import {
|
|
|
16
16
|
import { type FileIdentity, type IdentityReservation, IdentityAllocator } from "./file-identity.js";
|
|
17
17
|
import type { PublicationBuffers } from "./publication-internal.js";
|
|
18
18
|
import { type FileHandle, type FileStat, type ResolvedPath } from "../core/index.js";
|
|
19
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
fileTimeMs,
|
|
21
|
+
fileTimeNow,
|
|
22
|
+
msFileTime,
|
|
23
|
+
PROBE_PATH_LIMIT,
|
|
24
|
+
type FileTime,
|
|
25
|
+
type ProbeKind,
|
|
26
|
+
} from "../core/filesystem.js";
|
|
20
27
|
import { MemFS, type Node, type NamespacePlan } from "./memory.js";
|
|
21
28
|
import { FilesystemError, type FilesystemIdentity } from "./protocol.js";
|
|
22
29
|
import { SharedFilesystemState, type SharedSegment } from "./shared.js";
|
|
@@ -95,6 +102,16 @@ export interface FilesystemBackend {
|
|
|
95
102
|
/** Release startup resources when cancellation races backend creation. */
|
|
96
103
|
close?(): Promise<void>;
|
|
97
104
|
}
|
|
105
|
+
/** A `probe` request's pathnames: an array of at most PROBE_PATH_LIMIT strings, or EINVAL. */
|
|
106
|
+
function probePaths(value: unknown): readonly string[] {
|
|
107
|
+
if (!Array.isArray(value) || value.length > PROBE_PATH_LIMIT) throw new FilesystemError(28);
|
|
108
|
+
for (const path of value) if (typeof path !== "string") throw new FilesystemError(28);
|
|
109
|
+
return value as string[];
|
|
110
|
+
}
|
|
111
|
+
function probeKind(value: unknown): ProbeKind {
|
|
112
|
+
if (value !== "file" && value !== "directory") throw new FilesystemError(28);
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
98
115
|
type DirectoryTimes = Pick<FileStat, "atime" | "mtime" | "ctime">;
|
|
99
116
|
function sameTimes(left: DirectoryTimes | undefined, right: DirectoryTimes): boolean {
|
|
100
117
|
return (
|
|
@@ -465,6 +482,9 @@ export class MemoryBackend implements FilesystemBackend {
|
|
|
465
482
|
case "realpath":
|
|
466
483
|
this.check(a, "directory");
|
|
467
484
|
return this.fs.realpath(a, b, c === true);
|
|
485
|
+
case "probe":
|
|
486
|
+
this.check(a, "directory");
|
|
487
|
+
return this.fs.probe(a, probePaths(b), probeKind(c), args[3] === true);
|
|
468
488
|
case "readlink":
|
|
469
489
|
this.check(a);
|
|
470
490
|
return this.fs.readlink(a);
|
package/fs/client.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
isFileTime,
|
|
42
42
|
permissionsFor,
|
|
43
43
|
type FileTime,
|
|
44
|
+
type ProbeKind,
|
|
44
45
|
type WatchFileSystem,
|
|
45
46
|
} from "../core/filesystem.js";
|
|
46
47
|
import {
|
|
@@ -210,6 +211,7 @@ const PLAIN_REPLIES = new SetConstructor([
|
|
|
210
211
|
"setMode",
|
|
211
212
|
"read",
|
|
212
213
|
"readlink",
|
|
214
|
+
"probe",
|
|
213
215
|
]);
|
|
214
216
|
export class FilesystemCompletion {
|
|
215
217
|
static {
|
|
@@ -2612,6 +2614,9 @@ export class FilesystemClient implements FileSystem {
|
|
|
2612
2614
|
return errno(error);
|
|
2613
2615
|
}
|
|
2614
2616
|
}
|
|
2617
|
+
probe(directory: FileHandle, paths: readonly string[], kind: ProbeKind, rooted = false): number {
|
|
2618
|
+
return this.call("probe", [directory, paths, kind, rooted]);
|
|
2619
|
+
}
|
|
2615
2620
|
readlink(handle: FileHandle): string {
|
|
2616
2621
|
this.retain(handle);
|
|
2617
2622
|
try {
|
package/fs/cold-read-cache.ts
CHANGED
|
@@ -5,6 +5,12 @@ const COLD_READ_CACHE_BYTES = 32 * 1024 * 1024;
|
|
|
5
5
|
/** Charged per page on top of its bytes: an estimate of its map entry, key string and typed
|
|
6
6
|
* array and buffer headers. It keeps many tiny files from exceeding the bound in real heap. */
|
|
7
7
|
export const COLD_PAGE_OVERHEAD = 256;
|
|
8
|
+
/** Pages one storage read fetches for a sequential cold scan: 1 MiB, so a 13 MB binary costs
|
|
9
|
+
* 14 storage reads instead of 210, while its transport still moves one page per request. */
|
|
10
|
+
const READAHEAD_PAGES = 16;
|
|
11
|
+
export const READAHEAD_BYTES = READAHEAD_PAGES * PAGE;
|
|
12
|
+
/** Payloads whose last read end is remembered for sequential detection. */
|
|
13
|
+
const STREAMS = 64;
|
|
8
14
|
|
|
9
15
|
/** Verified bytes of cold payloads, kept by whole aligned page so a file read again while it
|
|
10
16
|
* stays unmodified skips storage and its checksums. A payload key names one immutable stored
|
|
@@ -13,8 +19,39 @@ export const COLD_PAGE_OVERHEAD = 256;
|
|
|
13
19
|
* cache owns private copies, so a caller may transfer or modify what it receives. */
|
|
14
20
|
export class ColdReadCache {
|
|
15
21
|
private readonly pages = new Map<string, Uint8Array>();
|
|
22
|
+
/** Where the last read of each recent payload ended. */
|
|
23
|
+
private readonly ends = new Map<string, number>();
|
|
16
24
|
private bytes = 0;
|
|
17
25
|
constructor(private readonly limit = COLD_READ_CACHE_BYTES) {}
|
|
26
|
+
/** Aligned storage range for a miss at `[offset, offset + count)`. A read that starts exactly
|
|
27
|
+
* where the previous read of this payload ended is a sequential scan, so it fetches a
|
|
28
|
+
* page-aligned readahead window (as Linux page-cache readahead does) up to `READAHEAD_BYTES`
|
|
29
|
+
* and half the cache budget; any other read fetches only what it needs. Aligning the start to a
|
|
30
|
+
* page boundary lets small sequential reads populate their current page too instead of
|
|
31
|
+
* repeatedly missing and refetching the window. */
|
|
32
|
+
readWindow(
|
|
33
|
+
key: string,
|
|
34
|
+
offset: number,
|
|
35
|
+
count: number,
|
|
36
|
+
size: number,
|
|
37
|
+
): { offset: number; length: number } {
|
|
38
|
+
if (this.ends.get(key) !== offset) return { offset, length: count };
|
|
39
|
+
const windowPages = Math.min(
|
|
40
|
+
READAHEAD_PAGES,
|
|
41
|
+
Math.floor(this.limit / 2 / (PAGE + COLD_PAGE_OVERHEAD)),
|
|
42
|
+
);
|
|
43
|
+
if (windowPages < 1) return { offset, length: count };
|
|
44
|
+
const start = Math.floor(offset / PAGE) * PAGE;
|
|
45
|
+
const end = Math.min(size, start + windowPages * PAGE);
|
|
46
|
+
if (end < offset + count) return { offset, length: count };
|
|
47
|
+
return { offset: start, length: end - start };
|
|
48
|
+
}
|
|
49
|
+
/** Record where a read of this payload ended, hit or miss. */
|
|
50
|
+
noteRead(key: string, end: number): void {
|
|
51
|
+
this.ends.delete(key);
|
|
52
|
+
if (this.ends.size >= STREAMS) this.ends.delete(this.ends.keys().next().value!);
|
|
53
|
+
this.ends.set(key, end);
|
|
54
|
+
}
|
|
18
55
|
/** A copy of the range, or undefined unless every page it touches is cached. */
|
|
19
56
|
read(key: string, offset: number, length: number): Uint8Array | undefined {
|
|
20
57
|
const first = Math.floor(offset / PAGE);
|
|
@@ -54,6 +91,7 @@ export class ColdReadCache {
|
|
|
54
91
|
}
|
|
55
92
|
clear(): void {
|
|
56
93
|
this.pages.clear();
|
|
94
|
+
this.ends.clear();
|
|
57
95
|
this.bytes = 0;
|
|
58
96
|
}
|
|
59
97
|
private remove(name: string): void {
|
package/fs/memory.ts
CHANGED
|
@@ -6,7 +6,9 @@ import {
|
|
|
6
6
|
permissionsFor,
|
|
7
7
|
FilesystemError,
|
|
8
8
|
fileTimeNow,
|
|
9
|
+
PROBE_PATH_LIMIT,
|
|
9
10
|
type FileTime,
|
|
11
|
+
type ProbeKind,
|
|
10
12
|
type SocketBinding,
|
|
11
13
|
} from "../core/filesystem.js";
|
|
12
14
|
import {
|
|
@@ -618,6 +620,22 @@ export class MemFS implements FileSystem {
|
|
|
618
620
|
if (resolved.node.kind === "dir") return this.pathOf(resolved.node);
|
|
619
621
|
return joinBelow(this.pathOf(resolved.parent!), "/" + resolved.name);
|
|
620
622
|
}
|
|
623
|
+
/** The index of the first of `paths` naming a `kind` node, each walked from `directory` with
|
|
624
|
+
* every link followed, as a stat with the follow flag would; -1 when none does. A path that
|
|
625
|
+
* fails to resolve (missing, ENOTDIR, ELOOP, EACCES, an escape or an absolute path) does not
|
|
626
|
+
* match. Only the start directory can fail the call. */
|
|
627
|
+
probe(directory: FileHandle, paths: readonly string[], kind: ProbeKind, rooted = false): number {
|
|
628
|
+
if (paths.length > PROBE_PATH_LIMIT) throw new FilesystemError(28);
|
|
629
|
+
const start = this.directory(directory);
|
|
630
|
+
const wanted = kind === "directory" ? "dir" : "file";
|
|
631
|
+
for (let index = 0; index < paths.length; index++) {
|
|
632
|
+
const path = paths[index]!;
|
|
633
|
+
if (path.startsWith("/")) continue;
|
|
634
|
+
const resolved = this.walkNodes(start, path, false, true, undefined, rooted);
|
|
635
|
+
if (typeof resolved !== "number" && resolved.node?.kind === wanted) return index;
|
|
636
|
+
}
|
|
637
|
+
return -1;
|
|
638
|
+
}
|
|
621
639
|
/** The absolute path of a live directory, by its parent edges up to the root. */
|
|
622
640
|
private pathOf(dir: DirNode): string | number {
|
|
623
641
|
const names: string[] = [];
|
package/fs/multitab-backend.ts
CHANGED
|
@@ -7325,7 +7325,8 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
7325
7325
|
case "readdir":
|
|
7326
7326
|
case "readdirPage":
|
|
7327
7327
|
case "resolve":
|
|
7328
|
-
case "realpath":
|
|
7328
|
+
case "realpath":
|
|
7329
|
+
case "probe": {
|
|
7329
7330
|
const remote = mapHandle(args[0]);
|
|
7330
7331
|
if (remote === undefined) return undefined;
|
|
7331
7332
|
return [remote, ...args.slice(1)];
|
package/fs/multitab-protocol.ts
CHANGED
package/fs/persistent.ts
CHANGED
|
@@ -412,17 +412,48 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
412
412
|
const cached = this.coldReads.read(key, offset, count);
|
|
413
413
|
if (cached) {
|
|
414
414
|
this.storage.checkPayload(key, size);
|
|
415
|
+
this.coldReads.noteRead(key, offset + count);
|
|
415
416
|
return this.cachedDescriptorRange(cached, cancelled);
|
|
416
417
|
}
|
|
418
|
+
// A sequential scan fetches a page-aligned readahead window in one storage read; its pages
|
|
419
|
+
// are verified with it and serve the next requests from the cache.
|
|
420
|
+
const window = this.coldReads.readWindow(key, offset, count, size);
|
|
417
421
|
const backing = this.coldBacking(node, key, undefined, cancelled);
|
|
422
|
+
this.coldReads.noteRead(key, offset + count);
|
|
418
423
|
const drained = Promise.withResolvers<void>();
|
|
419
424
|
this.ownWork(drained.promise);
|
|
420
|
-
|
|
425
|
+
const reply = this.readDescriptorWindow(backing, offset, count, window, cancelled);
|
|
426
|
+
return reply.finally(() => {
|
|
421
427
|
const release = Promise.resolve().then(() => backing.retention.release());
|
|
422
428
|
void release.then(drained.resolve, drained.reject);
|
|
423
429
|
return release;
|
|
424
430
|
});
|
|
425
431
|
}
|
|
432
|
+
private async readDescriptorWindow(
|
|
433
|
+
backing: Extract<ReadBacking, { kind: "cold" }>,
|
|
434
|
+
offset: number,
|
|
435
|
+
count: number,
|
|
436
|
+
window: { offset: number; length: number },
|
|
437
|
+
cancelled: () => boolean,
|
|
438
|
+
): Promise<Uint8Array> {
|
|
439
|
+
if (window.offset === offset && window.length === count)
|
|
440
|
+
return this.readDescriptorRange(backing, offset, count, cancelled);
|
|
441
|
+
try {
|
|
442
|
+
const bytes = await this.readDescriptorRange(
|
|
443
|
+
backing,
|
|
444
|
+
window.offset,
|
|
445
|
+
window.length,
|
|
446
|
+
cancelled,
|
|
447
|
+
);
|
|
448
|
+
return bytes.slice(offset - window.offset, offset - window.offset + count);
|
|
449
|
+
} catch (error) {
|
|
450
|
+
if (cancelled() || this.stopped) throw new FilesystemError(11);
|
|
451
|
+
if (error instanceof FilesystemError && error.errno === 11) throw error;
|
|
452
|
+
// An error in speculative readahead bytes must not fail a valid read; retry only the
|
|
453
|
+
// caller's requested range, which still enforces ownership and cancellation.
|
|
454
|
+
return this.readDescriptorRange(backing, offset, count, cancelled);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
426
457
|
private async readDescriptorRange(
|
|
427
458
|
backing: Extract<ReadBacking, { kind: "cold" }>,
|
|
428
459
|
offset: number,
|
package/fs/service.ts
CHANGED
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commit":"
|
|
1
|
+
{"commit":"be2e58bebc094d5185f24f45ed2b7be27d985f30","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
|