Skip to content

Commit 1871993

Browse files
Read the staged bytes after staging, where the escape undo can use them
The escape-undo capture read the staged temp before _streamDataToFile had written it, so every confined write that reached the publish failed with `ENOENT: no such file or directory, open '<dir>/.<name>.new_<ts>_<rand>.tmp'`. CI's ubuntu leg name is visible in the misc block of platform-unit-test (ubuntu-latest) at e3d6327 on "confines a write whose symlink referent stays inside the confined directory" and "confines a scope path that itself runs through a symlink and does not exist yet". Windows never showed it: both rows are test.skipIf(process.platform === "win32"), and fs.symlinkSync of a file fails EPERM on this box (measured, pinned node v22.23.4), so no local run of the misc shard executes them - local tallies read 2116 passed / 17 skipped before and after. The read now sits after the staging write and before any mode handling, so the bytes compared by a post-commit escape undo are the bytes that were staged and a restrictive target mode cannot turn the capture into a failure. A read failure still aborts the write, which is what the surrounding comment already required. At this commit the third ubuntu failure - "preserves a restrictive 0o600 target mode through the atomic publish", expected 420 to be 384 - is still red by design: it is a separate mechanism and the next commit is the one that answers it. Gates on this commit's tree, pinned node v22.23.4 (box node 25.9.0): misc exit 0 with 2116 passed / 17 skipped (unchanged tallies, and it cannot prove the two rows above); tsc -p src/tsconfig.json exit 0; prettier --check clean; eslint --max-warnings=0 exit 0. Refs: Zoo-Code-Org#1375, Zoo-Code-Org#1991, Zoo-Code-Org#2019.
1 parent e3d6327 commit 1871993

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎src/utils/safeWriteJson.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,18 @@ async function safeWriteJson(filePath: string, data: unknown, options?: SafeWrit
382382
// filesystems, and mixing the two spellings is how a publish ends up beside the link
383383
// it meant to replace.
384384

385-
// The bytes this write is about to publish identify the file a post-commit escape
386-
// undo may remove. They are only needed when confinement can trigger that undo, and
387-
// reading them here keeps the comparison independent of serialization details.
385+
await _streamDataToFile(actualTempNewFilePath, data, options?.prettyPrint)
386+
387+
// The bytes this write publishes identify the file a post-commit escape undo may
388+
// remove. They are read after the staged file is written - before it there is nothing
389+
// to read, and reading early failed with ENOENT on every confined write that reached
390+
// the publish - and before the mode copy below, so a restrictive target mode cannot
391+
// turn this read into a failure. A read failure aborts the write: publishing without
392+
// the bytes would leave the undo unable to verify (and remove) an escaped file.
388393
if (confinementRoot !== undefined) {
389-
// A read failure must abort the write: publishing without the bytes would make
390-
// the post-commit escape undo unable to verify (and remove) an escaped file.
391394
stagedBytes = await fs.readFile(actualTempNewFilePath)
392395
}
393396

394-
await _streamDataToFile(actualTempNewFilePath, data, options?.prettyPrint)
395397
await _flushFileToDisk(actualTempNewFilePath)
396398

397399
// Step 2: Commit the replacement with a single atomic rename. The
@@ -569,7 +571,7 @@ async function _streamDataToFile(targetPath: string, data: unknown, prettyPrint
569571
}
570572

571573
/**
572-
* Flushes a fully written temp file to storage before it is committed.
574+
* Syncs the directory that holds a freshly committed file.
573575
* Best effort: durability against power loss is a bonus here, process-crash
574576
* safety comes from the rename protocol itself, and a filesystem that
575577
* refuses fsync must not block the write.

0 commit comments

Comments
 (0)