79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
// Package roots discovers, sizes and deletes Nix garbage collector roots.
|
|
package roots
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Kind string
|
|
|
|
const (
|
|
// KindIndirect is a symlink outside the gcroots tree (e.g. ./result)
|
|
// registered through /nix/var/nix/gcroots/auto.
|
|
KindIndirect Kind = "indirect"
|
|
// KindDangling is an auto/ entry whose target link no longer exists.
|
|
KindDangling Kind = "dangling"
|
|
// KindProfile is a profile generation link (<profile>-<N>-link).
|
|
KindProfile Kind = "profile"
|
|
// KindDirect is a symlink inside the gcroots tree pointing into the store.
|
|
KindDirect Kind = "direct"
|
|
// KindRuntime is a root held by a running process (/proc/...).
|
|
KindRuntime Kind = "runtime"
|
|
)
|
|
|
|
var AllKinds = []Kind{KindIndirect, KindProfile, KindDirect, KindDangling, KindRuntime}
|
|
|
|
type Root struct {
|
|
// Link is the symlink that keeps Target alive. For runtime roots it is
|
|
// /proc/<pid>.
|
|
Link string `json:"link"`
|
|
// Via is the gcroots entry that registers Link (auto/<hash> for indirect
|
|
// roots), empty when Link itself is inside the gcroots tree.
|
|
Via string `json:"via,omitempty"`
|
|
Target string `json:"target"`
|
|
Kind Kind `json:"kind"`
|
|
|
|
UID int `json:"uid"`
|
|
Owner string `json:"owner"`
|
|
ModTime time.Time `json:"mtime"`
|
|
|
|
// Profile generation info, only for KindProfile.
|
|
Profile string `json:"profile,omitempty"`
|
|
Generation int `json:"generation,omitempty"`
|
|
Current bool `json:"current,omitempty"`
|
|
|
|
// Hidden roots are nix's own bookkeeping (e.g. ~/.cache/nix/flake-registry.json)
|
|
// and are left out of listings unless asked for.
|
|
Hidden bool `json:"hidden,omitempty"`
|
|
|
|
// Protected roots are refused by Delete unless forced.
|
|
Protected bool `json:"protected,omitempty"`
|
|
Reason string `json:"reason,omitempty"`
|
|
|
|
// Runtime roots: number of store paths held and the process command.
|
|
Count int `json:"count,omitempty"`
|
|
Command string `json:"command,omitempty"`
|
|
|
|
// Sizes in bytes, -1 while unknown.
|
|
ClosureSize int64 `json:"closureSize"`
|
|
NarSize int64 `json:"narSize"`
|
|
}
|
|
|
|
func (r Root) Deletable() bool {
|
|
return r.Kind != KindRuntime
|
|
}
|
|
|
|
// TargetName is a short human label for the target store path.
|
|
func (r Root) TargetName() string {
|
|
t := r.Target
|
|
if i := strings.LastIndexByte(t, '/'); i >= 0 {
|
|
t = t[i+1:]
|
|
}
|
|
// strip the 32 char hash
|
|
if len(t) > 33 && t[32] == '-' {
|
|
t = t[33:]
|
|
}
|
|
return t
|
|
}
|