1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

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.
This commit is contained in:
Jeffrey Finkelstein 2022-09-23 18:22:14 -04:00
parent 2f88ba8628
commit c370b678b1

View file

@ -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<()> { fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResult<()> {
let context = &*format!("{} -> {}", source.quote(), dest.quote()); let context = &*format!("{} -> {}", source.quote(), dest.quote());
let source_metadata = fs::symlink_metadata(source).context(context)?; 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. // the user does not have permission to write to the file.
fs::set_permissions(dest, dest_permissions).ok(); fs::set_permissions(dest, dest_permissions).ok();
} }
for attribute in &options.preserve_attributes { copy_attributes(source, dest, &options.preserve_attributes)?;
copy_attribute(source, dest, attribute)?;
}
Ok(()) Ok(())
} }