diskusage

package
v0.157.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package diskusage measures a directory tree twice: the size it appears to occupy, and the size deleting it would actually reclaim.

The two figures differ whenever content is hard-linked. pnpm links every package file from its content-addressed store into each project's node_modules, so a worktree that reports 1.4 GB of node_modules may free a few megabytes when it is removed. Measured across one WB fleet sweep: 11.7 GB apparent against 5.9 GB unshared over the same set of worktrees. Reporting only the apparent figure promises a reclaim the deletion cannot deliver, which is why every size WB reports carries both.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Human

func Human(bytes int64) string

Human renders a byte count for a terminal. It is deliberately coarse: these figures are read to decide whether a sweep is worth running, never to reconcile an exact total, and the JSON envelope carries the exact bytes.

Types

type Usage

type Usage struct {
	ApparentBytes int64 `json:"apparent_bytes"`
	UnsharedBytes int64 `json:"unshared_bytes"`
	SharedBytes   int64 `json:"shared_bytes"`
	Files         int64 `json:"files"`
}

Usage is one measured tree. ApparentBytes counts each inode's logical size once, the way `du --apparent-size` does. UnsharedBytes counts the physical blocks of the inodes whose every link lives inside the measured tree — the bytes removing the tree would return to the filesystem. SharedBytes is the apparent remainder: content the tree holds a link to but does not own.

func Measure

func Measure(ctx context.Context, root string) (Usage, error)

Measure walks root without following symlinks and reports both sizes. A root that does not exist measures zero: an absent tree occupies nothing, and a caller sweeping a fleet must not fail because one path was already removed. Unreadable subdirectories are skipped rather than fatal, for the same reason.

func (Usage) Add

func (u Usage) Add(other Usage) Usage

Add sums two measurements. Summing per-tree measurements is only exact when the trees share no inodes with each other; use a Walk when they might.

type Walk

type Walk struct {
	// contains filtered or unexported fields
}

Walk measures several trees as one accounting unit.

Summing per-tree measurements is wrong across a fleet, and wrong in both directions. Two worktrees that hard-link the same pnpm store file each report its apparent size, so the total double-counts it; and when the only links to a file live in two measured worktrees, each tree calls it shared with something outside itself, so the total under-reports what removing both would return. A Walk answers the question a reclaim footer is actually asking — "how much comes back if I remove all of these?" — by counting every inode once and treating it as unshared exactly when every link to it is inside the selected set.

func NewWalk

func NewWalk() *Walk

NewWalk starts an accounting unit.

func (*Walk) Measure

func (w *Walk) Measure(ctx context.Context, root string) (Usage, error)

Measure measures one tree and records it in the walk. The returned Usage is that tree on its own, exactly as Measure reports it; the cross-tree figures come from Total.

func (*Walk) Total

func (w *Walk) Total(roots ...string) Usage

Total reports what removing the named measured trees would reclaim. With no roots it covers every tree the walk measured.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL