Skip to content

cp: reject -s combined with -l - #16021

Open
Logic769 wants to merge 4 commits into
uutils:mainfrom
Logic769:cp-symbolic-link-hard-link-conflict
Open

Logic769 wants to merge 4 commits into
uutils:mainfrom
Logic769:cp-symbolic-link-hard-link-conflict

Conversation

@Logic769

Copy link
Copy Markdown
Contributor

Problem

cp -s -l a.txt b succeeds and creates b, whereas GNU cp rejects it:

cp: cannot make both hard and symbolic links
(exit code 1)

Change

Detect -s/--symbolic-link together with -l/--link and report an error. The flags are checked in the raw arguments because clap's mode overrides would otherwise let the later flag silently replace the earlier one. The corresponding -s/-l pairs were removed from test_cp_overriding_arguments, since they are now expected to conflict.

Tests

Added test_symbolic_link_and_link_conflict in tests/by-util/test_cp.rs.

Closes #15987

@xtqqczze

This comment was marked as outdated.

@fixfon

fixfon commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Scanning raw argv breaks valid calls that work on main. I built the PR head (2b5ab8f) and compared it with main and GNU cp 9.7:

command PR main GNU
cp -l -t./backups a.txt rc 1 rc 0 rc 0
cp -l -b -S.s a.txt b rc 1 rc 0 rc 0
cp -l -- -s.txt d rc 1 rc 0 rc 0
cp --sym -l a.txt c rc 0 rc 0 rc 1

The scan counts the s in attached values and in operands after --, and it misses the --sym abbreviation.

Letting clap handle it avoids this: drop the --link/--symbolic-link pair from the mutual overrides and declare conflicts_with between them instead. Tests for these four cases would cover the edge cases.

@Logic769
Logic769 force-pushed the cp-symbolic-link-hard-link-conflict branch 2 times, most recently from 9f4aeee to 0782e63 Compare October 30, 2026 13:02
@xtqqczze

Copy link
Copy Markdown
Collaborator

@fixfon Thanks for investigating, but please compare against GNU coreutils 9.12 or GNU coreutils master branch.

@fixfon

fixfon commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@xtqqczze Re-ran this against GNU coreutils 9.12 (release tarball, built locally on Linux) and the current head (0782e63):

command PR (0782e63) main GNU 9.12
cp -l -t./backups a.txt rc 0 rc 0 rc 0
cp -l -b -S.s a.txt b rc 0 rc 0 rc 0
cp -l -- -s.txt d rc 0 rc 0 rc 0
cp -s -l a.txt e rc 1 rc 0 rc 1
cp --sym -l a.txt c rc 0, c is a hard link rc 0 rc 1

9.12 behaves the same as 9.7 on all five. The new head fixes the first three, and -s -l now matches GNU. The abbreviated --sym still gets through, though: GNU fails with cannot make both hard and symbolic links and doesn't create c, while the PR makes a hard link.

@Logic769

Copy link
Copy Markdown
Contributor Author

The conflict check now reads long options the way clap does, so --sym and --symbol count as --symbolic-link and conflict with -l. Tests cover the three spellings, and the -t/-S/-- cases that a raw scan used to misread still pass.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@sylvestre
sylvestre force-pushed the cp-symbolic-link-hard-link-conflict branch from cb53e05 to afac51c Compare November 2, 2026 23:40
@sylvestre

Copy link
Copy Markdown
Contributor

a bunch of jobs are failing

@Logic769
Logic769 force-pushed the cp-symbolic-link-hard-link-conflict branch from afac51c to c81be25 Compare November 3, 2026 15:43
@sylvestre
sylvestre force-pushed the cp-symbolic-link-hard-link-conflict branch from c81be25 to 789de26 Compare November 6, 2026 11:48
@Logic769

Logic769 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

CI note (infrastructure issue, not related to the change in this PR):

The Tests job failed in the Doc-tests uucore step. The uucore unit tests themselves passed; the doctest compile then failed with error: extern location for ... does not exist: .../target/debug/deps/*.rlib and error[E0463]: can't find crate for ... (27–29 errors). The dependency .rlib files are missing from the restored target cache used by this run. The same failure is currently occurring on several unrelated PRs, so it is a repository CI/cache problem rather than this change. Re-running the job with a fresh build/cache rebuilds the artifacts.

No code changes in this PR are involved.

@Logic769
Logic769 force-pushed the cp-symbolic-link-hard-link-conflict branch from 789de26 to 0b2d732 Compare November 16, 2026 04:11
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 5 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 11 untouched benchmarks
⏩ 472 skipped benchmarks1


Comparing Logic769:cp-symbolic-link-hard-link-conflict (843f218) with main (e1b90fa)2

Open in CodSpeed

Footnotes

  1. 472 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (a663b44) during the generation of this report, so e1b90fa was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@sylvestre
sylvestre force-pushed the cp-symbolic-link-hard-link-conflict branch from 0b2d732 to f03a414 Compare November 16, 2026 20:36
@Logic769
Logic769 force-pushed the cp-symbolic-link-hard-link-conflict branch from f03a414 to 7e508d4 Compare November 18, 2026 04:39
@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.50 MB (+112 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

The conflict check compared raw long names, so `--sym -l` slipped past it
even though clap resolves `--sym` to `--symbolic-link`. Resolve the name
the way clap's `infer_long_args` does before comparing.
@sylvestre
sylvestre force-pushed the cp-symbolic-link-hard-link-conflict branch from 7e508d4 to 843f218 Compare November 19, 2026 11:29

This branch has not been deployed

No deployments
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: -s and -l together should be rejected

4 participants