diskspace

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package diskspace reports filesystem capacity for a path using a single statfs-class syscall (statfs(2) on Linux/macOS, GetDiskFreeSpaceExW on Windows). A Check call costs microseconds, so callers may poll it on a short interval or inline it on a hot path such as "before pulling an image".

It deliberately does NOT walk directories. A du-style traversal of <data-dir>/containerd is minutes of I/O on a node with hundreds of overlay snapshots and would itself become a source of load — the whole point of this helper is that asking the filesystem is free.

The per-OS implementations live in usage_unix.go, usage_windows.go and usage_unsupported.go; they were lifted from the equivalent checks in cmd/ephemerd/doctor_*.go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GiB

func GiB(b uint64) float64

GiB converts a byte count to gibibytes. Exported because every caller that logs a byte count wants the same conversion.

Types

type Usage

type Usage struct {
	// Path is the path that was measured (not necessarily a mount point).
	Path string
	// TotalBytes is the filesystem's total capacity.
	TotalBytes uint64
	// FreeBytes is the space available to this process.
	FreeBytes uint64
}

Usage is a point-in-time capacity reading for the filesystem backing a path.

FreeBytes is the space available to the *calling user*, not the raw free count: on Linux/macOS that is statfs.Bavail (which excludes the root-reserved blocks) and on Windows it is GetDiskFreeSpaceExW's lpFreeBytesAvailable (which respects quotas). UsedBytes is therefore derived as Total-Free and counts reserved space as used — deliberately conservative, since ephemerd does not run as a user that can spend the reserve.

func Check

func Check(path string) (Usage, error)

Check returns a capacity reading for the filesystem containing path. path need not be a mount point; any existing file or directory on the filesystem works.

func (Usage) FreePercent

func (u Usage) FreePercent() float64

FreePercent returns available capacity as a percentage in [0,100].

func (Usage) String

func (u Usage) String() string

String renders a compact human-readable summary for log messages.

func (Usage) UsedBytes

func (u Usage) UsedBytes() uint64

UsedBytes returns capacity minus space available to this process.

func (Usage) UsedPercent

func (u Usage) UsedPercent() float64

UsedPercent returns used capacity as a percentage in [0,100]. A zero-size filesystem (unreadable, or a stub value from a failed probe) reports 0 so callers never trigger garbage collection on a division artifact.

Jump to

Keyboard shortcuts

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