mv:fix file ownership changes when a file is mv'ed by root to a different file system - #9672
mv:fix file ownership changes when a file is mv'ed by root to a different file system#9672mattsu2020 wants to merge 6 commits into
Conversation
|
GNU testsuite comparison: |
This comment was marked as resolved.
This comment was marked as resolved.
| .env("BASE", &base) | ||
| .env("UUTILS", &scene.bin_path) | ||
| .output() | ||
| .expect("failed to run unshare"); |
There was a problem hiding this comment.
It might better to create a new function for uhshare (at different PR).
Ref: #9973
|
GNU testsuite comparison: |
| @@ -816,6 +816,44 @@ fn is_fifo(_filetype: fs::FileType) -> bool { | |||
| false | |||
| } | |||
|
|
|||
| #[cfg(unix)] | |||
| fn try_preserve_ownership(from_meta: &fs::Metadata, to: &Path, follow_symlinks: bool) { | |||
There was a problem hiding this comment.
document this function please
|
|
||
| unsafe { | ||
| if follow_symlinks { | ||
| let _ = libc::chown(to_cstr.as_ptr(), uid, gid); |
There was a problem hiding this comment.
please manage the errors
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn try_preserve_permissions(from_meta: &fs::Metadata, to: &Path) { |
There was a problem hiding this comment.
same, please document it
|
|
||
| // Keep mode bits only (file type bits are not allowed in chmod). | ||
| let mode = from_meta.mode() & 0o7777; | ||
| let _ = fs::set_permissions(to, fs::Permissions::from_mode(mode)); |
Merging this PR will not alter performance
Comparing Footnotes
|
|
GNU testsuite comparison: |
| fn copy_symlink(from: &Path, to: &Path) -> io::Result<()> { | ||
| let from_meta = from.symlink_metadata()?; | ||
| let path_symlink_points_to = fs::read_link(from)?; | ||
| unix::fs::symlink(path_symlink_points_to, to).map(|_| { |
There was a problem hiding this comment.
it should copy xattrs like rename_symlink_fallback does. no ?
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
I think we have a chmod helper that we can use here that implements all of the logic in the PR already in the uucore perms lib |
|
and it conflicts |
wrap_chown is too high-level for mv compatibility logic. It returns formatted Result<String, String>, so mv cannot inspect errno to implement GNU-like behavior (owner+group attempt, optional group-only fallback, and selective suppression for EPERM/EACCES/EINVAL). To avoid behavior regressions, I’d like to add a public low-level helper in uucore::perms that returns io::Result<()> (preserving errno), then switch mv to that helper while keeping GNU-compatible branching in mv. This still removes syscall duplication, but without changing user-visible behavior. |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
Reordered import statements in test_df.rs and test_mv.rs to follow a consistent pattern, moving conditional imports and utility functions to their proper positions within the import blocks. This improves code readability and maintains consistency across test files.
|
@mattsu2020 some lint issues, sorry! |
- Use entry.file_type() for efficient type checking (mv_fix approach) - Use copy_file_with_hardlinks_helper on Unix for hardlink support (main approach) - Use rename_symlink_fallback on non-Unix (main approach) - Symlinks handled before regular files, no need for second symlink check
|
GNU testsuite comparison: |
Summary
Fix cross-filesystem
mv(EXDEV copy+delete fallback) so that file ownership does not change to the invoking user (e.g.root) when moving a file across filesystems.Fixes #9635.
Background / Problem
When
mvcannotrename(2)across devices (EXDEV), uutils falls back to copy+delete. The copy path usedstd::fs::copy, which creates the destination owned by the caller. Ifrootmoves a file owned by another user to a different filesystem, the destination ends up owned byroot(compatibility + security concern).Changes
lchown, do not follow)chownto keep correct permission bits (sincechownmay clear setuid/setgid)./dev/shmtmpfs).bind_instead_of_map) in the copy path.Testing
cargo test -p uu_mvcargo clippy -p uu_mv -- -D warningsrelated
#9635