]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
cp: Never follow symbolic links in destination.
authorDag-Erling Smørgrav <des@FreeBSD.org>
Mon, 8 Apr 2024 22:41:33 +0000 (00:41 +0200)
committerDag-Erling Smørgrav <des@FreeBSD.org>
Mon, 8 Apr 2024 22:41:33 +0000 (00:41 +0200)
commit0729d1e8fd90afc2f19e9b9834533181caf6ddee
treeb0a34d69db8886dfc9dbdfea8754be6e58880ee6
parentd80a97def9a1db6f07f5d2e68f7ad62b27918947
cp: Never follow symbolic links in destination.

Historically, BSD cp has followed symbolic links in the destination
when copying recursively, while GNU cp has not.  POSIX is somewhat
vague on the topic, but both interpretations are within bounds.  In
33ad990ce974, cp was changed to apply the same logic for symbolic
links in the destination as for symbolic links in the source: follow
if not recursing (which is moot, as this situation can only arise
while recursing) or if the `-L` option was given.  There is no support
for this in POSIX.  We can either switch back, or go all the way.

Having carefully weighed the kind of trouble you can run into by
following unexpected symlinks up against the kind of trouble you can
run into by not following symlinks you expected to follow, we choose
to go all the way.

Note that this means we need to stat the destination twice: once,
following links, to check if it is or references the same file as the
source, and a second time, not following links, to set the dne flag
and determine the destination's type.

While here, remove a needless complication in the dne logic.  We don't
need to explicitly reject overwriting a directory with a non-directory,
because it will fail anyway.

Finally, add test cases for copying a directory to a symlink and
overwriting a directory with a non-directory.

MFC after: never
Relnotes:  yes
Sponsored by: Klara, Inc.
Reviewed by: kevans
Differential Revision: https://reviews.freebsd.org/D44578
bin/cp/cp.c
bin/cp/tests/cp_test.sh