mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #3647 from philgebhardt/backup-protect-source
cp: make `--b=simple` protective of source
This commit is contained in:
commit
c277e933c9
2 changed files with 28 additions and 1 deletions
|
@ -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);
|
let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
|
||||||
if let Some(backup_path) = backup_path {
|
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 {
|
match options.overwrite {
|
||||||
|
|
|
@ -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]
|
#[test]
|
||||||
fn test_cp_backup_never() {
|
fn test_cp_backup_never() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue