Skip to content

fix(du): deduplicate visited directories by target inode (Refs #1670) - #1727

Closed
caomengxuan666 wants to merge 1 commit into
mainfrom
fix/1670-du-inode-dedup
Closed

caomengxuan666 wants to merge 1 commit into
mainfrom
fix/1670-du-inode-dedup

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

du -L dirlink dir (dirlink → dir) printed the directory twice; GNU prints it once. Root cause: visited_dirs was keyed by the lexical du_dir_key (weakly_canonical never 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 from GetFileInformationByHandle, opening the directory following the link (FILE_FLAG_BACKUP_SEMANTICS, no FILE_FLAG_OPEN_REPARSE_POINT — the same open pattern operand_target_present already 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 d with d/self → .) can no longer recurse unboundedly.

GNU semantics

Probed against GNU coreutils 9.4 (Simular box, 2026-11-14):

form GNU 9.4 rc
du -L dirlink dir 8 dirlink/sub, 16 dirlink only 0
du -L dir dirlink 8 dir/sub, 16 dir only 0
du -L -s dirlink dir 16 dirlink only 0
du -H dirlink dir 8 dirlink/sub, 16 dirlink only 0
du dirlink dir (-P) 0 dirlink, 8 dir/sub, 16 dir (both lines) 0
du -L top (top holds sub/ + link → sub) 8 top/link, 12 top — sub printed once, under the first spelling 0
du -L d (d holds self → .) 8 d, no warning 0

First-encounter spelling wins; repeats are skipped silently with rc 0.

Verification

  • Regression nails: 5 new cases in 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).
  • fail-first (base red / head green) and the full du unit-test leg run on X299-2607; this VM cannot build Windows-only C++ and the local write wall is closed, so the work is staged from a /tmp clone and pushed from there.
  • No new user-visible text, no I18N impact.
  • UNVERIFIED-BY-CAPTAIN

What this PR deletes

  • No product code paths deleted. The lexical du_dir_key stays as the inode-key fallback; only the two visited_dirs call sites change which key function they call. No tests deleted (5 added).

Refs #1670

`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).
@caomengxuan666

Copy link
Copy Markdown
Member Author

Superseded: the du #1670 fix merged via #1730 (operand identity dedupe + tests).

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.

1 participant