Skip to content

Commit 0be3ca6

Browse files
committed
cp: Remove unneeded check
1 parent ccc9598 commit 0be3ca6

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/uu/cp/src/copydir.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub(crate) fn copy_directory(
506506
copy_attributes(
507507
&entry.source_absolute,
508508
&entry.local_to_target,
509-
options,
509+
&options.attributes,
510510
created_dir,
511511
)?;
512512
}
@@ -524,7 +524,12 @@ pub(crate) fn copy_directory(
524524
// Fix permissions for all directories we created
525525
// This ensures that even sibling directories get their permissions fixed
526526
for (source_path, dest_path, is_dir_created) in dirs_needing_permissions {
527-
copy_attributes(&source_path, &dest_path, options, is_dir_created)?;
527+
copy_attributes(
528+
&source_path,
529+
&dest_path,
530+
&options.attributes,
531+
is_dir_created,
532+
)?;
528533
}
529534

530535
// Also fix permissions for parent directories,
@@ -533,7 +538,7 @@ pub(crate) fn copy_directory(
533538
let dest = target.join(root.file_name().unwrap());
534539
for (x, y) in aligned_ancestors(root, dest.as_path()) {
535540
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
536-
copy_attributes(&src, y, options, false)?;
541+
copy_attributes(&src, y, &options.attributes, false)?;
537542
}
538543
}
539544
}

src/uu/cp/src/cp.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ fn copy_source(
15321532
if options.parents {
15331533
for (x, y) in aligned_ancestors(source, dest.as_path()) {
15341534
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
1535-
copy_attributes(&src, y, options, false)?;
1535+
copy_attributes(&src, y, &options.attributes, false)?;
15361536
}
15371537
}
15381538
}
@@ -1685,25 +1685,24 @@ fn copy_extended_attrs(source: &Path, dest: &Path) -> CopyResult<()> {
16851685
pub(crate) fn copy_attributes(
16861686
source: &Path,
16871687
dest: &Path,
1688-
options: &Options,
1689-
is_dir_created: bool,
1688+
attributes: &Attributes,
1689+
is_dest_created: bool,
16901690
) -> CopyResult<()> {
16911691
let context = &*format!("{} -> {}", source.quote(), dest.quote());
16921692
let source_metadata =
16931693
fs::symlink_metadata(source).map_err(|e| CpError::IoErrContext(e, context.to_owned()))?;
16941694

1695-
let is_preserve_required = matches!(options.attributes.mode, Preserve::Yes { required: true });
1696-
let is_explicit_true = matches!(options.attributes.mode, Preserve::No { explicit: true });
1695+
let is_explicit = matches!(attributes.mode, Preserve::No { explicit: true });
16971696

1698-
let mode = if !is_preserve_required && !is_explicit_true && dest.is_dir() && is_dir_created {
1697+
let mode = if !is_explicit && dest.is_dir() && is_dest_created {
16991698
Preserve::Yes { required: false }
17001699
} else {
1701-
options.attributes.mode
1700+
attributes.mode
17021701
};
17031702

17041703
// Ownership must be changed first to avoid interfering with mode change.
17051704
#[cfg(unix)]
1706-
handle_preserve(&options.attributes.ownership, || -> CopyResult<()> {
1705+
handle_preserve(&attributes.ownership, || -> CopyResult<()> {
17071706
use std::os::unix::prelude::MetadataExt;
17081707
use uucore::perms::Verbosity;
17091708
use uucore::perms::VerbosityLevel;
@@ -1749,7 +1748,7 @@ pub(crate) fn copy_attributes(
17491748
#[cfg(unix)]
17501749
{
17511750
let mut perms = source_metadata.permissions();
1752-
if is_dir_created && !is_preserve_required && !is_explicit_true {
1751+
if is_dest_created && !is_explicit {
17531752
use uucore::mode::get_umask;
17541753
let mode = perms.mode() & !get_umask();
17551754
perms.set_mode(mode);
@@ -1768,7 +1767,7 @@ pub(crate) fn copy_attributes(
17681767
Ok(())
17691768
})?;
17701769

1771-
handle_preserve(&options.attributes.timestamps, || -> CopyResult<()> {
1770+
handle_preserve(&attributes.timestamps, || -> CopyResult<()> {
17721771
let atime = FileTime::from_last_access_time(&source_metadata);
17731772
let mtime = FileTime::from_last_modification_time(&source_metadata);
17741773
if dest.is_symlink() {
@@ -1781,7 +1780,7 @@ pub(crate) fn copy_attributes(
17811780
})?;
17821781

17831782
#[cfg(all(feature = "selinux", target_os = "linux"))]
1784-
handle_preserve(&options.attributes.context, || -> CopyResult<()> {
1783+
handle_preserve(&attributes.context, || -> CopyResult<()> {
17851784
// Get the source context and apply it to the destination
17861785
if let Ok(context) = selinux::SecurityContext::of_path(source, false, false) {
17871786
if let Some(context) = context {
@@ -1799,7 +1798,7 @@ pub(crate) fn copy_attributes(
17991798
Ok(())
18001799
})?;
18011800

1802-
handle_preserve(&options.attributes.xattr, || -> CopyResult<()> {
1801+
handle_preserve(&attributes.xattr, || -> CopyResult<()> {
18031802
#[cfg(all(unix, not(target_os = "android")))]
18041803
{
18051804
copy_extended_attrs(source, dest)?;
@@ -2564,14 +2563,14 @@ fn copy_file(
25642563
.ok()
25652564
.filter(|p| p.exists())
25662565
.unwrap_or_else(|| source.to_path_buf());
2567-
copy_attributes(&src_for_attrs, dest, options, false)?;
2566+
copy_attributes(&src_for_attrs, dest, &options.attributes, false)?;
25682567
} else if source_is_stream && !source.exists() {
25692568
// Some stream files may not exist after we have copied it,
25702569
// like anonymous pipes. Thus, we can't really copy its
25712570
// attributes. However, this is already handled in the stream
25722571
// copy function (see `copy_stream` under platform/linux.rs).
25732572
} else {
2574-
copy_attributes(source, dest, options, false)?;
2573+
copy_attributes(source, dest, &options.attributes, false)?;
25752574
}
25762575

25772576
#[cfg(all(feature = "selinux", target_os = "linux"))]
@@ -2749,7 +2748,7 @@ fn copy_link(
27492748
delete_path(dest, options)?;
27502749
}
27512750
symlink_file(&link, dest, symlinked_files)?;
2752-
copy_attributes(source, dest, options, false)
2751+
copy_attributes(source, dest, &options.attributes, false)
27532752
}
27542753

27552754
/// Generate an error message if `target` is not the correct `target_type`

0 commit comments

Comments
 (0)