diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 1196b83b4df..bb2d4d9feec 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -2603,7 +2603,7 @@ fn handle_copy_mode( ) })?; } - CopyMode::Copy => { + CopyMode::Copy | CopyMode::AttrOnly => { copy_helper( source, dest, @@ -2687,42 +2687,6 @@ fn handle_copy_mode( )?; } } - CopyMode::AttrOnly => { - // The destination must have the source's type. Creating a regular - // file for a FIFO or a device both gets the type wrong and, for a - // FIFO, makes the later xattr copy open it and block forever with - // no writer. - #[cfg(unix)] - { - let ft = source_metadata.file_type(); - if ft.is_fifo() { - return copy_fifo(dest, options.overwrite, options.debug) - .map(|()| PerformedAction::Copied); - } - if ft.is_socket() { - return copy_socket(dest, options.overwrite, options.debug) - .map(|()| PerformedAction::Copied); - } - if ft.is_char_device() || ft.is_block_device() { - return copy_node(dest, source_metadata, options.overwrite, options.debug) - .map(|()| PerformedAction::Copied); - } - } - OpenOptions::new() - .write(true) - .truncate(false) - .create(true) - .open(dest) - .map_err(|e| { - CpError::IoErrContext( - e, - translate!( - "cp-error-cannot-create-regular-file", - "path" => dest.quote() - ), - ) - })?; - } } Ok(PerformedAction::Copied) @@ -3141,8 +3105,8 @@ fn handle_no_preserve_mode(options: &Options, org_mode: u32) -> u32 { org_mode } -/// Copy the file from `source` to `dest` either using the normal `fs::copy` or a -/// copy-on-write scheme if --reflink is specified and the filesystem supports it. +/// Create the destination according to the source file type and copy its data +/// unless only attributes were requested. #[allow(clippy::too_many_arguments)] fn copy_helper( source: &Path, @@ -3165,8 +3129,11 @@ fn copy_helper( return Err(CpError::NotADirectory(dest.to_path_buf())); } + // Library callers can select AttrOnly without setting the CLI flag. + // Conversely, -u --attributes-only selects Update and sets the flag. + let attributes_only = options.attributes_only || options.copy_mode == CopyMode::AttrOnly; #[cfg(unix)] - if options.recursive && !options.copy_contents { + if attributes_only || (options.recursive && !options.copy_contents) { let ft = source_metadata.file_type(); if ft.is_socket() { return copy_socket(dest, options.overwrite, options.debug); @@ -3181,6 +3148,21 @@ fn copy_helper( if source_metadata.is_symlink() { copy_link(source, dest, symlinked_files, options)?; + } else if attributes_only { + OpenOptions::new() + .write(true) + .truncate(false) + .create(true) + .open(dest) + .map_err(|e| { + CpError::IoErrContext( + e, + translate!( + "cp-error-cannot-create-regular-file", + "path" => dest.quote() + ), + ) + })?; } else { // Use O_NOFOLLOW on the source open iff cp is in no-dereference mode. // In that case source_metadata was obtained via lstat, so a path swap @@ -3202,7 +3184,7 @@ fn copy_helper( nofollow, )?; - if !options.attributes_only && options.debug { + if options.debug { show_debug(©_debug) .map_err(|e| CpError::IoErrContext(e, translate!("cp-error-write")))?; } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 06a9178ccf2..8ceba23536e 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -5545,6 +5545,99 @@ fn test_cp_attributes_only() { assert_eq!(mode_b, at.metadata(b).mode()); } +#[test] +fn test_cp_attr_only_copy_mode_preserves_contents() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.write("source", "source data"); + at.write("dest", "keep this"); + + let options = cp::Options { + copy_mode: cp::CopyMode::AttrOnly, + ..cp::Options::default() + }; + cp::copy(&[at.plus("source")], &at.plus("dest"), &options).unwrap(); + + assert_eq!(at.read("dest"), "keep this"); +} + +#[rstest] +#[case::empty_template("")] +#[case::nonempty_template("template contents")] +fn test_cp_update_attributes_only(#[case] template: &str) { + use filetime::{FileTime, set_file_mtime}; + + let (at, mut ucmd) = at_and_ucmd!(); + let src_time = FileTime::from_unix_time(1_600_003_600, 0); + let dest_time = FileTime::from_unix_time(1_600_000_000, 0); + at.write("template", template); + at.write("document", "keep this"); + set_file_mtime(at.plus("template"), src_time).unwrap(); + set_file_mtime(at.plus("document"), dest_time).unwrap(); + + ucmd.args(&[ + "-u", + "--attributes-only", + "--preserve=timestamps", + "template", + "document", + ]) + .succeeds() + .no_output(); + + assert_eq!(at.read("document"), "keep this"); + assert_eq!( + FileTime::from_last_modification_time(&at.metadata("document")), + src_time + ); +} + +#[test] +fn test_cp_update_attributes_only_missing_destination() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("template", "template contents"); + + ucmd.args(&["-u", "--attributes-only", "template", "new"]) + .succeeds() + .no_output(); + + assert_eq!(at.read("new"), ""); +} + +#[test] +fn test_cp_remove_destination_attributes_only_regular_file() { + use filetime::{FileTime, set_file_mtime}; + + let (at, mut ucmd) = at_and_ucmd!(); + at.write("template", "template contents"); + at.write("document", "old contents"); + #[cfg(unix)] + let old_document = at.open("document"); + let src_time = FileTime::from_unix_time(1_600_003_600, 0); + set_file_mtime(at.plus("template"), src_time).unwrap(); + + ucmd.args(&[ + "--remove-destination", + "--attributes-only", + "--preserve=timestamps", + "template", + "document", + ]) + .succeeds() + .no_output(); + + assert_eq!(at.read("document"), ""); + #[cfg(unix)] + assert_eq!( + std::io::read_to_string(old_document).unwrap(), + "old contents" + ); + assert_eq!( + FileTime::from_last_modification_time(&at.metadata("document")), + src_time + ); +} + #[test] #[cfg(unix)] fn test_cp_attributes_only_dest_open_error() {