fix(patcher): replace built game files atomically with retry

Write rebuilt game files to sibling temporary files, flush to disk, and replace targets via rename with bounded retries on transient EPERM/EBUSY/EACCES errors (clearing read-only attributes when needed). This prevents antivirus scans or interrupted writes from leaving missing or truncated client files while keeping hard-linked stock files untouched.

Checked: root typecheck, launcher typecheck, and atomic replacement runtime check on read-only and new files.
This commit is contained in:
N0VAN0M4LY committed 2026-12-02 07:20:43 +08:00
1 parent 1c6f5c9fc4
commit 90bd5a315d
1 file changed
+49 -8
+49 -8
View File
@@ -1,20 +1,61 @@
/**
* Write the built files into the game copy folder.
* Write the built files into the game copy folder atomically so transient antivirus locks or
* interrupted writes never leave a missing or truncated game file.
*/
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { randomBytes } from 'node:crypto';
import { chmodSync, closeSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import type { GameCopyFile } from './build-game-copy.ts';
const TRANSIENT_REPLACE_ERRORS = new Set(['EPERM', 'EBUSY', 'EACCES']);
const MAX_REPLACE_ATTEMPTS = 8;
const WAIT_BUFFER = new Int32Array(new SharedArrayBuffer(4));
/** Writes every file and returns the full paths written, in order. */
export function writeGameCopy(copyFolder: string, files: readonly GameCopyFile[]): string[] {
return files.map((file) => {
const path = join(copyFolder, file.path);
mkdirSync(dirname(path), { recursive: true });
// Remove before writing: a read-only file is replaced instead of failing with EPERM, and a
// file hard-linked to the stock folder gets a new file of its own instead of changing the
// stock one through the link.
rmSync(path, { force: true });
writeFileSync(path, file.content);
writeFileAtomically(path, file.content);
return path;
});
}
function writeFileAtomically(path: string, content: Uint8Array): void {
mkdirSync(dirname(path), { recursive: true });
// Replacing via rename keeps hard-linked stock files untouched by giving the copy its own inode,
// and avoids deleting the target before the replacement is fully flushed to disk.
const temporary = `${path}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
const fd = openSync(temporary, 'w');
try {
writeFileSync(fd, content);
fsyncSync(fd);
} catch (error) {
try { closeSync(fd); } catch { /* ignore */ }
try { rmSync(temporary, { force: true }); } catch { /* ignore */ }
throw error;
}
closeSync(fd);
try {
renameWithRetry(temporary, path);
} catch (error) {
try { rmSync(temporary, { force: true }); } catch { /* ignore */ }
throw error;
}
}
function renameWithRetry(source: string, target: string): void {
for (let attempt = 1; ; attempt += 1) {
try {
renameSync(source, target);
return;
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (attempt >= MAX_REPLACE_ATTEMPTS || !code || !TRANSIENT_REPLACE_ERRORS.has(code)) throw error;
// A read-only target fails rename with EPERM/EACCES on Windows; clear the attribute before retrying.
if (code === 'EPERM' || code === 'EACCES') {
try { chmodSync(target, 0o666); } catch { /* ignore */ }
}
Atomics.wait(WAIT_BUFFER, 0, 0, attempt * 30);
}
}
}