Repository navigation
fs: use renameatx_np on Darwin for atomic non-replacing rename - #454
Conversation
Replaces the non-atomic hardlink+delete fallback on macOS with renameatx_np(RENAME_EXCL), which is equivalent to Linux renameat2(RENAME_NOREPLACE). Also adds Dir.renamePreserve to the native API and tests for both the native and std.Io interfaces.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds Darwin ChangesRename-preserve operation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/fs.zig`:
- Around line 1155-1160: After asserting dir.renamePreserve(old_path, dir,
new_path) returns error.PathAlreadyExists, additionally verify the no-overwrite
invariant by opening/reading both paths and asserting their contents and
existence are unchanged: confirm new_path still exists and its original contents
are intact, confirm old_path still exists and contains the pre-rename data
(e.g., file2's contents), and do not delete or replace either file as part of
the failed rename. Apply the same extra checks to the parallel test in
src/io.zig so both implementations verify existence and content preservation
after the failure of renamePreserve.
In `@src/os/fs.zig`:
- Around line 1110-1124: The Darwin branch in renameatPreserve calls
darwin.renameatx_np and currently forwards unknown errno values into
errnoToDirRenameError, which causes ENOTSUP (filesystem doesn’t support
RENAME_EXCL) to become error.Unexpected and skip the hardlink+delete fallback;
update the switch on posix.errno(rc) in renameatPreserve to treat the
unsupported-case(s) specially: if posix.errno(rc) is ENOTSUP (and/or EOPNOTSUPP
if your platform exposes that variant) then break out of the Darwin fast-path
and fall through to the existing POSIX fallback (invoke dirHardLink and then
dirDeleteFile) instead of calling errnoToDirRenameError; reference
darwin.renameatx_np, renameatPreserve, errnoToDirRenameError, dirHardLink, and
dirDeleteFile when making the change.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 21924cb8-d17a-4580-8c2c-6d870a43bed9
📒 Files selected for processing (4)
src/fs.zigsrc/io.zigsrc/os/darwin.zigsrc/os/fs.zig
| // Rename again to an existing destination — should fail. | ||
| var file2 = try dir.createFile(old_path, .{}); | ||
| file2.close(); | ||
| defer dir.deleteFile(old_path) catch {}; | ||
|
|
||
| try std.testing.expectError(error.PathAlreadyExists, dir.renamePreserve(old_path, dir, new_path)); |
There was a problem hiding this comment.
Assert the no-overwrite invariant after the conflict path.
This only checks for error.PathAlreadyExists. It doesn't verify that new_path kept its original contents and that old_path still exists after the failed rename, which is the whole contract of renamePreserve. Add those checks here, and mirror them in src/io.zig, so a buggy implementation can't clobber one side and still pass.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/fs.zig` around lines 1155 - 1160, After asserting
dir.renamePreserve(old_path, dir, new_path) returns error.PathAlreadyExists,
additionally verify the no-overwrite invariant by opening/reading both paths and
asserting their contents and existence are unchanged: confirm new_path still
exists and its original contents are intact, confirm old_path still exists and
contains the pre-rename data (e.g., file2's contents), and do not delete or
replace either file as part of the failed rename. Apply the same extra checks to
the parallel test in src/io.zig so both implementations verify existence and
content preservation after the failure of renamePreserve.
renameatx_np returns ENOTSUP (= 45, OPNOTSUPP in Zig's std.c.E) when the underlying filesystem doesn't support RENAME_EXCL (e.g. FAT32, network mounts). Fall through to the hardlink+delete path in that case instead of returning error.Unexpected.
Summary
renameatx_npandRENAMEflags insrc/os/darwin.zig(macOS 10.12+, ref: https://www.unix.com/man_page/mojave/2/renameatx_np/)renameatPreserveon Darwin withrenameatx_np(RENAME_EXCL), matching the atomicity ofrenameat2(RENAME_NOREPLACE)on LinuxDir.renamePreserveto the native APIDir.renamePreserve(native API) andIo.Dir.renamePreserve(std.Io API)Test plan
./check.shpasses on Linux./check.sh --target aarch64-macoscompiles cleanlyPathAlreadyExistson conflict)