Skip to content
Open
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
64 changes: 23 additions & 41 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,7 @@ fn handle_copy_mode(
)
})?;
}
CopyMode::Copy => {
CopyMode::Copy | CopyMode::AttrOnly => {
copy_helper(
source,
dest,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopyMode::AttrOnly already implies options.attributes_only, isn't the second check redundant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s redundant for CLI calls, but Rust callers can set CopyMode::AttrOnly while leaving attributes_only at its default of false. I kept the check and added a test for that case. I also added the --remove-destination --attributes-only regression test

#[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);
Expand All @@ -3181,6 +3148,21 @@ fn copy_helper(

if source_metadata.is_symlink() {
copy_link(source, dest, symlinked_files, options)?;
} else if attributes_only {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also changes --remove-destination --attributes-only on a regular file (it now gives an empty dest instead of the data), could you please add a test for it?

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
Expand All @@ -3202,7 +3184,7 @@ fn copy_helper(
nofollow,
)?;

if !options.attributes_only && options.debug {
if options.debug {
show_debug(&copy_debug)
.map_err(|e| CpError::IoErrContext(e, translate!("cp-error-write")))?;
}
Expand Down
93 changes: 93 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading