@waffle-os/runtime 0.0.156 → 0.0.157
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/browser/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
package/core/devices.ts
CHANGED
|
@@ -1,7 +1,90 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { allocateSharedControl } from "../memory/allocator.ts";
|
|
2
|
+
import {
|
|
3
|
+
atomicsCompareExchange,
|
|
4
|
+
atomicsLoad,
|
|
5
|
+
atomicsStore,
|
|
6
|
+
BigInt64ArrayConstructor,
|
|
7
|
+
reflectApply,
|
|
8
|
+
} from "../proc/intrinsics/core.ts";
|
|
9
|
+
import type { Stream, VirtualFileState } from "./descriptors.js";
|
|
10
|
+
|
|
11
|
+
const BigIntConstructor = BigInt;
|
|
12
|
+
const Uint8ArrayConstructor = Uint8Array;
|
|
13
|
+
const WeakMapConstructor = WeakMap;
|
|
14
|
+
const weakMapGet = WeakMap.prototype.get;
|
|
15
|
+
const weakMapSet = WeakMap.prototype.set;
|
|
16
|
+
const stringCharCodeAt = String.prototype.charCodeAt;
|
|
17
|
+
const copyBytes = Uint8Array.prototype.set;
|
|
18
|
+
const sliceBytes = Uint8Array.prototype.subarray;
|
|
19
|
+
const apply = reflectApply;
|
|
20
|
+
|
|
21
|
+
const MAX_VIRTUAL_OFFSET = 0x7fff_ffff_ffff_ffffn;
|
|
22
|
+
|
|
23
|
+
interface SharedVirtualCursor {
|
|
24
|
+
buffer?: SharedArrayBuffer;
|
|
25
|
+
cell?: BigInt64Array<SharedArrayBuffer>;
|
|
26
|
+
local: bigint;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const virtualCursors = new WeakMapConstructor<VirtualFileState, SharedVirtualCursor>();
|
|
30
|
+
|
|
31
|
+
/** Synthetic inode for virtual `/etc/machine-id` (distinct from physical VFS inodes). */
|
|
32
|
+
export const MACHINE_ID_INO = 0x7fff_fffe;
|
|
33
|
+
/** Byte length of `/etc/machine-id` contents: 32 lowercase hex characters plus trailing newline. */
|
|
34
|
+
export const MACHINE_ID_FILE_SIZE = 33;
|
|
2
35
|
|
|
3
36
|
function discard(chunks: readonly Uint8Array[]): number {
|
|
4
|
-
|
|
37
|
+
let total = 0;
|
|
38
|
+
for (let i = 0; i < chunks.length; i++) total += chunks[i]!.length;
|
|
39
|
+
return total;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Validate a systemd machine-id(5) string: exactly 32 lowercase ASCII hex digits. */
|
|
43
|
+
export function isValidMachineId(value: string): boolean {
|
|
44
|
+
if (value.length !== 32) return false;
|
|
45
|
+
for (let i = 0; i < 32; i++) {
|
|
46
|
+
const code = apply(stringCharCodeAt, value, [i]);
|
|
47
|
+
const isDigit = code >= 48 && code <= 57;
|
|
48
|
+
const isLowerHex = code >= 97 && code <= 102;
|
|
49
|
+
if (!isDigit && !isLowerHex) return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Assert a valid 32-char lowercase hex machine-id(5) string. */
|
|
55
|
+
export function validateMachineId(value: unknown): string {
|
|
56
|
+
if (typeof value !== "string" || !isValidMachineId(value))
|
|
57
|
+
throw new TypeError("machineId must be 32 lowercase hexadecimal characters");
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Encode a validated 32-char hex machine-id as the 33-byte `/etc/machine-id` payload. */
|
|
62
|
+
function encodeMachineIdBytes(machineId: string): Uint8Array {
|
|
63
|
+
const bytes = new Uint8ArrayConstructor(MACHINE_ID_FILE_SIZE);
|
|
64
|
+
for (let i = 0; i < 32; i++) bytes[i] = apply(stringCharCodeAt, machineId, [i]);
|
|
65
|
+
bytes[32] = 10;
|
|
66
|
+
return bytes;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Copy from a read-only virtual byte buffer starting at `offset` into guest iovecs. */
|
|
70
|
+
export function readVirtualFileBytes(
|
|
71
|
+
data: Uint8Array,
|
|
72
|
+
chunks: readonly Uint8Array[],
|
|
73
|
+
offset: number,
|
|
74
|
+
): number {
|
|
75
|
+
let pos = offset;
|
|
76
|
+
let total = 0;
|
|
77
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
78
|
+
if (pos >= data.length) break;
|
|
79
|
+
const chunk = chunks[i]!;
|
|
80
|
+
const count = Math.min(chunk.length, data.length - pos);
|
|
81
|
+
if (count > 0) {
|
|
82
|
+
apply(copyBytes, chunk, [apply(sliceBytes, data, [pos, pos + count])]);
|
|
83
|
+
pos += count;
|
|
84
|
+
total += count;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return total;
|
|
5
88
|
}
|
|
6
89
|
|
|
7
90
|
/** Process-local character devices do not create nodes or retain filesystem payloads. */
|
|
@@ -18,3 +101,155 @@ export function createNullDevice(fdflags = 0): Stream {
|
|
|
18
101
|
close() {},
|
|
19
102
|
};
|
|
20
103
|
}
|
|
104
|
+
|
|
105
|
+
function readCursorBigInt(cursor: SharedVirtualCursor): bigint {
|
|
106
|
+
if (!cursor.cell) return cursor.local;
|
|
107
|
+
const raw = atomicsLoad(cursor.cell, 0);
|
|
108
|
+
return raw < 0n ? 0n : raw;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function writeCursorBigInt(cursor: SharedVirtualCursor, next: bigint): void {
|
|
112
|
+
let clamped = next;
|
|
113
|
+
if (clamped < 0n) clamped = 0n;
|
|
114
|
+
if (clamped > MAX_VIRTUAL_OFFSET) clamped = MAX_VIRTUAL_OFFSET;
|
|
115
|
+
if (cursor.cell) atomicsStore(cursor.cell, 0, clamped);
|
|
116
|
+
else cursor.local = clamped;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function claimCursorSpan(
|
|
120
|
+
cursor: SharedVirtualCursor,
|
|
121
|
+
dataLength: number,
|
|
122
|
+
requested: number,
|
|
123
|
+
): number {
|
|
124
|
+
const dataLenBig = BigIntConstructor(dataLength);
|
|
125
|
+
if (!cursor.cell) {
|
|
126
|
+
const start = cursor.local;
|
|
127
|
+
if (start >= dataLenBig) return dataLength;
|
|
128
|
+
const startNum = Number(start);
|
|
129
|
+
const count = Math.min(requested, dataLength - startNum);
|
|
130
|
+
cursor.local = start + BigIntConstructor(count);
|
|
131
|
+
return startNum;
|
|
132
|
+
}
|
|
133
|
+
for (;;) {
|
|
134
|
+
const raw = atomicsLoad(cursor.cell, 0);
|
|
135
|
+
const start = raw < 0n ? 0n : raw;
|
|
136
|
+
if (start >= dataLenBig) return dataLength;
|
|
137
|
+
const startNum = Number(start);
|
|
138
|
+
const count = Math.min(requested, dataLength - startNum);
|
|
139
|
+
if (count === 0) return startNum;
|
|
140
|
+
const next = start + BigIntConstructor(count);
|
|
141
|
+
if (atomicsCompareExchange(cursor.cell, 0, raw, next) === raw) return startNum;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function cursorForState(state: VirtualFileState): SharedVirtualCursor {
|
|
146
|
+
const cursor = apply(weakMapGet, virtualCursors, [state]) as SharedVirtualCursor | undefined;
|
|
147
|
+
if (!cursor) throw new TypeError("missing virtual file cursor");
|
|
148
|
+
return cursor;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Atomically read the current 64-bit byte offset of a virtual regular file. */
|
|
152
|
+
export function tellVirtualFile(state: VirtualFileState): bigint {
|
|
153
|
+
return readCursorBigInt(cursorForState(state));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Atomically seek a virtual regular file; returns the new 64-bit offset or negative WASI errno. */
|
|
157
|
+
export function seekVirtualFile(
|
|
158
|
+
state: VirtualFileState,
|
|
159
|
+
delta: bigint,
|
|
160
|
+
whence: number,
|
|
161
|
+
): bigint | number {
|
|
162
|
+
const cursor = cursorForState(state);
|
|
163
|
+
if (whence === 0 || whence === 2) {
|
|
164
|
+
const base = whence === 0 ? 0n : BigIntConstructor(state.data.length);
|
|
165
|
+
const next = base + delta;
|
|
166
|
+
if (next < 0n || next > MAX_VIRTUAL_OFFSET) return -28;
|
|
167
|
+
if (cursor.cell) atomicsStore(cursor.cell, 0, next);
|
|
168
|
+
else cursor.local = next;
|
|
169
|
+
return next;
|
|
170
|
+
}
|
|
171
|
+
if (whence !== 1) return -28;
|
|
172
|
+
if (!cursor.cell) {
|
|
173
|
+
const next = cursor.local + delta;
|
|
174
|
+
if (next < 0n || next > MAX_VIRTUAL_OFFSET) return -28;
|
|
175
|
+
cursor.local = next;
|
|
176
|
+
return next;
|
|
177
|
+
}
|
|
178
|
+
for (;;) {
|
|
179
|
+
const raw = atomicsLoad(cursor.cell, 0);
|
|
180
|
+
const current = raw < 0n ? 0n : raw;
|
|
181
|
+
const next = current + delta;
|
|
182
|
+
if (next < 0n || next > MAX_VIRTUAL_OFFSET) return -28;
|
|
183
|
+
if (delta === 0n) return current;
|
|
184
|
+
if (atomicsCompareExchange(cursor.cell, 0, raw, next) === raw) return next;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Promote a virtual `/etc/machine-id` file cursor to shared memory for cross-process transfer. */
|
|
189
|
+
export function shareMachineIdOffset(state: VirtualFileState): SharedArrayBuffer {
|
|
190
|
+
const cursor = cursorForState(state);
|
|
191
|
+
if (cursor.buffer) return cursor.buffer;
|
|
192
|
+
const buffer = allocateSharedControl(8);
|
|
193
|
+
const cell = new BigInt64ArrayConstructor(buffer);
|
|
194
|
+
atomicsStore(cell, 0, cursor.local);
|
|
195
|
+
cursor.buffer = buffer;
|
|
196
|
+
cursor.cell = cell;
|
|
197
|
+
return buffer;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Read-only virtual regular file serving `/etc/machine-id` when no physical file exists. */
|
|
201
|
+
export function createMachineIdFile(
|
|
202
|
+
machineId: string,
|
|
203
|
+
fdflags = 0,
|
|
204
|
+
offsetOrBuffer: number | SharedArrayBuffer = 0,
|
|
205
|
+
): Stream {
|
|
206
|
+
const data = encodeMachineIdBytes(machineId);
|
|
207
|
+
const cursor: SharedVirtualCursor =
|
|
208
|
+
typeof offsetOrBuffer === "number"
|
|
209
|
+
? { local: BigIntConstructor(Math.max(0, Math.trunc(offsetOrBuffer))) }
|
|
210
|
+
: {
|
|
211
|
+
buffer: offsetOrBuffer,
|
|
212
|
+
cell: new BigInt64ArrayConstructor(offsetOrBuffer),
|
|
213
|
+
local: 0n,
|
|
214
|
+
};
|
|
215
|
+
const state: VirtualFileState = {
|
|
216
|
+
ino: MACHINE_ID_INO,
|
|
217
|
+
machineId,
|
|
218
|
+
data,
|
|
219
|
+
get offset(): number {
|
|
220
|
+
return Number(readCursorBigInt(cursor));
|
|
221
|
+
},
|
|
222
|
+
set offset(next: number) {
|
|
223
|
+
if (!Number.isFinite(next)) return;
|
|
224
|
+
writeCursorBigInt(cursor, BigIntConstructor(Math.max(0, Math.trunc(next))));
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
apply(weakMapSet, virtualCursors, [state, cursor]);
|
|
228
|
+
const read = (chunks: readonly Uint8Array[]): number => {
|
|
229
|
+
let requested = 0;
|
|
230
|
+
for (let i = 0; i < chunks.length; i++) requested += chunks[i]!.length;
|
|
231
|
+
const start = claimCursorSpan(cursor, data.length, requested);
|
|
232
|
+
return readVirtualFileBytes(data, chunks, start);
|
|
233
|
+
};
|
|
234
|
+
return {
|
|
235
|
+
kind: "device",
|
|
236
|
+
device: "machine-id",
|
|
237
|
+
virtualFile: state,
|
|
238
|
+
fdflags,
|
|
239
|
+
read,
|
|
240
|
+
tryRead: read,
|
|
241
|
+
write: () => -2,
|
|
242
|
+
tryWrite: () => -2,
|
|
243
|
+
readiness: () => {
|
|
244
|
+
const current = readCursorBigInt(cursor);
|
|
245
|
+
const dataLenBig = BigIntConstructor(data.length);
|
|
246
|
+
return {
|
|
247
|
+
readable: true,
|
|
248
|
+
writable: false,
|
|
249
|
+
hangup: false,
|
|
250
|
+
nbytes: current >= dataLenBig ? 0 : data.length - Number(current),
|
|
251
|
+
};
|
|
252
|
+
},
|
|
253
|
+
close() {},
|
|
254
|
+
};
|
|
255
|
+
}
|
package/core/fd-reopen.ts
CHANGED
|
@@ -5,10 +5,32 @@ import {
|
|
|
5
5
|
RIGHT_PATH_FILESTAT_SET_SIZE as PATH_RESIZE,
|
|
6
6
|
} from "./rights.js";
|
|
7
7
|
import type { Descriptor, DescriptorTable } from "./descriptors.js";
|
|
8
|
+
import { createMachineIdFile } from "./devices.js";
|
|
8
9
|
import type { FileStat, FileSystem, ResolvedPath } from "./filesystem.js";
|
|
10
|
+
import { releaseOriginOfd } from "./open-file.js";
|
|
9
11
|
import { normalize } from "./path.js";
|
|
10
12
|
|
|
11
|
-
export type DescriptorPath =
|
|
13
|
+
export type DescriptorPath =
|
|
14
|
+
| { source: number }
|
|
15
|
+
| { device: "null" }
|
|
16
|
+
| { virtualFile: "machine-id" }
|
|
17
|
+
| { resolved: ResolvedPath };
|
|
18
|
+
|
|
19
|
+
function resolveNonFileRights(
|
|
20
|
+
source: Descriptor,
|
|
21
|
+
base: bigint,
|
|
22
|
+
inheriting: bigint,
|
|
23
|
+
intersectCapabilities: boolean,
|
|
24
|
+
accessMask: bigint,
|
|
25
|
+
): { readonly base: bigint; readonly inheriting: bigint } | number {
|
|
26
|
+
if (intersectCapabilities) {
|
|
27
|
+
if ((base & accessMask & ~source.rightsBase) !== 0n) return -2;
|
|
28
|
+
return { base: base & source.rightsBase, inheriting: inheriting & source.rightsInheriting };
|
|
29
|
+
}
|
|
30
|
+
if ((base & source.rightsBase) !== base) return -2;
|
|
31
|
+
if ((inheriting & source.rightsInheriting) !== inheriting) return -2;
|
|
32
|
+
return { base, inheriting };
|
|
33
|
+
}
|
|
12
34
|
|
|
13
35
|
/** Open the inode again, not its old pathname. Negative results are WASI errno. */
|
|
14
36
|
export function reopenDescriptor(
|
|
@@ -19,18 +41,40 @@ export function reopenDescriptor(
|
|
|
19
41
|
fdflags: number,
|
|
20
42
|
base: bigint,
|
|
21
43
|
inheriting = 0n,
|
|
44
|
+
intersectCapabilities = false,
|
|
22
45
|
): number {
|
|
23
46
|
const source = table.get(fd);
|
|
24
47
|
if (!source) return -8;
|
|
25
48
|
if (oflags & ~15 || fdflags & ~31 || oflags & 5) return -28;
|
|
26
49
|
base = BigInt.asUintN(64, base);
|
|
27
50
|
inheriting = BigInt.asUintN(64, inheriting);
|
|
51
|
+
if (source.resource.kind === "device" && source.resource.virtualFile) {
|
|
52
|
+
if (oflags & 2) return -54;
|
|
53
|
+
if ((oflags & 8) !== 0 || (base & WRITE) !== 0n) return -2;
|
|
54
|
+
const rights = resolveNonFileRights(
|
|
55
|
+
source,
|
|
56
|
+
base,
|
|
57
|
+
inheriting,
|
|
58
|
+
intersectCapabilities,
|
|
59
|
+
READ | WRITE,
|
|
60
|
+
);
|
|
61
|
+
if (typeof rights === "number") return rights;
|
|
62
|
+
return table.open(
|
|
63
|
+
createMachineIdFile(source.resource.virtualFile.machineId, fdflags),
|
|
64
|
+
rights.base,
|
|
65
|
+
rights.inheriting,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
28
68
|
if (source.resource.kind !== "file") {
|
|
29
69
|
if (oflags & 2 && source.resource.kind !== "directory") return -54;
|
|
30
70
|
if (oflags & 8 && source.resource.kind === "directory") return -31;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
71
|
+
const access = source.resource.kind === "directory" ? WRITE : READ | WRITE;
|
|
72
|
+
const rights = resolveNonFileRights(source, base, inheriting, intersectCapabilities, access);
|
|
73
|
+
if (typeof rights === "number") return rights;
|
|
74
|
+
return table.add(source.description, {
|
|
75
|
+
rightsBase: rights.base,
|
|
76
|
+
rightsInheriting: rights.inheriting,
|
|
77
|
+
});
|
|
34
78
|
}
|
|
35
79
|
if (oflags & 2) return -54;
|
|
36
80
|
const root = table.rootPreopen();
|
|
@@ -64,14 +108,11 @@ export function openInode(
|
|
|
64
108
|
if (error) return -error;
|
|
65
109
|
}
|
|
66
110
|
fs.retain(handle);
|
|
67
|
-
|
|
111
|
+
const resource =
|
|
68
112
|
kind === "directory"
|
|
69
|
-
? { kind, handle }
|
|
70
|
-
: { kind, handle, offset: 0, append: (fdflags & 1) !== 0, fdflags }
|
|
71
|
-
|
|
72
|
-
inheriting,
|
|
73
|
-
() => fs.release(handle),
|
|
74
|
-
);
|
|
113
|
+
? ({ kind, handle } as const)
|
|
114
|
+
: { kind, handle, offset: 0, append: (fdflags & 1) !== 0, fdflags };
|
|
115
|
+
return table.open(resource, base, inheriting, () => releaseOriginOfd(fs, resource));
|
|
75
116
|
}
|
|
76
117
|
|
|
77
118
|
/** These names are process-local WASI paths, never entries in the shared filesystem. */
|
|
@@ -92,10 +133,11 @@ function devicePath(path: string): "null" | undefined {
|
|
|
92
133
|
* climbing from a directory descriptor are physical only. `resolved` is the lookup that found it. */
|
|
93
134
|
interface PathEntry {
|
|
94
135
|
prefix: string | undefined;
|
|
95
|
-
kind: FileStat["kind"] | "device" | "missing";
|
|
136
|
+
kind: FileStat["kind"] | "device" | "virtual-file" | "missing";
|
|
96
137
|
handle?: number;
|
|
97
138
|
source?: number;
|
|
98
139
|
resolved?: ResolvedPath;
|
|
140
|
+
syntheticEtc?: true;
|
|
99
141
|
}
|
|
100
142
|
const virtualDirectories = ["/dev", "/dev/fd", "/proc", "/proc/self", "/proc/self/fd"];
|
|
101
143
|
|
|
@@ -118,6 +160,7 @@ function lookupEntry(
|
|
|
118
160
|
fs: FileSystem,
|
|
119
161
|
parent: PathEntry,
|
|
120
162
|
name: string,
|
|
163
|
+
hasMachineId: boolean,
|
|
121
164
|
): PathEntry | number {
|
|
122
165
|
const prefix =
|
|
123
166
|
parent.prefix === undefined || parent.source !== undefined
|
|
@@ -129,6 +172,8 @@ function lookupEntry(
|
|
|
129
172
|
if (typeof path === "number") return path;
|
|
130
173
|
if (path?.node !== undefined)
|
|
131
174
|
return { prefix, handle: path.node, kind: fs.stat(path.node).kind, resolved: path };
|
|
175
|
+
if (hasMachineId && prefix === "/etc/machine-id") return { prefix, kind: "virtual-file" };
|
|
176
|
+
if (hasMachineId && prefix === "/etc") return { prefix, kind: "directory", syntheticEtc: true };
|
|
132
177
|
if (prefix !== undefined && virtualDirectories.includes(prefix))
|
|
133
178
|
return { prefix, kind: "directory" };
|
|
134
179
|
return path ? { prefix, kind: "missing", resolved: path } : 44;
|
|
@@ -162,6 +207,21 @@ function virtualShadow(fs: FileSystem, root: number, path: ResolvedPath): boolea
|
|
|
162
207
|
return false;
|
|
163
208
|
}
|
|
164
209
|
|
|
210
|
+
function virtualMachineIdMatch(
|
|
211
|
+
fs: FileSystem,
|
|
212
|
+
root: number,
|
|
213
|
+
resolved: ResolvedPath,
|
|
214
|
+
hasMachineId: boolean,
|
|
215
|
+
): { virtualFile: "machine-id" } | number | undefined {
|
|
216
|
+
if (!hasMachineId || resolved.node !== undefined || resolved.name !== "machine-id")
|
|
217
|
+
return undefined;
|
|
218
|
+
if (resolved.parent === undefined) return undefined;
|
|
219
|
+
const etc = fs.resolve(root, "etc", true, true);
|
|
220
|
+
if (typeof etc === "number" || etc.node === undefined) return undefined;
|
|
221
|
+
if (!sameInode(fs, etc.node, resolved.parent)) return undefined;
|
|
222
|
+
return resolved.trailingSlash ? 54 : { virtualFile: "machine-id" };
|
|
223
|
+
}
|
|
224
|
+
|
|
165
225
|
type FinalLinkPolicy = "follow" | "nofollow" | "exclusive-create";
|
|
166
226
|
|
|
167
227
|
function resolveOrdinaryPath(
|
|
@@ -171,7 +231,8 @@ function resolveOrdinaryPath(
|
|
|
171
231
|
path: string,
|
|
172
232
|
finalLink: FinalLinkPolicy,
|
|
173
233
|
rooted: boolean,
|
|
174
|
-
|
|
234
|
+
hasMachineId: boolean,
|
|
235
|
+
): DescriptorPath | number | undefined {
|
|
175
236
|
if (path.includes("dev") || path.includes("proc")) {
|
|
176
237
|
const normalized = normalize(path);
|
|
177
238
|
if (descriptorPath(normalized) !== undefined || devicePath(normalized) !== undefined)
|
|
@@ -179,7 +240,10 @@ function resolveOrdinaryPath(
|
|
|
179
240
|
}
|
|
180
241
|
const resolved = fs.resolve(handle, path, finalLink === "follow", rooted);
|
|
181
242
|
if (typeof resolved === "number") return resolved === 44 ? undefined : resolved;
|
|
182
|
-
|
|
243
|
+
if (virtualShadow(fs, root(), resolved)) return undefined;
|
|
244
|
+
const virtualFile = virtualMachineIdMatch(fs, root(), resolved, hasMachineId);
|
|
245
|
+
if (virtualFile !== undefined) return virtualFile;
|
|
246
|
+
return { resolved };
|
|
183
247
|
}
|
|
184
248
|
|
|
185
249
|
/** The target of a link the alias walker meets with `rest` still to walk, or the errno that stops it. */
|
|
@@ -217,6 +281,7 @@ class AliasWalk {
|
|
|
217
281
|
path: string,
|
|
218
282
|
private readonly finalLink: FinalLinkPolicy,
|
|
219
283
|
private readonly rooted: boolean,
|
|
284
|
+
private readonly hasMachineId: boolean,
|
|
220
285
|
) {
|
|
221
286
|
this.current = start;
|
|
222
287
|
this.pending = path.split("/");
|
|
@@ -233,10 +298,11 @@ class AliasWalk {
|
|
|
233
298
|
if (!part) return undefined;
|
|
234
299
|
const error = searchEntry(this.fs, this.current);
|
|
235
300
|
if (error) return error;
|
|
301
|
+
if (this.current.syntheticEtc && part !== "." && part !== "machine-id") return 44;
|
|
236
302
|
this.dot = part === "." || part === "..";
|
|
237
303
|
if (part === ".") return undefined;
|
|
238
304
|
if (part === "..") return this.up();
|
|
239
|
-
const next = lookupEntry(this.table, this.fs, this.current, part);
|
|
305
|
+
const next = lookupEntry(this.table, this.fs, this.current, part, this.hasMachineId);
|
|
240
306
|
if (typeof next === "number") return next;
|
|
241
307
|
if (next.kind === "missing") return this.missing(next);
|
|
242
308
|
if (next.kind === "symlink" && next.handle !== undefined) return this.expand(next.handle);
|
|
@@ -306,6 +372,7 @@ class AliasWalk {
|
|
|
306
372
|
if (trailing && current.kind !== "directory") return 54;
|
|
307
373
|
if (current.source !== undefined) return { source: current.source };
|
|
308
374
|
if (current.kind === "device") return { device: "null" };
|
|
375
|
+
if (current.kind === "virtual-file") return { virtualFile: "machine-id" };
|
|
309
376
|
// A virtual directory with no physical one behind it: ordinary resolution answers.
|
|
310
377
|
if (current.handle === undefined) return undefined;
|
|
311
378
|
if (!this.dot && current.resolved)
|
|
@@ -336,21 +403,39 @@ export function resolveDescriptorPath(
|
|
|
336
403
|
path: string,
|
|
337
404
|
finalLink: FinalLinkPolicy,
|
|
338
405
|
rooted = false,
|
|
406
|
+
hasMachineId = fs.machineId !== undefined,
|
|
339
407
|
): DescriptorPath | number | undefined {
|
|
340
408
|
if (directory.resource.kind !== "directory") return undefined;
|
|
341
409
|
const handle = directory.resource.handle;
|
|
342
|
-
if (
|
|
343
|
-
|
|
410
|
+
if (
|
|
411
|
+
!rooted &&
|
|
412
|
+
directory.preopen !== "/" &&
|
|
413
|
+
fs.stat(handle).ino !== fs.stat(fs.preopen("/")).ino
|
|
414
|
+
) {
|
|
415
|
+
if (!hasMachineId || !path.includes("machine-id")) return undefined;
|
|
416
|
+
const resolved = fs.resolve(handle, path, finalLink === "follow", false);
|
|
417
|
+
if (typeof resolved === "number") return undefined;
|
|
418
|
+
return virtualMachineIdMatch(fs, fs.preopen("/"), resolved, true);
|
|
419
|
+
}
|
|
344
420
|
let rootCache: number | undefined;
|
|
345
421
|
const root = () => (rootCache ??= rooted ? rootHandle(table, fs, directory) : handle);
|
|
346
|
-
const ordinary = resolveOrdinaryPath(fs, handle, root, path, finalLink, rooted);
|
|
422
|
+
const ordinary = resolveOrdinaryPath(fs, handle, root, path, finalLink, rooted, hasMachineId);
|
|
347
423
|
if (ordinary !== undefined || !path) return ordinary;
|
|
348
424
|
const rootEntry = { prefix: "", kind: "directory", handle: root() } as const;
|
|
349
425
|
if (!sameInode(fs, handle, rootEntry.handle)) {
|
|
350
426
|
// Preview1 paths are relative to their directory; only the root binding spells `/dev/...`.
|
|
351
427
|
if (path.startsWith("/")) return 63;
|
|
352
428
|
const start: PathEntry = { prefix: undefined, kind: "directory", handle };
|
|
353
|
-
return new AliasWalk(table, fs, rootEntry, start, path, finalLink, rooted).run();
|
|
429
|
+
return new AliasWalk(table, fs, rootEntry, start, path, finalLink, rooted, hasMachineId).run();
|
|
354
430
|
}
|
|
355
|
-
return new AliasWalk(
|
|
431
|
+
return new AliasWalk(
|
|
432
|
+
table,
|
|
433
|
+
fs,
|
|
434
|
+
rootEntry,
|
|
435
|
+
rootEntry,
|
|
436
|
+
path,
|
|
437
|
+
finalLink,
|
|
438
|
+
rooted,
|
|
439
|
+
hasMachineId,
|
|
440
|
+
).run();
|
|
356
441
|
}
|
package/core/filesystem.ts
CHANGED
|
@@ -160,6 +160,77 @@ export interface FileSystem {
|
|
|
160
160
|
rename(from: ResolvedPath, to: ResolvedPath): number;
|
|
161
161
|
/** Synchronous committed-watch subscriptions for guest imports; absent means ENOTSUP. */
|
|
162
162
|
readonly watches?: WatchFileSystem;
|
|
163
|
+
/** Advisory whole-file (flock) and byte-range (fcntl) locks; absent means ENOTSUP. */
|
|
164
|
+
readonly locks?: FileLockSystem;
|
|
165
|
+
/** Stable 32-char lowercase hex machine identifier served via virtual `/etc/machine-id`. */
|
|
166
|
+
readonly machineId?: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Advisory whole-file lock operations matching sys/file.h (flock(2)). */
|
|
170
|
+
export const LOCK_SH = 1;
|
|
171
|
+
export const LOCK_EX = 2;
|
|
172
|
+
export const LOCK_NB = 4;
|
|
173
|
+
export const LOCK_UN = 8;
|
|
174
|
+
|
|
175
|
+
/** Advisory byte-range lock commands (fcntl(2) POSIX and OFD locks). */
|
|
176
|
+
export const F_GETLK = 0;
|
|
177
|
+
export const F_SETLK = 1;
|
|
178
|
+
export const F_SETLKW = 2;
|
|
179
|
+
export const F_OFD_GETLK = 3;
|
|
180
|
+
export const F_OFD_SETLK = 4;
|
|
181
|
+
export const F_OFD_SETLKW = 5;
|
|
182
|
+
|
|
183
|
+
/** Advisory byte-range lock types (fcntl(2)). */
|
|
184
|
+
export const F_RDLCK = 0;
|
|
185
|
+
export const F_WRLCK = 1;
|
|
186
|
+
export const F_UNLCK = 2;
|
|
187
|
+
|
|
188
|
+
export interface FileLockRange {
|
|
189
|
+
readonly type: 0 | 1 | 2;
|
|
190
|
+
readonly start: number;
|
|
191
|
+
/** Length in bytes; 0 means to EOF (infinity). */
|
|
192
|
+
readonly len: number;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export interface FileLockConflict {
|
|
196
|
+
readonly type: 0 | 1 | 2;
|
|
197
|
+
readonly pid: number;
|
|
198
|
+
readonly start: number;
|
|
199
|
+
readonly len: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface LockWaitControl {
|
|
203
|
+
/** Checks for fatal signals / process termination before sleeping; throws ExecutionUnwind if fatal. */
|
|
204
|
+
checkActive?(): void;
|
|
205
|
+
/** Returns true when a signal is pending so a blocking lock wait returns EINTR. */
|
|
206
|
+
interruptPending?(): boolean;
|
|
207
|
+
/** Optional custom wait hook for deterministic tests or embedders. */
|
|
208
|
+
wait?(word: Int32Array, expected: number, milliseconds: number): void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Advisory file-lock service attached to a FileSystem. */
|
|
212
|
+
export interface FileLockSystem {
|
|
213
|
+
/** Session owner token used to namespace local OFD lock IDs across workers (0 for MemFS). */
|
|
214
|
+
readonly owner?: number;
|
|
215
|
+
flock(handle: FileHandle, ofdId: string, operation: number, control?: LockWaitControl): number;
|
|
216
|
+
setRangeLock(
|
|
217
|
+
handle: FileHandle,
|
|
218
|
+
ofdId: string | undefined,
|
|
219
|
+
pid: number,
|
|
220
|
+
range: FileLockRange,
|
|
221
|
+
blocking: boolean,
|
|
222
|
+
control?: LockWaitControl,
|
|
223
|
+
): number;
|
|
224
|
+
getRangeLock(
|
|
225
|
+
handle: FileHandle,
|
|
226
|
+
ofdId: string | undefined,
|
|
227
|
+
range: FileLockRange,
|
|
228
|
+
pid?: number,
|
|
229
|
+
): FileLockConflict | number;
|
|
230
|
+
releaseOfd(handle: FileHandle, ofdId: string, inFlight?: boolean): void;
|
|
231
|
+
releasePosix(handle: FileHandle, pid?: number): void;
|
|
232
|
+
handoffPosix?(successorOwner: number): void;
|
|
233
|
+
hasActivePosixLocks?(): boolean;
|
|
163
234
|
}
|
|
164
235
|
|
|
165
236
|
/** One ready subscription returned by a batched owner drain. */
|