Repository navigation
mv: recreate special files when moving them across devices - #13334
abendrothj wants to merge 5 commits into
Conversation
|
GNU testsuite comparison: |
da75e2c to
eaf69db
Compare
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 withSymlinkBehavior::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 destwherelink -> realdir). Previously the source was removed viafs::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_fallbackusesopen_destination_parent(from)for the source cleanup path. Sinceopen_destination_parentintentionally opens parents withSymlinkBehavior::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())
Merging this PR will degrade performance by 1.5%1 warning, 1 note
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
|
this is a lot of cfg for redox |
|
Yes, I think the Redox-specific implementations should move into
The extraction will move the actual Redox behavior while leaving only small dispatch points and shared helpers, such as This keeps the change focused without introducing a broader platform abstraction than necessary. |
|
Infrastructure failure with precommit.ci |
f6ec44e to
44dd7d7
Compare
44dd7d7 to
c05f50e
Compare
|
Redox code is out of Two other things from the rebase onto main. The branch's local The failing musl job isn't from this PR: it's |
cb7be93 to
35ac41f
Compare
|
this is very complex, I think it would benefit to be smaller |
35ac41f to
88fd411
Compare
|
I split it up. The shared pieces are separate PRs: 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 |
ccd87db to
7c744c8
Compare
|
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. |
7c744c8 to
25c9249
Compare
fd92ccb to
92c01f5
Compare
d539d76 to
ad5e353
Compare
ad5e353 to
d89dfa7
Compare
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.
d89dfa7 to
ebc5cef
Compare
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
ebc5cef to
2cfb03e
Compare
Based on #15122; only the last three commits are new here.
A cross-device
mvof 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. AddsDirFd::mknod_at; on Apple targetsmknodatis 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_entryraces 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