From 934e85f4cd255cbdc1aa5bc6e02617809ac4eb78 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 6 Jan 2025 09:40:41 +0100 Subject: [PATCH] cp: make --backup and --no-clobber are mutually exclusive (#7082) * cp: make --backup and --no-clobber are mutually exclusive Should fix tests/cp/cp-i.sh * simplify the test Co-authored-by: Daniel Hofstetter --------- Co-authored-by: Daniel Hofstetter --- src/uu/cp/src/cp.rs | 10 ++++++++++ tests/by-util/test_cp.rs | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index c1ebc20bf..b87898c78 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -932,6 +932,16 @@ impl Options { }; let update_mode = update_control::determine_update_mode(matches); + if backup_mode != BackupMode::NoBackup + && matches + .get_one::(update_control::arguments::OPT_UPDATE) + .map_or(false, |v| v == "none" || v == "none-fail") + { + return Err(Error::InvalidArgument( + "--backup is mutually exclusive with -n or --update=none-fail".to_string(), + )); + } + let backup_suffix = backup_control::determine_backup_suffix(matches); let overwrite = OverwriteMode::from_matches(matches); diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index fc56dc316..2132b9363 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -2422,6 +2422,17 @@ fn test_cp_reflink_bad() { .stderr_contains("error: invalid value 'bad' for '--reflink[=]'"); } +#[test] +fn test_cp_conflicting_update() { + new_ucmd!() + .arg("-b") + .arg("--update=none") + .arg("a") + .arg("b") + .fails() + .stderr_contains("--backup is mutually exclusive with -n or --update=none-fail"); +} + #[test] #[cfg(any(target_os = "linux", target_os = "android"))] fn test_cp_reflink_insufficient_permission() {