Skip to content

Commit

Permalink
cp: correct error message on copying dir to itself
Browse files Browse the repository at this point in the history
Correct the error message produced when attempting to copy a directory
into itself with `cp`. Before this commit, the error message was

    $ cp -R d d
    cp: cannot copy a directory, 'd', into itself, 'd'

After this commit, the error message is

    $ cp -R d d
    cp: cannot copy a directory, 'd', into itself, 'd/d'
  • Loading branch information
jfinkels committed Sep 25, 2022
1 parent b43bbe9 commit c6797e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ fn copy_directory(
return Err(format!(
"cannot copy a directory, {}, into itself, {}",
root.quote(),
target.quote()
root.join(target).quote()
)
.into());
}
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2077,3 +2077,13 @@ fn test_cp_mode_hardlink_no_dereference() {
assert!(at.symlink_exists("z"));
assert_eq!(at.read_symlink("z"), "slink");
}

/// Test that copying a directory to itself is disallowed.
#[test]
fn test_copy_directory_to_itself_disallowed() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("d");
ucmd.args(&["-R", "d", "d"])
.fails()
.stderr_only("cp: cannot copy a directory, 'd', into itself, 'd/d'");
}

0 comments on commit c6797e2

Please sign in to comment.