Skip to content

fs: use renameatx_np on Darwin for atomic non-replacing rename - #454

Merged
lalinsky merged 2 commits into
mainfrom
darwin-renameatx-np
Jun 2, 2026
Merged

lalinsky merged 2 commits into
mainfrom
darwin-renameatx-np

Conversation

@lalinsky

@lalinsky lalinsky commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Summary

Test plan

  • ./check.sh passes on Linux
  • ./check.sh --target aarch64-macos compiles cleanly
  • Manually test on macOS (success path + PathAlreadyExists on conflict)

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.
@coderabbitai

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8111e38d-ff7c-4e25-901e-7b20c2931cc1

📥 Commits

Reviewing files that changed from the base of the PR and between cf90813 and 3b09652.

📒 Files selected for processing (1)
  • src/os/fs.zig
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/os/fs.zig

📝 Walkthrough

Walkthrough

Adds Darwin renameatx_np bindings; updates OS rename-at-preserve to use the Darwin syscall with exclusive flag and proper errno mapping; exposes a Dir.renamePreserve() API and adds tests for success and EEXIST failure.

Changes

Rename-preserve operation

Layer / File(s) Summary
Darwin rename syscall bindings
src/os/darwin.zig
RENAME packed struct and renameatx_np extern C declaration for atomic rename with flag-controlled destination behavior.
OS-level implementation
src/os/fs.zig
Import darwin module; update renameatPreserve() to call darwin.renameatx_np with EXCL on macOS, loop on EINTR, map EEXIST to error.PathAlreadyExists, and fall back on OPNOTSUPP.
Public Dir API and tests
src/fs.zig, src/io.zig
Add Dir.renamePreserve() and RenamePreserveError; tests assert rename succeeds to a free target and fails with error.PathAlreadyExists when target exists.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • lalinsky/zio#382: Related work extending rename-preservation behavior in src/os/fs.zig (Windows-focused changes there) which intersects with this PR's filesystem rename logic.

Poem

A careful move from old to new,
Darwin whispers syscall true,
OS routes the exclusive call,
Dir exposes it for all,
Tests clap when collisions fall 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding renameatx_np on Darwin for atomic non-replacing rename operations, which aligns with the changeset's core objective.
Description check ✅ Passed The description directly relates to the changeset, covering the Darwin declaration, the replacement of the fallback approach, API additions, and test coverage plans.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch darwin-renameatx-np

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 49218cd and cf90813.

📒 Files selected for processing (4)
  • src/fs.zig
  • src/io.zig
  • src/os/darwin.zig
  • src/os/fs.zig

Comment thread src/fs.zig
Comment on lines +1155 to +1160
// 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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

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.

Comment thread src/os/fs.zig Resolved
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.
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.

1 participant