From 8591d99c1b85852080519fec454c368314e48832 Mon Sep 17 00:00:00 2001 From: Emil Suleymanov Date: Wed, 9 Nov 2022 14:43:27 +0100 Subject: [PATCH 1/2] cp: make `--preserve` use the defaults when empty --- src/uu/cp/src/cp.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 9fbc727dd..39823ea45 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -745,7 +745,12 @@ impl Options { attributes.push(Attribute::from_str(attribute_str)?); } } - attributes + // `--preserve` case, use the defaults + if attributes.is_empty() { + DEFAULT_ATTRIBUTES.to_vec() + } else { + attributes + } } } } else if matches.get_flag(options::ARCHIVE) { From a6c48b084cb7ad7e98869b04f8cf7008d16155b3 Mon Sep 17 00:00:00 2001 From: Emil Suleymanov Date: Wed, 16 Nov 2022 01:39:25 +0100 Subject: [PATCH 2/2] test_cp: verify metadata for preserve no args case --- tests/by-util/test_cp.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index ade4d820f..306740e03 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -881,11 +881,29 @@ fn test_cp_issue_1665() { #[test] fn test_cp_preserve_no_args() { - new_ucmd!() - .arg(TEST_COPY_FROM_FOLDER_FILE) - .arg(TEST_HELLO_WORLD_DEST) + let (at, mut ucmd) = at_and_ucmd!(); + let src_file = "a"; + let dst_file = "b"; + + // Prepare the source file + at.touch(src_file); + #[cfg(unix)] + at.set_mode(src_file, 0o0500); + + // Copy + ucmd.arg(src_file) + .arg(dst_file) .arg("--preserve") .succeeds(); + + #[cfg(unix)] + { + // Assert that the mode, ownership, and timestamps are preserved + // NOTICE: the ownership is not modified on the src file, because that requires root permissions + let metadata_src = at.metadata(src_file); + let metadata_dst = at.metadata(dst_file); + assert_metadata_eq!(metadata_src, metadata_dst); + } } #[test]