Files
bsdaily/internal/bsdaily/disk.go
T
2026-02-12 13:01:52 -08:00

27 lines
734 B
Go

package bsdaily
import (
"fmt"
"log/slog"
"golang.org/x/sys/unix"
)
func CheckFreeSpace(path string, minBytes uint64, label string) error {
var stat unix.Statfs_t
if err := unix.Statfs(path, &stat); err != nil {
return fmt.Errorf("statfs %s (%s): %w", path, label, err)
}
free := uint64(stat.Bavail) * uint64(stat.Bsize)
freeGB := float64(free) / float64(bytesPerGB)
minGB := float64(minBytes) / float64(bytesPerGB)
slog.Info("disk space check", "label", label, "path", path,
"free_gb", fmt.Sprintf("%.1f", freeGB),
"required_gb", fmt.Sprintf("%.1f", minGB))
if free < minBytes {
return fmt.Errorf("insufficient disk space on %s (%s): %.1f GB free, need %.1f GB",
path, label, freeGB, minGB)
}
return nil
}