Skip to content

df: implement it - #241

Open
metif12 wants to merge 2 commits into
vlang:mainfrom
metif12:df-implementation
Open

metif12 wants to merge 2 commits into
vlang:mainfrom
metif12:df-implementation

Conversation

@metif12

@metif12 metif12 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

df for the disk-space listing: per-volume capacity, used and available space, a percentage, the file-system type and the mount point.

The presentation layer lives in common/df and src/df is an eleven-line main, because v test <dir> only compiles _test.v files, so a test in src/df cannot see code defined in src/df. Moving the logic into a module made the column layout assertable directly instead of only through the built binary.

Backends:

  • Windows: GetLogicalDrives, GetDiskFreeSpaceExA and GetVolumeInformationA. Device name is the drive letter, since that is the name a Windows user has for the volume.
  • Unix: /proc/mounts plus os.disk_usage (statvfs), including the kernel's octal-escape decoding for device and mount-point names.

Options: -a -B -h -H -i -k -l -m -g -P -T -t -x --total, file operands. Clustered short options (-hT), -B512 and comma-separated --type=a,b are parsed here rather than by common.flag_parser, which does not expand clusters.

Measured against GNU 9.4 before writing the code:

  • Column widths are the widest of the header, the data, and a minimum GNU reserves: device 14, Size/Used 5, Use% 4. The last column is not padded, so there are no trailing spaces.
  • --si keeps a lowercase k but uses M, G, T for the rest; -h uses K. 132k versus 128K is what distinguishes them.
  • The --total row prints - for both type and mount point, and the percentage is rounded to nearest, not truncated.

Not implemented, and refused rather than silently wrong:

  • -i on Windows. No API there exposes inode counts, so it prints df: doesn't support -i option and exits 1, which is what uutils does too. It works on Unix.
  • --block-size accepts plain decimal only. 1K, 1M and KB parse as nothing and produce a diagnostic instead of a wrong number.
  • --output=FIELD_LIST is not implemented.

Two reference behaviours are deliberately not copied. uutils 0.0.17 on Windows reports Available as 0 and Use% as 100% for every volume, and df C: resolves to the wrong one. Those are defects in the reference, so this port reports the real numbers and the tests do not diff against the rig for those cases; the column layout is pinned against GNU instead, following the same approach unexpand takes.

18 test functions, all green. v fmt -l is clean for everything this changes; it reports src/tr/tr.v, which comes from upstream #239 untouched here.

Reports per-volume capacity, used and available space, with -a -B -h -H -i -k -l -m -g -P -T -t -x --total and file operands. The presentation layer lives in common/df so the column layout is unit-testable; src/df is a thin main. Windows enumerates fixed drives through GetLogicalDrives/GetDiskFreeSpaceExA, Unix reads /proc/mounts and statvfs. Column widths, the -H suffix table (-si keeps a lowercase k but M, G, T for the rest) and the total row's '-' placeholders were measured against GNU 9.4. -i is refused on Windows because no API there exposes inode counts. Two reference quirks are deliberately not copied: uutils 0.0.17 reports Available as 0 and Use% as 100% on Windows, and this port reports the real numbers instead.
@metif12 metif12 mentioned this pull request Oct 9, 2026
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