Skip to content
Merged
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
12 changes: 11 additions & 1 deletion src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use uucore::uio_error;
use walkdir::{DirEntry, WalkDir};

use crate::{
CopyResult, CpError, Options, aligned_ancestors, context_for, copy_attributes, copy_file,
CopyMode, CopyResult, CpError, Options, aligned_ancestors, context_for, copy_attributes,
copy_file,
};

/// Ensure a Windows path starts with a `\\?`.
Expand Down Expand Up @@ -469,6 +470,15 @@ pub(crate) fn copy_directory(
let is_dir_for_permissions =
entry_is_dir_no_follow || (options.dereference && direntry_path.is_dir());
if is_dir_for_permissions {
// For --link mode, copy attributes immediately to avoid O(n) memory
if options.copy_mode == CopyMode::Link {
copy_attributes(
&entry.source_absolute,
&entry.local_to_target,
&options.attributes,
)?;
continue;
}
// Add this directory to our list for permission fixing later
dirs_needing_permissions
.push((entry.source_absolute.clone(), entry.local_to_target.clone()));
Expand Down
16 changes: 11 additions & 5 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,9 @@ fn copy_file(
return Err(translate!("cp-error-cannot-change-attribute", "dest" => dest.quote()).into());
}

if options.preserve_hard_links() {
// When using --link mode, hard link structure is automatically preserved
// because we link to source files (which share inodes).
if options.preserve_hard_links() && options.copy_mode != CopyMode::Link {
// if we encounter a matching device/inode pair in the source tree
// we can arrange to create a hard link between the corresponding names
// in the destination tree.
Expand Down Expand Up @@ -2536,10 +2538,14 @@ fn copy_file(
}
}

copied_files.insert(
FileInformation::from_path(source, options.dereference(source_in_command_line))?,
dest.to_path_buf(),
);
// Skip tracking copied files when using --link mode since hard link
// structure is automatically preserved
if options.copy_mode != CopyMode::Link {
copied_files.insert(
FileInformation::from_path(source, options.dereference(source_in_command_line))?,
dest.to_path_buf(),
);
}

if let Some(progress_bar) = progress_bar {
progress_bar.inc(source_metadata.len());
Expand Down
Loading