@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
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import fs, {} from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { sidecarStagingPath, cleanupSidecarStages } from "./sidecar-lock-staging.js";
|
|
4
|
+
import { createSuppressedError } from "./suppressed-error.js";
|
|
5
|
+
import { FsSafeError } from "./errors.js";
|
|
6
|
+
import { sameFileIdentity, sameFileIdentityForCleanup } from "./file-identity.js";
|
|
7
|
+
import { readSidecarLockRawSnapshotSync } from "./sidecar-lock-reclaim.js";
|
|
8
|
+
import { isHardlinkFallbackError } from "./hardlink-fallback.js";
|
|
9
|
+
import { getNativeBinding } from "./native.js";
|
|
10
|
+
import { noReplaceUnavailable } from "./native-noreplace.js";
|
|
11
|
+
import { getFsSafeTestHooks } from "./test-hooks.js";
|
|
12
|
+
function optionalDirectoryPersistence(error) {
|
|
13
|
+
return ["EACCES", "EPERM", "EINVAL", "ENOTSUP", "EOPNOTSUPP"].includes(error?.code ?? "");
|
|
14
|
+
}
|
|
15
|
+
function openPublicationParent(target) {
|
|
16
|
+
try {
|
|
17
|
+
return fs.openSync(path.dirname(target), fs.constants.O_RDONLY | (fs.constants.O_DIRECTORY ?? 0));
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (!optionalDirectoryPersistence(error))
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Final lock ownership never comes from staging cleanup or an observed PID.
|
|
25
|
+
class SidecarPublication {
|
|
26
|
+
targetPath;
|
|
27
|
+
raw;
|
|
28
|
+
options;
|
|
29
|
+
temporaryPath;
|
|
30
|
+
identity;
|
|
31
|
+
temporaryIdentity;
|
|
32
|
+
publishedIdentity;
|
|
33
|
+
published = false;
|
|
34
|
+
ownedParent;
|
|
35
|
+
parentIdentity;
|
|
36
|
+
close() {
|
|
37
|
+
const fd = this.ownedParent;
|
|
38
|
+
this.ownedParent = undefined;
|
|
39
|
+
if (fd !== undefined)
|
|
40
|
+
try {
|
|
41
|
+
fs.closeSync(fd);
|
|
42
|
+
}
|
|
43
|
+
catch { /* Preserve the creation outcome. */ }
|
|
44
|
+
}
|
|
45
|
+
constructor(targetPath, raw, options) {
|
|
46
|
+
this.targetPath = targetPath;
|
|
47
|
+
this.raw = raw;
|
|
48
|
+
this.options = options;
|
|
49
|
+
if (!options.nativeParent) {
|
|
50
|
+
const binding = getNativeBinding();
|
|
51
|
+
if (binding && (binding.renameNoReplace || binding.linkBeneath)) {
|
|
52
|
+
this.ownedParent = openPublicationParent(targetPath);
|
|
53
|
+
if (this.ownedParent !== undefined)
|
|
54
|
+
options = { ...options, nativeParent: { fd: this.ownedParent, binding } };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.options = options;
|
|
58
|
+
try {
|
|
59
|
+
this.parentIdentity = options.nativeParent
|
|
60
|
+
? fs.fstatSync(options.nativeParent.fd, { bigint: true })
|
|
61
|
+
: fs.statSync(path.dirname(targetPath), { bigint: true });
|
|
62
|
+
this.temporaryPath = sidecarStagingPath(targetPath);
|
|
63
|
+
cleanupSidecarStages(targetPath, candidate => {
|
|
64
|
+
this.assertParent();
|
|
65
|
+
if (candidate)
|
|
66
|
+
options.assertCleanupCandidate?.(candidate);
|
|
67
|
+
}, options.nativeParent);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
this.close();
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
assertParent() {
|
|
75
|
+
this.options.assertCurrent?.();
|
|
76
|
+
const parent = fs.statSync(path.dirname(this.targetPath), { bigint: true });
|
|
77
|
+
if (!parent.isDirectory() || !sameFileIdentityForCleanup(parent, this.parentIdentity)) {
|
|
78
|
+
throw new FsSafeError("path-mismatch", "sidecar publication parent changed");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
remember(fd, exclusive = false) {
|
|
82
|
+
this.published = exclusive;
|
|
83
|
+
this.identity = fs.fstatSync(fd, { bigint: true });
|
|
84
|
+
if (this.options.requireExactIdentity && (!this.identity.isFile() || this.identity.nlink !== 1n ||
|
|
85
|
+
!sameFileIdentityForCleanup(this.identity, this.identity))) {
|
|
86
|
+
throw new FsSafeError("path-mismatch", "sidecar stage has no exact creation identity");
|
|
87
|
+
}
|
|
88
|
+
return this.identity;
|
|
89
|
+
}
|
|
90
|
+
assertNamed(name, fd, links) {
|
|
91
|
+
this.assertParent();
|
|
92
|
+
const opened = fs.fstatSync(fd, { bigint: true });
|
|
93
|
+
const named = fs.lstatSync(name, { bigint: true });
|
|
94
|
+
const previous = name === this.temporaryPath ? this.temporaryIdentity : this.publishedIdentity;
|
|
95
|
+
const pathStable = !previous || sameFileIdentityForCleanup(named, previous);
|
|
96
|
+
const heldMatches = this.identity && (this.options.requireExactIdentity
|
|
97
|
+
? sameFileIdentityForCleanup(opened, this.identity) : sameFileIdentity(opened, this.identity));
|
|
98
|
+
let namedMatches = this.identity && sameFileIdentityForCleanup(named, this.identity);
|
|
99
|
+
if (!namedMatches && !this.options.requireExactIdentity && named.isFile() && !named.isSymbolicLink()) {
|
|
100
|
+
// Raw lock ownership already tolerates descriptor/path identity drift:
|
|
101
|
+
// verify the complete in-memory owner token through a stable pathname.
|
|
102
|
+
const snapshot = readSidecarLockRawSnapshotSync(name, { allowDescriptorIdentityDrift: true });
|
|
103
|
+
namedMatches = !!snapshot?.stat && snapshot.raw === this.raw && sameFileIdentityForCleanup(named, snapshot.stat);
|
|
104
|
+
}
|
|
105
|
+
if (!opened.isFile() || !named.isFile() || named.isSymbolicLink() || !heldMatches ||
|
|
106
|
+
!namedMatches || !pathStable || opened.nlink !== links || named.nlink !== links) {
|
|
107
|
+
throw new FsSafeError("path-mismatch", "sidecar publication identity changed");
|
|
108
|
+
}
|
|
109
|
+
if (name === this.temporaryPath)
|
|
110
|
+
this.temporaryIdentity ??= named;
|
|
111
|
+
else
|
|
112
|
+
this.publishedIdentity ??= named;
|
|
113
|
+
}
|
|
114
|
+
publish(fd) {
|
|
115
|
+
this.assertNamed(this.temporaryPath, fd, 1n);
|
|
116
|
+
getFsSafeTestHooks()?.beforeSidecarLockPublish?.(this.targetPath, this.temporaryPath);
|
|
117
|
+
this.assertNamed(this.temporaryPath, fd, 1n);
|
|
118
|
+
const native = this.options.nativeParent;
|
|
119
|
+
const source = path.basename(this.temporaryPath), target = path.basename(this.targetPath);
|
|
120
|
+
if (native?.binding.renameNoReplace) {
|
|
121
|
+
try {
|
|
122
|
+
native.binding.renameNoReplace(native.fd, source, native.fd, target);
|
|
123
|
+
this.published = true;
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (!noReplaceUnavailable(error, "sidecar publication", true)) {
|
|
127
|
+
if (error.code === "EEXIST")
|
|
128
|
+
this.options.onConflict?.(error);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!this.published) {
|
|
134
|
+
try {
|
|
135
|
+
if (native?.binding.linkBeneath)
|
|
136
|
+
native.binding.linkBeneath(native.fd, source, native.fd, target);
|
|
137
|
+
else
|
|
138
|
+
fs.linkSync(this.temporaryPath, this.targetPath);
|
|
139
|
+
this.published = true;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
// These errors establish that linking cannot publish here. Permission
|
|
143
|
+
// refusals still have to pass the final exclusive open; never mask it.
|
|
144
|
+
if (isHardlinkFallbackError(error))
|
|
145
|
+
return false;
|
|
146
|
+
if (error.code === "EEXIST")
|
|
147
|
+
this.options.onConflict?.(error);
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
this.assertNamed(this.targetPath, fd, 2n);
|
|
151
|
+
this.assertNamed(this.temporaryPath, fd, 2n);
|
|
152
|
+
this.removeTemporary(fd);
|
|
153
|
+
}
|
|
154
|
+
this.finish(fd);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
finish(fd) {
|
|
158
|
+
this.assertNamed(this.targetPath, fd, 1n);
|
|
159
|
+
const native = this.options.nativeParent;
|
|
160
|
+
const parent = native?.fd ?? (process.platform !== "win32" ? openPublicationParent(this.targetPath) : undefined);
|
|
161
|
+
if (parent !== undefined) {
|
|
162
|
+
try {
|
|
163
|
+
this.assertParent();
|
|
164
|
+
try {
|
|
165
|
+
fs.fsyncSync(parent);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
if (!optionalDirectoryPersistence(error))
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
if (!native)
|
|
174
|
+
fs.closeSync(parent);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
this.assertNamed(this.targetPath, fd, 1n);
|
|
178
|
+
}
|
|
179
|
+
cleanup(fd) {
|
|
180
|
+
if (this.published)
|
|
181
|
+
this.removeName(this.targetPath, fd);
|
|
182
|
+
this.removeTemporary(fd);
|
|
183
|
+
}
|
|
184
|
+
removeTemporary(fd) {
|
|
185
|
+
this.removeName(this.temporaryPath, fd);
|
|
186
|
+
}
|
|
187
|
+
removeName(pathname, fd) {
|
|
188
|
+
if (!this.identity)
|
|
189
|
+
return;
|
|
190
|
+
this.assertParent();
|
|
191
|
+
const current = fs.lstatSync(pathname, { bigint: true, throwIfNoEntry: false });
|
|
192
|
+
if (!current)
|
|
193
|
+
return;
|
|
194
|
+
const expected = pathname === this.temporaryPath ? this.temporaryIdentity ?? this.identity
|
|
195
|
+
: this.publishedIdentity ?? this.identity;
|
|
196
|
+
if (!current.isFile() || current.isSymbolicLink() || !sameFileIdentityForCleanup(current, expected)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const native = this.options.nativeParent;
|
|
200
|
+
if (native?.binding.removeStagedFile) {
|
|
201
|
+
const removed = native.binding.removeStagedFile(native.fd, path.basename(pathname), fd);
|
|
202
|
+
if (removed !== "removed" && removed !== "name-absent") {
|
|
203
|
+
throw new FsSafeError("path-mismatch", "sidecar stage cleanup lost identity");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
fs.unlinkSync(pathname);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export function createSidecarFileSync(targetPath, raw, open, options = {}) {
|
|
211
|
+
const publication = new SidecarPublication(targetPath, raw, options);
|
|
212
|
+
let fd;
|
|
213
|
+
try {
|
|
214
|
+
publication.assertParent();
|
|
215
|
+
fd = open(publication.temporaryPath);
|
|
216
|
+
let identity = publication.remember(fd);
|
|
217
|
+
options.prepare?.(fd);
|
|
218
|
+
fs.writeFileSync(fd, raw, "utf8");
|
|
219
|
+
fs.fsyncSync(fd);
|
|
220
|
+
if (!publication.publish(fd)) {
|
|
221
|
+
publication.removeTemporary(fd);
|
|
222
|
+
const stagedFd = fd;
|
|
223
|
+
fd = undefined;
|
|
224
|
+
fs.closeSync(stagedFd);
|
|
225
|
+
publication.assertParent();
|
|
226
|
+
fd = open(targetPath);
|
|
227
|
+
identity = publication.remember(fd, true);
|
|
228
|
+
options.prepare?.(fd);
|
|
229
|
+
// Unwritten NUL padding keeps even a complete JSON prefix unverifiable
|
|
230
|
+
// until the trailing ownership token has also been written.
|
|
231
|
+
fs.ftruncateSync(fd, Buffer.byteLength(raw));
|
|
232
|
+
fs.writeFileSync(fd, raw, "utf8");
|
|
233
|
+
fs.fsyncSync(fd);
|
|
234
|
+
publication.finish(fd);
|
|
235
|
+
}
|
|
236
|
+
return { fd, identity };
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
if (fd !== undefined) {
|
|
240
|
+
try {
|
|
241
|
+
try {
|
|
242
|
+
publication.cleanup(fd);
|
|
243
|
+
}
|
|
244
|
+
finally {
|
|
245
|
+
fs.closeSync(fd);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
catch (cleanupError) {
|
|
249
|
+
throw createSuppressedError(error, cleanupError, "sidecar preparation and cleanup both failed");
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
throw error;
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
publication.close();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
export async function createSidecarFile(targetPath, raw, open, options = {}) {
|
|
259
|
+
const publication = new SidecarPublication(targetPath, raw, options);
|
|
260
|
+
let handle;
|
|
261
|
+
try {
|
|
262
|
+
publication.assertParent();
|
|
263
|
+
handle = await open(publication.temporaryPath);
|
|
264
|
+
let identity = publication.remember(handle.fd);
|
|
265
|
+
options.prepare?.(handle.fd);
|
|
266
|
+
await handle.writeFile(raw, "utf8");
|
|
267
|
+
fs.fsyncSync(handle.fd);
|
|
268
|
+
if (!publication.publish(handle.fd)) {
|
|
269
|
+
publication.removeTemporary(handle.fd);
|
|
270
|
+
const stagedHandle = handle;
|
|
271
|
+
handle = undefined;
|
|
272
|
+
await stagedHandle.close();
|
|
273
|
+
publication.assertParent();
|
|
274
|
+
handle = await open(targetPath);
|
|
275
|
+
identity = publication.remember(handle.fd, true);
|
|
276
|
+
options.prepare?.(handle.fd);
|
|
277
|
+
fs.ftruncateSync(handle.fd, Buffer.byteLength(raw));
|
|
278
|
+
await handle.writeFile(raw, "utf8");
|
|
279
|
+
fs.fsyncSync(handle.fd);
|
|
280
|
+
publication.finish(handle.fd);
|
|
281
|
+
}
|
|
282
|
+
return { handle, identity };
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
if (handle) {
|
|
286
|
+
try {
|
|
287
|
+
try {
|
|
288
|
+
publication.cleanup(handle.fd);
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
await handle.close();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
catch (cleanupError) {
|
|
295
|
+
throw createSuppressedError(cleanupError, error, "sidecar preparation and cleanup both failed");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
throw error;
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
publication.close();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -23,6 +23,7 @@ export declare function serializeSidecarLockPayload(payload: Record<string, unkn
|
|
|
23
23
|
};
|
|
24
24
|
export declare function relativeSidecarLockPath(lockRoot: Root, lockPath: string): string;
|
|
25
25
|
export declare function parseSidecarLockPayload(raw: string, parser?: (raw: string) => unknown): unknown;
|
|
26
|
+
export declare function isIncompleteSidecarLockRecord(raw: string | undefined): boolean;
|
|
26
27
|
export declare function readSidecarLockRawSnapshot(lockPath: string, options?: {
|
|
27
28
|
lockRoot?: Root;
|
|
28
29
|
rejectNonFile?: boolean;
|
|
@@ -33,6 +34,7 @@ export declare function readSidecarLockRawSnapshot(lockPath: string, options?: {
|
|
|
33
34
|
export declare function readSidecarLockSnapshotSync(lockPath: string, parsePayload?: (raw: string) => unknown, options?: Parameters<typeof readSidecarLockRawSnapshotSync>[1]): SidecarLockSnapshot | null;
|
|
34
35
|
export declare function readSidecarLockRawSnapshotSync(lockPath: string, options?: {
|
|
35
36
|
rejectNonFile?: boolean;
|
|
37
|
+
allowDescriptorIdentityDrift?: boolean;
|
|
36
38
|
onOpenFailure?: (error: unknown) => void;
|
|
37
39
|
}): SidecarLockRawSnapshot | null;
|
|
38
40
|
export declare function removeSidecarLockIfUnchangedSync(lockPath: string, observed: SidecarLockSnapshot, assertAuthorized?: () => void): boolean;
|
|
@@ -52,6 +52,8 @@ export function relativeSidecarLockPath(lockRoot, lockPath) {
|
|
|
52
52
|
return relative.split(path.sep).join(path.posix.sep);
|
|
53
53
|
}
|
|
54
54
|
export function parseSidecarLockPayload(raw, parser) {
|
|
55
|
+
if (isIncompleteSidecarLockRecord(raw))
|
|
56
|
+
return null;
|
|
55
57
|
if (parser) {
|
|
56
58
|
return parser(raw);
|
|
57
59
|
}
|
|
@@ -63,6 +65,11 @@ export function parseSidecarLockPayload(raw, parser) {
|
|
|
63
65
|
return null;
|
|
64
66
|
}
|
|
65
67
|
}
|
|
68
|
+
// Compatibility writes reserve their length before writing, so unwritten bytes
|
|
69
|
+
// remain NULs. Legacy formats still belong to the caller's parser.
|
|
70
|
+
export function isIncompleteSidecarLockRecord(raw) {
|
|
71
|
+
return raw === undefined || raw.length === 0 || raw.includes("\0");
|
|
72
|
+
}
|
|
66
73
|
async function readSidecarLockComparisonSnapshot(lockPath, options) {
|
|
67
74
|
const snapshot = await readSidecarLockRawSnapshot(lockPath, options);
|
|
68
75
|
const parsePayload = options?.parsePayload;
|
|
@@ -180,7 +187,8 @@ export function readSidecarLockRawSnapshotSync(lockPath, options = {}) {
|
|
|
180
187
|
}
|
|
181
188
|
const raw = readFileDescriptorBoundedSync(fd, MAX_LOCK_PAYLOAD_BYTES).toString("utf8");
|
|
182
189
|
const after = lstatSidecarLockSync(lockPath);
|
|
183
|
-
if (!after || !
|
|
190
|
+
if (!after || !after.isFile() || after.isSymbolicLink() || !sameFileIdentity(before, after) ||
|
|
191
|
+
(!options.allowDescriptorIdentityDrift && (!sameFileIdentity(before, opened) || !sameFileIdentity(opened, after))))
|
|
184
192
|
return null;
|
|
185
193
|
return {
|
|
186
194
|
raw,
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import type { OpenResult, Root } from "./root-impl.js";
|
|
2
|
+
export declare function rootSidecarPublicationPending(lockRoot: Root, relative: string, options?: {
|
|
3
|
+
cleanAbandoned?: boolean;
|
|
4
|
+
requireStage?: boolean;
|
|
5
|
+
identity?: {
|
|
6
|
+
dev: bigint;
|
|
7
|
+
ino: bigint;
|
|
8
|
+
};
|
|
9
|
+
}): Promise<boolean>;
|
|
2
10
|
export declare function openSidecarRoot(lockRoot: Root, relative: string, discardObservation?: "unlinked" | "changed", onOpenFailure?: (error: unknown) => void): Promise<OpenResult | null>;
|
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import { cleanupSidecarStages, sidecarPublicationPending } from "./sidecar-lock-staging.js";
|
|
3
|
+
import { admitFileLockSyncRootPath, assertFileLockSyncRootMutationAllowed, assertFileLockSyncRootPathsCurrent, assertFileLockSyncRootResolvedPathCurrent, captureFileLockSyncRootAuthority } from "./file-lock-sync-root.js";
|
|
4
|
+
import { assertRetainedParentCurrent, observeDirectory } from "./file-lock-sync-root-io.js";
|
|
2
5
|
import { inspectDirectoryIdentity } from "./directory-guard.js";
|
|
3
6
|
import { FsSafeError } from "./errors.js";
|
|
4
7
|
import { fileObservation } from "./file-observation.js";
|
|
5
8
|
import { isNotFoundPathError } from "./path.js";
|
|
6
9
|
import { resolveRootPath } from "./root-path.js";
|
|
10
|
+
export async function rootSidecarPublicationPending(lockRoot, relative, options = {}) {
|
|
11
|
+
const resolved = await lockRoot.resolve(relative);
|
|
12
|
+
const parent = path.dirname(resolved);
|
|
13
|
+
const identity = await inspectDirectoryIdentity(parent);
|
|
14
|
+
if (options.cleanAbandoned) {
|
|
15
|
+
const admitted = admitFileLockSyncRootPath(captureFileLockSyncRootAuthority(lockRoot), resolved);
|
|
16
|
+
const receipt = observeDirectory(path.dirname(admitted.path));
|
|
17
|
+
cleanupSidecarStages(admitted.path, candidate => {
|
|
18
|
+
assertFileLockSyncRootPathsCurrent([admitted]);
|
|
19
|
+
assertFileLockSyncRootResolvedPathCurrent(admitted);
|
|
20
|
+
assertRetainedParentCurrent(admitted, receipt);
|
|
21
|
+
if (candidate)
|
|
22
|
+
assertFileLockSyncRootMutationAllowed(candidate, admitted.authority.denyMutations);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const pending = sidecarPublicationPending(resolved, undefined, options);
|
|
26
|
+
await inspectDirectoryIdentity(parent, identity);
|
|
27
|
+
return pending && await lockRoot.resolve(relative) === resolved;
|
|
28
|
+
}
|
|
7
29
|
export async function openSidecarRoot(lockRoot, relative, discardObservation, onOpenFailure) {
|
|
8
30
|
const resolved = await lockRoot.resolve(relative);
|
|
9
31
|
const canonicalPath = async () => (await resolveRootPath({
|
|
@@ -32,7 +54,10 @@ export async function openSidecarRoot(lockRoot, relative, discardObservation, on
|
|
|
32
54
|
break;
|
|
33
55
|
}
|
|
34
56
|
}
|
|
35
|
-
const observation = fileObservation(
|
|
57
|
+
const observation = fileObservation({
|
|
58
|
+
publicationPending: async (candidate, identity) => discardObservation === "changed" && candidate === resolved &&
|
|
59
|
+
await rootSidecarPublicationPending(lockRoot, relative, { requireStage: true, identity }),
|
|
60
|
+
});
|
|
36
61
|
try {
|
|
37
62
|
const opened = await observation.run(() => lockRoot.open(relative));
|
|
38
63
|
if (opened.realPath !== expectedRealPath) {
|
|
@@ -42,6 +67,9 @@ export async function openSidecarRoot(lockRoot, relative, discardObservation, on
|
|
|
42
67
|
return opened;
|
|
43
68
|
}
|
|
44
69
|
catch (error) {
|
|
70
|
+
if (discardObservation && error instanceof FsSafeError && error.code === "hardlink" &&
|
|
71
|
+
await rootSidecarPublicationPending(lockRoot, relative))
|
|
72
|
+
return null;
|
|
45
73
|
const openFailed = observation.has(error, `open:${resolved}`);
|
|
46
74
|
if (openFailed)
|
|
47
75
|
onOpenFailure?.(error);
|
|
@@ -50,13 +78,19 @@ export async function openSidecarRoot(lockRoot, relative, discardObservation, on
|
|
|
50
78
|
return null;
|
|
51
79
|
const discardable = observation.has(error, `unlinked:${resolved}`) ||
|
|
52
80
|
(discardObservation === "changed" && observation.has(error, `changed:${resolved}`));
|
|
53
|
-
|
|
81
|
+
const linkedChange = discardObservation === "changed" && observation.has(error, `linked-change:${resolved}`);
|
|
82
|
+
if (!discardObservation || (!missing && (!completeParents || (!discardable && !linkedChange))))
|
|
54
83
|
throw error;
|
|
55
84
|
try {
|
|
85
|
+
// Linked-change receipts already proved a matching stage while the exact
|
|
86
|
+
// descriptor was open; retained ancestry and the current path still fence it.
|
|
56
87
|
const probe = fileObservation();
|
|
57
88
|
try {
|
|
58
89
|
const current = await probe.run(() => lockRoot.stat(relative));
|
|
59
|
-
|
|
90
|
+
// A successor can still hold its matching publication-stage link.
|
|
91
|
+
// Discard only the old read; retained ancestry is revalidated below.
|
|
92
|
+
if (!current.isFile || current.isSymbolicLink || (current.nlink !== 1 &&
|
|
93
|
+
!(current.nlink === 2 && await rootSidecarPublicationPending(lockRoot, relative))))
|
|
60
94
|
throw error;
|
|
61
95
|
}
|
|
62
96
|
catch (probeError) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import type { NativeWriteParent } from "./pinned-write-types.js";
|
|
3
|
+
export declare function sidecarStagingPath(target: string): string;
|
|
4
|
+
export declare function cleanupSidecarStages(target: string, assertCurrent: (candidate?: string) => void, native?: NativeWriteParent, onlyStage?: {
|
|
5
|
+
name: string;
|
|
6
|
+
identity: fs.BigIntStats;
|
|
7
|
+
}): void;
|
|
8
|
+
export declare function sidecarPublicationPending(target: string, onStage?: (name: string, identity: fs.BigIntStats) => void, options?: {
|
|
9
|
+
requireStage?: boolean;
|
|
10
|
+
identity?: {
|
|
11
|
+
dev: bigint;
|
|
12
|
+
ino: bigint;
|
|
13
|
+
};
|
|
14
|
+
}): boolean;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { sameFileIdentityForCleanup, sha256Hex } from "./file-identity.js";
|
|
6
|
+
function stagingPrefix(target) {
|
|
7
|
+
return `.fs-safe-lock-${sha256Hex(`${os.hostname()}\0${path.basename(target)}`).slice(0, 16)}-`;
|
|
8
|
+
}
|
|
9
|
+
export function sidecarStagingPath(target) {
|
|
10
|
+
return path.join(path.dirname(target), `${stagingPrefix(target)}${process.pid}-${randomUUID()}.tmp`);
|
|
11
|
+
}
|
|
12
|
+
function ownerAbsent(pid) {
|
|
13
|
+
try {
|
|
14
|
+
process.kill(pid, 0);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return error.code === "ESRCH";
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
// Bound opportunistic cleanup to 64 entries plus one exact publication link.
|
|
22
|
+
// Never remove the final lock, live owners, or unverifiable identities.
|
|
23
|
+
export function cleanupSidecarStages(target, assertCurrent, native, onlyStage) {
|
|
24
|
+
const directory = path.dirname(target), prefix = stagingPrefix(target);
|
|
25
|
+
let entries;
|
|
26
|
+
let authorityFailure;
|
|
27
|
+
const assertAuthority = (candidate) => {
|
|
28
|
+
try {
|
|
29
|
+
assertCurrent(candidate);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
authorityFailure = { error };
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
try {
|
|
37
|
+
assertAuthority();
|
|
38
|
+
if (!onlyStage)
|
|
39
|
+
entries = fs.opendirSync(directory);
|
|
40
|
+
for (let count = 0; count < (onlyStage ? 1 : 64); count++) {
|
|
41
|
+
const entry = onlyStage ?? entries.readSync();
|
|
42
|
+
if (!entry)
|
|
43
|
+
break;
|
|
44
|
+
if (!entry.name.startsWith(prefix))
|
|
45
|
+
continue;
|
|
46
|
+
const suffix = entry.name.slice(prefix.length);
|
|
47
|
+
const match = /^([1-9]\d*)-[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\.tmp$/.exec(suffix);
|
|
48
|
+
if (!match)
|
|
49
|
+
continue;
|
|
50
|
+
const pid = Number(match[1]);
|
|
51
|
+
if (!Number.isSafeInteger(pid) || !ownerAbsent(pid))
|
|
52
|
+
continue;
|
|
53
|
+
const candidate = path.join(directory, entry.name);
|
|
54
|
+
let fd;
|
|
55
|
+
try {
|
|
56
|
+
assertAuthority(candidate);
|
|
57
|
+
const before = fs.lstatSync(candidate, { bigint: true });
|
|
58
|
+
if (onlyStage && !sameFileIdentityForCleanup(before, onlyStage.identity))
|
|
59
|
+
continue;
|
|
60
|
+
if (!before.isFile() || before.isSymbolicLink() || before.nlink > 2n ||
|
|
61
|
+
(process.getuid && before.uid !== BigInt(process.getuid())))
|
|
62
|
+
continue;
|
|
63
|
+
fd = fs.openSync(candidate, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0));
|
|
64
|
+
const held = fs.fstatSync(fd, { bigint: true });
|
|
65
|
+
if (!sameFileIdentityForCleanup(before, held))
|
|
66
|
+
continue;
|
|
67
|
+
assertAuthority(candidate);
|
|
68
|
+
const current = fs.lstatSync(candidate, { bigint: true });
|
|
69
|
+
if (!sameFileIdentityForCleanup(current, held) || !ownerAbsent(pid))
|
|
70
|
+
continue;
|
|
71
|
+
if (native?.binding.removeStagedFile)
|
|
72
|
+
native.binding.removeStagedFile(native.fd, entry.name, fd);
|
|
73
|
+
else
|
|
74
|
+
fs.unlinkSync(candidate);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
if (authorityFailure)
|
|
78
|
+
throw authorityFailure.error;
|
|
79
|
+
// Cleanup is optional; the unique new stage does not depend on it.
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (fd !== undefined)
|
|
83
|
+
try {
|
|
84
|
+
fs.closeSync(fd);
|
|
85
|
+
}
|
|
86
|
+
catch { /* Best effort. */ }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
if (authorityFailure)
|
|
92
|
+
throw authorityFailure.error;
|
|
93
|
+
// Denied enumeration must not turn a working exclusive create into a failure.
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
try {
|
|
97
|
+
entries?.closeSync();
|
|
98
|
+
}
|
|
99
|
+
catch { /* Best effort. */ }
|
|
100
|
+
}
|
|
101
|
+
if (!onlyStage)
|
|
102
|
+
sidecarPublicationPending(target, (name, identity) => cleanupSidecarStages(target, assertCurrent, native, { name, identity }));
|
|
103
|
+
}
|
|
104
|
+
// Readers reject the linked record and retry. Authorized cleanup may handle
|
|
105
|
+
// the one matching stage; recognition streams past the opportunistic budget.
|
|
106
|
+
export function sidecarPublicationPending(target, onStage, options = {}) {
|
|
107
|
+
let entries;
|
|
108
|
+
let callbackFailure;
|
|
109
|
+
try {
|
|
110
|
+
const current = fs.lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
111
|
+
if (!current)
|
|
112
|
+
return !options.requireStage;
|
|
113
|
+
if (options.identity && !sameFileIdentityForCleanup(current, options.identity))
|
|
114
|
+
return false;
|
|
115
|
+
if (!current.isFile() || current.isSymbolicLink())
|
|
116
|
+
return false;
|
|
117
|
+
if (current.nlink === 1n)
|
|
118
|
+
return !options.requireStage;
|
|
119
|
+
if (current.nlink !== 2n)
|
|
120
|
+
return false;
|
|
121
|
+
const prefix = stagingPrefix(target);
|
|
122
|
+
entries = fs.opendirSync(path.dirname(target));
|
|
123
|
+
for (;;) {
|
|
124
|
+
const entry = entries.readSync();
|
|
125
|
+
if (!entry)
|
|
126
|
+
break;
|
|
127
|
+
if (!entry.name.startsWith(prefix) || !/^[1-9]\d*-[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\.tmp$/.test(entry.name.slice(prefix.length)))
|
|
128
|
+
continue;
|
|
129
|
+
const stage = fs.lstatSync(path.join(path.dirname(target), entry.name), { bigint: true, throwIfNoEntry: false });
|
|
130
|
+
if (stage?.isFile() && !stage.isSymbolicLink() && sameFileIdentityForCleanup(stage, current)) {
|
|
131
|
+
try {
|
|
132
|
+
onStage?.(entry.name, current);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
callbackFailure = { error };
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const after = fs.lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
142
|
+
return !options.requireStage && (!after || !sameFileIdentityForCleanup(after, current) || after.nlink === 1n);
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
if (callbackFailure)
|
|
146
|
+
throw callbackFailure.error;
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
try {
|
|
151
|
+
entries?.closeSync();
|
|
152
|
+
}
|
|
153
|
+
catch { /* Observation remains inconclusive. */ }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultSidecarLockShouldReclaim, isTransientLockFileDenial, sidecarLockStale } from "./sidecar-lock-policy.js";
|
|
2
|
-
import { readSidecarLockRawSnapshot, removeStaleSidecarLockIfAllowed, sidecarLockSnapshotStillPresent, tryAcquireSidecarReclaimGuard, } from "./sidecar-lock-reclaim.js";
|
|
2
|
+
import { isIncompleteSidecarLockRecord, readSidecarLockRawSnapshot, removeStaleSidecarLockIfAllowed, sidecarLockSnapshotStillPresent, tryAcquireSidecarReclaimGuard, } from "./sidecar-lock-reclaim.js";
|
|
3
3
|
import { observeSidecarLockParser, parseSidecarLockSnapshotUnderAdmission, scopedSidecarLockParser, } from "./sidecar-lock-admission-parser.js";
|
|
4
4
|
/** Handle one historical EEXIST attempt while its canonical admission is held. */
|
|
5
5
|
export async function handleStaleSidecarAdmission(params) {
|
|
@@ -65,10 +65,12 @@ export async function handleStaleSidecarAdmission(params) {
|
|
|
65
65
|
return;
|
|
66
66
|
throw error;
|
|
67
67
|
}
|
|
68
|
+
// An incomplete owner can still be writing; age is not liveness evidence.
|
|
68
69
|
if (!snapshot) {
|
|
69
70
|
await params.waitForRetry();
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
73
|
+
const incomplete = (!params.parserState.parser && snapshot.payload === null) || isIncompleteSidecarLockRecord(snapshot.raw);
|
|
72
74
|
const staleOptions = params.staleOptionsState;
|
|
73
75
|
try {
|
|
74
76
|
if (!staleOptions.shouldReclaimObserved) {
|
|
@@ -104,7 +106,7 @@ export async function handleStaleSidecarAdmission(params) {
|
|
|
104
106
|
assertUnheld();
|
|
105
107
|
try {
|
|
106
108
|
if (!shouldReclaim)
|
|
107
|
-
return defaultSidecarLockShouldReclaim(snapshot, params.staleMs, Date.now());
|
|
109
|
+
return !incomplete && defaultSidecarLockShouldReclaim(snapshot, params.staleMs, Date.now());
|
|
108
110
|
return await Reflect.apply(shouldReclaim, undefined, [{
|
|
109
111
|
lockPath,
|
|
110
112
|
normalizedTargetPath,
|
|
@@ -150,6 +152,9 @@ export async function handleStaleSidecarAdmission(params) {
|
|
|
150
152
|
await params.waitForRetry();
|
|
151
153
|
return;
|
|
152
154
|
}
|
|
155
|
+
// Policy may diagnose an incomplete record, but can never authorize its removal.
|
|
156
|
+
if (incomplete)
|
|
157
|
+
throw sidecarLockStale(lockPath, normalizedTargetPath);
|
|
153
158
|
if ((staleOptions.staleRecovery ?? "fail-closed") === "remove-if-unchanged") {
|
|
154
159
|
const acquiredGuard = await admission.run(() => tryAcquireSidecarReclaimGuard(params.reclaimGuards, params.reclaimGuardPath, params.lockRoot));
|
|
155
160
|
if (acquiredGuard)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type AnyAsyncDirectoryGuard } from "./directory-guard.js";
|
|
2
|
+
import type { NativeWriteParent, PinnedWriteParams } from "./pinned-write-types.js";
|
|
3
|
+
export declare class SidecarFallbackWriteError extends Error {
|
|
4
|
+
readonly failure: unknown;
|
|
5
|
+
constructor(failure: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare function writeSidecarAtParent(params: PinnedWriteParams, parent: AnyAsyncDirectoryGuard, native?: NativeWriteParent): Promise<{
|
|
8
|
+
dev: bigint;
|
|
9
|
+
ino: bigint;
|
|
10
|
+
}>;
|