Skip to content

mv: preserve hard links when renaming a directory across filesystems - #16125

Open
gonzalemario wants to merge 2 commits into
uutils:mainfrom
gonzalemario:dev/try-on-issue-4833
Open

gonzalemario wants to merge 2 commits into
uutils:mainfrom
gonzalemario:dev/try-on-issue-4833

Conversation

@gonzalemario

Copy link
Copy Markdown
Contributor

Hard-linked files inside a directory were copied as separate files when the directory was moved to another filesystem under a new name (or with -T). Scan the source for hard links in these cases too, as is already done when moving into an existing directory.

Closes #4833

@codspeed

codspeed Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 4 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

✅ 8 untouched benchmarks
⏩ 482 skipped benchmarks1


Comparing gonzalemario:dev/try-on-issue-4833 (bc5d994) with main (ce58468)2

Open in CodSpeed

Footnotes

  1. 482 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 (2a072db) during the generation of this report, so ce58468 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Comment thread src/uu/mv/src/mv.rs Resolved
@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the PR!
The hard-link scan now runs before every rename, even when no copy is needed, and CodSpeed shows -33.9% on mv_directory. Please scan only after the cross-device fallback is hit.

Comment thread tests/by-util/test_mv.rs Resolved
@gonzalemario

Copy link
Copy Markdown
Contributor Author

thanks for the PR! The hard-link scan now runs before every rename, even when no copy is needed, and CodSpeed shows -33.9% on mv_directory. Please scan only after the cross-device fallback is hit.

I'm comparing locally with cargo bench --bench mv_test. Also, I moved the change to the hardlink.rs because I thought it'd be natural to read. And also, as a side effect, less platform dependent code was required IMO. Could you please have a look? I'll wait until the tests complete but I'm fairly confident this pr's not adding regression. Hopefully not breaking any other than linux either.

@sylvestre

Copy link
Copy Markdown
Contributor

rustfmt is sad :)
could you please run cargo fmt?

Diff in /home/runner/work/coreutils/coreutils/src/uu/mv/src/mv.rs:376:
 }

 #[cfg(unix)]
-fn scan_hardlinks(
-    files: &[PathBuf],
-    options: &Options,
-) -> (HardlinkTracker, HardlinkGroupScanner) {
+fn scan_hardlinks(files: &[PathBuf], options: &Options) -> (HardlinkTracker, HardlinkGroupScanner) {
     // Use hardlink options
     let hardlink_options = HardlinkOptions {
         verbose: options.verbose || options.debug,

@gonzalemario

gonzalemario commented Nov 7, 2026 via email •

Copy link
Copy Markdown
Contributor Author

@gonzalemario

Copy link
Copy Markdown
Contributor Author

rustfmt is sad :) could you please run cargo fmt?

Done it in 84d35e5

I pushed it as a WIP commit, we should squash before merging. I'll do it and rebase from main once you give me the ok.

Comment thread tests/by-util/test_mv.rs Resolved
Comment thread tests/by-util/test_mv.rs Resolved
Comment thread src/uu/mv/src/mv.rs
// Use hardlink options
let hardlink_options = HardlinkOptions {
verbose: options.verbose || options.debug,
};

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.

it's only used once now, do we still need this function?
if we keep it, these two comments don't add much

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my mind, the fn looks good. It does little but specifics and it helps to "document" the code without writing extra document IMO. I'll remove the comments though

@sylvestre
sylvestre force-pushed the dev/try-on-issue-4833 branch from 1d20e0d to 5c57e94 Compare November 11, 2026 13:57
@RenjiSann
RenjiSann force-pushed the dev/try-on-issue-4833 branch from 5c57e94 to b5b4bea Compare November 12, 2026 14:00
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

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

@gonzalemario

gonzalemario commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

/rerun

@gonzalemario
gonzalemario force-pushed the dev/try-on-issue-4833 branch 2 times, most recently from ce15bf0 to cfc1553 Compare November 17, 2026 21:45
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Binary size comparison:

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

Total size of compared binaries: 155.48 MB (+100 KB, +0.06%)

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

@sylvestre
sylvestre force-pushed the dev/try-on-issue-4833 branch 2 times, most recently from 5d78092 to c773308 Compare November 23, 2026 17:02
Hard-linked files inside a directory were copied as separate files
when the directory was moved to another filesystem under a new name,
or with -T onto an existing empty directory, because no hard-link
scan was done on that path.

When the copy fallback gets no pre-scanned context, scan the source
there, so the scan only runs once a cross-filesystem copy is needed.

Closes uutils#4833
There is no need to use `pub(crate)` in this module, also, this follows
convention found in other sources like test_cp.rs or test_ls.rs. The
change about removing `cfg(unix)`, it's redundant because the parent
inter_partition_copying already defines its targes as "linux", and we
need to run those test_* functions when /dev/shm is available only.

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.

mv hardlinks should be preserved when moving into a different directory

2 participants