Skip to content

Commit

Permalink
fix-issue#6266
Browse files Browse the repository at this point in the history
  • Loading branch information
Luv-Ray committed Jul 20, 2024
1 parent 5baf382 commit c95617d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,11 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
let dest = construct_dest_path(source, target, target_type, options)
.unwrap_or_else(|_| target.to_path_buf());

if fs::metadata(&dest).is_ok() && !fs::symlink_metadata(&dest)?.file_type().is_symlink()
if (fs::metadata(&dest).is_ok()
&& !fs::symlink_metadata(&dest)?.file_type().is_symlink())
// if both `source` and `dest` are symlinks, it should be considered as an overwrite.
|| (fs::metadata(source).is_ok()
&& fs::symlink_metadata(source)?.file_type().is_symlink())
{
// There is already a file and it isn't a symlink (managed in a different place)
if copied_destinations.contains(&dest)
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5696,3 +5696,26 @@ fn test_cp_parents_absolute_path() {
let res = format!("dest{}/a/b/f", at.root_dir_resolved());
at.file_exists(res);
}

#[test]
fn test_copy_symlink_overwrite() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
at.mkdir("b");
at.mkdir("c");
at.write("t", "hello");
at.relative_symlink_file("../t", "a/1");
at.relative_symlink_file("../t", "b/1");

ucmd.arg("--no-dereference")
.arg("a/1")
.arg("b/1")
.arg("c")
.fails()
.stderr_only(if cfg!(not(target_os = "windows")) {
"cp: will not overwrite just-created 'c/1' with 'b/1'\n"
} else {
"cp: will not overwrite just-created 'c\\1' with 'b/1'\n"
});
assert_eq!(at.read("c/1"), "hello");
}

0 comments on commit c95617d

Please sign in to comment.