1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cp: make --b=simple protective of source

When `--backup` is supplied, `cp` will take a backup of *destination* before *source* is copied. When `--backup=simple` is supplied, it is possible for the backup path for *destination* to equal the path for *source*, destroying source before the copy is made. This change prevents this by returning an error instead.

This fixes https://github.com/uutils/coreutils/issues/3629
This commit is contained in:
Phil Gebhardt 2022-06-18 03:53:18 -07:00
parent fa51f8b986
commit c49d8e6113
No known key found for this signature in database
GPG key ID: 0CB299805A6F020F
4 changed files with 33 additions and 1 deletions

View file

@ -38,6 +38,8 @@ static TEST_COPY_FROM_FOLDER: &str = "hello_dir_with_file/";
static TEST_COPY_FROM_FOLDER_FILE: &str = "hello_dir_with_file/hello_world.txt";
static TEST_COPY_TO_FOLDER_NEW: &str = "hello_dir_new";
static TEST_COPY_TO_FOLDER_NEW_FILE: &str = "hello_dir_new/hello_world.txt";
static TEST_PROTECT_BACKUP_SRC: &str = "protected.txt.bak";
static TEST_PROTECT_BACKUP_DEST: &str = "protected.txt";
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
static TEST_MOUNT_COPY_FROM_FOLDER: &str = "dir_with_mount";
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
@ -558,6 +560,25 @@ fn test_cp_backup_simple() {
);
}
#[test]
fn test_cp_backup_simple_protect_source() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("--backup=simple")
.arg("--suffix")
.arg(".bak")
.arg(TEST_PROTECT_BACKUP_SRC)
.arg(TEST_PROTECT_BACKUP_DEST)
.fails()
.stderr_only(format!(
"cp: backing up '{}' might destroy source; '{}' not copied",
TEST_PROTECT_BACKUP_DEST,
TEST_PROTECT_BACKUP_SRC,
));
assert_eq!(at.read(TEST_PROTECT_BACKUP_SRC), "original text\n");
assert_eq!(at.read(TEST_PROTECT_BACKUP_DEST), "new text\n");
}
#[test]
fn test_cp_backup_never() {
let (at, mut ucmd) = at_and_ucmd!();