Skip to content

Add hardlink flag to cp - #14359

Closed
jasha-hrp wants to merge 3 commits into
nushell:mainfrom
Jasha10:jasha10/cp-l-flag-to-create-hardlinks
Closed

jasha-hrp wants to merge 3 commits into
nushell:mainfrom
Jasha10:jasha10/cp-l-flag-to-create-hardlinks

Conversation

@jasha-hrp

Copy link
Copy Markdown
Contributor

Add flag to cp builtin to enable creating hardlinks.

This make use of the -l/--links flag from coreutils' cp command.
See https://uutils.github.io/coreutils/docs/utils/cp.html

User-Facing Changes

Should add extra flag to cp.
Default behavior of cp should not change

Tests + Formatting

Added some unit tests

TODO:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass (on Windows make sure to enable developer mode)
  • cargo run -- -c "use toolkit.nu; toolkit test stdlib" to run the tests for the standard library

Note
from nushell you can also use the toolkit as follows

use toolkit.nu  # or use an `env_change` hook to activate it automatically
toolkit check pr

-->

After Submitting

@jasha-hrp
jasha-hrp marked this pull request as draft November 16, 2024 19:13
@Jasha10

Jasha10 commented Nov 16, 2024

Copy link
Copy Markdown
Contributor

Closes #14306.

@NotTheDr01ds, these tests pass when I run them locally.

This is confusing because I haven't made any changes to src and would expect nu's cp command to reject the -l flag. Is the cp used in these tests not the same as the nu cp builtin?

Awaiting workflow approval to see if the CI runs.

@Jasha10

Jasha10 commented Nov 16, 2024

Copy link
Copy Markdown
Contributor

Guidance regarding which area of the codebase to modify / focus on would be appreciated.

@Jasha10

Jasha10 commented Nov 17, 2024

Copy link
Copy Markdown
Contributor

Taken from the uutils cp docs:

--link, -l
hard-link files instead of copying

...

--symbolic-link, -s
make symbolic links instead of copying

@jasha-hrp jasha-hrp closed this Nov 17, 2024
@Jasha10

Jasha10 commented Nov 17, 2024

Copy link
Copy Markdown
Contributor

Closed in favor of #14365

fdncred pushed a commit that referenced this pull request Nov 20, 2026
## Description

Implements #14306: `cp` gains a `-l`/`--link` flag matching GNU `cp -l`,
which creates hard links to regular files instead of copying them.
Combined with `-r` it can hard-link a whole directory tree
(`cp -lr source dest`), the use case in the issue.

The flag just exposes `uu_cp`'s existing `CopyMode::Link`.

- `-l` cannot be combined with `-u`: uu_cp ignores `UpdateMode` in
  `CopyMode::Link`, so the combination is rejected with
  `IncompatibleParameters` rather than silently misbehaving.
- When linking, symlinks are followed and their targets are linked,
  matching uutils' `resolve_dereference` (`!recursive || is_link`).
  `-P`/`--no-dereference` links the links themselves, as noted in the
  flag's help text.

Fresh take on #14359, which went stale.

## User-facing changes (Release notes)

`cp` now supports `-l`/`--link` to create hard links to regular files
instead of copying, e.g. `cp -l a.txt b.txt`, or `cp -lr dir_a dir_b` to
recursively link a directory tree (symlinks inside the tree are followed
unless `-P` is given). `-l` cannot be combined with `-u`.

## Additional notes

- closes #14306
- supersedes #14359
- Tests in `crates/nu-command/tests/commands/ucp.rs` check hard-link
  semantics portably (write-through, no inode APIs): `cp -l` on a single
  file, `cp -lr` on a tree with a symlink, `cp -l` over an existing
  destination, `cp -lf` replacing it, and `cp -lu` erroring.
- `linking_does_not_change_source_mode` is a regression test for
  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_default` fails on macOS on unmodified
  `main` (uu_cp reports `reflink: yes` on APFS); unrelated to this
  change.
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.

2 participants