Skip to content
Merged
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
14 changes: 5 additions & 9 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,9 @@ fn handle_existing_dest(
return Err(format!("{} and {} are the same file", source.quote(), dest.quote()).into());
}

options.overwrite.verify(dest)?;
if options.update != UpdateMode::ReplaceIfOlder {
options.overwrite.verify(dest)?;
}

let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
if let Some(backup_path) = backup_path {
Expand Down Expand Up @@ -1770,6 +1772,8 @@ fn handle_copy_mode(
if src_time <= dest_time {
return Ok(());
} else {
options.overwrite.verify(dest)?;

copy_helper(
source,
dest,
Expand Down Expand Up @@ -1866,14 +1870,6 @@ fn copy_file(
copied_files: &mut HashMap<FileInformation, PathBuf>,
source_in_command_line: bool,
) -> CopyResult<()> {
if (options.update == UpdateMode::ReplaceIfOlder || options.update == UpdateMode::ReplaceNone)
&& options.overwrite == OverwriteMode::Interactive(ClobberMode::Standard)
{
// `cp -i --update old new` when `new` exists doesn't copy anything
// and exit with 0
return Ok(());
}

// Fail if dest is a dangling symlink or a symlink this program created previously
if dest.is_symlink() {
if FileInformation::from_path(dest, false)
Expand Down
31 changes: 14 additions & 17 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,6 @@ fn test_cp_target_directory_is_file() {
.stderr_contains(format!("'{TEST_HOW_ARE_YOU_SOURCE}' is not a directory"));
}

#[test]
// FixMe: for FreeBSD, flaky test; track repair progress at GH:uutils/coreutils/issue/4725
#[cfg(not(target_os = "freebsd"))]
fn test_cp_arg_update_interactive() {
new_ucmd!()
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("-i")
.arg("--update")
.succeeds()
.no_stdout()
.no_stderr();
}

#[test]
fn test_cp_arg_update_interactive_error() {
new_ucmd!()
Expand Down Expand Up @@ -498,7 +484,7 @@ fn test_cp_arg_interactive() {

#[test]
#[cfg(not(any(target_os = "android", target_os = "freebsd")))]
fn test_cp_arg_interactive_update() {
fn test_cp_arg_interactive_update_overwrite_newer() {
// -u -i won't show the prompt to validate the override or not
// Therefore, the error code will be 0
let (at, mut ucmd) = at_and_ucmd!();
Expand All @@ -517,19 +503,30 @@ fn test_cp_arg_interactive_update() {

#[test]
#[cfg(not(any(target_os = "android", target_os = "freebsd")))]
#[ignore = "known issue #6019"]
fn test_cp_arg_interactive_update_newer() {
fn test_cp_arg_interactive_update_overwrite_older() {
// -u -i *WILL* show the prompt to validate the override.
// Therefore, the error code depends on the prompt response.
// Option N
let (at, mut ucmd) = at_and_ucmd!();
at.touch("b");
std::thread::sleep(Duration::from_secs(1));
at.touch("a");
ucmd.args(&["-i", "-u", "a", "b"])
.pipe_in("N\n")
.fails()
.code_is(1)
.no_stdout()
.stderr_is("cp: overwrite 'b'? ");

// Option Y
let (at, mut ucmd) = at_and_ucmd!();
at.touch("b");
std::thread::sleep(Duration::from_secs(1));
at.touch("a");
ucmd.args(&["-i", "-u", "a", "b"])
.pipe_in("Y\n")
.succeeds()
.no_stdout();
}

#[test]
Expand Down