Skip to content

Commit

Permalink
Merge pull request #5762 from cakebaker/cp_link_same_file
Browse files Browse the repository at this point in the history
cp: show no "same file" error for `--link a a`
  • Loading branch information
sylvestre committed Jan 14, 2024
2 parents ef03a46 + 7ddeba4 commit e3beda0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub enum TargetType {
}

/// Copy action to perform
#[derive(PartialEq)]
pub enum CopyMode {
Link,
SymLink,
Expand Down Expand Up @@ -1714,6 +1715,7 @@ fn copy_file(
&& !options.force()
&& options.backup == BackupMode::NoBackup
&& source != dest
|| (source == dest && options.copy_mode == CopyMode::Link)
{
return Ok(());
}
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,22 @@ fn test_cp_arg_link_with_dest_hardlink_to_source() {
assert!(at.file_exists(hardlink));
}

#[test]
#[cfg(target_os = "linux")]
fn test_cp_arg_link_with_same_file() {
use std::os::linux::fs::MetadataExt;

let (at, mut ucmd) = at_and_ucmd!();
let file = "file";

at.touch(file);

ucmd.args(&["--link", file, file]).succeeds();

assert_eq!(at.metadata(file).st_nlink(), 1);
assert!(at.file_exists(file));
}

#[test]
fn test_cp_arg_symlink() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down

0 comments on commit e3beda0

Please sign in to comment.