Skip to content

mv: keep mode and xattrs of subdirectories on a cross-device move - #16222

Merged
sylvestre merged 5 commits into
uutils:mainfrom
nishant9144:mv-subdir-mode-xattrs
Nov 9, 2026
Merged

sylvestre merged 5 commits into
uutils:mainfrom
nishant9144:mv-subdir-mode-xattrs

Conversation

@nishant9144

Copy link
Copy Markdown
Contributor

When mv falls back to copying a directory tree across filesystems, the directories below the operand were created with the default mode and never got their xattrs, so POSIX ACLs were lost and a 0700 subdirectory came out as 0755.

After a subdirectory's contents are copied, its xattrs are now copied and then its mode is set, with setuid/setgid dropped if the chown did not take, as for files. Doing it after the contents means a default ACL or a read-only mode does not affect the entries created inside it, and setting the mode last keeps the ACL mask right.

The new test checks the mode, and the ACL when both filesystems support ACLs (/dev/shm tmpfs often does not, so I also checked it by hand with an ext4 destination).

Closes #15784

The cross-device fallback created each directory below the operand with the default mode and never copied its xattrs, so POSIX ACLs were lost and a 0700 directory became 0755. Copy the xattrs and then set the mode once the directory's contents are in place. The mode goes last because chmod sets the ACL mask from the group bits.

Closes uutils#15784
Copilot AI balanced review requested due to automatic review settings November 6, 2026 09:15

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 insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/mv/src/mv.rs Outdated
)?;

#[cfg(unix)]
copy_subdir_xattrs_and_mode(&from_path, &to_path, ownership_preserved);

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.

the top-level dir has the same problem: mv of a 0700 dir across devices still gives 0755.
could you please apply the mode in rename_dir_fallback too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed

Comment thread src/uu/mv/src/mv.rs Outdated
} else {
mode & !0o6000
};
let _ = fs::set_permissions(to, fs::Permissions::from_mode(mode));

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.

rename_file_fallback does this through fds (copy_xattrs_fd_ignore_unsupported + set_permissions on the File), could you do the same here?
the path-based chmod follows a symlink swapped in at to.

@nishant9144 nishant9144 Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added new helper open_dir_nofollow

Comment thread src/uu/mv/src/mv.rs Outdated
let mode = if ownership_preserved {
mode
} else {
mode & !0o6000

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.

same setuid/setgid strip as in rename_file_fallback, could be a small shared helper, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added new helper moved_mode

Comment thread tests/by-util/test_mv.rs
.permissions()
.mode()
& 0o7777;
assert_eq!(mode, 0o700, "site/private should stay 0700, got {mode:o}");

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.

please also chmod site to 0700 and check it after the move

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed

Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:20

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 insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:21

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 insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 6, 2026 21:55

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 insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit cf030b8 into uutils:main Nov 9, 2026
100 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

RenjiSann added a commit to RenjiSann/coreutils that referenced this pull request Nov 9, 2026
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: cross-device move drops subdirectory ACLs and modes, widening access

3 participants