Skip to content

fix(du): count a hard-linked file once, not once per link - #19283

Merged
fdncred merged 2 commits into
nushell:mainfrom
intertermux-code:fix/19019-du-hardlink-double-count
Oct 18, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
intertermux-code:fix/19019-du-hardlink-double-count

Conversation

@intertermux-code

@intertermux-code intertermux-code commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

du walked every path and added each file's size independently, so a file with N hard links was counted N times and both size columns over-reported.

The walk now tracks the identities of files it has already counted and skips the repeats. The set holds uucore::fs::FileInformation (device+inode on Unix, volume serial+file index on Windows) instead of a Unix-only (dev, ino) pair, so the dedup applies on Linux, macOS and Windows; the gate is not(wasm32). One set covers the whole du invocation following the POSIX/GNU rule: a file counts once even across different path operands, so du and du d1 d2 agree (first occurrence wins). Only files with more than one link enter the set; single-link files never touch it. DirBuilder stays settings-only — the set is threaded through as &mut rather than living in a cloned struct. ls --du uses the new signature; within each listed directory a hard-linked file now counts once, matching du.

Adds regression tests: a file with multiple hard links contributes its size exactly once, including when the links sit under different path operands.

User-facing changes (Release notes)

du counts a hard-linked file once, not once per link

  • Fixed du over-reporting sizes for hard-linked files: a file with multiple hard links is now counted once instead of once per link, on all platforms (Linux, macOS, Windows).
  • du --long lists the first-seen link only, matching GNU du -a.
  • ls --du now counts a hard-linked file once inside each listed directory, like du does.
  • First link wins: when the same file is reachable through multiple hard links, only the first occurrence is counted and listed — e.g. du c b a on three links to one file shows a single row for c. This matches POSIX/BSD/GNU du, but scripts that expect one row per operand will see fewer rows.

Additional notes

du walked every path and added each file's size independently, so a
file with N hard links was counted N times in both size columns.
Track (device, inode) pairs already seen during the walk (Unix) and
skip files whose identity was already counted, matching POSIX and
GNU/BSD du behavior.

Adds regression tests covering hard-linked files.

@fdncred fdncred 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.

Review overview

🟡 Changes recommended

Linux and macOS now count a hard-linked file once inside one directory walk, but Windows is unchanged and the set resets for each path operand, so du still over-reports in cases #19019 covers.

Findings: 2 High severity · 3 Medium severity · 4 Low severity

Open (9)

Notes from my clanker, checked on 56a5248 by reading the code; not built or run.

Comment thread crates/nu-command/src/filesystem/du.rs Resolved
Comment thread crates/nu-command/tests/commands/du.rs Resolved
Comment thread crates/nu-command/tests/commands/du.rs Resolved
…cross operands

Address the review findings on this PR:

- Track file identity with uucore::fs::FileInformation instead of Unix-only
  (dev, ino) pairs, so Windows (NTFS hard links) counts each file once too.
- Keep one seen-set for the whole `du` invocation instead of one per path
  operand, so `du d1 d2` agrees with `du` (POSIX: first occurrence wins).
- Only files with more than one link enter the set.
- Run top-level file operands through the same check.
- Drop the RefCell from DirBuilder; thread the set through the walk as &mut.
  `ls --du` keeps a fresh set per directory, behavior unchanged.

Tests: run the hard-link test on Windows too, harden it with a lower bound
on the apparent size, and add a cross-operand regression test.
@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

when you're done, please comment on each review comment so we can be sure everything was addressed. also, make sure you use the PR template because we create our release notes from it.

@intertermux-code

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review — all fair points. Pushed a rework addressing each:

  • Windows is covered now: the set holds uucore::fs::FileInformation (device+inode on Unix, volume serial+file index on Windows) instead of the #[cfg(unix)] (dev, ino) pair, so du gives the same answer on all three platforms. The gate is not(wasm32) now.
  • One set for the whole du call, not one per operand — du d1 d2 and du on the same tree now agree. Went with the POSIX/GNU rule (first occurrence wins, so row sizes follow operand order).
  • Inserts gated on link count > 1, keeping the set small on big trees.
  • Top-level file operands go through the same check now.
  • The test runs on Windows too (hard links work fine on NTFS), and I hardened it: single du run for both columns, lower bound on apparent so a skip-everything regression fails, physical kept as an upper bound only (compression filesystems).
  • DirBuilder is settings-only again; the set is threaded through as &mut.
  • ls --du updated for the new signature, same per-directory behavior.
  • --long keeps listing only the first link, same as GNU du -a — called that out in the PR body.

The Windows CI failure was the test's #[cfg(unix)] gate leaving FileWithContent imported but unused — gone with the un-gated test.

@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

please comment at each review comment

@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Notes from my clanker, checked on 7a47bb1: built on macOS, the 13 du tests pass, both new tests fail with count_once forced to true, and the Windows CI job ran both hard-link tests. All seven review threads are done and resolved. Two items from the review body had no thread, so their status is here:

  • Top-level file operands: fixed. In a directory holding three links to one file, plain du and du c b a now return one row, the same as BSD du.
  • Low severity ls --du changed, but the description says it didn't. It says ls --du "keeps its per-directory behavior", but its sizes changed (first row below). The new number is right. The release notes should say ls --du now counts a hard-linked file once inside each listed directory.
  • Low severity Extra links get no row. First link wins, so other links passed as operands or matched by plain du drop out of the table (second row below). POSIX, BSD and GNU do the same, but a script that expects one row per operand gets fewer, so this belongs in the release notes too.
command 0.116.2 7a47bb1
ls --du, a directory holding three links to one 1 MiB file 3 MiB 1 MiB
du c b a, three links to one 1 MiB file 3 rows of 1 MiB 1 row (c)

@intertermux-code

Copy link
Copy Markdown
Contributor Author

Updated the release notes — the ls --du per-directory counting and the first-wins row behavior are in the PR body now. Thanks for running the clanker over it.

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 18, 2026
@fdncred
fdncred merged commit 199cf98 into nushell:main Oct 18, 2026
11 checks passed
@fdncred

fdncred commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

du counts hard-linked files once per path, so both size columns over-report

2 participants