@openclaw/matrix 2026.11.10-beta.3 → 2026.12.1-beta.1
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/node_modules/@openclaw/fs-safe/CHANGELOG.md +57 -0
- package/node_modules/@openclaw/fs-safe/README.md +6 -0
- package/node_modules/@openclaw/fs-safe/dist/archive-parser.wasm +0 -0
- package/node_modules/@openclaw/fs-safe/dist/atomic-io.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/atomic-io.js +10 -0
- package/node_modules/@openclaw/fs-safe/dist/bounded-read.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/bounded-read.js +15 -4
- package/node_modules/@openclaw/fs-safe/dist/directory-guard.js +11 -2
- package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/drvfs-parent.js +38 -0
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-acquisition.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-acquire.js +13 -8
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-io.js +13 -0
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.d.ts +2 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-root-mutation.js +34 -24
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync-stale-admission.js +7 -2
- package/node_modules/@openclaw/fs-safe/dist/file-lock-sync.js +20 -20
- package/node_modules/@openclaw/fs-safe/dist/file-observation.d.ts +13 -2
- package/node_modules/@openclaw/fs-safe/dist/file-observation.js +8 -4
- package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store-sync-write.js +11 -1
- package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store-write-options.js +17 -0
- package/node_modules/@openclaw/fs-safe/dist/file-store.d.ts +2 -1
- package/node_modules/@openclaw/fs-safe/dist/file-store.js +12 -16
- package/node_modules/@openclaw/fs-safe/dist/guest-native-python.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/guest.js +3 -4
- package/node_modules/@openclaw/fs-safe/dist/json-document-store.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/json-document-store.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/json-store.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/json.d.ts +5 -2
- package/node_modules/@openclaw/fs-safe/dist/json.js +6 -2
- package/node_modules/@openclaw/fs-safe/dist/local-file-descriptor.js +16 -10
- package/node_modules/@openclaw/fs-safe/dist/native-binding.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/native-pinned-write.js +4 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.d.ts +20 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file-metadata.js +18 -0
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file.d.ts +3 -18
- package/node_modules/@openclaw/fs-safe/dist/native-staged-file.js +48 -24
- package/node_modules/@openclaw/fs-safe/dist/opened-file-failure.js +19 -7
- package/node_modules/@openclaw/fs-safe/dist/output.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/output.js +3 -0
- package/node_modules/@openclaw/fs-safe/dist/path-policy.js +4 -1
- package/node_modules/@openclaw/fs-safe/dist/permissions-windows.js +35 -10
- package/node_modules/@openclaw/fs-safe/dist/pinned-open.d.ts +9 -2
- package/node_modules/@openclaw/fs-safe/dist/pinned-open.js +42 -23
- package/node_modules/@openclaw/fs-safe/dist/pinned-write-staged.js +85 -34
- package/node_modules/@openclaw/fs-safe/dist/pinned-write-types.d.ts +3 -0
- package/node_modules/@openclaw/fs-safe/dist/pinned-write.js +4 -0
- package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/prepare-directory-mode.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/private-temp-workspace.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/publication-recovery.d.ts +24 -0
- package/node_modules/@openclaw/fs-safe/dist/publication-recovery.js +151 -0
- package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/publish-copy-stage.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/realpath.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/realpath.js +22 -11
- package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-descriptor.js +5 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.d.ts +7 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file-temp-owner.js +75 -2
- package/node_modules/@openclaw/fs-safe/dist/replace-file-types.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/replace-file.js +14 -10
- package/node_modules/@openclaw/fs-safe/dist/root-context.js +28 -12
- package/node_modules/@openclaw/fs-safe/dist/root-copy.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/root-create-input.js +2 -2
- package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.d.ts +4 -0
- package/node_modules/@openclaw/fs-safe/dist/root-file-final-admission.js +53 -36
- package/node_modules/@openclaw/fs-safe/dist/root-file-open-batch-sync.js +3 -1
- package/node_modules/@openclaw/fs-safe/dist/root-file.d.ts +2 -1
- package/node_modules/@openclaw/fs-safe/dist/root-file.js +24 -13
- package/node_modules/@openclaw/fs-safe/dist/root-impl.js +17 -1
- package/node_modules/@openclaw/fs-safe/dist/root-options.d.ts +4 -2
- package/node_modules/@openclaw/fs-safe/dist/root-path.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/root-path.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/root-read-admission.js +31 -9
- package/node_modules/@openclaw/fs-safe/dist/root-write-admission.js +6 -2
- package/node_modules/@openclaw/fs-safe/dist/secret-file.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/secret-file.js +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-staged-file.js +84 -4
- package/node_modules/@openclaw/fs-safe/dist/sibling-temp.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/sibling-temp.js +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-acquire.js +22 -15
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.d.ts +22 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-publication.js +303 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.d.ts +2 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-reclaim.js +9 -1
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.d.ts +8 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-root.js +37 -3
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.d.ts +14 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-staging.js +155 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-stale-admission.js +7 -2
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.d.ts +10 -0
- package/node_modules/@openclaw/fs-safe/dist/sidecar-lock-write.js +143 -0
- package/node_modules/@openclaw/fs-safe/dist/temp-target.js +28 -7
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.d.ts +2 -2
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-admission.js +34 -18
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-child-admission.js +13 -3
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-ownership.js +1 -1
- package/node_modules/@openclaw/fs-safe/dist/temp-workspace-types.d.ts +9 -0
- package/node_modules/@openclaw/fs-safe/dist/test-hooks.d.ts +6 -0
- package/node_modules/@openclaw/fs-safe/dist/text-atomic.d.ts +1 -1
- package/node_modules/@openclaw/fs-safe/dist/text-atomic.js +1 -0
- package/node_modules/@openclaw/fs-safe/dist/windows-owner.d.ts +1 -0
- package/node_modules/@openclaw/fs-safe/dist/windows-owner.js +2 -1
- package/node_modules/@openclaw/fs-safe/dist/windows-security-helper.exe +0 -0
- package/node_modules/@openclaw/fs-safe/docs/advanced.md +6 -1
- package/node_modules/@openclaw/fs-safe/docs/atomic.md +64 -0
- package/node_modules/@openclaw/fs-safe/docs/contributing.md +69 -0
- package/node_modules/@openclaw/fs-safe/docs/file-store.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/json-store.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/json.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/output.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/permissions.md +20 -7
- package/node_modules/@openclaw/fs-safe/docs/reading.md +24 -9
- package/node_modules/@openclaw/fs-safe/docs/root.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/security-model.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/sidecar-lock.md +12 -7
- package/node_modules/@openclaw/fs-safe/docs/staged-file.md +2 -0
- package/node_modules/@openclaw/fs-safe/docs/temp-isolation-reference.md +73 -0
- package/node_modules/@openclaw/fs-safe/docs/temp.md +65 -0
- package/node_modules/@openclaw/fs-safe/docs/testing.md +1 -0
- package/node_modules/@openclaw/fs-safe/docs/writing.md +2 -0
- package/node_modules/@openclaw/fs-safe/package.json +25 -20
- package/node_modules/@openclaw/fs-safe-darwin-arm64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-darwin-arm64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-darwin-x64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-darwin-x64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-freebsd-arm64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-freebsd-arm64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-freebsd-x64/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-freebsd-x64/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-arm64-gnu/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-arm64-musl/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-arm64-musl/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-x64-gnu/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-x64-gnu/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-linux-x64-musl/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-linux-x64-musl/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-win32-arm64-msvc/package.json +1 -1
- package/node_modules/@openclaw/fs-safe-win32-x64-msvc/fs-safe-native.node +0 -0
- package/node_modules/@openclaw/fs-safe-win32-x64-msvc/package.json +1 -1
- package/package.json +5 -5
|
@@ -9,6 +9,8 @@ import { sameFileIdentityForCleanup, sha256Hex } from "./file-identity.js";
|
|
|
9
9
|
import { withAsyncDirectoryGuards } from "./guarded-mutation.js";
|
|
10
10
|
import { inspectAtomicIdentity, wait } from "./atomic-io.js";
|
|
11
11
|
import { registerTempPathForExit } from "./temp-cleanup.js";
|
|
12
|
+
import { admitsDrvfsRenameRecoveryAtFd, assertRecoveredPublication, shouldPrepareDrvfsRecovery } from "./publication-recovery.js";
|
|
13
|
+
import { retainDrvfsParent } from "./drvfs-parent.js";
|
|
12
14
|
const PUBLISHED_READ_FLAGS = resolveReadOpenFlags();
|
|
13
15
|
function describeFailure(error) {
|
|
14
16
|
try {
|
|
@@ -72,6 +74,10 @@ export class AtomicTempOwner {
|
|
|
72
74
|
resource;
|
|
73
75
|
recordedIdentity;
|
|
74
76
|
exists = false;
|
|
77
|
+
recoveryParent;
|
|
78
|
+
recoveryIdentity;
|
|
79
|
+
recoveryContent;
|
|
80
|
+
recovered = false;
|
|
75
81
|
unregister;
|
|
76
82
|
constructor(pathname, io) {
|
|
77
83
|
this.pathname = pathname;
|
|
@@ -90,6 +96,24 @@ export class AtomicTempOwner {
|
|
|
90
96
|
throw new Error("Atomic temp owner has no identity");
|
|
91
97
|
return this.recordedIdentity;
|
|
92
98
|
}
|
|
99
|
+
get canRecoverHiddenRename() {
|
|
100
|
+
return this.recoveryParent !== undefined;
|
|
101
|
+
}
|
|
102
|
+
get fd() {
|
|
103
|
+
if (!this.resource)
|
|
104
|
+
throw new Error("Atomic temp owner has no descriptor");
|
|
105
|
+
return this.resource.fd;
|
|
106
|
+
}
|
|
107
|
+
*prepareRecovery(content, directory = path.dirname(this.pathname), filesystemChecked = false) {
|
|
108
|
+
if (!shouldPrepareDrvfsRecovery(true))
|
|
109
|
+
return;
|
|
110
|
+
this.recoveryParent = retainDrvfsParent(directory, this.identity.dev, filesystemChecked);
|
|
111
|
+
if (!this.recoveryParent)
|
|
112
|
+
return;
|
|
113
|
+
const observed = this.resource.statExact();
|
|
114
|
+
this.recoveryIdentity = (this.io.asynchronous ? yield observed : observed);
|
|
115
|
+
this.recoveryContent = Buffer.from(content);
|
|
116
|
+
}
|
|
93
117
|
markRenamed() {
|
|
94
118
|
this.exists = false;
|
|
95
119
|
this.unregister();
|
|
@@ -110,10 +134,13 @@ export class AtomicTempOwner {
|
|
|
110
134
|
*assertCurrent(pathname = this.pathname) {
|
|
111
135
|
const openedInspection = this.inspectOwned(() => this.resource.statExact(), pathname, false, this.identity);
|
|
112
136
|
const opened = (this.io.asynchronous ? (yield openedInspection) : openedInspection);
|
|
137
|
+
if (this.recovered)
|
|
138
|
+
assertRecoveredPublication(opened, this.recoveryIdentity);
|
|
113
139
|
try {
|
|
114
140
|
const currentInspection = this.inspectOwned(() => this.io.lstatExact(pathname), pathname, true, opened);
|
|
115
|
-
|
|
116
|
-
|
|
141
|
+
const current = (this.io.asynchronous ? yield currentInspection : currentInspection);
|
|
142
|
+
if (this.recovered)
|
|
143
|
+
assertRecoveredPublication(current, this.recoveryIdentity);
|
|
117
144
|
}
|
|
118
145
|
catch (error) {
|
|
119
146
|
if (hasErrorCode(error, "ENOENT")) {
|
|
@@ -129,6 +156,45 @@ export class AtomicTempOwner {
|
|
|
129
156
|
identityCurrent = true;
|
|
130
157
|
}
|
|
131
158
|
catch (error) {
|
|
159
|
+
if (this.recoveryParent && (hasErrorCode(error, "ENOENT") ||
|
|
160
|
+
(error instanceof FsSafeError && hasErrorCode(error.cause, "ENOENT")))) {
|
|
161
|
+
this.recoveryParent.assertCurrent();
|
|
162
|
+
if (!admitsDrvfsRenameRecoveryAtFd(this.recoveryParent.fd))
|
|
163
|
+
throw error;
|
|
164
|
+
const previous = this.takeResource();
|
|
165
|
+
const closing = previous.close();
|
|
166
|
+
if (this.io.asynchronous)
|
|
167
|
+
yield closing;
|
|
168
|
+
for (let attempt = 0;; attempt++) {
|
|
169
|
+
try {
|
|
170
|
+
this.recoveryParent.assertCurrent();
|
|
171
|
+
this.resource = yield* this.io.open(pathname, PUBLISHED_READ_FLAGS);
|
|
172
|
+
yield* this.assertCurrent(pathname);
|
|
173
|
+
const observed = this.resource.statExact();
|
|
174
|
+
const stat = (this.io.asynchronous ? yield observed : observed);
|
|
175
|
+
assertRecoveredPublication(stat, this.recoveryIdentity);
|
|
176
|
+
if (!Buffer.from(yield* this.resource.readFileBounded(this.recoveryContent.byteLength)).equals(this.recoveryContent)) {
|
|
177
|
+
throw new FsSafeError("path-mismatch", "published content changed during DrvFS recovery");
|
|
178
|
+
}
|
|
179
|
+
yield* this.assertCurrent(pathname);
|
|
180
|
+
this.recoveryParent.assertCurrent();
|
|
181
|
+
this.recovered = true;
|
|
182
|
+
onVerified?.(this.identity);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
catch (recoveryError) {
|
|
186
|
+
const failed = this.takeResource();
|
|
187
|
+
if (failed) {
|
|
188
|
+
const settled = failed.close();
|
|
189
|
+
if (this.io.asynchronous)
|
|
190
|
+
yield settled;
|
|
191
|
+
}
|
|
192
|
+
if (!hasErrorCode(recoveryError, "ENOENT") || attempt === 4)
|
|
193
|
+
throw recoveryError;
|
|
194
|
+
yield* this.io.delay(20);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
132
198
|
if (!(error instanceof FsSafeError) || !hasErrorCode(error, "path-mismatch") || !expectedHash) {
|
|
133
199
|
throw error;
|
|
134
200
|
}
|
|
@@ -223,6 +289,13 @@ export class AtomicTempOwner {
|
|
|
223
289
|
catch (closeError) {
|
|
224
290
|
deferredFailure = closeFailure(closeError, params, deferredFailure);
|
|
225
291
|
}
|
|
292
|
+
try {
|
|
293
|
+
this.recoveryParent?.close();
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
deferredFailure ??= { error };
|
|
297
|
+
}
|
|
298
|
+
this.recoveryParent = undefined;
|
|
226
299
|
if (deferredFailure)
|
|
227
300
|
throw deferredFailure.error;
|
|
228
301
|
}
|
|
@@ -26,6 +26,8 @@ export type ReplaceFileAtomicBaseOptions = AtomicMutationOptions & {
|
|
|
26
26
|
destinationHardlinks?: ReplaceFileDestinationHardlinkPolicy;
|
|
27
27
|
/** Strict by default; locked content verification is an explicit FUSE compatibility policy. */
|
|
28
28
|
renameIdentity?: RenameIdentityPolicy;
|
|
29
|
+
/** Opt in to close-all recovery only on a verified DrvFS mount; identity reuse remains possible. */
|
|
30
|
+
allowDrvfsRenameRecovery?: boolean;
|
|
29
31
|
syncTempFile?: boolean;
|
|
30
32
|
syncParentDir?: boolean;
|
|
31
33
|
throwOnCleanupError?: boolean;
|
|
@@ -102,10 +102,11 @@ export async function replaceFileAtomicWithDirectorySync(options, syncParent) {
|
|
|
102
102
|
const filePath = validateReplaceFilePath(options.filePath);
|
|
103
103
|
validateRestoreOptions(options);
|
|
104
104
|
const renameIdentity = options.renameIdentity;
|
|
105
|
+
const allowDrvfsRenameRecovery = options.allowDrvfsRenameRecovery === true;
|
|
105
106
|
validateRenameIdentity(renameIdentity);
|
|
106
107
|
return await serializePathWrite(path.resolve(filePath), async () => {
|
|
107
108
|
if (renameIdentity !== "verify-content-with-lock") {
|
|
108
|
-
return await replaceAsync(options, filePath, renameIdentity, mutation, syncParent);
|
|
109
|
+
return await replaceAsync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery, syncParent);
|
|
109
110
|
}
|
|
110
111
|
const fsModule = options.fileSystem?.promises ?? fs;
|
|
111
112
|
const dir = path.dirname(filePath);
|
|
@@ -119,20 +120,21 @@ export async function replaceFileAtomicWithDirectorySync(options, syncParent) {
|
|
|
119
120
|
recursive: true, mode: dirMode,
|
|
120
121
|
});
|
|
121
122
|
mutation.assert();
|
|
122
|
-
return await withAtomicRenameIdentityLock(filePath, () => replaceAsync(options, filePath, renameIdentity, mutation, syncParent));
|
|
123
|
+
return await withAtomicRenameIdentityLock(filePath, () => replaceAsync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery, syncParent));
|
|
123
124
|
});
|
|
124
125
|
}
|
|
125
|
-
function replaceAsync(options, filePath, renameIdentity, mutation, syncParent) {
|
|
126
|
-
return runAsync(replace(AtomicIo.async(options.fileSystem?.promises ?? fs), options, filePath, renameIdentity, mutation, syncParent));
|
|
126
|
+
function replaceAsync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery, syncParent) {
|
|
127
|
+
return runAsync(replace(AtomicIo.async(options.fileSystem?.promises ?? fs), options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery, syncParent));
|
|
127
128
|
}
|
|
128
129
|
export function replaceFileAtomicSync(options) {
|
|
129
130
|
const mutation = new AtomicMutation(options);
|
|
130
131
|
const filePath = validateReplaceFilePath(options.filePath);
|
|
131
132
|
validateRestoreOptions(options);
|
|
132
133
|
const renameIdentity = options.renameIdentity;
|
|
134
|
+
const allowDrvfsRenameRecovery = options.allowDrvfsRenameRecovery === true;
|
|
133
135
|
validateRenameIdentity(renameIdentity);
|
|
134
136
|
if (renameIdentity !== "verify-content-with-lock") {
|
|
135
|
-
return replaceSync(options, filePath, renameIdentity, mutation);
|
|
137
|
+
return replaceSync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery);
|
|
136
138
|
}
|
|
137
139
|
const fsModule = options.fileSystem ?? syncFs;
|
|
138
140
|
const dir = path.dirname(filePath);
|
|
@@ -146,12 +148,12 @@ export function replaceFileAtomicSync(options) {
|
|
|
146
148
|
recursive: true, mode: dirMode,
|
|
147
149
|
});
|
|
148
150
|
mutation.assert();
|
|
149
|
-
return withAtomicRenameIdentityLockSync(filePath, () => replaceSync(options, filePath, renameIdentity, mutation));
|
|
151
|
+
return withAtomicRenameIdentityLockSync(filePath, () => replaceSync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery));
|
|
150
152
|
}
|
|
151
|
-
function replaceSync(options, filePath, renameIdentity, mutation) {
|
|
152
|
-
return runSync(replace(AtomicIo.sync(options.fileSystem ?? syncFs), options, filePath, renameIdentity, mutation));
|
|
153
|
+
function replaceSync(options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery) {
|
|
154
|
+
return runSync(replace(AtomicIo.sync(options.fileSystem ?? syncFs), options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery));
|
|
153
155
|
}
|
|
154
|
-
function* replace(io, options, filePath, renameIdentity, mutation, syncParent) {
|
|
156
|
+
function* replace(io, options, filePath, renameIdentity, mutation, allowDrvfsRenameRecovery, syncParent) {
|
|
155
157
|
const dir = path.dirname(filePath);
|
|
156
158
|
const dirMode = options.dirMode ?? 0o700;
|
|
157
159
|
const mode = yield* resolveMode(io, options, filePath);
|
|
@@ -168,7 +170,7 @@ function* replace(io, options, filePath, renameIdentity, mutation, syncParent) {
|
|
|
168
170
|
const tempOwner = new AtomicTempOwner(tempPath, io);
|
|
169
171
|
let originalFailure;
|
|
170
172
|
try {
|
|
171
|
-
yield* prepareDirectoryMode(io, { dirPath: dir, mode: dirMode, mutation });
|
|
173
|
+
yield* prepareDirectoryMode(io, { dirPath: dir, mode: dirMode, mutation, allowDrvfsRenameRecovery });
|
|
172
174
|
tempOwner.start();
|
|
173
175
|
tempOwner.adopt(yield* writeTempFile(io, {
|
|
174
176
|
tempPath,
|
|
@@ -191,6 +193,8 @@ function* replace(io, options, filePath, renameIdentity, mutation, syncParent) {
|
|
|
191
193
|
yield* assertDestinationHardlinkPolicy(io, filePath, options.destinationHardlinks);
|
|
192
194
|
yield* tempOwner.assertCurrent();
|
|
193
195
|
}
|
|
196
|
+
if (allowDrvfsRenameRecovery)
|
|
197
|
+
yield* tempOwner.prepareRecovery(options.content, dir, true);
|
|
194
198
|
const result = yield* renameWithRetry(io, tempOwner, filePath, options, mutation);
|
|
195
199
|
if (result.method === "rename") {
|
|
196
200
|
tempOwner.markRenamed();
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import fs, {} from "node:fs";
|
|
2
|
+
import asyncFs from "node:fs/promises";
|
|
2
3
|
import os from "node:os";
|
|
3
4
|
import path from "node:path";
|
|
4
|
-
import { extendDirectoryObservationGuard, inspectDirectoryIdentitySync, inspectDirectoryObservationSync, } from "./directory-guard.js";
|
|
5
|
+
import { extendDirectoryObservationGuard, inspectDirectoryIdentitySync, inspectDirectoryIdentity, inspectDirectoryObservationSync, } from "./directory-guard.js";
|
|
5
6
|
import { FsSafeError } from "./errors.js";
|
|
6
7
|
import { assertNoNulPathInput, assertNoUnsafeDeviceReadPath, hasNodeErrorCode, isNotFoundPathError, isPathInside, } from "./path.js";
|
|
7
8
|
import { ROOT_PATH_ALIAS_POLICIES, resolveRootPath } from "./root-path.js";
|
|
8
9
|
import { admitPathInsideRoot } from "./root-boundary.js";
|
|
9
10
|
import { errorCauseOptions, outsideWorkspaceError, pathTraversalPermissionError, rootPathChangedError } from "./root-errors.js";
|
|
10
11
|
import { isDriveRelativePath } from "./safe-path-segment.js";
|
|
11
|
-
import { realpathSync } from "./realpath.js";
|
|
12
|
+
import { realpathNative, realpathSync } from "./realpath.js";
|
|
13
|
+
import { absolutePathWithRawSegments } from "./root-path-existing.js";
|
|
12
14
|
import { inspectFileIdentity } from "./strict-file-identity.js";
|
|
13
15
|
import { assertNoWindowsPathAlias, pathForWindowsFilesystem, resolvePathFromBasePreservingWindowsRoot, resolvePathPreservingWindowsRoot, } from "./windows-path-alias.js";
|
|
14
16
|
export const ensureTrailingSep = (value) => value.endsWith(path.sep) ? value : value + path.sep;
|
|
@@ -34,21 +36,24 @@ export async function expandRelativePathWithHome(relativePath) {
|
|
|
34
36
|
}
|
|
35
37
|
const rawHome = process.env.HOME || process.env.USERPROFILE || os.homedir();
|
|
36
38
|
assertNoWindowsPathAlias(rawHome, "filesystem", "home path uses a Windows filesystem namespace alias");
|
|
37
|
-
|
|
39
|
+
let home = cachedHomePath?.raw === rawHome ? cachedHomePath.real : undefined;
|
|
40
|
+
if (home === undefined) {
|
|
38
41
|
let realHome = rawHome;
|
|
39
42
|
try {
|
|
40
|
-
|
|
43
|
+
const homePath = pathForWindowsFilesystem(rawHome);
|
|
44
|
+
realHome = process.platform === "win32" ? realpathSync.native(homePath) : await realpathNative(homePath);
|
|
41
45
|
}
|
|
42
46
|
catch {
|
|
43
47
|
// If the home dir cannot be canonicalized, keep lexical expansion behavior.
|
|
44
48
|
}
|
|
45
49
|
cachedHomePath = { raw: rawHome, real: realHome };
|
|
50
|
+
home = realHome;
|
|
46
51
|
}
|
|
47
52
|
if (homeOnly) {
|
|
48
|
-
assertNoWindowsPathAlias(
|
|
49
|
-
return
|
|
53
|
+
assertNoWindowsPathAlias(home, "filesystem", "home path uses a Windows filesystem namespace alias");
|
|
54
|
+
return home;
|
|
50
55
|
}
|
|
51
|
-
const expanded = `${ensureTrailingSep(
|
|
56
|
+
const expanded = `${ensureTrailingSep(home)}${relativePath.slice(2)}`;
|
|
52
57
|
assertNoWindowsPathAlias(expanded, "filesystem", "expanded path uses a Windows filesystem namespace alias");
|
|
53
58
|
return expanded;
|
|
54
59
|
}
|
|
@@ -61,14 +66,17 @@ export async function resolveRootContext(rootDir) {
|
|
|
61
66
|
let rootStat;
|
|
62
67
|
let rootIdentity;
|
|
63
68
|
try {
|
|
64
|
-
|
|
69
|
+
const rootPath = pathForWindowsFilesystem(absolutePathWithRawSegments(rootDir));
|
|
70
|
+
rootReal = process.platform === "win32" ? realpathSync.native(rootPath) : await realpathNative(rootPath);
|
|
65
71
|
assertNoWindowsPathAlias(rootReal, "filesystem", "canonical root path uses a Windows filesystem namespace alias");
|
|
66
|
-
|
|
67
|
-
const stat = fs.statSync(rootReal, { bigint: true });
|
|
72
|
+
const inspect = (stat) => {
|
|
68
73
|
if (!stat.isDirectory())
|
|
69
74
|
throw new FsSafeError("invalid-path", "root dir is not a directory");
|
|
70
75
|
return stat;
|
|
71
|
-
}
|
|
76
|
+
};
|
|
77
|
+
rootStat = await inspectFileIdentity(() => process.platform === "win32"
|
|
78
|
+
? inspect(fs.statSync(rootReal, { bigint: true }))
|
|
79
|
+
: asyncFs.stat(rootReal, { bigint: true }).then(inspect));
|
|
72
80
|
rootIdentity = { dev: rootStat.dev, ino: rootStat.ino };
|
|
73
81
|
}
|
|
74
82
|
catch (err) {
|
|
@@ -128,7 +136,15 @@ export function assertRootIdentityCurrentSync(root, observe) {
|
|
|
128
136
|
}
|
|
129
137
|
}
|
|
130
138
|
export async function assertRootIdentityCurrent(root, observe) {
|
|
131
|
-
|
|
139
|
+
if (process.platform === "win32")
|
|
140
|
+
return assertRootIdentityCurrentSync(root, observe);
|
|
141
|
+
try {
|
|
142
|
+
const stat = await inspectDirectoryIdentity(root.rootReal, { dev: root.rootIdentity.dev, ino: root.rootIdentity.ino });
|
|
143
|
+
observe?.(stat);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
throw rootPathChangedError(error instanceof Error ? error : undefined);
|
|
147
|
+
}
|
|
132
148
|
}
|
|
133
149
|
/**
|
|
134
150
|
* Observe the current Root with an exact, operation-local receipt.
|
|
@@ -101,6 +101,7 @@ async function copyInputInRoot(root, params, input, mode, verifyAdmission = inpu
|
|
|
101
101
|
const observer = createCopyPublicationObserver(pinned.targetPath, params.onDestinationPublished);
|
|
102
102
|
try {
|
|
103
103
|
await runPinnedWriteHelper({
|
|
104
|
+
allowDrvfsRenameRecovery: params.allowDrvfsRenameRecovery,
|
|
104
105
|
rootPath: pinned.rootReal, relativeParentPath: pinned.relativeParentPath, basename: pinned.basename,
|
|
105
106
|
mkdir: params.mkdir !== false, mode: pinned.mode, overwrite: params.overwrite !== false,
|
|
106
107
|
rejectFinalSymlink: params.mutationSymlinks !== undefined, maxBytes: params.maxBytes,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { normalizeMaxBytes } from "./byte-budget.js";
|
|
2
2
|
import { FsSafeError } from "./errors.js";
|
|
3
3
|
import { MutationAuthorityError } from "./mutation-authority.js";
|
|
4
|
-
// Lock records
|
|
4
|
+
// Lock records require complete exclusive publication.
|
|
5
5
|
// Keep this composition hook off the public Root options and package exports.
|
|
6
6
|
const exclusiveSidecarCreate = Symbol("exclusiveSidecarCreate");
|
|
7
7
|
export const sidecarExclusiveCreate = Object.freeze({ [exclusiveSidecarCreate]: true });
|
|
@@ -48,7 +48,7 @@ export function rootWriteInput(params) {
|
|
|
48
48
|
const data = params.data;
|
|
49
49
|
if (typeof data === "string" || Buffer.isBuffer(data)) {
|
|
50
50
|
return {
|
|
51
|
-
kind: "buffer", data, encoding: params.encoding,
|
|
51
|
+
kind: "buffer", data, encoding: params.encoding, sidecar: params[exclusiveSidecarCreate],
|
|
52
52
|
stageBeforePublish: params[exclusiveSidecarCreate] ? false : params.atomic === true ? true : undefined,
|
|
53
53
|
};
|
|
54
54
|
}
|
|
@@ -14,7 +14,11 @@ export type CanonicalRootObservation = {
|
|
|
14
14
|
error: unknown;
|
|
15
15
|
};
|
|
16
16
|
export declare function observeCanonicalRoot(ioFs: RootFileAdmissionFs, rootPath: string): CanonicalRootObservation;
|
|
17
|
+
export declare function observeCanonicalRootAsync(rootPath: string): Promise<CanonicalRootObservation>;
|
|
17
18
|
export declare function createRootFileFinalAdmission(ioFs: RootFileAdmissionFs, root: Extract<CanonicalRootObservation, {
|
|
18
19
|
ok: true;
|
|
19
20
|
}>, boundaryLabel: string, rejectHardlinks?: boolean): PinnedOpenSyncFinalAdmission;
|
|
21
|
+
export declare function createRootFileFinalAdmissionAsync(root: Extract<CanonicalRootObservation, {
|
|
22
|
+
ok: true;
|
|
23
|
+
}>, boundaryLabel: string, rejectHardlinks: boolean): (params: Parameters<PinnedOpenSyncFinalAdmission>[0]) => Promise<string>;
|
|
20
24
|
export {};
|
|
@@ -1,24 +1,28 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import asyncFs from "node:fs/promises";
|
|
3
|
+
import { runAsync, runSync, wait } from "./atomic-io.js";
|
|
2
4
|
import { FsSafeError } from "./errors.js";
|
|
3
5
|
import { isExpectedPathError } from "./pinned-open.js";
|
|
4
|
-
import { realpathSync } from "./realpath.js";
|
|
6
|
+
import { realpathNative, realpathSync } from "./realpath.js";
|
|
5
7
|
import { admitPathInsideRoot } from "./root-boundary.js";
|
|
6
8
|
import { hardlinkedPathNotAllowedError } from "./root-errors.js";
|
|
7
|
-
import { inspectFileIdentitySync } from "./strict-file-identity.js";
|
|
9
|
+
import { inspectFileIdentity, inspectFileIdentitySync } from "./strict-file-identity.js";
|
|
8
10
|
import { assertNoWindowsPathAlias, pathForWindowsFilesystem } from "./windows-path-alias.js";
|
|
9
11
|
function rootIdentityMismatch(cause) {
|
|
10
12
|
return new FsSafeError("path-mismatch", "canonical root directory identity changed or could not be verified", cause === undefined ? {} : { cause });
|
|
11
13
|
}
|
|
12
|
-
function inspectCanonicalRoot(ioFs, rootPath, expected) {
|
|
14
|
+
function* inspectCanonicalRoot(ioFs, rootPath, expected, asynchronous = false) {
|
|
13
15
|
try {
|
|
14
16
|
assertNoWindowsPathAlias(rootPath);
|
|
15
17
|
const operationPath = ioFs === fs ? pathForWindowsFilesystem(rootPath) : rootPath;
|
|
16
|
-
|
|
17
|
-
const stat = ioFs.lstatSync(operationPath, { bigint: true });
|
|
18
|
+
const inspect = (stat) => {
|
|
18
19
|
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
19
20
|
throw rootIdentityMismatch();
|
|
20
21
|
return stat;
|
|
21
|
-
}
|
|
22
|
+
};
|
|
23
|
+
return asynchronous
|
|
24
|
+
? yield* wait(inspectFileIdentity(async () => inspect(await asyncFs.lstat(operationPath, { bigint: true })), expected))
|
|
25
|
+
: inspectFileIdentitySync(() => inspect(ioFs.lstatSync(operationPath, { bigint: true })), expected);
|
|
22
26
|
}
|
|
23
27
|
catch (error) {
|
|
24
28
|
if (expected && isExpectedPathError(error))
|
|
@@ -27,16 +31,23 @@ function inspectCanonicalRoot(ioFs, rootPath, expected) {
|
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
export function observeCanonicalRoot(ioFs, rootPath) {
|
|
34
|
+
return runSync(observeCanonicalRootProcedure(ioFs, rootPath, false));
|
|
35
|
+
}
|
|
36
|
+
export function observeCanonicalRootAsync(rootPath) {
|
|
37
|
+
return runAsync(observeCanonicalRootProcedure(fs, rootPath, true));
|
|
38
|
+
}
|
|
39
|
+
function* observeCanonicalRootProcedure(ioFs, rootPath, asynchronous) {
|
|
30
40
|
try {
|
|
31
|
-
const stat = inspectCanonicalRoot(ioFs, rootPath);
|
|
41
|
+
const stat = yield* inspectCanonicalRoot(ioFs, rootPath, undefined, asynchronous);
|
|
32
42
|
const identity = Object.freeze({ dev: stat.dev, ino: stat.ino });
|
|
33
43
|
let observedPath = rootPath;
|
|
34
44
|
if (ioFs === fs) {
|
|
35
45
|
// Match native leaf resolution (including case and Windows short names)
|
|
36
46
|
// without replacing the already observed root identity.
|
|
37
|
-
|
|
47
|
+
const operationPath = pathForWindowsFilesystem(rootPath);
|
|
48
|
+
observedPath = asynchronous ? yield* wait(realpathNative(operationPath)) : realpathSync.native(operationPath);
|
|
38
49
|
if (observedPath !== rootPath)
|
|
39
|
-
inspectCanonicalRoot(ioFs, observedPath, identity);
|
|
50
|
+
yield* inspectCanonicalRoot(ioFs, observedPath, identity, asynchronous);
|
|
40
51
|
}
|
|
41
52
|
return { ok: true, path: observedPath, identity };
|
|
42
53
|
}
|
|
@@ -48,34 +59,40 @@ function resolveConsumedPath(ioFs, filePath) {
|
|
|
48
59
|
return ioFs === fs ? realpathSync.native(filePath) : ioFs.realpathSync(filePath);
|
|
49
60
|
}
|
|
50
61
|
export function createRootFileFinalAdmission(ioFs, root, boundaryLabel, rejectHardlinks = true) {
|
|
62
|
+
return params => runSync(admitRootFile(ioFs, root, boundaryLabel, rejectHardlinks, params, false));
|
|
63
|
+
}
|
|
64
|
+
export function createRootFileFinalAdmissionAsync(root, boundaryLabel, rejectHardlinks) {
|
|
65
|
+
return params => runAsync(admitRootFile(fs, root, boundaryLabel, rejectHardlinks, params, true));
|
|
66
|
+
}
|
|
67
|
+
function* admitRootFile(ioFs, root, boundaryLabel, rejectHardlinks, { path: consumedPath, descriptorIdentity }, asynchronous) {
|
|
51
68
|
const inspectRoot = (candidateRootPath, expected) => {
|
|
52
|
-
inspectCanonicalRoot(ioFs, candidateRootPath, expected);
|
|
69
|
+
runSync(inspectCanonicalRoot(ioFs, candidateRootPath, expected));
|
|
53
70
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
throw error;
|
|
71
|
+
yield* inspectCanonicalRoot(ioFs, root.path, root.identity, asynchronous);
|
|
72
|
+
const canonicalPath = asynchronous ? yield* wait(realpathNative(consumedPath)) : resolveConsumedPath(ioFs, consumedPath);
|
|
73
|
+
assertNoWindowsPathAlias(canonicalPath, "filesystem", "resolved file path uses a Windows filesystem namespace alias");
|
|
74
|
+
const admitted = admitPathInsideRoot({
|
|
75
|
+
rootPath: root.path,
|
|
76
|
+
candidatePath: canonicalPath,
|
|
77
|
+
rootIdentity: root.identity,
|
|
78
|
+
inspectCandidateRoot: inspectRoot,
|
|
79
|
+
});
|
|
80
|
+
if (!admitted) {
|
|
81
|
+
throw new FsSafeError("outside-workspace", `resolved path escapes ${boundaryLabel}`);
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const current = asynchronous
|
|
85
|
+
? yield* wait(inspectFileIdentity(() => asyncFs.lstat(admitted.path, { bigint: true }), descriptorIdentity))
|
|
86
|
+
: inspectFileIdentitySync(() => ioFs.lstatSync(admitted.path, { bigint: true }), descriptorIdentity);
|
|
87
|
+
if (rejectHardlinks && current.isFile() && current.nlink > 1n)
|
|
88
|
+
throw hardlinkedPathNotAllowedError();
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (isExpectedPathError(error)) {
|
|
92
|
+
throw new FsSafeError("path-mismatch", "canonical file identity changed or could not be verified", { cause: error });
|
|
77
93
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
yield* inspectCanonicalRoot(ioFs, root.path, root.identity, asynchronous);
|
|
97
|
+
return admitted.path;
|
|
81
98
|
}
|
|
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { inspectDirectoryIdentitySync } from "./directory-guard.js";
|
|
4
4
|
import { FsSafeError } from "./errors.js";
|
|
5
|
+
import { captureNativeFdClose } from "./native-binding.js";
|
|
5
6
|
import { getNativeBinding } from "./native.js";
|
|
6
7
|
import { openExactNativeRootSync } from "./native-parent-admission.js";
|
|
7
8
|
import { isExpectedPathError } from "./pinned-open.js";
|
|
@@ -84,6 +85,7 @@ export function createRootFileOpenBatchSync() {
|
|
|
84
85
|
}
|
|
85
86
|
const { owner, observation } = retained;
|
|
86
87
|
inspectDirectoryIdentitySync(observation.path, observation.identity);
|
|
88
|
+
const closeFd = captureNativeFdClose(binding);
|
|
87
89
|
let admittedFd;
|
|
88
90
|
try {
|
|
89
91
|
// Reject reparses before any full-leaf pathname observation can follow them.
|
|
@@ -96,7 +98,7 @@ export function createRootFileOpenBatchSync() {
|
|
|
96
98
|
}
|
|
97
99
|
throw error;
|
|
98
100
|
}
|
|
99
|
-
return openRootFileWithRetainedRootSync(params, observation, admittedFd);
|
|
101
|
+
return openRootFileWithRetainedRootSync(params, observation, admittedFd, closeFd);
|
|
100
102
|
}
|
|
101
103
|
catch (error) {
|
|
102
104
|
return failure(error);
|
|
@@ -13,6 +13,7 @@ export type RootFileOpenResult = {
|
|
|
13
13
|
stat: fs.Stats;
|
|
14
14
|
exactIdentity: ExactFileIdentity;
|
|
15
15
|
rootRealPath: string;
|
|
16
|
+
close?: () => void;
|
|
16
17
|
} | {
|
|
17
18
|
ok: false;
|
|
18
19
|
reason: RootFileOpenFailureReason;
|
|
@@ -48,6 +49,6 @@ export declare function matchRootFileOpenFailure<T>(failure: RootFileOpenFailure
|
|
|
48
49
|
/** Internal retained-root adapter; the native opener owns component admission. */
|
|
49
50
|
export declare function openRootFileWithRetainedRootSync(params: OpenRootFileSyncParams, root: Extract<CanonicalRootObservation, {
|
|
50
51
|
ok: true;
|
|
51
|
-
}>, admittedFd: number): RootFileOpenResult;
|
|
52
|
+
}>, admittedFd: number, closeFd: (fd: number) => void): RootFileOpenResult;
|
|
52
53
|
export declare function openRootFile(params: OpenRootFileParams): Promise<RootFileOpenResult>;
|
|
53
54
|
export {};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { exactFileIdentity } from "./exact-identity.js";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { runAsync, runSync, wait } from "./atomic-io.js";
|
|
4
5
|
import { resolveRootPathSyncWithCanonicalRootObservation, resolveRootPathWithCanonicalRootObservation, } from "./root-path.js";
|
|
5
6
|
import { readSymlinkResolution } from "./root-symlink-policy.js";
|
|
6
|
-
import { isExpectedPathError, openPinnedFileSync, } from "./pinned-open.js";
|
|
7
|
+
import { isExpectedPathError, openPinnedFileSync, openPinnedFile, } from "./pinned-open.js";
|
|
7
8
|
import { assertNoWindowsPathAlias } from "./windows-path-alias.js";
|
|
8
9
|
import { FsSafeError } from "./errors.js";
|
|
9
|
-
import {
|
|
10
|
+
import { ownFileDescriptorSync } from "./create-owned-file.js";
|
|
11
|
+
import { createRootFileFinalAdmission, createRootFileFinalAdmissionAsync, observeCanonicalRoot, observeCanonicalRootAsync, } from "./root-file-final-admission.js";
|
|
10
12
|
export function canUseRootFileOpen(ioFs) {
|
|
11
13
|
return (typeof ioFs.openSync === "function" &&
|
|
12
14
|
typeof ioFs.closeSync === "function" &&
|
|
@@ -71,22 +73,27 @@ export function matchRootFileOpenFailure(failure, handlers) {
|
|
|
71
73
|
return handlers.fallback(failure);
|
|
72
74
|
}
|
|
73
75
|
function finalizeRootFileOpen(params) {
|
|
76
|
+
return runSync(finalizeRootFileOpenProcedure(params, false));
|
|
77
|
+
}
|
|
78
|
+
function* finalizeRootFileOpenProcedure(params, asynchronous) {
|
|
74
79
|
if ("ok" in params.resolved) {
|
|
75
80
|
return params.resolved;
|
|
76
81
|
}
|
|
77
82
|
const resolved = params.resolved;
|
|
78
83
|
const rejectHardlinks = params.rejectHardlinks ?? true;
|
|
79
|
-
const
|
|
84
|
+
const input = {
|
|
80
85
|
filePath: resolved.absolutePath,
|
|
81
86
|
resolvedPath: resolved.resolvedPath,
|
|
82
87
|
rejectHardlinks,
|
|
83
88
|
maxBytes: params.maxBytes,
|
|
84
89
|
allowedType: params.allowedType,
|
|
85
90
|
ioFs: params.ioFs,
|
|
86
|
-
|
|
91
|
+
admittedFile: params.admittedFile,
|
|
87
92
|
rejectLeafSymlinks: params.rejectLeafSymlinks,
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
};
|
|
94
|
+
const opened = asynchronous
|
|
95
|
+
? yield* wait(openPinnedFile({ ...input, finalAdmission: createRootFileFinalAdmissionAsync(resolved.rootObservation, resolved.boundaryLabel, rejectHardlinks) }))
|
|
96
|
+
: openPinnedFileSync({ ...input, finalAdmission: createRootFileFinalAdmission(params.ioFs, resolved.rootObservation, resolved.boundaryLabel, rejectHardlinks) });
|
|
90
97
|
if (!opened.ok) {
|
|
91
98
|
return opened;
|
|
92
99
|
}
|
|
@@ -97,21 +104,25 @@ function finalizeRootFileOpen(params) {
|
|
|
97
104
|
stat: opened.stat,
|
|
98
105
|
exactIdentity: exactFileIdentity(opened.identity),
|
|
99
106
|
rootRealPath: resolved.rootRealPath,
|
|
107
|
+
...(opened.close ? { close: opened.close } : {}),
|
|
100
108
|
};
|
|
101
109
|
}
|
|
102
110
|
/** Internal retained-root adapter; the native opener owns component admission. */
|
|
103
|
-
export function openRootFileWithRetainedRootSync(params, root, admittedFd) {
|
|
111
|
+
export function openRootFileWithRetainedRootSync(params, root, admittedFd, closeFd) {
|
|
104
112
|
return finalizeRootFileOpen({
|
|
105
113
|
resolved: {
|
|
106
114
|
absolutePath: params.absolutePath, resolvedPath: params.absolutePath,
|
|
107
115
|
rootRealPath: root.path, rootObservation: root, boundaryLabel: params.boundaryLabel,
|
|
108
116
|
},
|
|
109
117
|
maxBytes: params.maxBytes, rejectHardlinks: params.rejectHardlinks,
|
|
110
|
-
allowedType: params.allowedType, ioFs: fs,
|
|
118
|
+
allowedType: params.allowedType, ioFs: fs,
|
|
119
|
+
admittedFile: ownFileDescriptorSync(admittedFd, closeFd), rejectLeafSymlinks: true,
|
|
111
120
|
});
|
|
112
121
|
}
|
|
113
122
|
export async function openRootFile(params) {
|
|
114
123
|
const ioFs = params.ioFs ?? fs;
|
|
124
|
+
// Windows sharing and explicit synchronous adapters retain their existing owner.
|
|
125
|
+
const asynchronous = process.platform !== "win32" && ioFs === fs;
|
|
115
126
|
const rawAbsolutePath = params.absolutePath;
|
|
116
127
|
let resolutionSnapshot;
|
|
117
128
|
try {
|
|
@@ -143,22 +154,22 @@ export async function openRootFile(params) {
|
|
|
143
154
|
rejectSymlinks: resolutionSnapshot.rejectSymlinks,
|
|
144
155
|
rejectFinalSymlink: resolutionSnapshot.rejectFinalSymlink,
|
|
145
156
|
skipLexicalRootCheck: resolutionSnapshot.skipLexicalRootCheck,
|
|
146
|
-
},
|
|
147
|
-
rootObservation =
|
|
148
|
-
|
|
157
|
+
}, asynchronous
|
|
158
|
+
? async (rootCanonicalPath) => { rootObservation = await observeCanonicalRootAsync(rootCanonicalPath); }
|
|
159
|
+
: rootCanonicalPath => { rootObservation = observeCanonicalRoot(ioFs, rootCanonicalPath); });
|
|
149
160
|
resolved = mapResolvedRootPath(resolutionSnapshot.absolutePath, resolutionSnapshot.boundaryLabel, resolvedPath, rootObservation);
|
|
150
161
|
}
|
|
151
162
|
catch (error) {
|
|
152
163
|
resolved = toBoundaryValidationError(error);
|
|
153
164
|
}
|
|
154
165
|
}
|
|
155
|
-
return
|
|
166
|
+
return runAsync(finalizeRootFileOpenProcedure({
|
|
156
167
|
resolved,
|
|
157
168
|
maxBytes: openSnapshot.maxBytes,
|
|
158
169
|
rejectHardlinks: openSnapshot.rejectHardlinks,
|
|
159
170
|
allowedType: openSnapshot.allowedType,
|
|
160
171
|
ioFs,
|
|
161
|
-
});
|
|
172
|
+
}, asynchronous));
|
|
162
173
|
}
|
|
163
174
|
function toBoundaryValidationError(error) {
|
|
164
175
|
return { ok: false, reason: "validation", error };
|