@evomap/evolver-proxy 2.0.46 → 2.0.48
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/dist/bin/evolver-proxy.d.ts +4 -0
- package/dist/bin/evolver-proxy.js +49 -5
- package/dist/bin/proxyStopSignal.d.ts +8 -0
- package/dist/bin/proxyStopSignal.js +13 -0
- package/dist/bin/triggerFeedbackStartup.d.ts +8 -0
- package/dist/bin/triggerFeedbackStartup.js +23 -0
- package/dist/daemon/localRecall.d.ts +37 -0
- package/dist/daemon/localRecall.js +112 -0
- package/dist/daemon/proxyDaemon.d.ts +33 -1
- package/dist/daemon/proxyDaemon.js +263 -18
- package/dist/daemon/publishBindingLedger.d.ts +38 -0
- package/dist/daemon/publishBindingLedger.js +171 -1
- package/dist/daemon/recallContext.d.ts +3 -0
- package/dist/daemon/recallContext.js +10 -0
- package/dist/lifecycle/claimNudge.js +1 -1
- package/dist/lifecycle/deployGuard.js +1 -1
- package/dist/lifecycle/legacyNodeId.js +1 -1
- package/dist/lifecycle/manager.js +1 -1
- package/dist/llm/index.js +1 -1
- package/dist/llm/server.js +1 -1
- package/dist/llm/traceBackfill.js +1 -1
- package/dist/llm/traceConfig.js +1 -1
- package/dist/llm/traceControl.js +1 -1
- package/dist/llm/traceEnvelope.js +1 -1
- package/dist/llm/traceSink.js +1 -1
- package/dist/llm/traceUploadPayload.js +1 -1
- package/dist/llm/upstream.js +1 -1
- package/dist/router/cachePassthrough.js +1 -1
- package/dist/router/features.js +1 -1
- package/dist/router/index.js +1 -1
- package/dist/router/messagesRoute.js +1 -1
- package/dist/router/modelRouter.js +1 -1
- package/dist/router/providerRoutes.js +1 -1
- package/dist/router/sseScan.js +1 -1
- package/dist/selfUpdate/bootstrap.js +29 -23
- package/dist/selfUpdate/bootstrapReadiness.js +4 -1
- package/dist/selfUpdate/fileMetadata.d.ts +2 -0
- package/dist/selfUpdate/fileMetadata.js +25 -0
- package/dist/selfUpdate/migration.js +19 -13
- package/dist/selfUpdate/recoveryChildStartGate.js +4 -2
- package/dist/selfUpdate/transaction.js +278 -64
- package/dist/selfUpdate/windowsUpdater.js +2 -1
- package/dist/sync/engine.js +1 -1
- package/package.json +3 -3
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { createHash, randomBytes } from 'node:crypto';
|
|
2
2
|
import { execFile } from 'node:child_process';
|
|
3
|
-
import { constants } from 'node:fs';
|
|
3
|
+
import { constants, watch } from 'node:fs';
|
|
4
4
|
import { chmod, link, lstat, mkdir, open, realpath, rename, rm, writeFile, } from 'node:fs/promises';
|
|
5
5
|
import { basename, dirname, isAbsolute, join, resolve, win32 } from 'node:path';
|
|
6
6
|
import { promisify } from 'node:util';
|
|
7
7
|
import { ops } from '@evomap/evolver-core';
|
|
8
8
|
import { resolveSelfUpdateTarget } from './releaseBinary.js';
|
|
9
9
|
import { SELF_UPDATE_FAILURE_CODES, selfUpdateFailure } from './failureCodes.js';
|
|
10
|
+
import { selfUpdateFileMetadata } from './fileMetadata.js';
|
|
10
11
|
import { bindWindowsManagedExecutable, prepareWindowsExecutableSwap, resolveWindowsUpdaterPaths, } from './windowsUpdater.js';
|
|
11
12
|
const execFileAsync = promisify(execFile);
|
|
12
13
|
const JOURNAL_SCHEMA_VERSION = 2;
|
|
@@ -47,7 +48,9 @@ export async function inspectDurableSelfUpdate(options) {
|
|
|
47
48
|
const paths = await resolveTransactionPaths(options, false);
|
|
48
49
|
if (!paths)
|
|
49
50
|
return { outcome: 'none' };
|
|
51
|
+
await assertStateRootIdentity(paths);
|
|
50
52
|
const loadedJournal = await readJournal(paths.journal);
|
|
53
|
+
await assertStateRootIdentity(paths);
|
|
51
54
|
if (!loadedJournal)
|
|
52
55
|
return { outcome: 'none' };
|
|
53
56
|
const journal = await bindJournalToTarget(paths, loadedJournal);
|
|
@@ -75,30 +78,35 @@ export function stableUnixRecoveryControllerPathForTarget(targetPath, stateDir)
|
|
|
75
78
|
export async function provisionStableUnixRecoveryController(options) {
|
|
76
79
|
assertUnixControllerPlatform(options.platform ?? process.platform);
|
|
77
80
|
const paths = await resolveTransactionPaths(options, true);
|
|
78
|
-
const
|
|
79
|
-
const controllerDirectory = join(paths.root, UNIX_CONTROLLER_DIRECTORY);
|
|
81
|
+
const controllerDirectory = join(paths.stateRoot, UNIX_CONTROLLER_DIRECTORY);
|
|
80
82
|
const controllerPath = join(controllerDirectory, UNIX_CONTROLLER_NAME);
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
const configuredControllerPath = join(paths.root, UNIX_CONTROLLER_DIRECTORY, UNIX_CONTROLLER_NAME);
|
|
84
|
+
const configuredTemporaryPath = options.artifactClaimPath
|
|
85
|
+
?? join(dirname(configuredControllerPath), `.${UNIX_CONTROLLER_NAME}.${randomBytes(8).toString('hex')}.tmp`);
|
|
86
|
+
if (dirname(resolve(configuredTemporaryPath)) !== dirname(configuredControllerPath)) {
|
|
84
87
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'unix_controller_claim_outside_directory');
|
|
85
88
|
}
|
|
89
|
+
const temporaryPath = join(controllerDirectory, basename(configuredTemporaryPath));
|
|
86
90
|
const ownershipReceiptPath = options.replaceExisting === false && options.artifactClaimPath !== undefined
|
|
87
91
|
? bootstrapClaimOwnershipReceiptPath(temporaryPath)
|
|
88
92
|
: undefined;
|
|
89
93
|
if (ownershipReceiptPath && !options.onArtifactPublished) {
|
|
90
94
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'bootstrap_controller_claim_has_no_durable_owner_callback');
|
|
91
95
|
}
|
|
96
|
+
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, undefined, paths.stateRootIdentity);
|
|
97
|
+
const notifyPublished = () => observeArtifactPublicationAliases(paths, () => options.onArtifactPublished?.(configuredControllerPath, configuredTemporaryPath));
|
|
92
98
|
let retainClaimOnFailure = options.replaceExisting === false && options.artifactClaimPath !== undefined;
|
|
93
99
|
try {
|
|
100
|
+
await assertStateRootIdentity(paths);
|
|
94
101
|
const loadedJournal = await readJournal(paths.journal);
|
|
102
|
+
await assertStateRootIdentity(paths);
|
|
95
103
|
if (loadedJournal) {
|
|
96
104
|
const journal = await bindJournalToTarget(paths, loadedJournal);
|
|
97
105
|
if (!isTerminal(journal.stage) || journal.stage === 'rollback_failed') {
|
|
98
106
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.RECOVERY_REQUIRED, `controller_install_pending_${journal.stage}`);
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
|
-
await ensureSecureDirectory(paths.
|
|
109
|
+
await ensureSecureDirectory(paths.stateRoot);
|
|
102
110
|
await ensureSecureDirectory(controllerDirectory);
|
|
103
111
|
if (options.replaceExisting === false) {
|
|
104
112
|
try {
|
|
@@ -115,14 +123,14 @@ export async function provisionStableUnixRecoveryController(options) {
|
|
|
115
123
|
await writeExclusiveFile(temporaryPath, targetBytes, 0o700, ownershipReceiptPath
|
|
116
124
|
? {
|
|
117
125
|
ownershipReceiptPath,
|
|
118
|
-
onClaimCreated:
|
|
126
|
+
onClaimCreated: notifyPublished,
|
|
119
127
|
}
|
|
120
128
|
: undefined);
|
|
121
129
|
await assertSameFileIdentity(paths.targetPath, targetIdentity);
|
|
122
130
|
if (options.replaceExisting === false) {
|
|
123
131
|
await link(temporaryPath, controllerPath);
|
|
124
132
|
await syncDirectoryDurably(controllerDirectory);
|
|
125
|
-
await
|
|
133
|
+
await notifyPublished();
|
|
126
134
|
if (ownershipReceiptPath) {
|
|
127
135
|
await rm(ownershipReceiptPath, { force: true });
|
|
128
136
|
await syncDirectoryDurably(controllerDirectory);
|
|
@@ -136,13 +144,19 @@ export async function provisionStableUnixRecoveryController(options) {
|
|
|
136
144
|
await syncDirectoryDurably(controllerDirectory);
|
|
137
145
|
}
|
|
138
146
|
await chmod(controllerPath, 0o700);
|
|
147
|
+
await assertStateRootAlias(paths);
|
|
139
148
|
return controllerPath;
|
|
140
149
|
}
|
|
141
150
|
finally {
|
|
142
151
|
if (!retainClaimOnFailure) {
|
|
143
|
-
|
|
144
|
-
await
|
|
145
|
-
|
|
152
|
+
try {
|
|
153
|
+
await assertStateRootIdentity(paths);
|
|
154
|
+
if (ownershipReceiptPath)
|
|
155
|
+
await rm(ownershipReceiptPath, { force: true });
|
|
156
|
+
await assertStateRootIdentity(paths);
|
|
157
|
+
await rm(temporaryPath, { force: true });
|
|
158
|
+
}
|
|
159
|
+
catch { /* Never clean a replacement state tree after a failed publication. */ }
|
|
146
160
|
}
|
|
147
161
|
await releaseLock(paths.lock, owner).catch(() => { });
|
|
148
162
|
}
|
|
@@ -153,7 +167,7 @@ export async function bindStableUnixRecoveryController(options, processExecPath)
|
|
|
153
167
|
if (!paths) {
|
|
154
168
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.RECOVERY_REQUIRED, 'unix_controller_state_missing');
|
|
155
169
|
}
|
|
156
|
-
const controllerPath = join(paths.
|
|
170
|
+
const controllerPath = join(paths.stateRoot, UNIX_CONTROLLER_DIRECTORY, UNIX_CONTROLLER_NAME);
|
|
157
171
|
await assertRegularNonSymlink(controllerPath, 'unix_controller');
|
|
158
172
|
await assertRegularNonSymlink(paths.targetPath, 'target');
|
|
159
173
|
const [actualController, expectedController] = await Promise.all([
|
|
@@ -175,7 +189,7 @@ export async function bindStableWindowsRecoveryController(options, processExecPa
|
|
|
175
189
|
}
|
|
176
190
|
await assertRegularNonSymlink(paths.targetPath, 'target');
|
|
177
191
|
const boundController = await bindWindowsManagedExecutable({
|
|
178
|
-
stateDir: paths.
|
|
192
|
+
stateDir: paths.stateRoot,
|
|
179
193
|
executablePath: processExecPath,
|
|
180
194
|
relativePath: [WINDOWS_CONTROLLER_DIRECTORY, WINDOWS_CONTROLLER_NAME],
|
|
181
195
|
label: 'windows_controller',
|
|
@@ -200,22 +214,27 @@ export async function provisionStableWindowsRecoveryController(options, processE
|
|
|
200
214
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'windows_controller_unsupported_platform');
|
|
201
215
|
}
|
|
202
216
|
const paths = await resolveTransactionPaths(options, true);
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
const
|
|
207
|
-
if (dirname(resolve(
|
|
217
|
+
const controllerDirectory = join(paths.stateRoot, WINDOWS_CONTROLLER_DIRECTORY);
|
|
218
|
+
const controllerPath = stableWindowsRecoveryControllerPathForStateDir(paths.stateRoot);
|
|
219
|
+
const configuredControllerPath = stableWindowsRecoveryControllerPathForStateDir(paths.root);
|
|
220
|
+
const configuredTemporaryPath = options.artifactClaimPath ?? join(dirname(configuredControllerPath), `.${WINDOWS_CONTROLLER_NAME}.${randomBytes(8).toString('hex')}.tmp`);
|
|
221
|
+
if (dirname(resolve(configuredTemporaryPath)) !== dirname(configuredControllerPath)) {
|
|
208
222
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'windows_controller_claim_outside_directory');
|
|
209
223
|
}
|
|
224
|
+
const temporaryPath = join(controllerDirectory, basename(configuredTemporaryPath));
|
|
210
225
|
const ownershipReceiptPath = options.replaceExisting === false && options.artifactClaimPath !== undefined
|
|
211
226
|
? bootstrapClaimOwnershipReceiptPath(temporaryPath)
|
|
212
227
|
: undefined;
|
|
213
228
|
if (ownershipReceiptPath && !options.onArtifactPublished) {
|
|
214
229
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'bootstrap_windows_controller_claim_has_no_durable_owner_callback');
|
|
215
230
|
}
|
|
231
|
+
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, undefined, paths.stateRootIdentity);
|
|
232
|
+
const notifyPublished = () => observeArtifactPublicationAliases(paths, () => options.onArtifactPublished?.(configuredControllerPath, configuredTemporaryPath));
|
|
216
233
|
let retainClaimOnFailure = options.replaceExisting === false && options.artifactClaimPath !== undefined;
|
|
217
234
|
try {
|
|
235
|
+
await assertStateRootIdentity(paths);
|
|
218
236
|
const loadedJournal = await readJournal(paths.journal);
|
|
237
|
+
await assertStateRootIdentity(paths);
|
|
219
238
|
if (loadedJournal) {
|
|
220
239
|
const journal = await bindJournalToTarget(paths, loadedJournal);
|
|
221
240
|
if (!isTerminal(journal.stage) || journal.stage === 'rollback_failed') {
|
|
@@ -230,7 +249,7 @@ export async function provisionStableWindowsRecoveryController(options, processE
|
|
|
230
249
|
!== normalizeCanonicalSelfUpdateTargetPath(expectedSource, 'win32')) {
|
|
231
250
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'windows_controller_source_mismatch');
|
|
232
251
|
}
|
|
233
|
-
await ensureSecureDirectory(paths.
|
|
252
|
+
await ensureSecureDirectory(paths.stateRoot);
|
|
234
253
|
await ensureSecureDirectory(controllerDirectory);
|
|
235
254
|
if (options.replaceExisting === false) {
|
|
236
255
|
try {
|
|
@@ -247,7 +266,7 @@ export async function provisionStableWindowsRecoveryController(options, processE
|
|
|
247
266
|
await writeExclusiveFile(temporaryPath, targetBytes, 0o700, ownershipReceiptPath
|
|
248
267
|
? {
|
|
249
268
|
ownershipReceiptPath,
|
|
250
|
-
onClaimCreated:
|
|
269
|
+
onClaimCreated: notifyPublished,
|
|
251
270
|
}
|
|
252
271
|
: undefined);
|
|
253
272
|
await assertSameFileIdentity(paths.targetPath, targetIdentity);
|
|
@@ -264,7 +283,7 @@ export async function provisionStableWindowsRecoveryController(options, processE
|
|
|
264
283
|
if (options.replaceExisting === false) {
|
|
265
284
|
await link(temporaryPath, controllerPath);
|
|
266
285
|
await syncDirectoryDurably(controllerDirectory);
|
|
267
|
-
await
|
|
286
|
+
await notifyPublished();
|
|
268
287
|
if (ownershipReceiptPath) {
|
|
269
288
|
await rm(ownershipReceiptPath, { force: true });
|
|
270
289
|
await syncDirectoryDurably(controllerDirectory);
|
|
@@ -278,25 +297,33 @@ export async function provisionStableWindowsRecoveryController(options, processE
|
|
|
278
297
|
await syncDirectoryDurably(controllerDirectory);
|
|
279
298
|
}
|
|
280
299
|
await chmod(controllerPath, 0o700);
|
|
300
|
+
await assertStateRootAlias(paths);
|
|
281
301
|
return controllerPath;
|
|
282
302
|
}
|
|
283
303
|
finally {
|
|
284
304
|
if (!retainClaimOnFailure) {
|
|
285
|
-
|
|
286
|
-
await
|
|
287
|
-
|
|
305
|
+
try {
|
|
306
|
+
await assertStateRootIdentity(paths);
|
|
307
|
+
if (ownershipReceiptPath)
|
|
308
|
+
await rm(ownershipReceiptPath, { force: true });
|
|
309
|
+
await assertStateRootIdentity(paths);
|
|
310
|
+
await rm(temporaryPath, { force: true });
|
|
311
|
+
}
|
|
312
|
+
catch { /* Never clean a replacement state tree after a failed publication. */ }
|
|
288
313
|
}
|
|
289
314
|
await releaseLock(paths.lock, owner).catch(() => { });
|
|
290
315
|
}
|
|
291
316
|
}
|
|
292
317
|
export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
293
318
|
const paths = await resolveTransactionPaths(options, true);
|
|
294
|
-
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim);
|
|
319
|
+
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim, paths.stateRootIdentity);
|
|
295
320
|
const now = options.now ?? (() => new Date());
|
|
296
321
|
let journal;
|
|
297
322
|
let released = false;
|
|
298
323
|
try {
|
|
324
|
+
await assertStateRootIdentity(paths);
|
|
299
325
|
const loadedExisting = await readJournal(paths.journal);
|
|
326
|
+
await assertStateRootIdentity(paths);
|
|
300
327
|
const existing = loadedExisting ? await bindJournalToTarget(paths, loadedExisting) : undefined;
|
|
301
328
|
if (existing?.stage === 'rollback_failed') {
|
|
302
329
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.ROLLBACK_FAILED, 'pending_rollback_failed');
|
|
@@ -304,11 +331,16 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
304
331
|
if (existing && !isTerminal(existing.stage)) {
|
|
305
332
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.RECOVERY_REQUIRED, `pending_${existing.stage}`);
|
|
306
333
|
}
|
|
307
|
-
await
|
|
334
|
+
await assertStateRootIdentity(paths);
|
|
335
|
+
await ensureSecureDirectory(paths.stateRoot);
|
|
336
|
+
await assertStateRootIdentity(paths);
|
|
308
337
|
await ensureSecureDirectory(paths.staging);
|
|
338
|
+
await assertStateRootIdentity(paths);
|
|
309
339
|
await ensureSecureDirectory(paths.backups);
|
|
340
|
+
await assertStateRootIdentity(paths);
|
|
310
341
|
if (existing)
|
|
311
342
|
await rm(paths.journal, { force: true });
|
|
343
|
+
await assertStateRootIdentity(paths);
|
|
312
344
|
const transactionId = `${Date.now()}-${randomBytes(8).toString('hex')}`;
|
|
313
345
|
const createdAt = now().toISOString();
|
|
314
346
|
journal = {
|
|
@@ -326,10 +358,14 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
326
358
|
target_path: await canonicalLogicalTargetPath(paths.targetPath),
|
|
327
359
|
configured_target_path: paths.configuredTargetPath,
|
|
328
360
|
};
|
|
361
|
+
await assertStateRootIdentity(paths);
|
|
329
362
|
await writeJournal(paths.journal, journal);
|
|
363
|
+
await assertStateRootIdentity(paths);
|
|
330
364
|
const persist = async (stage, patch = {}) => {
|
|
331
365
|
journal = { ...journal, ...patch, stage, updated_at: now().toISOString() };
|
|
366
|
+
await assertStateRootIdentity(paths);
|
|
332
367
|
await writeJournal(paths.journal, journal);
|
|
368
|
+
await assertStateRootIdentity(paths);
|
|
333
369
|
return journal;
|
|
334
370
|
};
|
|
335
371
|
const rollback = async (failureCode) => {
|
|
@@ -342,6 +378,7 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
342
378
|
};
|
|
343
379
|
return {
|
|
344
380
|
async adoptDownloaded(download) {
|
|
381
|
+
await assertStateRootIdentity(paths);
|
|
345
382
|
const stagedName = `${journal.transaction_id}.staged`;
|
|
346
383
|
const managedPath = join(paths.staging, stagedName);
|
|
347
384
|
await moveRegularFile(download.stagedPath, managedPath, 0o700);
|
|
@@ -363,6 +400,7 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
363
400
|
await persist('verified', { verified_sha256: verifiedSha256 });
|
|
364
401
|
},
|
|
365
402
|
async install() {
|
|
403
|
+
await assertStateRootIdentity(paths);
|
|
366
404
|
if (!journal?.staged_name || !journal.verified_sha256 || journal.stage !== 'verified') {
|
|
367
405
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.RECOVERY_REQUIRED, 'install_before_verified');
|
|
368
406
|
}
|
|
@@ -375,6 +413,7 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
375
413
|
await cleanupTransactionFiles(paths, journal);
|
|
376
414
|
throw error;
|
|
377
415
|
}
|
|
416
|
+
await assertStateRootIdentity(paths);
|
|
378
417
|
const backupName = `${journal.transaction_id}.backup`;
|
|
379
418
|
const backupPath = join(paths.backups, backupName);
|
|
380
419
|
const targetStat = await assertRegularNonSymlink(paths.targetPath, 'target');
|
|
@@ -398,7 +437,7 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
398
437
|
stagedPath,
|
|
399
438
|
expectedStagedSha256: journal.verified_sha256,
|
|
400
439
|
backupPath,
|
|
401
|
-
stateDir: paths.
|
|
440
|
+
stateDir: paths.stateRoot,
|
|
402
441
|
platform: 'win32',
|
|
403
442
|
});
|
|
404
443
|
return;
|
|
@@ -443,8 +482,8 @@ export async function beginDurableSelfUpdate(targetVersion, options) {
|
|
|
443
482
|
async release() {
|
|
444
483
|
if (released)
|
|
445
484
|
return;
|
|
446
|
-
released = true;
|
|
447
485
|
await releaseLock(paths.lock, owner);
|
|
486
|
+
released = true;
|
|
448
487
|
},
|
|
449
488
|
};
|
|
450
489
|
}
|
|
@@ -457,18 +496,24 @@ export async function recoverDurableSelfUpdate(options) {
|
|
|
457
496
|
const paths = await resolveTransactionPaths(options, false);
|
|
458
497
|
if (!paths)
|
|
459
498
|
return { outcome: 'none' };
|
|
460
|
-
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim);
|
|
499
|
+
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim, paths.stateRootIdentity);
|
|
461
500
|
const now = options.now ?? (() => new Date());
|
|
462
501
|
try {
|
|
502
|
+
await assertStateRootIdentity(paths);
|
|
463
503
|
const loadedJournal = await readJournal(paths.journal);
|
|
504
|
+
await assertStateRootIdentity(paths);
|
|
464
505
|
if (!loadedJournal)
|
|
465
506
|
return { outcome: 'none' };
|
|
466
507
|
const boundJournal = await bindJournalToTarget(paths, loadedJournal);
|
|
508
|
+
await assertStateRootIdentity(paths);
|
|
467
509
|
await options.beforeJournalMutation?.();
|
|
510
|
+
await assertStateRootIdentity(paths);
|
|
468
511
|
let journal = boundJournal;
|
|
469
512
|
const persist = async (stage, patch = {}) => {
|
|
470
513
|
journal = { ...journal, ...patch, stage, updated_at: now().toISOString() };
|
|
514
|
+
await assertStateRootIdentity(paths);
|
|
471
515
|
await writeJournal(paths.journal, journal);
|
|
516
|
+
await assertStateRootIdentity(paths);
|
|
472
517
|
return journal;
|
|
473
518
|
};
|
|
474
519
|
const terminal = recoveryResultForTerminal(journal);
|
|
@@ -572,18 +617,24 @@ async function mutateRecoveredTransaction(options, mutate) {
|
|
|
572
617
|
const paths = await resolveTransactionPaths(options, false);
|
|
573
618
|
if (!paths)
|
|
574
619
|
return { outcome: 'none' };
|
|
575
|
-
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim);
|
|
620
|
+
const owner = await acquireLock(paths.lock, options.pid ?? process.pid, options.beforeStaleLockReclaim, paths.stateRootIdentity);
|
|
576
621
|
const now = options.now ?? (() => new Date());
|
|
577
622
|
try {
|
|
623
|
+
await assertStateRootIdentity(paths);
|
|
578
624
|
const loadedJournal = await readJournal(paths.journal);
|
|
625
|
+
await assertStateRootIdentity(paths);
|
|
579
626
|
if (!loadedJournal)
|
|
580
627
|
return { outcome: 'none' };
|
|
581
628
|
const boundJournal = await bindJournalToTarget(paths, loadedJournal);
|
|
629
|
+
await assertStateRootIdentity(paths);
|
|
582
630
|
await options.beforeJournalMutation?.();
|
|
631
|
+
await assertStateRootIdentity(paths);
|
|
583
632
|
let journal = boundJournal;
|
|
584
633
|
const persist = async (stage, patch = {}) => {
|
|
585
634
|
journal = { ...journal, ...patch, stage, updated_at: now().toISOString() };
|
|
635
|
+
await assertStateRootIdentity(paths);
|
|
586
636
|
await writeJournal(paths.journal, journal);
|
|
637
|
+
await assertStateRootIdentity(paths);
|
|
587
638
|
return journal;
|
|
588
639
|
};
|
|
589
640
|
return await mutate(paths, journal, persist);
|
|
@@ -618,11 +669,11 @@ async function rollbackJournal(paths, persist, failureCode, options) {
|
|
|
618
669
|
return recoveryResult(journal, 'rolled_back', !alreadyRestored);
|
|
619
670
|
}
|
|
620
671
|
async function prepareWindowsRollback(paths, current, persist, failureCode, options) {
|
|
621
|
-
const pendingPath = resolveWindowsUpdaterPaths(paths.
|
|
672
|
+
const pendingPath = resolveWindowsUpdaterPaths(paths.stateRoot).pendingPath;
|
|
622
673
|
let pendingExists = await regularFileExists(pendingPath);
|
|
623
674
|
if ((current.stage === 'install_pending' || current.stage === 'restarted') && pendingExists) {
|
|
624
675
|
const targetStillMatchesBackup = await targetMatchesBackup(paths, current);
|
|
625
|
-
await removeRegularWindowsPending(paths.
|
|
676
|
+
await removeRegularWindowsPending(paths.stateRoot);
|
|
626
677
|
pendingExists = false;
|
|
627
678
|
if (targetStillMatchesBackup) {
|
|
628
679
|
const cancelled = await persist('rolled_back', { failure_code: failureCode });
|
|
@@ -635,7 +686,7 @@ async function prepareWindowsRollback(paths, current, persist, failureCode, opti
|
|
|
635
686
|
: await persist('rollback_pending', { failure_code: failureCode });
|
|
636
687
|
if (await targetMatchesVersion(paths.targetPath, journal.from_version, options.readBackVersion)) {
|
|
637
688
|
try {
|
|
638
|
-
await removeRegularWindowsPending(paths.
|
|
689
|
+
await removeRegularWindowsPending(paths.stateRoot);
|
|
639
690
|
}
|
|
640
691
|
catch {
|
|
641
692
|
journal = await persist('rollback_failed', { failure_code: SELF_UPDATE_FAILURE_CODES.ROLLBACK_FAILED });
|
|
@@ -655,7 +706,7 @@ async function prepareWindowsRollback(paths, current, persist, failureCode, opti
|
|
|
655
706
|
operation: 'rollback',
|
|
656
707
|
targetPath: paths.targetPath,
|
|
657
708
|
backupPath: join(paths.backups, journal.backup_name),
|
|
658
|
-
stateDir: paths.
|
|
709
|
+
stateDir: paths.stateRoot,
|
|
659
710
|
helperSourcePath: options.processExecPath ?? process.execPath,
|
|
660
711
|
platform: 'win32',
|
|
661
712
|
});
|
|
@@ -757,12 +808,14 @@ async function resolveTransactionPaths(options, create) {
|
|
|
757
808
|
// journal itself binds mutations to the canonical target, while this path
|
|
758
809
|
// preserves discovery of state created through a symlinked launcher path.
|
|
759
810
|
const root = resolve(configuredStateDir ?? join(dirname(logicalTargetPath), '.evolver-update'));
|
|
811
|
+
const stateRoot = await directoryCreationPath(root, true);
|
|
760
812
|
if (create) {
|
|
761
|
-
await ensureStateRootForLock(
|
|
813
|
+
await ensureStateRootForLock(stateRoot);
|
|
762
814
|
}
|
|
763
815
|
else {
|
|
764
816
|
try {
|
|
765
817
|
await assertDirectoryNonSymlink(root);
|
|
818
|
+
await assertDirectoryNonSymlink(stateRoot);
|
|
766
819
|
}
|
|
767
820
|
catch (error) {
|
|
768
821
|
if (isErrno(error, 'ENOENT'))
|
|
@@ -770,14 +823,18 @@ async function resolveTransactionPaths(options, create) {
|
|
|
770
823
|
throw error;
|
|
771
824
|
}
|
|
772
825
|
}
|
|
826
|
+
const stateRootIdentity = await readDirectoryIdentity(stateRoot);
|
|
827
|
+
await assertStateRootAlias({ root, stateRoot, stateRootIdentity });
|
|
773
828
|
return {
|
|
774
829
|
configuredTargetPath: normalizedConfiguredTargetPath,
|
|
775
830
|
targetPath,
|
|
776
831
|
root,
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
832
|
+
stateRoot,
|
|
833
|
+
stateRootIdentity,
|
|
834
|
+
journal: join(stateRoot, 'journal.json'),
|
|
835
|
+
lock: join(stateRoot, 'update.lock'),
|
|
836
|
+
staging: join(stateRoot, 'staging'),
|
|
837
|
+
backups: join(stateRoot, 'backups'),
|
|
781
838
|
};
|
|
782
839
|
}
|
|
783
840
|
function nonBlank(value) {
|
|
@@ -789,8 +846,142 @@ function assertUnixControllerPlatform(platform) {
|
|
|
789
846
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'unix_controller_unsupported_platform');
|
|
790
847
|
}
|
|
791
848
|
}
|
|
849
|
+
async function readDirectoryIdentity(path) {
|
|
850
|
+
const info = await lstat(path, { bigint: true });
|
|
851
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
852
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'unsafe_state_directory');
|
|
853
|
+
}
|
|
854
|
+
return { dev: info.dev, ino: info.ino };
|
|
855
|
+
}
|
|
856
|
+
async function assertDirectoryIdentity(path, expected) {
|
|
857
|
+
let current;
|
|
858
|
+
try {
|
|
859
|
+
current = await readDirectoryIdentity(path);
|
|
860
|
+
}
|
|
861
|
+
catch (error) {
|
|
862
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_root_changed', { cause: error });
|
|
863
|
+
}
|
|
864
|
+
if (current.dev !== expected.dev || current.ino !== expected.ino) {
|
|
865
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_root_changed');
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
async function assertStateRootIdentity(paths) {
|
|
869
|
+
await assertDirectoryIdentity(paths.stateRoot, paths.stateRootIdentity);
|
|
870
|
+
}
|
|
871
|
+
async function captureArtifactAliasWitness(path) {
|
|
872
|
+
const info = await lstat(path, { bigint: true });
|
|
873
|
+
return {
|
|
874
|
+
path, dev: info.dev, ino: info.ino, ctimeNs: info.ctimeNs, mtimeNs: info.mtimeNs,
|
|
875
|
+
resolvedPath: normalizeCanonicalSelfUpdateTargetPath(await realpath(path)),
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
async function captureArtifactAliasWitnesses(paths) {
|
|
879
|
+
const names = new Set([paths.stateRoot]);
|
|
880
|
+
for (let path = dirname(paths.root);;) {
|
|
881
|
+
const info = await lstat(path);
|
|
882
|
+
// Sibling BootstrapJournal writes are legitimate callback work. Observe the
|
|
883
|
+
// bound root and actual links, not unrelated changes to their parent entries.
|
|
884
|
+
if (info.isSymbolicLink())
|
|
885
|
+
names.add(path);
|
|
886
|
+
const parent = dirname(path);
|
|
887
|
+
if (parent === path)
|
|
888
|
+
break;
|
|
889
|
+
path = parent;
|
|
890
|
+
}
|
|
891
|
+
return Promise.all([...names].map(captureArtifactAliasWitness));
|
|
892
|
+
}
|
|
893
|
+
async function observeArtifactPublicationAliases(paths, callback) {
|
|
894
|
+
const aliases = await captureArtifactAliasWitnesses(paths);
|
|
895
|
+
const watchers = [], closed = [];
|
|
896
|
+
let renamed = false, uncertain = false;
|
|
897
|
+
try {
|
|
898
|
+
for (const alias of aliases) {
|
|
899
|
+
const name = basename(alias.path);
|
|
900
|
+
const watcher = watch(dirname(alias.path), { persistent: false }, (event, file) => {
|
|
901
|
+
if (file === null) {
|
|
902
|
+
uncertain = true;
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
const current = String(file);
|
|
906
|
+
if (event === 'rename' && (process.platform === 'win32' ? current.toLowerCase() === name.toLowerCase() : current === name))
|
|
907
|
+
renamed = true;
|
|
908
|
+
});
|
|
909
|
+
watchers.push(watcher);
|
|
910
|
+
closed.push(new Promise(resolve => watcher.once('close', resolve)));
|
|
911
|
+
watcher.on('error', () => { uncertain = true; });
|
|
912
|
+
}
|
|
913
|
+
// Watch only the bound names: legitimate sibling journal writes do not invalidate the callback.
|
|
914
|
+
await assertStateRootAlias(paths);
|
|
915
|
+
await assertArtifactAliasWitnesses(aliases);
|
|
916
|
+
await callback();
|
|
917
|
+
await assertStateRootAlias(paths);
|
|
918
|
+
await assertArtifactAliasWitnesses(aliases);
|
|
919
|
+
// Drain the event-loop turn as well as metadata I/O; Windows directory times can remain unchanged on rename-back.
|
|
920
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
921
|
+
if (renamed)
|
|
922
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_alias_changed_during_callback');
|
|
923
|
+
if (uncertain)
|
|
924
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_alias_observation_unconfirmed');
|
|
925
|
+
}
|
|
926
|
+
finally {
|
|
927
|
+
for (const watcher of watchers)
|
|
928
|
+
watcher.close();
|
|
929
|
+
await Promise.all(closed);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
async function assertArtifactAliasWitnesses(expected) {
|
|
933
|
+
for (const witness of expected) {
|
|
934
|
+
const current = await captureArtifactAliasWitness(witness.path);
|
|
935
|
+
if (current.dev !== witness.dev || current.ino !== witness.ino || current.ctimeNs !== witness.ctimeNs
|
|
936
|
+
|| current.mtimeNs !== witness.mtimeNs || current.resolvedPath !== witness.resolvedPath) {
|
|
937
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_alias_changed_during_callback');
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
async function assertStateRootAlias(paths) {
|
|
942
|
+
await assertStateRootIdentity(paths);
|
|
943
|
+
if (normalizeCanonicalSelfUpdateTargetPath(await realpath(paths.root))
|
|
944
|
+
!== normalizeCanonicalSelfUpdateTargetPath(await realpath(paths.stateRoot))) {
|
|
945
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_parent_changed');
|
|
946
|
+
}
|
|
947
|
+
await assertStateRootIdentity(paths);
|
|
948
|
+
}
|
|
949
|
+
async function directoryCreationPath(path, resolveParent = process.platform === 'win32') {
|
|
950
|
+
if (!resolveParent)
|
|
951
|
+
return path;
|
|
952
|
+
// Resolve existing parents, not the leaf: directory links can reject mkdir
|
|
953
|
+
// even while ordinary reads and writes through the same link succeed.
|
|
954
|
+
let parent = dirname(resolve(path));
|
|
955
|
+
const missing = [basename(path)];
|
|
956
|
+
let physicalParent;
|
|
957
|
+
for (;;) {
|
|
958
|
+
try {
|
|
959
|
+
physicalParent = await realpath(parent);
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
catch (error) {
|
|
963
|
+
const ancestor = dirname(parent);
|
|
964
|
+
if (!isErrno(error, 'ENOENT') || ancestor === parent)
|
|
965
|
+
throw error;
|
|
966
|
+
missing.unshift(basename(parent));
|
|
967
|
+
parent = ancestor;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
return join(physicalParent, ...missing);
|
|
971
|
+
}
|
|
972
|
+
async function createStateDirectory(path) {
|
|
973
|
+
const physical = await directoryCreationPath(path);
|
|
974
|
+
await mkdir(physical, { recursive: true, mode: 0o700 });
|
|
975
|
+
if (process.platform !== 'win32')
|
|
976
|
+
return;
|
|
977
|
+
await assertDirectoryNonSymlink(physical);
|
|
978
|
+
if (normalizeCanonicalSelfUpdateTargetPath(await realpath(path))
|
|
979
|
+
!== normalizeCanonicalSelfUpdateTargetPath(await realpath(physical))) {
|
|
980
|
+
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'state_parent_changed');
|
|
981
|
+
}
|
|
982
|
+
}
|
|
792
983
|
async function ensureSecureDirectory(path) {
|
|
793
|
-
await
|
|
984
|
+
await createStateDirectory(path);
|
|
794
985
|
await assertDirectoryNonSymlink(path);
|
|
795
986
|
await chmod(path, 0o700);
|
|
796
987
|
}
|
|
@@ -803,7 +994,7 @@ async function ensureStateRootForLock(path) {
|
|
|
803
994
|
if (!isErrno(error, 'ENOENT'))
|
|
804
995
|
throw error;
|
|
805
996
|
}
|
|
806
|
-
await
|
|
997
|
+
await createStateDirectory(path);
|
|
807
998
|
await assertDirectoryNonSymlink(path);
|
|
808
999
|
await chmod(path, 0o700);
|
|
809
1000
|
}
|
|
@@ -905,7 +1096,7 @@ async function readRegularFile(source) {
|
|
|
905
1096
|
}
|
|
906
1097
|
}
|
|
907
1098
|
async function writeExclusiveFile(destination, bytes, mode, publication) {
|
|
908
|
-
const stagingPath = publication?.ownershipReceiptPath ?? destination;
|
|
1099
|
+
const stagingPath = await directoryCreationPath(publication?.ownershipReceiptPath ?? destination);
|
|
909
1100
|
if (publication
|
|
910
1101
|
&& dirname(resolve(publication.ownershipReceiptPath)) !== dirname(resolve(destination))) {
|
|
911
1102
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'bootstrap_controller_ownership_receipt_outside_directory');
|
|
@@ -922,16 +1113,16 @@ async function writeExclusiveFile(destination, bytes, mode, publication) {
|
|
|
922
1113
|
await syncDirectoryDurably(dirname(destination));
|
|
923
1114
|
const [opened, receipt] = await Promise.all([
|
|
924
1115
|
destinationHandle.stat({ bigint: true }),
|
|
925
|
-
|
|
1116
|
+
selfUpdateFileMetadata(publication.ownershipReceiptPath, constants.O_WRONLY),
|
|
926
1117
|
]);
|
|
927
1118
|
if (!opened.isFile() || opened.dev <= 0n || opened.ino <= 0n
|
|
928
1119
|
|| receipt.dev !== opened.dev || receipt.ino !== opened.ino) {
|
|
929
1120
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'bootstrap_controller_ownership_receipt_mismatch');
|
|
930
1121
|
}
|
|
931
|
-
await link(
|
|
1122
|
+
await link(stagingPath, await directoryCreationPath(destination));
|
|
932
1123
|
await syncDirectoryDurably(dirname(destination));
|
|
933
1124
|
claimPublished = true;
|
|
934
|
-
const claim = await
|
|
1125
|
+
const claim = await selfUpdateFileMetadata(destination, constants.O_WRONLY);
|
|
935
1126
|
if (claim.dev !== opened.dev || claim.ino !== opened.ino) {
|
|
936
1127
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'bootstrap_controller_claim_receipt_mismatch');
|
|
937
1128
|
}
|
|
@@ -947,6 +1138,7 @@ async function writeExclusiveFile(destination, bytes, mode, publication) {
|
|
|
947
1138
|
}
|
|
948
1139
|
}
|
|
949
1140
|
async function restoreBackup(paths, journal, force = false) {
|
|
1141
|
+
await assertStateRootIdentity(paths);
|
|
950
1142
|
if (!journal.backup_name)
|
|
951
1143
|
return;
|
|
952
1144
|
const backupPath = join(paths.backups, journal.backup_name);
|
|
@@ -972,12 +1164,16 @@ async function restoreBackup(paths, journal, force = false) {
|
|
|
972
1164
|
await rename(restoreTmp, paths.targetPath);
|
|
973
1165
|
}
|
|
974
1166
|
async function cleanupTransactionFiles(paths, journal) {
|
|
1167
|
+
await assertStateRootIdentity(paths);
|
|
975
1168
|
if (journal.staged_name)
|
|
976
1169
|
await rm(join(paths.staging, journal.staged_name), { force: true }).catch(() => { });
|
|
1170
|
+
await assertStateRootIdentity(paths);
|
|
977
1171
|
if (journal.backup_name)
|
|
978
1172
|
await rm(join(paths.backups, journal.backup_name), { force: true }).catch(() => { });
|
|
1173
|
+
await assertStateRootIdentity(paths);
|
|
979
1174
|
}
|
|
980
1175
|
async function writeJournal(path, journal) {
|
|
1176
|
+
path = await directoryCreationPath(path);
|
|
981
1177
|
const tmp = `${path}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
|
|
982
1178
|
await writeFile(tmp, `${JSON.stringify(journal)}\n`, { flag: 'wx', mode: 0o600 });
|
|
983
1179
|
await rename(tmp, path);
|
|
@@ -986,7 +1182,7 @@ async function writeJournal(path, journal) {
|
|
|
986
1182
|
async function readJournal(path) {
|
|
987
1183
|
let journalHandle;
|
|
988
1184
|
try {
|
|
989
|
-
const pathInfo = await
|
|
1185
|
+
const pathInfo = await selfUpdateFileMetadata(path);
|
|
990
1186
|
if (pathInfo.isSymbolicLink() || !pathInfo.isFile()) {
|
|
991
1187
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'unsafe_journal');
|
|
992
1188
|
}
|
|
@@ -998,14 +1194,14 @@ async function readJournal(path) {
|
|
|
998
1194
|
throw error;
|
|
999
1195
|
}
|
|
1000
1196
|
try {
|
|
1001
|
-
const info = await journalHandle.stat();
|
|
1002
|
-
const currentPathInfo = await
|
|
1197
|
+
const info = await journalHandle.stat({ bigint: true });
|
|
1198
|
+
const currentPathInfo = await selfUpdateFileMetadata(path);
|
|
1003
1199
|
if (!info.isFile()
|
|
1004
1200
|
|| currentPathInfo.isSymbolicLink()
|
|
1005
1201
|
|| !currentPathInfo.isFile()
|
|
1006
|
-
||
|
|
1007
|
-
||
|
|
1008
|
-
|| info.size > MAX_JOURNAL_BYTES) {
|
|
1202
|
+
|| currentPathInfo.dev !== info.dev
|
|
1203
|
+
|| currentPathInfo.ino !== info.ino
|
|
1204
|
+
|| info.size > BigInt(MAX_JOURNAL_BYTES)) {
|
|
1009
1205
|
throw selfUpdateFailure(SELF_UPDATE_FAILURE_CODES.UNSAFE_UPDATE_PATH, 'unsafe_journal');
|
|
1010
1206
|
}
|
|
1011
1207
|
const parsed = JSON.parse(await journalHandle.readFile('utf8'));
|
|
@@ -1065,15 +1261,21 @@ function isJournalStage(value) {
|
|
|
1065
1261
|
function isTerminal(stage) {
|
|
1066
1262
|
return stage === 'confirmed' || stage === 'rolled_back';
|
|
1067
1263
|
}
|
|
1068
|
-
async function acquireLock(path, pid, beforeStaleLockReclaim) {
|
|
1264
|
+
async function acquireLock(path, pid, beforeStaleLockReclaim, expectedRootIdentity) {
|
|
1265
|
+
path = await directoryCreationPath(path);
|
|
1266
|
+
const rootDirectory = dirname(path);
|
|
1267
|
+
const rootIdentity = expectedRootIdentity ?? await readDirectoryIdentity(rootDirectory);
|
|
1268
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1069
1269
|
const owner = { pid, token: randomBytes(16).toString('hex') };
|
|
1070
1270
|
const visited = new Set();
|
|
1071
1271
|
let generationPath = path;
|
|
1072
1272
|
while (true) {
|
|
1273
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1073
1274
|
const existing = await readLockGeneration(generationPath);
|
|
1074
1275
|
if (!existing) {
|
|
1075
|
-
if (await publishLockGeneration(path, generationPath, owner)) {
|
|
1076
|
-
|
|
1276
|
+
if (await publishLockGeneration(path, generationPath, owner, rootIdentity)) {
|
|
1277
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1278
|
+
return { ...owner, rootPath: path, rootIdentity, generationPath };
|
|
1077
1279
|
}
|
|
1078
1280
|
continue;
|
|
1079
1281
|
}
|
|
@@ -1089,8 +1291,11 @@ async function acquireLock(path, pid, beforeStaleLockReclaim) {
|
|
|
1089
1291
|
generationPath = successorLockGenerationPath(path, existing.owner.token);
|
|
1090
1292
|
}
|
|
1091
1293
|
}
|
|
1092
|
-
async function releaseLock(
|
|
1294
|
+
async function releaseLock(_path, lease) {
|
|
1295
|
+
const path = lease.rootPath;
|
|
1296
|
+
await assertDirectoryIdentity(dirname(path), lease.rootIdentity);
|
|
1093
1297
|
const current = await readLockGeneration(lease.generationPath);
|
|
1298
|
+
await assertDirectoryIdentity(dirname(path), lease.rootIdentity);
|
|
1094
1299
|
if (current?.owner.pid !== lease.pid || current.owner.token !== lease.token)
|
|
1095
1300
|
return;
|
|
1096
1301
|
try {
|
|
@@ -1101,14 +1306,19 @@ async function releaseLock(path, lease) {
|
|
|
1101
1306
|
throw error;
|
|
1102
1307
|
await lockGenerationReleased(path, lease.token);
|
|
1103
1308
|
}
|
|
1309
|
+
await assertDirectoryIdentity(dirname(path), lease.rootIdentity);
|
|
1104
1310
|
}
|
|
1105
|
-
async function publishLockGeneration(rootPath, generationPath, owner) {
|
|
1311
|
+
async function publishLockGeneration(rootPath, generationPath, owner, rootIdentity) {
|
|
1312
|
+
const rootDirectory = dirname(rootPath);
|
|
1106
1313
|
const candidatePath = `${rootPath}.${owner.token}.candidate`;
|
|
1107
1314
|
let candidateCreated = false;
|
|
1108
1315
|
try {
|
|
1316
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1109
1317
|
await mkdir(candidatePath, { mode: 0o700 });
|
|
1110
1318
|
candidateCreated = true;
|
|
1319
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1111
1320
|
await writeExclusiveFile(join(candidatePath, 'owner.json'), Buffer.from(`${JSON.stringify(owner)}\n`), 0o600);
|
|
1321
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1112
1322
|
try {
|
|
1113
1323
|
// A fully populated, non-empty directory is published atomically. Unlike
|
|
1114
1324
|
// renaming the stale shared lock itself, a losing rename cannot replace a
|
|
@@ -1130,7 +1340,11 @@ async function publishLockGeneration(rootPath, generationPath, owner) {
|
|
|
1130
1340
|
}
|
|
1131
1341
|
finally {
|
|
1132
1342
|
if (candidateCreated) {
|
|
1133
|
-
|
|
1343
|
+
try {
|
|
1344
|
+
await assertDirectoryIdentity(rootDirectory, rootIdentity);
|
|
1345
|
+
await rm(candidatePath, { force: true, recursive: true });
|
|
1346
|
+
}
|
|
1347
|
+
catch { /* Retain candidates when their original state tree is unavailable. */ }
|
|
1134
1348
|
}
|
|
1135
1349
|
}
|
|
1136
1350
|
}
|
|
@@ -1164,15 +1378,15 @@ async function readLockGeneration(path) {
|
|
|
1164
1378
|
async function readLockOwner(path) {
|
|
1165
1379
|
let handle;
|
|
1166
1380
|
try {
|
|
1167
|
-
const before = await
|
|
1168
|
-
if (before.isSymbolicLink() || !before.isFile() || before.size >
|
|
1381
|
+
const before = await selfUpdateFileMetadata(path);
|
|
1382
|
+
if (before.isSymbolicLink() || !before.isFile() || before.size > 4096n)
|
|
1169
1383
|
return undefined;
|
|
1170
1384
|
handle = await open(path, constants.O_RDONLY | noFollowFlag());
|
|
1171
|
-
const opened = await handle.stat();
|
|
1385
|
+
const opened = await handle.stat({ bigint: true });
|
|
1172
1386
|
if (!opened.isFile()
|
|
1173
|
-
|| opened.size >
|
|
1174
|
-
||
|
|
1175
|
-
||
|
|
1387
|
+
|| opened.size > 4096n
|
|
1388
|
+
|| opened.dev !== before.dev
|
|
1389
|
+
|| opened.ino !== before.ino)
|
|
1176
1390
|
return undefined;
|
|
1177
1391
|
const bytes = await handle.readFile();
|
|
1178
1392
|
if (bytes.byteLength > 4_096)
|