Skip to content

cp: refuse to copy symlink over itself #7729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dan-hipschman
Copy link
Contributor

Fixes #6589

Here's a quick recap of the issue:

# Test setup
$ touch exists
$ ln -s exists good
$ ln -s nonexistent bad

# GNU test
$ cp -P good bad .
cp: 'good' and './good' are the same file
cp: 'bad' and './bad' are the same file

# uutils test
$ cargo run -q cp -P good bad .  # succeeds!

This doesn't affect the GNU compatibility tests.

Note that the other cases described in #6589 have the correct behavior and exit statuses, just different error messages. So I didn't bother to change those since I don't think matching error messages is a goal of this project (correct me if I'm wrong).

if dest_is_symlink
&& source_is_symlink
&& source.file_name() != dest.file_name()
&& !options.dereference
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, if source and dest are both the same symlink but with different names, then the existing behavior (cp succeeds) is correct. This can occur, for example, if source and dest are both hardlinks to the same symlink. There's actually an existing unit test for this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add this as a comment in the code too ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"hardlinks to the same symlink" also sounds like something that we should include in the test suite, especially because this PR does NOT handle it correctly, despite your comment:

# Test setup
$ ln -s nonexistent bad
$ cp -P --link bad bad2
$ ls -1i bad bad2
20145169 bad
20145169 bad2

# GNU
$ cp -P bad bad2
$ ls -1i bad bad2
20145169 bad
20145169 bad2

# This PR (7677be2075a0401f05fdf9e138d821a5673f4404)
$ cargo run -q cp -P bad bad2
$ ls -1i bad bad2
20145169 bad
20178774 bad2

As you can see, the second file is no longer a hardlink of the first, even though it should still be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Interesting test case. I looked into it and it seems related but separate from this PR (it occurs on main too BTW), so I made a separate PR for it: #7753

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I thought it's the same issue, sorry :D

@dan-hipschman dan-hipschman force-pushed the cp-refuse-to-copy-symlink-over-itself branch from 610ad10 to a43a2ff Compare April 11, 2025 19:50
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)

@dan-hipschman dan-hipschman force-pushed the cp-refuse-to-copy-symlink-over-itself branch from a43a2ff to 7677be2 Compare April 13, 2025 15:23
Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/head/head-c is no longer failing!

@sylvestre sylvestre merged commit e92e419 into uutils:main Apr 28, 2025
69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cp: copying symlink over itself is broken
3 participants