fix(safe-write-file): replace truncate-then-write with write-then-truncate#68
Merged
Conversation
…ncate Keep in-place update semantics to preserve inode, hard link, birthtime, owner, ACL, xattr etc. Write from offset 0 first, then truncate to new length, avoiding the truncate(0) worst case where a failed write leaves an empty file. Add handle.sync() to request content and size metadata flush. Add mode preservation and tail truncation tests.
511b721 to
78937f8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #67
变更
O_WRONLY改为O_RDWR,继续使用O_NOFOLLOWtruncate(0) → write改为write from offset 0 → truncate(newLength)handle.write()改为循环写入,不假设一次写完全部 bufferhandle.sync(),请求将文件内容和 size 元数据刷盘改进点
消除
truncate(0)成功但写入失败时,目标文件变成空文件的最坏情况。相比 temp-file + rename,本方案保留 in-place update 语义,更符合
lint-md --fix对修改原文件的用户预期。它能保留 inode / hard link / owner / ACL / xattr / birthtime 等文件身份相关属性;mtime / ctime 会按正常文件修改语义更新。仍不保证
测试