Skip to content

ln: report same file for any backup mode - #15545

Merged
cakebaker merged 1 commit into
uutils:mainfrom
Priyanshu-choudhary:ln-backup-same-file
Oct 21, 2026
Merged

cakebaker merged 1 commit into
uutils:mainfrom
Priyanshu-choudhary:ln-backup-same-file

Conversation

@Priyanshu-choudhary

Copy link
Copy Markdown
Contributor

Fixes #15527

Problem

With --backup=simple or --backup=numbered, linking a file onto itself gives a misleading error. ln renames the destination to its backup first, so the source is gone by the time the hard link is created:

$ echo x > a
$ ln -f --backup=simple a a
ln: failed to create hard link 'a' => 'a': No such file or directory

GNU coreutils 9.12 reports ln: 'a' and 'a' are the same file. Both exit 1 and leave a unchanged.

The same thing happens without -f, with --backup=numbered, with ./a a, and with a directory target (ln -f --backup=simple d/a d).

Fix

link() already detects the same file before backing up, but only for --backup=existing. This PR runs that check for every backup mode except none, since every other mode renames the destination before linking. Symbolic links are still excluded: ln -sf --backup=simple a a stays valid and behaves like GNU.

Testing

Compared against GNU coreutils 9.12 (LC_ALL=C, Linux):

Command Before After / GNU 9.12
ln -f --backup=simple a a No such file or directory 'a' and 'a' are the same file
ln -f --backup=numbered a a No such file or directory 'a' and 'a' are the same file
ln --backup=simple a a (no -f) No such file or directory 'a' and 'a' are the same file
ln -f --backup=numbered ./a a No such file or directory './a' and 'a' are the same file
ln -f --backup=simple d/a d No such file or directory 'd/a' and 'd/a' are the same file
ln -f --backup=simple sl sl (symlink) No such file or directory 'sl' and 'sl' are the same file

Unchanged and still matching GNU: --backup=existing, --backup=none, -f -b a a, ln -sf --backup=simple a a (succeeds, keeps a~), and ln -f --backup=simple a b where b is a hard link to a (succeeds, creates b~).

  • Added test_backup_simple_numbered_same_file and test_backup_simple_same_file_into_directory. Both failed before the fix and pass after it.
  • cargo test --features ln --no-default-features test_ln: 64 passed.
  • cargo fmt --check and cargo clippy -p uu_ln --all-targets -- -D warnings are clean.

@Priyanshu-choudhary
Priyanshu-choudhary marked this pull request as ready for review October 20, 2026 13:56
With --backup=simple or --backup=numbered, linking a file onto itself
renamed the destination to its backup before linking, so the link then
failed with a misleading "No such file or directory" error. Run the
same-file check for every backup mode except none, as was already done
for --backup=existing.

Fixes uutils#15527
@cakebaker
cakebaker merged commit 720381d into uutils:main Oct 21, 2026
98 checks passed
@cakebaker

Copy link
Copy Markdown
Contributor

Thanks for your PR!

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.

ln -f with a simple or numbered backup: wrong error for linking a file onto itself

2 participants