Skip to content

Commit

Permalink
Merge branch 'main' into show_tab_size_too_large_error
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre authored Jun 20, 2022
2 parents ad2bb47 + c277e93 commit 86644b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,16 @@ fn handle_existing_dest(source: &Path, dest: &Path, options: &Options) -> CopyRe

let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
if let Some(backup_path) = backup_path {
backup_dest(dest, &backup_path)?;
if paths_refer_to_same_file(source, &backup_path)? {
return Err(format!(
"backing up {} might destroy source; {} not copied",
dest.quote(),
source.quote()
)
.into());
} else {
backup_dest(dest, &backup_path)?;
}
}

match options.overwrite {
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ fn test_cp_backup_simple() {
);
}

#[test]
fn test_cp_backup_simple_protect_source() {
let (at, mut ucmd) = at_and_ucmd!();
let source = format!("{}~", TEST_HELLO_WORLD_SOURCE);
at.touch(&source);
ucmd.arg("--backup=simple")
.arg(&source)
.arg(TEST_HELLO_WORLD_SOURCE)
.fails()
.stderr_only(format!(
"cp: backing up '{}' might destroy source; '{}' not copied",
TEST_HELLO_WORLD_SOURCE, source,
));

assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), "Hello, World!\n");
assert_eq!(at.read(&source), "");
}

#[test]
fn test_cp_backup_never() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down

0 comments on commit 86644b8

Please sign in to comment.