Skip to content

safe-write-file: truncate-then-write 可能导致文件变为空文件 #67

Description

@luojiyin1987

问题

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 可靠性条目。

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions