Repository navigation
fix(du): count a hard-linked file once, not once per link - #19283
Conversation
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.
There was a problem hiding this comment.
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
Open (9)
Windows still counts each hard linkdu d1 d2still counts a shared hard link twice Top-level file operands skip theseenset. Indu_for_one_pattern, a match that isn't a directory goes straight toFileInfo::new(du.rs:234) and never touchesseen. In a directory holdinga,bandc(three links to one 1 MiB file),dureturns three 1 MiB rows. With a top-levelaand a link atd/b,dureports 1 MiB foraand 1 MiB ford; put both links under subdirectories and the second one counts as 0. The hard-link test doesn't run on Windows Every file goes into the set, not just files with more than one linkdu --longdrops the extra links fromfiles The test has no lower bound and runsdutwicels --duchanges too.ls.rs:835builds aDirBuilderper directory, sols --dusizes now count a hard-linked file once within each directory. That's the right result, but the PR body and release notes only mentiondu. Walk state lives in aClonesettings struct
Notes from my clanker, checked on 56a5248 by reading the code; not built or run.
…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.
|
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. |
|
Thanks for the thorough review — all fair points. Pushed a rework addressing each:
The Windows CI failure was the test's |
|
please comment at each review comment |
|
Notes from my clanker, checked on 7a47bb1: built on macOS, the 13
|
|
Updated the release notes — the |
|
Thanks |



Description
duwalked 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 isnot(wasm32). One set covers the wholeduinvocation following the POSIX/GNU rule: a file counts once even across different path operands, soduanddu d1 d2agree (first occurrence wins). Only files with more than one link enter the set; single-link files never touch it.DirBuilderstays settings-only — the set is threaded through as&mutrather than living in a cloned struct.ls --duuses the new signature; within each listed directory a hard-linked file now counts once, matchingdu.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)
ducounts a hard-linked file once, not once per linkduover-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 --longlists the first-seen link only, matching GNUdu -a.ls --dunow counts a hard-linked file once inside each listed directory, likedudoes.du c b aon three links to one file shows a single row forc. This matches POSIX/BSD/GNUdu, but scripts that expect one row per operand will see fewer rows.Additional notes
ducounts hard-linked files once per path, so both size columns over-report #19019