问题
safe-write-file.ts 当前使用 truncate-then-write 模式:
await handle.truncate(0);
await handle.writeFile(content, 'utf8');
truncate(0) 成功后、writeFile 完成前若发生崩溃或 I/O 错误,
目标文件变为空文件,原始内容无法恢复。
方案
改为 write-then-truncate:先从 offset 0 写入完整新内容,再截断到新长度。
保留 in-place update 语义,避免 temp-file + rename 改变 inode 等文件身份
属性。消除空文件这一最坏失败模式。
参见 docs/performance-security-analysis.md P2 可靠性条目。
问题
safe-write-file.ts当前使用 truncate-then-write 模式:truncate(0)成功后、writeFile完成前若发生崩溃或 I/O 错误,目标文件变为空文件,原始内容无法恢复。
方案
改为 write-then-truncate:先从 offset 0 写入完整新内容,再截断到新长度。
保留 in-place update 语义,避免 temp-file + rename 改变 inode 等文件身份
属性。消除空文件这一最坏失败模式。
参见 docs/performance-security-analysis.md P2 可靠性条目。