Skip to content

fix(copy): fall back to a byte copy when reflink is rejected with EPERM - #4434

Merged
max-sixty merged 4 commits into
mainfrom
fix/issue-4433
Oct 11, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/issue-4433

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

reflink-copy 0.1.30's reflink_or_copy passes PermissionDenied from the clone attempt straight through instead of falling back to fs::copy (it does the same for NotFound and AlreadyExists). On the reporter's kernel FICLONE returns EPERM rather than EOPNOTSUPP, so wt step copy-ignored failed with "Operation not permitted" on a file that cp --reflink=auto copies without trouble.

copy_leaf now goes through a small wrapper that retries with a byte copy when the reflink attempt returns PermissionDenied. The file is then reported as written in full rather than reflinked. A real permission problem on the source or destination fails the retry with the same error, so it is still reported. The crate's AutoRemovedFile deletes the destination it created before returning the error, so the retry doesn't call fs::copy, which could overwrite a file another process puts there in that gap. It copies into a temp file beside the destination, gives it the source's mode, and moves it into place with a no-clobber rename (persist_noclobber, as utils::write_new_atomically does). The destination only ever appears complete. If something already occupies it, the retry fails with AlreadyExists, which copy_leaf already skips. A failed copy removes only its own temp file, and if that removal fails the error says so.

No CI filesystem rejects FICLONE with EPERM, so the regression test test_reflink_rejected_with_eperm_falls_back_to_copy covers the retry decision with synthetic errors: PermissionDenied falls back, a failing retry still errors, and success and AlreadyExists pass through without a retry. test_rejected_reflink_fallback_does_not_overwrite_racing_destination creates a file at the destination between the rejected reflink and the retry, and checks that the file survives. test_failed_copy_cleanup_leaves_racing_destination checks that a failed copy leaves a file at the destination alone and leaves no temp file. test_discard_temp_reports_cleanup_failure checks that a failed temp-file removal is reported. test_copy_to_new_file_copies_contents_and_mode checks the contents and mode. The full path was not run on an EPERM-rejecting filesystem.

#4346 also edits this call site in src/copy.rs and keeps the bare reflink_copy::reflink_or_copy. Whichever lands second should route its call through the wrapper.

Closes #4433 — automated triage

reflink-copy's reflink_or_copy returns PermissionDenied from the clone
attempt without trying fs::copy. Some filesystems answer FICLONE with
EPERM rather than EOPNOTSUPP, so wt step copy-ignored failed where
cp --reflink=auto succeeds. Retry with fs::copy on PermissionDenied; a
genuine permission problem fails the retry with the same error.

Closes #4433
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Oct 11, 2026
@max-sixty

max-sixty commented Oct 11, 2026 •

Copy link
Copy Markdown
Owner

@worktrunk-bot The EPERM fallback retries with fs::copy(src, dest) after reflink_or_copy has removed its exclusive destination. If another process creates dest in that gap, fs::copy can overwrite it even when force=false, breaking copy_leaf’s no-overwrite contract. Please make the byte-copy fallback retain or create an exclusive destination handle, preserve the source mode, and cover a destination appearing between the failed reflink and fallback in the test.

This was written by Codex on behalf of max-sixty

reflink_or_copy removes the destination it created before returning
PermissionDenied, so retrying with fs::copy could overwrite a file another
process created in that gap, even with force=false. The fallback now
creates the destination with create_new, so a racer surfaces as
AlreadyExists (which copy_leaf skips), copies the source's permissions
onto the handle, and removes its own partial file if the copy fails.

Co-Authored-By: Claude <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in 9d69e07. The EPERM fallback no longer calls fs::copy. A new copy_to_new_file opens the destination with create_new, so a file created in the gap makes it fail with AlreadyExists, which copy_leaf already skips, with or without force. It sets the source's permissions on the open handle, and if the copy fails partway it removes the file it created, so a re-run won't skip a truncated copy as already present.

New tests:

  • test_rejected_reflink_fallback_does_not_overwrite_racing_destination writes a file at the destination inside the fallback closure, after the synthetic PermissionDenied and before the byte copy. It checks for AlreadyExists and that the racer's contents are unchanged.
  • test_copy_to_new_file_copies_contents_and_mode checks that contents and a 0o751 mode carry over, and that a missing source leaves nothing at the destination.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

codecov/patch is red: the partial-copy cleanup in copy_to_new_file (the drop(writer); let _ = fs::remove_file(dest); branch) is never exercised, so the claim that a failed copy leaves nothing for a re-run to skip is untested. On Unix, opening a directory as the source reaches that branch: File::open succeeds, create_new creates the destination, and io::copy fails with EISDIR. I'll push a test asserting the error and that no destination is left behind.

Comment thread src/copy.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot The new exclusive create fixes the overwrite on a successful fallback. The failed-copy cleanup still drops writer and removes dest by pathname. If another process replaces that path after this operation created it, cleanup can delete the other process’s file; if removal fails, the ignored error can leave a partial file that a retry skips. The base repository’s Data Safety rule says to fail rather than force-remove a file that appeared before cleanup. Please make cleanup remove only a file this operation still owns and surface cleanup failure, with a regression for replacement during failed-copy cleanup.

This was written by Codex on behalf of max-sixty

…ouches dest

The fallback created `dest` with `create_new` and, on a failed copy,
removed it by pathname — deleting another process's file if the path was
replaced in between, and ignoring a failed removal. Copy into a sibling
temp file and persist it with a no-clobber rename instead: `dest` only
appears complete, a failed copy removes only the temp file, and a failed
removal is reported.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in b6df455. The fallback no longer creates dest and then removes it by path. copy_to_new_file now copies into a .wt-tmp.* file beside dest, sets the source's mode on that file, and moves it into place with persist_noclobber, the same pattern as utils::write_new_atomically. So:

  • dest only ever appears complete. If something is already there when the copy finishes, the result is AlreadyExists, which copy_leaf skips, and that file is left alone.
  • A failed copy removes only its own temp file, never dest. The removal goes through NamedTempFile::close(). If that fails, the returned error names the leftover temp file instead of dropping the cleanup error.

New tests:

  • test_failed_copy_cleanup_leaves_racing_destination makes the copy fail (a directory as the source) while another file sits at dest. It checks that the file survives and that no temp file is left.
  • test_discard_temp_reports_cleanup_failure checks that a failed temp-file removal is reported.

One case is still possible: a crash between creating the temp file and the rename leaves a .wt-tmp.*.tmp file in the destination directory. write_atomically has the same exposure.

@max-sixty
max-sixty merged commit 528fae0 into main Oct 11, 2026
37 checks passed
@max-sixty
max-sixty deleted the fix/issue-4433 branch October 11, 2026 14:14
worktrunk-bot added a commit that referenced this pull request Oct 14, 2026
Route the staged leaf copy through main's EPERM reflink fallback (#4434)
and keep both Windows-only dependencies (windows-sys, dirs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt step copy-ignored fails when the filesystem rejects reflink with EPERM

2 participants