1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #3044 from water-ghosts/cp-dir-vs-file

cp: Fail when copying a directory to a file
This commit is contained in:
Sylvestre Ledru 2022-02-06 22:01:37 +01:00 committed by GitHub
commit 1ac45c9961
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -1025,7 +1025,10 @@ fn copy_directory(
if is_symlink && !options.dereference { if is_symlink && !options.dereference {
copy_link(&path, &local_to_target, symlinked_files)?; copy_link(&path, &local_to_target, symlinked_files)?;
} else if path.is_dir() && !local_to_target.exists() { } else if path.is_dir() && !local_to_target.exists() {
or_continue!(fs::create_dir_all(local_to_target)); if target.is_file() {
return Err("cannot overwrite non-directory with directory".into());
}
fs::create_dir_all(local_to_target)?;
} else if !path.is_dir() { } else if !path.is_dir() {
if preserve_hard_links { if preserve_hard_links {
let mut found_hard_link = false; let mut found_hard_link = false;

View file

@ -1462,3 +1462,12 @@ fn test_cp_archive_on_nonexistent_file() {
"cp: cannot stat 'nonexistent_file.txt': No such file or directory (os error 2)", "cp: cannot stat 'nonexistent_file.txt': No such file or directory (os error 2)",
); );
} }
#[test]
fn test_cp_dir_vs_file() {
new_ucmd!()
.arg("-R")
.arg(TEST_COPY_FROM_FOLDER)
.arg(TEST_EXISTING_FILE)
.fails()
.stderr_only("cp: cannot overwrite non-directory with directory");
}