Skip to content

cp: don't change the source's mode with --link - #15088

Merged
sylvestre merged 1 commit into
uutils:mainfrom
abendrothj:cp-link-no-chmod
Oct 5, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
abendrothj:cp-link-no-chmod

Conversation

@abendrothj

Copy link
Copy Markdown
Contributor

Closes #15087

After creating the hard link, cp -l still ran the final chmod meant for newly copied files. Because the link shares the source's inode, this changed the mode of the source file. GNU leaves the linked file untouched.

This skips that chmod in link mode. Directories created by cp -rl are handled elsewhere and keep their current behaviour.

Checked against GNU coreutils 9.12 for cp -l under umask 077 and 022, -lf and -l --backup over an existing file, -lp, -la, -l --no-preserve=mode and cp -rl; source and destination modes and timestamps now match GNU in all cases. Added test_cp_arg_link_keeps_source_mode, which fails without the change.

After creating a hard link, cp -l still ran the final chmod meant for newly
copied files on the destination. Since the link shares the source's inode,
this changed the mode of the source file: under umask 077 a 755 source became
700, and with -f over an existing file the source took the old destination's
mode. GNU cp leaves the linked file untouched. Skip that chmod in link mode.
Copilot AI balanced review requested due to automatic review settings October 5, 2026 10:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)

@sylvestre
sylvestre merged commit 065068a into uutils:main Oct 5, 2026
131 of 135 checks passed
Ariakage added a commit to Ariakage/nushell that referenced this pull request Nov 17, 2026
- Reject `-l` together with `-u`: uu_cp 0.12.0 ignores UpdateMode in
  Link copy mode and IfOlder disables overwrite verification, so the
  combination silently did the wrong thing. Return
  IncompatibleParameters instead.
- Follow symlinks when linking recursively, matching uutils'
  resolve_dereference (`!recursive || is_link`); `-P` keeps linking
  the links themselves. Documented in the flag's help text.
- Replace inode/file-index assertions with a portable write-through
  hard-link check: std's Windows MetadataExt::file_index is unstable
  and would break the tests target on Windows.
- Replace the directory-error test, which never exercised `-l`, with
  an existing-destination case, a `-lu` rejection test, and a
  recursive test covering a symlink in the tree. Add an ignored
  unix-only regression test for the source-mode chmod bug
  (uutils/coreutils#15087); unignore once a uu_cp release containing
  uutils/coreutils#15088 is available.
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 -l changes the mode of the source file

3 participants