Skip to content

Commit c95617d

Browse files
committed
fix-issue#6266
1 parent 5baf382 commit c95617d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/uu/cp/src/cp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,11 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
12661266
let dest = construct_dest_path(source, target, target_type, options)
12671267
.unwrap_or_else(|_| target.to_path_buf());
12681268

1269-
if fs::metadata(&dest).is_ok() && !fs::symlink_metadata(&dest)?.file_type().is_symlink()
1269+
if (fs::metadata(&dest).is_ok()
1270+
&& !fs::symlink_metadata(&dest)?.file_type().is_symlink())
1271+
// if both `source` and `dest` are symlinks, it should be considered as an overwrite.
1272+
|| (fs::metadata(source).is_ok()
1273+
&& fs::symlink_metadata(source)?.file_type().is_symlink())
12701274
{
12711275
// There is already a file and it isn't a symlink (managed in a different place)
12721276
if copied_destinations.contains(&dest)

tests/by-util/test_cp.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5696,3 +5696,26 @@ fn test_cp_parents_absolute_path() {
56965696
let res = format!("dest{}/a/b/f", at.root_dir_resolved());
56975697
at.file_exists(res);
56985698
}
5699+
5700+
#[test]
5701+
fn test_copy_symlink_overwrite() {
5702+
let (at, mut ucmd) = at_and_ucmd!();
5703+
at.mkdir("a");
5704+
at.mkdir("b");
5705+
at.mkdir("c");
5706+
at.write("t", "hello");
5707+
at.relative_symlink_file("../t", "a/1");
5708+
at.relative_symlink_file("../t", "b/1");
5709+
5710+
ucmd.arg("--no-dereference")
5711+
.arg("a/1")
5712+
.arg("b/1")
5713+
.arg("c")
5714+
.fails()
5715+
.stderr_only(if cfg!(not(target_os = "windows")) {
5716+
"cp: will not overwrite just-created 'c/1' with 'b/1'\n"
5717+
} else {
5718+
"cp: will not overwrite just-created 'c\\1' with 'b/1'\n"
5719+
});
5720+
assert_eq!(at.read("c/1"), "hello");
5721+
}

0 commit comments

Comments
 (0)