Skip to content

Commit e92e419

Browse files
dan-hipschmansylvestre
authored andcommitted
cp: refuse to copy symlink over itself
1 parent d957e64 commit e92e419

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/uu/cp/src/cp.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,13 @@ fn is_forbidden_to_copy_to_same_file(
18021802
if options.copy_mode == CopyMode::SymLink && dest_is_symlink {
18031803
return false;
18041804
}
1805-
if dest_is_symlink && source_is_symlink && !options.dereference {
1805+
// If source and dest are both the same symlink but with different names, then allow the copy.
1806+
// This can occur, for example, if source and dest are both hardlinks to the same symlink.
1807+
if dest_is_symlink
1808+
&& source_is_symlink
1809+
&& source.file_name() != dest.file_name()
1810+
&& !options.dereference
1811+
{
18061812
return false;
18071813
}
18081814
true

tests/by-util/test_cp.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,6 +5218,31 @@ mod same_file {
52185218
assert_eq!(symlink1, at.resolve_link(symlink2));
52195219
}
52205220

5221+
#[test]
5222+
fn test_same_symlink_to_itself_no_dereference() {
5223+
let scene = TestScenario::new(util_name!());
5224+
let at = &scene.fixtures;
5225+
at.write(FILE_NAME, CONTENTS);
5226+
at.symlink_file(FILE_NAME, SYMLINK_NAME);
5227+
scene
5228+
.ucmd()
5229+
.args(&["-P", SYMLINK_NAME, SYMLINK_NAME])
5230+
.fails()
5231+
.stderr_contains("are the same file");
5232+
}
5233+
5234+
#[test]
5235+
fn test_same_dangling_symlink_to_itself_no_dereference() {
5236+
let scene = TestScenario::new(util_name!());
5237+
let at = &scene.fixtures;
5238+
at.symlink_file("nonexistent_file", SYMLINK_NAME);
5239+
scene
5240+
.ucmd()
5241+
.args(&["-P", SYMLINK_NAME, SYMLINK_NAME])
5242+
.fails()
5243+
.stderr_contains("are the same file");
5244+
}
5245+
52215246
// the following tests tries to copy file to a hardlink of the same file with
52225247
// various options
52235248
#[test]

0 commit comments

Comments
 (0)