Skip to content

du: implement it - #242

Open
metif12 wants to merge 1 commit into
vlang:mainfrom
metif12:du-implementation
Open

metif12 wants to merge 1 commit into
vlang:mainfrom
metif12:du-implementation

Conversation

@metif12

@metif12 metif12 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

du for recursive apparent-size summaries: one line per directory with its total, children printed before their parent, plus the usual modifiers.

The walk and the formatting live in common/du, with src/du an eleven-line main, for the same reason df's logic does: v test <dir> compiles only _test.v files, so a test beside the implementation cannot see it.

Options: -a -b -B -c -d -h -H -k -m -s --max-depth --total --apparent-size and file operands. Clusters like -sb and -ab are parsed directly, since common.flag_parser does not expand them.

  • Traversal is sorted, so output is deterministic instead of depending on directory order.
  • A missing path prints du: cannot access <p>: No such file or directory, continues with the remaining operands, and exits 1, which is what GNU does.
  • Block sizes round up: a path of one byte is one block, not zero.

Measured against GNU 9.4 before writing the code, including -d0, -d1, -a, -s, -c, multiple operands, and the --apparent-size -k form.

Two notes on things I did not share:

human_readable is duplicated from common/df rather than imported. An earlier revision imported it, which stacked this PR on the df one; a base branch in a fork cannot back a pull request here, so that draft could not be built at all. Keeping the two tools independent matters more than the ~25 lines, and the duplication is called out in a comment so it can be folded once both land.

Two documented divergences. The default reports apparent size in 1K blocks, which is GNU's --apparent-size accounting rather than its st_blocks allocation — V's os.Stat exposes no blocks field, so matching GNU here needs a hand-declared stat struct per platform for a difference that only shows on small files. And --block-size accepts plain decimal only.

uutils 0.0.17's du on Windows reports 0 for every block count, omits "cannot access" from its diagnostics, and exits 0 when a path is missing. None of those are copied; the tests pin GNU's behaviour instead.

20 test functions, all green, including a control that confirmed they fail when the block rounding is broken.

Recursive apparent-size summary with -a -b -B -c -d -h -H -k -m -s --max-depth --total --apparent-size and file operands. The walk and the size formatting live in common/du and src/du is a thin main, for the same reason df's logic does: v test only compiles _test.v files, so tests cannot see code sitting beside them. Human-readable sizes are reused from common/df rather than written a second time. Traversal is sorted so output is deterministic, and a missing path is reported as 'du: cannot access <p>: No such file or directory' with exit 1 while the remaining operands still run, which is what GNU does. Two documented divergences: the default reports apparent size in 1K blocks, which is GNU's --apparent-size accounting rather than its st_blocks allocation (V's Stat has no blocks field, and matching GNU here would need a hand-declared stat struct per platform), and --block-size takes plain decimal only.
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