Repository navigation
feat(cp): add -l/--link flag to create hard links - #19583
Conversation
Match GNU `cp -l`: regular files are created as hard links to the source instead of copied. Combined with `-r` this recursively links a directory hierarchy, e.g. `cp -lr source dest`. Implemented by exposing `uu_cp`'s existing `CopyMode::Link` through a new switch; no dependency changes needed. `UpdateMode` and `CopyMode` are now resolved independently so `cp -lu` behaves like upstream `cp`. Closes nushell#14306.
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
cp -l and cp -lr make hard links on macOS, and clippy, fmt and the ucp tests pass. The blocker is upstream: uu_cp 0.12.0 chmods the destination after linking, which changes the source file's mode, and that fix isn't released yet. The new test helper also won't compile on Windows.
Findings: 1
Open (5)
Notes from my clanker, checked on a05ab4a. I built the branch and ran each case on it, on 0.117.1 (which has no -l), and on uutils cp 0.12.0, the version nushell links. cargo test -p nu-command --test tests -- commands::ucp gave 79 passed and 6 ignored, and clippy with CI's flags, fmt and typos were clean, all on macOS. The Windows finding comes from compile-checking the test helper in a separate crate. Nothing ran on Windows or Linux, and I had no GNU cp here to compare against.
- 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.
|
Thanks for the thorough review — all five findings addressed in 9f32029:
|
|
We may need to wait for the next uutils release |
|
Sounds good — I'll keep an eye on the next uutils release. Once |
|
i'll ping one of the maintainers to see if they have an outlook for the next release. |
|
that maintainer said they'll have a release in the next few days. |
|
Thanks for checking! I'll watch for the release. |
'File exists' is the POSIX errno suffix; on Windows the same failure reads differently. The 'cannot create hard link' prefix comes from uutils' fluent strings and renders the same on every platform.
|
i created #19620 to bump uutils to 0.13.0 |
|
|
|
it's merged now |
uu_cp 0.13.0 is on main now and contains the fix for the chmod after hard link, so linking_does_not_change_source_mode runs for real. The link tests also move to the injected-Playground API that main switched to.
|
Merged main (uu_cp 0.13.0). |
|
Just need to update the description of this PR and i think we're good to go. |
|
done |
|
Thanks |



Description
Implements #14306:
cpgains a-l/--linkflag matching GNUcp -l,which creates hard links to regular files instead of copying them.
Combined with
-rit can hard-link a whole directory tree(
cp -lr source dest), the use case in the issue.The flag just exposes
uu_cp's existingCopyMode::Link.-lcannot be combined with-u: uu_cp ignoresUpdateModeinCopyMode::Link, so the combination is rejected withIncompatibleParametersrather than silently misbehaving.matching uutils'
resolve_dereference(!recursive || is_link).-P/--no-dereferencelinks the links themselves, as noted in theflag's help text.
Fresh take on #14359, which went stale.
User-facing changes (Release notes)
cpnow supports-l/--linkto create hard links to regular files instead of copying, e.g.cp -l a.txt b.txt, orcp -lr dir_a dir_bto recursively link a directory tree (symlinks inside the tree are followed unless-Pis given).-lcannot be combined with-u.Additional notes
cp -lflag to create hardlinks in a file heirarchy #14306crates/nu-command/tests/commands/ucp.rscheck hard-linksemantics portably (write-through, no inode APIs):
cp -lon a singlefile,
cp -lron a tree with a symlink,cp -lover an existingdestination,
cp -lfreplacing it, andcp -luerroring.linking_does_not_change_source_modeis a regression test forcp -l changes the mode of the source file uutils/coreutils#15087 (chmod after hard link used to mutate the
source's mode); uu_cp 0.13.0 on main has the fix.
commands::ucp::test_cp_debug_defaultfails on macOS on unmodifiedmain(uu_cp reportsreflink: yeson APFS); unrelated to thischange.