Skip to content

mv: recreate special files when moving them across devices - #13334

Draft
abendrothj wants to merge 5 commits into
uutils:mainfrom
abendrothj:fix/mv-special-file-dest-loss
Draft

abendrothj wants to merge 5 commits into
uutils:mainfrom
abendrothj:fix/mv-special-file-dest-loss

Conversation

@abendrothj

@abendrothj abendrothj commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Based on #15122; only the last three commits are new here.

A cross-device mv of a socket or device node went through the regular file copy, which removed the destination and then failed to open the source. A FIFO also removed the destination before the new one was created, and lost its mode.

FIFOs, sockets and device nodes are now created with their mode and ownership in a private directory next to the destination, then renamed over it relative to the parent's descriptor. If the node can't be created, the destination is left alone. The parent is opened with DirAnchor, so symlinked and write/search-only parents work. As with regular files, setuid and setgid are dropped when ownership can't be kept. Adds DirFd::mknod_at; on Apple targets mknodat is looked up at run time, as it exists only from macOS 13. Redox has a simpler inline fallback that removes the destination first, without the staging. As non-root, device nodes can't be created (EPERM, as with GNU), nor sockets on macOS; the destination is then kept, where GNU has already removed it. Where the file system gives the new private directory another owner (NFS with root_squash), the move fails with an error saying so rather than going on without it.

test_mv_cross_device_special_file_mode_not_applied_to_swapped_entry races a thread that links another file over the destination. Main never applies the mode, so it can't fail there; it guards the staging. Against a version that creates and chmods by path it failed 18 of 20 runs, and with this change it passed 20 of 20, about 0.8 s each. Run on macOS and Linux; Redox, FreeBSD, Android, Windows and WASI are only cross-checked with clippy.

Special files inside a directory moved across devices are in #15123.

Closes #13145

@github-actions

github-actions Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/mv/mv-special-1 is no longer failing!

Copilot AI lite review requested due to automatic review settings August 17, 2026 03:46
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from da75e2c to eaf69db Compare August 17, 2026 03:46

Copilot AI 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.

Pull request overview

This PR fixes mv cross-device (EXDEV) fallback behavior to prevent data loss when moving special files (e.g., sockets/fifos/device nodes) and when the source cannot be opened. It updates the mv implementation to recreate special files and to avoid destroying an existing destination unless replacement is guaranteed.

Changes:

  • Recreate FIFOs/sockets/device nodes during EXDEV fallback (instead of attempting content copy) and preserve metadata.
  • Replace destinations via temp-name creation + atomic rename to avoid leaving the destination missing on failure.
  • Add Linux integration tests covering socket/fifo replacement, directory-with-socket moves, and unreadable source preservation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/by-util/test_mv.rs Adds regression/integration tests for cross-device special-file moves and destination-preservation behavior.
src/uu/mv/src/mv.rs Implements special-file recreation and safer destination replacement logic in EXDEV fallback paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/uu/mv/src/mv.rs Resolved
Comment thread tests/by-util/test_mv.rs Resolved
Copilot AI review requested due to automatic review settings August 17, 2026 09:18

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/uu/mv/src/mv.rs:1623

  • open_destination_parent(from) opens the source’s parent directory with SymlinkBehavior::NoFollow. That makes cross-device moves fail (before touching the destination) when the source path is under a symlinked directory (e.g. mv link/file dest where link -> realdir). Previously the source was removed via fs::remove_file(from), which follows symlinks in parent components as normal path resolution does.

Consider opening the source parent with SymlinkBehavior::Follow (while keeping NoFollow for the destination side) so normal mv semantics continue to work for sources located under symlinked directories.

    #[cfg(all(unix, not(target_os = "redox")))]
    let (src_parent_fd, src_basename) = open_destination_parent(from)
        .map_err(|err| io::Error::new(err.kind(), translate!("mv-error-permission-denied")))?;

src/uu/mv/src/mv.rs:1121

  • rename_special_fallback uses open_destination_parent(from) for the source cleanup path. Since open_destination_parent intentionally opens parents with SymlinkBehavior::NoFollow, this makes cross-device moves of special files fail if the source lives under a symlinked directory (even though opening/reading the source itself follows symlinks in parent components).

Recommendation: keep NoFollow for the destination parent (security), but open the source parent with SymlinkBehavior::Follow so source paths under symlinked directories continue to work as they did previously.

This issue also appears on line 1621 of the same file.

    let (dir_fd, basename) = open_destination_parent(to)?;
    let (src_parent_fd, src_basename) = open_destination_parent(from)?;
    let basename_cstr = CString::new(basename.as_bytes())

Copilot AI review requested due to automatic review settings August 18, 2026 03:07

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 26, 2026 08:48

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 26, 2026 09:52

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codspeed

codspeed Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 1.5%

1 warning, 1 note

⚠️ 120 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

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 5 improved benchmarks
❌ 2 regressed benchmarks
✅ 398 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation mv_force_overwrite 128.3 ms 156.8 ms -18.2%
❌ CPU Simulation ls_recursive_wide_tree[(10000, 1000)] 68.1 ms 75 ms -9.21%
⚡ CPU Simulation uniq_case_insensitive[10000] 2.9 ms 2.8 ms +5.11%
⚡ CPU Simulation uniq_with_count[10000] 3.6 ms 3.5 ms +4.13%
⚡ CPU Simulation uniq_heavy_duplicates[10000] 4.2 ms 4 ms +3.49%
⚡ CPU Simulation uniq_check_chars[(10000, "1")] 4.2 ms 4.1 ms +3.47%
⚡ CPU Simulation du_wide_tree[(5000, 500)] 15.7 ms 15.2 ms +3.33%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing abendrothj:fix/mv-special-file-dest-loss (2cfb03e) with main (156d715)

Open in CodSpeed

Footnotes

  1. 70 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. ↩

@sylvestre

Copy link
Copy Markdown
Contributor

this is a lot of cfg for redox
maybe the redox specific code should be moved into src/uu/mv/src/platform/redox.rs like in other programs
wdyt?

@abendrothj

Copy link
Copy Markdown
Contributor Author

Yes, I think the Redox-specific implementations should move into src/uu/mv/src/platform/redox.rs.

mv.rs now has several sizeable Redox branches, particularly for special-file and symlink fallbacks, which makes the generic fallback flow harder to follow.

The extraction will move the actual Redox behavior while leaving only small dispatch points and shared helpers, such as random_temp_name, in mv.rs. Capability-based conditionals for xattrs and safe traversal can remain inline since they also cover non-Redox platforms.

This keeps the change focused without introducing a broader platform abstraction than necessary.

Copilot AI review requested due to automatic review settings August 26, 2026 22:35

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 26, 2026 22:36

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@abendrothj

Copy link
Copy Markdown
Contributor Author

Infrastructure failure with precommit.ci

Copilot AI review requested due to automatic review settings August 31, 2026 23:00

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings September 8, 2026 23:19
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from f6ec44e to 44dd7d7 Compare September 8, 2026 23:20
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from 44dd7d7 to c05f50e Compare September 22, 2026 01:26
@abendrothj

Copy link
Copy Markdown
Contributor Author

Redox code is out of mv.rs now: the implementations live in src/uu/mv/src/platform/redox.rs behind #[cfg(target_os = "redox")], re-exported through platform/mod.rs, and mv.rs keeps only the dispatch plus the shared random_temp_name helper.

Two other things from the rebase onto main. The branch's local replace_symlink is gone — main grew uucore::fs::replace_link meanwhile and it does the same job, so the non-Redox arm calls that. And renameat/fchown go through rustix now; mknodat stays on libc because rustix doesn't expose it on Apple targets, with a SAFETY comment on the remaining block.

The failing musl job isn't from this PR: it's uu_dd::tests::copy_buffer_does_not_touch_its_pages panicking with attempt to subtract with overflow, which is flaky on main as well. Fix in #14804.

@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch 2 times, most recently from cb7be93 to 35ac41f Compare October 5, 2026 06:13
Comment thread src/uu/mv/src/mv.rs Resolved
@sylvestre

Copy link
Copy Markdown
Contributor

this is very complex, I think it would benefit to be smaller

Comment thread src/uu/mv/src/mv.rs Resolved
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from 35ac41f to 88fd411 Compare October 6, 2026 07:15
@abendrothj abendrothj changed the title fix(mv): don't destroy the destination when a cross-device move fails mv: recreate special files when moving them across devices Oct 6, 2026
@abendrothj

Copy link
Copy Markdown
Contributor Author

I split it up. The shared pieces are separate PRs: DirFd::open_anchor (#15120) and replace_entry_at (#15122), which also moves replace_link onto it. Special files inside directories moved across devices are in #15123. What's left here is 4 files, +423/-15, and 252 of those lines are tests. Until the two helper PRs land, the diff shown here still includes them.

About redox.rs: I went the other way from what I said earlier. The module is gone, and aix, hurd and redox get one inline copy_special_file fallback of about 30 lines. It removes the destination before mknod and uses pathname metadata, so on those targets there's no staging and the destination isn't kept if creating the node fails. None of the three was run.

@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from ccd87db to 7c744c8 Compare October 7, 2026 00:54
@abendrothj

Copy link
Copy Markdown
Contributor Author

one more commit: the staging directory gets its 0700 from a chmod right after the mkdir, instead of a temporary umask of 077, so mv no longer changes the process umask and this no longer needs #15121. test_mv_cross_device_special_file_under_owner_umask covers it, on Linux only.
on redox.rs: no redox-only code is left here. main already handles the symlink case, and the one path-based fallback left is shared by aix, hurd and redox, so it stays inline next to the safe_traversal version.

@abendrothj

Copy link
Copy Markdown
Contributor Author

folded the 0700 staging commit into the first one, so each commit builds on its own (the first one used with_umask, which is only in #15121). also rebased on #15030: hurd and aix now take the staged path too, only cross-checked with clippy there.

Comment thread src/uu/mv/src/mv.rs Resolved
Comment thread tests/by-util/test_mv.rs Resolved
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch 2 times, most recently from fd92ccb to 92c01f5 Compare October 12, 2026 10:05
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch 3 times, most recently from d539d76 to ad5e353 Compare October 22, 2026 08:50
@abendrothj
abendrothj marked this pull request as draft October 23, 2026 07:30
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from ad5e353 to d89dfa7 Compare November 7, 2026 06:33
For ln -f/-sf, cp -lf/-sf and mv's cross-device symlink. Without O_PATH
or O_SEARCH (OpenBSD, Hurd) it falls back to the directory's path.
ln and mv's cross-device move use it next. The no-unique-temp-name
error is now translated.
On Apple targets mknodat is looked up at run time, as it exists only
from macOS 13; before that the node is created by the directory's path.
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from d89dfa7 to ebc5cef Compare November 11, 2026 04:22
Sockets and device nodes are recreated like fifos, with their mode, and
the destination is replaced only once the new node exists.

Fixes uutils#13145
@abendrothj
abendrothj force-pushed the fix/mv-special-file-dest-loss branch from ebc5cef to 2cfb03e Compare November 11, 2026 04:54

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: moving a special file across filesystems deletes the destination on failure

3 participants