fix(core): atomicWrite 别走 fsp.open / fsyncSync / closeSync

hostFs.promises 只包了 readFile/writeFile/mkdir/unlink/rm/copyFile,没 open;
hostFs 的同步侧也没 fsyncSync/closeSync。启动种子调 atomicWriteFile 立刻
抛 TypeError: fsp.open is not a function,applyBoot 整个倒下。

现在 atomicWriteSync/atomicWriteFile 只用 writeFile+rename,原子性靠 rename
(POSIX / Windows MoveFileEx),断电丢最新一次写入可接受,下一次 apply 补。
atomicCopyFile 原本就只用 copyFile+rename,不受影响。
This commit is contained in:
叫我小杨同学 committed 2026-10-09 12:16:55 +08:00
1 parent 1726711f52
commit 19f5e26b84
1 file changed
+5 -15
+5 -15
View File
@@ -43,18 +43,14 @@ const HIDDEN_EXEC = {
timeout: 8000,
};
// H19:原子写。write-to-tmp + fsync + rename。断电或 EBUSY 都不会半写坏原文件。
// H19:原子写。write-to-tmp + rename。EBUSY/ENOSPC 都不会半写坏原文件。
// 不走 FileHandle:hostFs.promises 只包了 readFile/writeFile/mkdir/unlink/rm/copyFile,无 open。
// 不显式 fsync:原子性靠 rename;KB 级配置文件断电丢最新一次写入可接受,下一次 apply 会补。
export function atomicWriteSync(fp, content, encoding = "utf8") {
const dir = path.dirname(fp);
fs.mkdirSync(dir, { recursive: true });
const tmp = `${fp}.${process.pid}.${Date.now()}.tmp`;
const fd = fs.openSync(tmp, "w");
try {
fs.writeFileSync(fd, content, encoding);
try { fs.fsyncSync(fd); } catch { /* FS 不支持 fsync 时忽略 */ }
} finally {
try { fs.closeSync(fd); } catch { /* ignore */ }
}
fs.writeFileSync(tmp, content, encoding);
try {
fs.renameSync(tmp, fp);
} catch (e) {
@@ -67,13 +63,7 @@ export async function atomicWriteFile(fp, content, encoding = "utf8") {
const dir = path.dirname(fp);
await fsp.mkdir(dir, { recursive: true });
const tmp = `${fp}.${process.pid}.${Date.now()}.tmp`;
const handle = await fsp.open(tmp, "w");
try {
await handle.writeFile(content, encoding);
try { await handle.sync(); } catch { /* ignore */ }
} finally {
try { await handle.close(); } catch { /* ignore */ }
}
await fsp.writeFile(tmp, content, encoding);
try {
await fsp.rename(tmp, fp);
} catch (e) {