Repository navigation
fix(du): deduplicate visited directories by target inode (Refs #1670) - #1727
Closed
caomengxuan666 wants to merge 1 commit into
Closed
caomengxuan666 wants to merge 1 commit into
caomengxuan666 wants to merge 1 commit into
Conversation
`du -L dirlink dir` printed the directory twice: visited_dirs was keyed by the lexical du_dir_key, and weakly_canonical never resolves symlinks, so the link name and the real name keyed differently. Key visited directories by the target inode (volume serial + file index from GetFileInformationByHandle, opened following the link) instead, with the lexical key kept as the fallback when the inode cannot be read. The same key now guards both sites: the operand skip and the in-traversal subtree skip, so a subdirectory reached through a link name and its real name is also visited once, and a self-referential link can no longer recurse unboundedly. GNU coreutils 9.4 probed: `du -L dirlink dir` prints only `dirlink`, `du -L dir dirlink` only `dir`, `du -H dirlink dir` only `dirlink`, and `du dirlink dir` (-P) still prints both lines; cycles skip silently with rc 0. Tests: 5 new cases in tests/unit/du/du_unit_test.cpp (-L/-H operand dedup in both orders, -P prints-twice lock, nested subtree dedup).
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
du -L dirlink dir(dirlink → dir) printed the directory twice; GNU prints it once. Root cause:visited_dirswas keyed by the lexicaldu_dir_key(weakly_canonicalnever resolves symlinks), so the link name and the real name keyed differently and both were traversed. This PR keys visited directories by the target inode instead: volume serial + file index fromGetFileInformationByHandle, opening the directory following the link (FILE_FLAG_BACKUP_SEMANTICS, noFILE_FLAG_OPEN_REPARSE_POINT— the same open patternoperand_target_presentalready trusts). The lexical key stays as the fallback when the inode cannot be read (e.g. an unreadable directory). The same key guards both sites — the operand skip and the in-traversal subtree skip — so a subdirectory reached through a link name and its real name is also visited once, and a self-referential link (du -L dwithd/self → .) can no longer recurse unboundedly.GNU semantics
Probed against GNU coreutils 9.4 (Simular box, 2026-11-14):
du -L dirlink dir8 dirlink/sub,16 dirlinkonlydu -L dir dirlink8 dir/sub,16 dironlydu -L -s dirlink dir16 dirlinkonlydu -H dirlink dir8 dirlink/sub,16 dirlinkonlydu dirlink dir(-P)0 dirlink,8 dir/sub,16 dir(both lines)du -L top(top holdssub/+link → sub)8 top/link,12 top— sub printed once, under the first spellingdu -L d(d holdsself → .)8 d, no warningFirst-encounter spelling wins; repeats are skipped silently with rc 0.
Verification
tests/unit/du/du_unit_test.cpp(du_dereference_dirlink_and_dir_operands_print_once,du_dereference_dir_and_dirlink_operands_print_once,du_dereference_args_dirlink_and_dir_operands_print_once,du_no_dereference_dirlink_and_dir_operands_print_twice(-P lock),du_dereference_nested_dirlink_dedupes_subtree).What this PR deletes
du_dir_keystays as the inode-key fallback; only the twovisited_dirscall sites change which key function they call. No tests deleted (5 added).Refs #1670