Skip to content

Commit 4398381

Browse files
codspeedbotsylvestre
authored andcommitted
cp: apply the finalize chmod with a single fchmodat call
The no-follow chmod opened the destination's parent directory to get a dirfd for every copied entry, which added an openat(2) and a close(2) per file and directory on top of the chmod itself. On a 341-directory, 3410-file tree, cp -a went from 175055 to 182217 syscalls. Resolve the whole path against AT_FDCWD instead: fchmodat2/fchmodat with AT_SYMLINK_NOFOLLOW still refuses to follow a symlink at the final component, which is the property the change is about, and it costs exactly the chmod(2) it replaces. The dirfd variant only resolved the parent immediately before the chmod, so nothing was pinned across the copy and no protection is lost. The fchmodat implementation, including the fchmodat2 fast path and the musl O_PATH fallback, moves to a shared helper used by both DirFd::chmod_at and the new uucore::safe_traversal::chmod_nofollow.
1 parent 2c4eb31 commit 4398381

3 files changed

Lines changed: 199 additions & 143 deletions

File tree

‎src/uu/cp/src/cp.rs‎

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
// spell-checker:ignore (ToDO) copydir fiemap ftruncate linkgs lstat nlink nlinks pathbuf pwrite reflink strs xattrs symlinked deduplicated advcpmv nushell IRWXG IRWXO IRWXU IRWXUGO IRWXU IRWXG IRWXO IRWXUGO sflag
6-
// spell-checker:ignore RDONLY futimens utimensat
6+
// spell-checker:ignore RDONLY futimens utimensat fchmodat FDCWD
77

88
use std::cmp::Ordering;
99
use std::collections::{HashMap, HashSet};
@@ -2501,16 +2501,6 @@ fn calculate_dest_permissions(
25012501
}
25022502
}
25032503

2504-
/// Copy the a file from `source` to `dest`. `source` will be dereferenced if
2505-
/// `options.dereference` is set to true. `dest` will be dereferenced only if
2506-
/// the source was not a symlink.
2507-
///
2508-
/// Behavior when copying to existing files is contingent on the
2509-
/// `options.overwrite` mode. If a file is skipped, the return type
2510-
/// should be `Error:Skipped`
2511-
///
2512-
/// The original permissions of `source` will be copied to `dest`
2513-
/// after a successful copy.
25142504
/// Apply `permissions` to `dest` without following a symlink at the final component.
25152505
///
25162506
/// Both callers test `dest` for being a symlink well before the chmod, so a symlink
@@ -2519,24 +2509,9 @@ fn calculate_dest_permissions(
25192509
/// exactly the "do nothing" case the callers' guards describe.
25202510
#[cfg(unix)]
25212511
fn chmod_nofollow(dest: &Path, permissions: &Permissions) -> io::Result<()> {
2522-
use uucore::safe_traversal::{DirFd, SymlinkBehavior};
2523-
2524-
let (Some(parent), Some(name)) = (dest.parent(), dest.file_name()) else {
2525-
// `.`, `..` and `/` have no final component to anchor against. They are
2526-
// always directories and never a symlink themselves, so there is nothing
2527-
// to be redirected: chmod the directory through its own fd.
2528-
let dir_fd = DirFd::open(dest, SymlinkBehavior::NoFollow)?;
2529-
return dir_fd.fchmod(permissions.mode());
2530-
};
2531-
// `Path::parent()` yields "" for a bare filename.
2532-
let parent = if parent.as_os_str().is_empty() {
2533-
Path::new(".")
2534-
} else {
2535-
parent
2536-
};
2537-
2538-
let dir_fd = DirFd::open(parent, SymlinkBehavior::Follow)?;
2539-
match dir_fd.chmod_at(name, permissions.mode(), SymlinkBehavior::NoFollow) {
2512+
// A single `fchmodat(AT_FDCWD, dest, AT_SYMLINK_NOFOLLOW)`: it costs the same
2513+
// as the `chmod(2)` it replaces, no extra descriptor for the parent directory.
2514+
match uucore::safe_traversal::chmod_nofollow(dest, permissions.mode()) {
25402515
Err(e) if matches!(e.raw_os_error(), Some(libc::EOPNOTSUPP) | Some(libc::ELOOP)) => Ok(()),
25412516
other => other,
25422517
}
@@ -2547,6 +2522,16 @@ fn chmod_nofollow(dest: &Path, permissions: &Permissions) -> io::Result<()> {
25472522
fs::set_permissions(dest, permissions.clone())
25482523
}
25492524

2525+
/// Copy the a file from `source` to `dest`. `source` will be dereferenced if
2526+
/// `options.dereference` is set to true. `dest` will be dereferenced only if
2527+
/// the source was not a symlink.
2528+
///
2529+
/// Behavior when copying to existing files is contingent on the
2530+
/// `options.overwrite` mode. If a file is skipped, the return type
2531+
/// should be `Error:Skipped`
2532+
///
2533+
/// The original permissions of `source` will be copied to `dest`
2534+
/// after a successful copy.
25502535
#[allow(clippy::cognitive_complexity, clippy::too_many_arguments)]
25512536
fn copy_file(
25522537
progress_bar: Option<&ProgressBar>,

0 commit comments

Comments
 (0)