Skip to content

mv: report specific errors instead of generic permission denied on cross-device fallback - #16057

Open
afnan-mojumder wants to merge 1 commit into
uutils:mainfrom
afnan-mojumder:fix-mv-cross-device-errors
Open

afnan-mojumder wants to merge 1 commit into
uutils:mainfrom
afnan-mojumder:fix-mv-cross-device-errors

Conversation

@afnan-mojumder

Copy link
Copy Markdown

Fixes #15939
Closes #9458

Description

Previously, rename_file_fallback unconditionally converted any error during cross-device fallback moves to translate!("mv-error-permission-denied"). This resulted in misleading error messages (e.g. reporting "Permission denied" when a destination filesystem is full with ENOSPC, or omitting the target file path in errors).

This PR introduces RenameError to distinguish raw I/O errors from pre-formatted diagnostics, propagating specific GNU Coreutils-compatible messages:

  1. Unreadable source:
    • Before: mv: Permission denied
    • After: mv: cannot open '<src>' for reading: Permission denied (matching GNU coreutils)
  2. Unwritable destination directory:
    • Before: mv: Permission denied
    • After: mv: cannot create regular file '<dst>': Permission denied (matching GNU coreutils)
  3. Unwritable source directory (cannot remove source):
    • Before: mv: Permission denied
    • After: mv: cannot remove '<src>': Permission denied (matching GNU coreutils)
  4. Destination already exists in unwritable directory (part-fail):
    • Before: mv: Permission denied
    • After: mv: inter-device move failed: '<src>' to '<dst>'; unable to remove target: Permission denied (matching GNU coreutils)
  5. Disk full (ENOSPC) on destination:
    • Before: mv: Permission denied
    • After: mv: error writing '<dst>': No space left on device (matching GNU coreutils)
  6. Same-filesystem rename(2) errors:
    • Formatted as mv: cannot move '<src>' to '<dst>': <err> (matching GNU coreutils)
  7. Prompt decline:
    • When declining an interactive overwrite prompt, exits cleanly with exit code 1 without printing empty or redundant error messages.

Translations updated for en-US and fr-FR.

Tests

  • Added integration tests to tests/by-util/test_mv.rs:
    • test_mv_cross_device_source_unreadable
    • test_mv_cross_device_dest_directory_unwritable
    • test_mv_cross_device_source_directory_unwritable
  • Verified exact stderr parity against GNU Coreutils 9.12.
  • Verified test suite: cargo test --package uu_mv and cargo test --test tests test_mv (all 154 passed).
  • Verified linter and formatting: cargo fmt -- --check and cargo clippy --package uu_mv --all-targets -- -D warnings.

AI disclosure: Written with an AI coding assistant and verified locally.

@afnan-mojumder
afnan-mojumder force-pushed the fix-mv-cross-device-errors branch from c53e8fa to e249b0f Compare October 31, 2026 04:07
@codspeed

codspeed Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 4 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 8 untouched benchmarks
⏩ 468 skipped benchmarks1


Comparing afnan-mojumder:fix-mv-cross-device-errors (b7a7b08) with main (fca3de1)2

Open in CodSpeed

Footnotes

  1. 468 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (4735bbe) during the generation of this report, so fca3de1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@fixfon

fixfon commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

File moves across devices match GNU 9.12 now, nice. Directories still lose the real error, though. Moving a dir from ext4 into a read-only (0555) dir on tmpfs gives cannot move 'src/d' to '/dev/shm/dst/d': No such file or directory, where GNU says cannot create directory '/dev/shm/dst/d': Permission denied. And if a file inside the dir is unreadable, GNU names that file (cannot open 'src/d/b' for reading: Permission denied), while this PR names the dir.

mkdir -p src/d /dev/shm/dst && touch src/d/a && chmod 555 /dev/shm/dst
mv src/d /dev/shm/dst/

@afnan-mojumder

Copy link
Copy Markdown
Author

Thanks for catching this @fixfon!

There were two issues at play here for directories:

  1. In rename_dir_fallback, the destination directory was being opened for applying xattrs (File::open(to)?) before checking copy_dir_contents's result. When destination directory creation was rejected (e.g. Permission denied on a read-only destination), File::open(to) was attempting to open the non-existent destination and masking the actual failure with ENOENT ("No such file or directory").
  2. Directory creation and entry copying inside copy_dir_contents were returning raw io::Errors, which got wrapped at the top level as cannot move '<src>' to '<dst>' rather than formatting the specific path and operation.

We've updated this to:

  • Check copy_dir_contents(...)?: prior to attempting any xattr operations or cleanups.
  • Return cannot create directory '<dst>': <err> whenever create_dir_fail_closed fails.
  • Unify regular file copying across fallback paths to ensure safe fd-pinned copying and report cannot open '<file>' for reading: <err> when an inner file is unreadable.
  • Report cannot access '<dir>': <err> if an inner directory cannot be accessed.
  • Add regression tests covering cross-device directory move permission failures.

@fixfon

fixfon commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Thanks, 797c4f5 fixes what I saw earlier: moving a directory across filesystems into a read-only target now reports "cannot create directory ...: Permission denied", and an unreadable file inside it is named in the error, both matching GNU 9.12. The remaining differences I noticed (the mode of a copied unreadable subdirectory, and the wording when the target directory is not empty) are the same on main, so they seem out of scope here.

@afnan-mojumder

Copy link
Copy Markdown
Author

Thanks for verifying and confirming the GNU 9.12 parity @fixfon! I've also pushed a clean cargo fmt pass on the mkfifo error mapping to keep CI style checks green.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/misc/usage_vs_getopt (fails in this run but passes in the 'main' branch)

@afnan-mojumder
afnan-mojumder force-pushed the fix-mv-cross-device-errors branch from c30b8d5 to 5c84d83 Compare November 4, 2026 13:55
@afnan-mojumder
afnan-mojumder force-pushed the fix-mv-cross-device-errors branch from 5c84d83 to ebeaae0 Compare November 14, 2026 07:31
…oss-device fallback

Previously, rename_file_fallback unconditionally mapped errors during
cross-device fallback moves to translate!("mv-error-permission-denied"),
causing misleading errors such as "Permission denied" when the disk is full
(ENOSPC), or missing contextual path information (e.g. "cannot open ... for
reading", "cannot create regular file ...", "cannot remove ...").

Additionally, when moving directories across devices, rename_dir_fallback
previously attempted to open the destination directory for applying xattrs
before checking if copy_dir_contents succeeded, and directory/entry errors were
reported against the top-level move rather than the failing path.

- Introduce RenameError to distinguish raw I/O errors from pre-formatted
  diagnostics, and preserve synthetic non-OS I/O messages as-is.
- Report accurate GNU Coreutils-compatible messages for cross-device fallback
  failures (unreadable source, unwritable dest dir, ENOSPC, unremovable source
  or target, cannot create directory, cannot access subdirectory).
- Unify regular file copying in copy_regular_file so fallback paths use
  fd-pinned safe copying while preserving xattrs and setuid/setgid rules.
- Handle declined interactive prompts cleanly without printing empty errors.
- Add regression tests covering cross-device file and directory permission
  failures.

Fixes uutils#15939
Closes uutils#9458
@sylvestre
sylvestre force-pushed the fix-mv-cross-device-errors branch from ebeaae0 to b7a7b08 Compare November 16, 2026 13:28

This branch has not been deployed

No deployments
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.

mv: a failed cross-device move always reports "Permission denied" mv: Cannot move huge sparse file to another small filesystem

2 participants