Page MenuHomeFreeBSD

cp: Never follow symbolic links in destination.
ClosedPublic

Authored by des on Apr 1 2024, 1:21 PM.
Tags
None
Referenced Files
F102142298: D44578.diff
Fri, Nov 8, 3:40 AM
Unknown Object (File)
Tue, Nov 5, 5:12 AM
Unknown Object (File)
Tue, Oct 22, 3:07 AM
Unknown Object (File)
Oct 7 2024, 2:37 AM
Unknown Object (File)
Oct 7 2024, 2:37 AM
Unknown Object (File)
Oct 7 2024, 2:36 AM
Unknown Object (File)
Oct 7 2024, 2:36 AM
Unknown Object (File)
Oct 7 2024, 2:04 AM
Subscribers

Details

Summary

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.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

des requested review of this revision.Apr 1 2024, 1:21 PM
This revision is now accepted and ready to land.Apr 1 2024, 2:28 PM

fix “same file” logic, add tests

This revision now requires review to proceed.Apr 1 2024, 3:14 PM
des edited the summary of this revision. (Show Details)
This revision is now accepted and ready to land.Apr 1 2024, 3:29 PM
This revision was automatically updated to reflect the committed changes.