diff --git a/lib/core.js b/lib/core.js index ad4ce92..3105f5d 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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) {