1
Fork 0
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:
Sylvestre Ledru 2022-06-20 17:27:11 +02:00 committed by GitHub
commit c277e933c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View file

@ -1256,8 +1256,17 @@ 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 {
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 {
// FIXME: print that the file was removed if --verbose is enabled

View file

@ -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!();