Skip to content

fix(safe-write-file): atomic replace via temp file + rename#66

Closed
luojiyin1987 wants to merge 1 commit into
masterfrom
fix/safe-write-atomic
Closed

fix(safe-write-file): atomic replace via temp file + rename#66
luojiyin1987 wants to merge 1 commit into
masterfrom
fix/safe-write-atomic

Conversation

@luojiyin1987

Copy link
Copy Markdown
Contributor

Closes #65

变更

  • safe-write-file.ts: 将 truncate-then-write 模式替换为 write-to-temp + rename 原子替换。保留 O_NOFOLLOW 符号链接保护(open() 仍为第一步),保留原文件 mode。
  • symlink-security.spec.ts: 新增 2 项测试:
    • preserves original file mode — 写入后 mode 与原始一致
    • leaves no temp file residue on success — 无 .lint-md-*.tmp 残留

不覆盖(明确在范围外)

  • 不提供 fsync 掉电持久性
  • 不保留 owner、ACL、xattr 等其他元数据
  • 不提供跨文件事务保证

验证

  • 39 项测试通过,lint 和 build 通过
  • safe-write-file.ts 覆盖率 93.75%

Replace truncate-then-write with write-to-temp + rename pattern.
O_NOFOLLOW symlink protection preserved by opening target first.
Explicit chmod after writeFile prevents umask truncating file mode.
Restructured try/finally to always close the target handle.

Not in scope: fsync, owner/ACL/xattr, cross-file transactions.
@luojiyin1987 luojiyin1987 force-pushed the fix/safe-write-atomic branch from 8ea2936 to eef3a88 Compare July 5, 2026 15:52
@luojiyin1987

Copy link
Copy Markdown
Contributor Author

我觉得保留文件创建时间很重要,放弃该pr

@luojiyin1987 luojiyin1987 deleted the fix/safe-write-atomic branch July 5, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

safe-write-file: truncate-then-write 非原子写入可能导致文件损坏

1 participant