MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / installWindowsBinaryAtomically

Function installWindowsBinaryAtomically

pkg/npm/install.js:519–596  ·  view source on GitHub ↗
(sourceDir, destDir, verifier = verifyCandidate)

Source from the content-addressed store, hash-verified

517}
518
519function installWindowsBinaryAtomically(sourceDir, destDir, verifier = verifyCandidate) {
520 // Authenticate the source binary before it can contend for the cache.
521 verifier(path.join(sourceDir, WINDOWS_BINARY_NAME));
522 const lock = acquireWindowsBinaryLock(destDir);
523 let operationError = null;
524 try {
525 // A contender may have completed while this process waited. Its executable
526 // wins and is never moved or deleted.
527 if (windowsBinaryReady(destDir, verifier)) return;
528
529 const transaction = crypto.randomBytes(16).toString('hex');
530 const stagedPaths = new Map();
531 const stagedDigests = new Map();
532 const backups = new Map();
533 const publishedDigests = new Map();
534 try {
535 for (const name of [WINDOWS_BINARY_NAME]) {
536 const staged = path.join(destDir, `.cbm-pair-stage-${transaction}-${name}`);
537 fs.copyFileSync(path.join(sourceDir, name), staged, fs.constants.COPYFILE_EXCL);
538 fs.chmodSync(staged, 0o755);
539 stagedPaths.set(name, staged);
540 stagedDigests.set(name, fileSha256(staged));
541 }
542
543 for (const name of [WINDOWS_BINARY_NAME]) {
544 const target = path.join(destDir, name);
545 const status = fs.lstatSync(target, { throwIfNoEntry: false });
546 if (!status) continue;
547 if (!status.isFile() || status.isSymbolicLink()) {
548 throw new Error(`refusing unsafe Windows package-cache target: ${target}`);
549 }
550 const backup = path.join(destDir, `.cbm-pair-backup-${transaction}-${name}`);
551 fs.renameSync(target, backup);
552 backups.set(name, backup);
553 }
554
555 for (const name of [WINDOWS_BINARY_NAME]) {
556 const target = path.join(destDir, name);
557 fs.renameSync(stagedPaths.get(name), target);
558 stagedPaths.delete(name);
559 publishedDigests.set(name, stagedDigests.get(name));
560 }
561 if (!windowsBinaryReady(destDir, verifier)) {
562 throw new Error('published Windows package-cache binary failed verification');
563 }
564 for (const backup of backups.values()) {
565 try { fs.unlinkSync(backup); } catch (_) { /* valid binary is already committed */ }
566 }
567 return;
568 } catch (err) {
569 // If a non-cooperating contender nevertheless published a valid binary,
570 // preserve that winner. Otherwise remove only our exact staged bytes and
571 // restore the prior files when their target names are still absent.
572 if (windowsBinaryReady(destDir, verifier)) {
573 for (const backup of backups.values()) {
574 try { fs.unlinkSync(backup); } catch (_) { /* winner remains authoritative */ }
575 }
576 return;

Callers 2

mainFunction · 0.85

Calls 7

acquireWindowsBinaryLockFunction · 0.85
windowsBinaryReadyFunction · 0.85
fileSha256Function · 0.85
restoreBinaryBackupsFunction · 0.85
releaseWindowsBinaryLockFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected