coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

cp: symlink overwrite-detection is broken

Open BenWiederhake opened this issue 1 year ago • 0 comments

Steps to reproduce:

  • Create two regular files, README.md and good/README.md, and an initially-empty directory tmp/.
  • Inside tmp, create an empty regular file foo, and a symlink README.md that points to foo.
  • Try to copy README.md and good/README.md to tmp/

Expected result: README.md is copied to tmp/ into the file foo, and the second attempt is aborted, leading to an error message and a non-zero (failure) exit code.

Actual result: README.md is copied to tmp/ into the file foo, and the second attempt is successful, leading to foo containing the contents of the second file, no error message, and a zero (success) exit code.

Demonstrator:

$ mkdir -p good && cp README.md good/README.md
$ rm -rf tmp/ && mkdir tmp/ && ln -s foo tmp/README.md && touch tmp/foo
$ ../gnu/src/cp README.md good/README.md tmp # Yes error!
../gnu/src/cp: will not copy 'good/README.md' through just-created symlink 'tmp/README.md'
[$? = 1]
$
$ mkdir -p good && cp README.md good/README.md
$ rm -rf tmp/ && mkdir tmp/ && ln -s foo tmp/README.md && touch tmp/foo
$ cargo run cp README.md good/README.md tmp # No error?!
$

Found while reviewing #6208

BenWiederhake avatar Apr 24 '24 00:04 BenWiederhake