Skip to content

Commit 823acfe

Browse files
author
easonLiangWorldedtech
committed
fix(file-safety): inherit unit 1 committed guard and exact rmdir assertion
This unit was rebuilt from the pre-fix content source, so its copy of safeWriteText.ts still restored the backup over a write whose commit rename had already succeeded when the parent-directory fsync failed, and its spec asserted rmdir generically rather than against the staging directory this write created. Both are already settled in unit 1 (fws/u1-atomic-publish). Taking those files here keeps the shared code byte-identical across the units, so merging the chain in order does not overwrite unit 1's fix. Tests: 52 passed in safeWriteText.spec.ts.
1 parent d7eab3d commit 823acfe

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ async function _restoreDaclWindows(dirPath: string, dumpPath: string, execFileRu
167167
*/
168168
export async function resolvePublishTarget(absoluteFilePath: string): Promise<string> {
169169
return fs.realpath(absoluteFilePath).catch(async (error: unknown) => {
170-
const code =
171-
typeof error === "object" && error !== null && "code" in error
172-
? (error as { code?: string }).code
173-
: undefined
174-
if (code !== "ENOENT") throw error
170+
if (errorCode(error) !== "ENOENT") throw error
175171
// ENOENT also covers a dangling symlink, which must never be written through.
176172
// Only a lstat that also reports the path as absent may fall back to the
177173
// given path; a real lstat failure (EACCES, EIO) says nothing about whether
@@ -289,6 +285,10 @@ export async function safeWriteText(
289285

290286
let backupPath: string | null = null
291287
let releaseBackupOnSuccess = false
288+
// Set once the commit rename has published the new content. After that point the
289+
// backup is no longer a safe restore source: rolling it back would overwrite
290+
// content the caller can already observe at the target path.
291+
let committed = false
292292
// Non-null only when the win32 step-2 block saved a successful DACL dump:
293293
// it gates the step-5 restore and is tracked for the cleanup unlinks.
294294
let daclDumpPath: string | null = null
@@ -397,16 +397,13 @@ export async function safeWriteText(
397397
await fs.rename(targetPath, backupPath)
398398
releaseBackupOnSuccess = true
399399
} catch (err: unknown) {
400-
const code =
401-
typeof err === "object" && err !== null && "code" in err
402-
? (err as { code?: string }).code
403-
: undefined
404-
if (code !== "ENOENT") throw err
400+
if (errorCode(err) !== "ENOENT") throw err
405401
}
406402
}
407403

408404
// -- Step 4: atomic rename temp -> target ---------------------
409405
await fs.rename(tempPath, targetPath)
406+
committed = true
410407

411408
// -- Step 4b (POSIX): fsync the parent directory so the directory entry
412409
// changed by the commit rename is durable, not just the file content.
@@ -463,7 +460,10 @@ export async function safeWriteText(
463460
await fs.rmdir(stagingDir).catch(() => {})
464461
}
465462
} catch (originalError: unknown) {
466-
if (backupPath && releaseBackupOnSuccess) {
463+
// Only a pre-commit failure can restore the backup. Once the commit rename
464+
// published, a later failure (for example the post-commit directory fsync)
465+
// must not overwrite the published content with the old file.
466+
if (backupPath && releaseBackupOnSuccess && !committed) {
467467
try {
468468
await fs.rename(backupPath, targetPath)
469469
} catch (rollbackError: unknown) {

0 commit comments

Comments
 (0)