Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) copydir fiemap linkgs lstat nlink nlinks pathbuf reflink strs xattrs symlinked deduplicated advcpmv nushell IRWXG IRWXO IRWXU IRWXUGO IRWXU IRWXG IRWXO IRWXUGO sflag
// spell-checker:ignore RDONLY futimens utimensat unioned
// spell-checker:ignore RDONLY futimens utimensat unioned fchmodat FDCWD

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -1834,7 +1834,7 @@ fn copy_extended_attrs(source: &Path, dest: &Path, skip_selinux: bool) -> CopyRe
if was_readonly {
#[allow(clippy::permissions_set_readonly_false)]
perms.set_readonly(false);
fs::set_permissions(dest, perms)?;
chmod_nofollow(dest, &perms)?;
}

// Perform the xattr copy and capture any potential error,
Expand Down Expand Up @@ -1863,7 +1863,7 @@ fn copy_extended_attrs(source: &Path, dest: &Path, skip_selinux: bool) -> CopyRe
if was_readonly {
let mut revert_perms = fs::symlink_metadata(dest)?.permissions();
revert_perms.set_readonly(true);
fs::set_permissions(dest, revert_perms)?;
chmod_nofollow(dest, &revert_perms)?;
}

// If copying xattrs failed, propagate that error now with context.
Expand Down Expand Up @@ -1953,11 +1953,9 @@ pub(crate) fn copy_attributes(
})?;

handle_preserve(mode, || -> CopyResult<()> {
// The `chmod()` system call that underlies the
// `fs::set_permissions()` call is unable to change the
// permissions of a symbolic link. In that case, we just
// do nothing, since every symbolic link has the same
// permissions.
// Leave a symlink alone, like GNU. `chmod_nofollow` alone is not
// enough: FreeBSD and macOS would change the link's own mode, and
// the fallback on other targets follows it.
if !dest.is_symlink() {
#[cfg(unix)]
let source_perms = {
Expand All @@ -1981,7 +1979,7 @@ pub(crate) fn copy_attributes(
#[cfg(not(unix))]
let source_perms = source_metadata.permissions();

fs::set_permissions(dest, source_perms)
chmod_nofollow(dest, &source_perms)
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?;
// GNU `cp -p` preserves POSIX ACLs as part of mode. On Linux the
// ACLs are stored as `system.posix_acl_*` xattrs; copy just those
Expand Down Expand Up @@ -2769,6 +2767,32 @@ fn calculate_dest_permissions(
}
}

/// Apply `permissions` to `dest` without following a symlink at the final component.
///
/// The callers check `dest` well before the chmod, so a symlink swapped in meanwhile
/// must not redirect the mode onto its target. Finding one is not an error.
#[cfg(all(unix, not(target_os = "redox")))]
fn chmod_nofollow(dest: &Path, permissions: &Permissions) -> io::Result<()> {
let Err(e) = uucore::safe_traversal::chmod_nofollow(dest, permissions.mode()) else {
Comment on lines +2770 to +2776
return Ok(());
};
// ENOTSUP and EOPNOTSUPP share a value on Linux but not on the BSDs; a guard
// rather than an or-pattern keeps the duplicate from being unreachable. The errno
// cannot tell a symlink from a platform that cannot chmod without following, so
// confirm it rather than report success on a mode that was never applied.
let refused = matches!(e.raw_os_error(),
Some(c) if c == libc::EOPNOTSUPP || c == libc::ENOTSUP || c == libc::ELOOP);
if refused && fs::symlink_metadata(dest).is_ok_and(|md| md.file_type().is_symlink()) {
return Ok(());
}
Err(e)
}

#[cfg(not(all(unix, not(target_os = "redox"))))]
fn chmod_nofollow(dest: &Path, permissions: &Permissions) -> io::Result<()> {
fs::set_permissions(dest, permissions.clone())
Comment on lines +2792 to +2793
}

/// Copy the a file from `source` to `dest`. `source` will be dereferenced if
/// `options.dereference` is set to true. `dest` will be dereferenced only if
/// the source was not a symlink.
Expand Down Expand Up @@ -2987,8 +3011,8 @@ fn copy_file(
//
// Check the destination as it is now, not as it was before the copy:
// copying a symlink without dereferencing recreates it as a symlink here,
// and chmod() would follow it and change the mode of the link target,
// which can live outside the copied tree. Conversely, --remove-destination
// which is left alone (see copy_attributes for why `chmod_nofollow` does
// not make this check redundant). Conversely, --remove-destination
// replaces a symlink with a regular file that still needs its mode set.
// With --link, dest shares the source's inode, so chmod would change the
// mode of the source file as well.
Expand All @@ -2999,7 +3023,7 @@ fn copy_file(
//
// FWIW, the OS will throw an error later, on the write op, if
// the user does not have permission to write to the file.
fs::set_permissions(dest, dest_permissions).ok();
chmod_nofollow(dest, &dest_permissions).ok();

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.

chmod_nofollow already treats a symlink as a no-op, so do we still need the !dest.is_symlink() above (here and in copy_attributes)? it's one more lstat per file, and the comment above it about chmod() following is now stale

}

let copy_attributes_result = if options.dereference(source_in_command_line) {
Expand Down
Loading
Loading