From c370b678b12adcaa9aa5cdba0367d63d7d22b0f4 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Fri, 23 Sep 2022 18:22:14 -0400 Subject: [PATCH] cp: refactor copy_attributes() function Create a `copy_attributes()` function to contain the loop that copies each of a specified set of attributes in turn. --- src/uu/cp/src/cp.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index c60a0a02b..b1b3ec901 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1206,6 +1206,14 @@ impl OverwriteMode { } } +/// Copy the specified attributes from one path to another. +fn copy_attributes(source: &Path, dest: &Path, attributes: &[Attribute]) -> CopyResult<()> { + for attribute in attributes { + copy_attribute(source, dest, attribute)?; + } + Ok(()) +} + fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResult<()> { let context = &*format!("{} -> {}", source.quote(), dest.quote()); let source_metadata = fs::symlink_metadata(source).context(context)?; @@ -1545,9 +1553,7 @@ fn copy_file( // the user does not have permission to write to the file. fs::set_permissions(dest, dest_permissions).ok(); } - for attribute in &options.preserve_attributes { - copy_attribute(source, dest, attribute)?; - } + copy_attributes(source, dest, &options.preserve_attributes)?; Ok(()) }