Files
gohoarder/pkg/scanner/diskspace.go
T
lukaszraczylo 2de0bf029d feat(security): defer scans when the disk is low on space
Scanners unpack packages in the temp dir. When the volume is full, every
scan fails, and the same full volume can stop the metadata database.

The new security.min_free_bytes setting defers a scan while the temp dir
has less free space than the limit. The deferred scan records an
infrastructure error, so the existing retry path scans it again later.
A failed measurement does not block scans. The default is 0, which
keeps the check off.
2026-11-27 10:48:03 +00:00

39 lines
1.0 KiB
Go

package scanner
import (
stderrors "errors"
"fmt"
"os"
"syscall"
"github.com/rs/zerolog/log"
)
var errLowDiskSpace = stderrors.New("not enough free disk space to scan")
// checkDiskSpace refuses a scan while the temp dir, where scanners unpack packages, is short
// of space. A failed measurement never blocks scans.
func (m *Manager) checkDiskSpace() error {
if m.config.MinFreeBytes <= 0 || m.freeBytes == nil {
return nil
}
dir := os.TempDir()
free, err := m.freeBytes(dir)
if err != nil {
log.Warn().Err(err).Str("dir", dir).Msg("Cannot measure free disk space")
return nil
}
if need := uint64(m.config.MinFreeBytes); free < need {
return fmt.Errorf("%w: %d bytes free in %s, need %d", errLowDiskSpace, free, dir, need)
}
return nil
}
func diskFree(path string) (uint64, error) {
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return 0, fmt.Errorf("statfs %s: %w", path, err)
}
return st.Bavail * uint64(st.Bsize), nil // #nosec G115 -- block size is never negative
}