520 lines
12 KiB
Go
520 lines
12 KiB
Go
package roots
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Config locates the Nix state on disk. Tests point it at a fake tree.
|
|
type Config struct {
|
|
StoreDir string // /nix/store
|
|
StateDir string // /nix/var/nix
|
|
// NixStoreBin is used for --print-roots and --gc.
|
|
NixStoreBin string
|
|
// NixBin is used for path-info.
|
|
NixBin string
|
|
// NixEnvBin is used to delete profile generations.
|
|
NixEnvBin string
|
|
}
|
|
|
|
func DefaultConfig() Config {
|
|
c := Config{
|
|
StoreDir: "/nix/store",
|
|
StateDir: "/nix/var/nix",
|
|
NixStoreBin: "nix-store",
|
|
NixBin: "nix",
|
|
NixEnvBin: "nix-env",
|
|
}
|
|
if v := os.Getenv("NIX_STORE_DIR"); v != "" {
|
|
c.StoreDir = v
|
|
}
|
|
if v := os.Getenv("NIX_STATE_DIR"); v != "" {
|
|
c.StateDir = v
|
|
}
|
|
return c
|
|
}
|
|
|
|
func (c Config) gcrootsDir() string { return filepath.Join(c.StateDir, "gcroots") }
|
|
func (c Config) autoDir() string { return filepath.Join(c.StateDir, "gcroots", "auto") }
|
|
func (c Config) profilesDir() string { return filepath.Join(c.StateDir, "profiles") }
|
|
|
|
func (c Config) inStore(p string) bool {
|
|
return strings.HasPrefix(p, c.StoreDir+"/")
|
|
}
|
|
|
|
// storePathOf trims a path inside the store to the top level store path.
|
|
func (c Config) storePathOf(p string) string {
|
|
rest := strings.TrimPrefix(p, c.StoreDir+"/")
|
|
if i := strings.IndexByte(rest, '/'); i >= 0 {
|
|
rest = rest[:i]
|
|
}
|
|
return c.StoreDir + "/" + rest
|
|
}
|
|
|
|
var protectedLinks = map[string]bool{
|
|
"/run/current-system": true,
|
|
"/run/booted-system": true,
|
|
}
|
|
|
|
// hiddenPatterns mark roots that nix creates for its own caches.
|
|
var hiddenPatterns = []string{"/.cache/nix/"}
|
|
|
|
func isHidden(link string) bool {
|
|
for _, p := range hiddenPatterns {
|
|
if strings.Contains(link, p) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var genLinkRe = regexp.MustCompile(`^(.*)-(\d+)-link$`)
|
|
|
|
type scanner struct {
|
|
cfg Config
|
|
seen map[string]*Root
|
|
order []*Root
|
|
users map[int]string
|
|
}
|
|
|
|
// Scan finds all persistent roots: the gcroots tree (including indirect
|
|
// roots registered under auto/) and the profiles tree. Runtime roots are
|
|
// gathered separately by ScanRuntime because that requires running
|
|
// nix-store and is slow.
|
|
func Scan(cfg Config) ([]*Root, error) {
|
|
s := &scanner{cfg: cfg, seen: map[string]*Root{}, users: map[int]string{}}
|
|
if err := s.walk(cfg.gcrootsDir(), true); err != nil {
|
|
return nil, err
|
|
}
|
|
// The profiles dir is not required to exist (e.g. in tests).
|
|
if err := s.walk(cfg.profilesDir(), false); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
for _, r := range s.order {
|
|
s.classifyProfile(r)
|
|
s.fillOwner(r)
|
|
r.Hidden = isHidden(r.Link)
|
|
}
|
|
return s.order, nil
|
|
}
|
|
|
|
// walk visits every symlink below dir. dir itself may be a symlink; paths are
|
|
// reported under dir rather than its resolved location.
|
|
func (s *scanner) walk(dir string, isGCRoots bool) error {
|
|
real, err := filepath.EvalSymlinks(dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return filepath.WalkDir(real, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
// unreadable subdirectories are skipped, not fatal
|
|
if d != nil && d.IsDir() && p != real {
|
|
return fs.SkipDir
|
|
}
|
|
if p == real {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if d.Type()&fs.ModeSymlink == 0 {
|
|
return nil
|
|
}
|
|
shown := dir + strings.TrimPrefix(p, real)
|
|
s.visitLink(shown, p, isGCRoots)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *scanner) add(r *Root) {
|
|
if _, ok := s.seen[r.Link]; ok {
|
|
return
|
|
}
|
|
r.ClosureSize, r.NarSize = -1, -1
|
|
s.seen[r.Link] = r
|
|
s.order = append(s.order, r)
|
|
}
|
|
|
|
// visitLink mirrors the logic of findRoots in libstore/gc.cc.
|
|
func (s *scanner) visitLink(shown, real string, isGCRoots bool) {
|
|
target, err := os.Readlink(real)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if s.cfg.inStore(target) {
|
|
s.add(&Root{Link: shown, Target: s.cfg.storePathOf(target), Kind: KindDirect})
|
|
return
|
|
}
|
|
if !filepath.IsAbs(target) {
|
|
target = filepath.Join(filepath.Dir(shown), target)
|
|
}
|
|
inAuto := isGCRoots && strings.HasPrefix(shown, s.cfg.autoDir()+"/")
|
|
|
|
st, err := os.Lstat(target)
|
|
if err != nil {
|
|
if !inAuto {
|
|
return
|
|
}
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
s.add(&Root{Link: target, Via: shown, Kind: KindDangling})
|
|
} else {
|
|
s.add(&Root{Link: target, Via: shown, Kind: KindIndirect, Reason: "unreadable: " + errReason(err)})
|
|
}
|
|
return
|
|
}
|
|
if st.Mode()&fs.ModeSymlink == 0 {
|
|
return
|
|
}
|
|
target2, err := os.Readlink(target)
|
|
if err != nil || !s.cfg.inStore(target2) {
|
|
return
|
|
}
|
|
if inAuto {
|
|
s.add(&Root{Link: target, Via: shown, Target: s.cfg.storePathOf(target2), Kind: KindIndirect})
|
|
} else if isGCRoots {
|
|
// e.g. gcroots/booted-system -> /run/booted-system -> store.
|
|
// Deleting the gcroots entry is what drops the root.
|
|
r := &Root{Link: shown, Via: target, Target: s.cfg.storePathOf(target2), Kind: KindDirect}
|
|
if protectedLinks[target] {
|
|
r.Protected, r.Reason = true, "points to "+target
|
|
}
|
|
s.add(r)
|
|
}
|
|
// In the profiles tree, links like system -> system-38-link resolve to a
|
|
// generation link that is visited on its own.
|
|
}
|
|
|
|
func errReason(err error) string {
|
|
var pe *fs.PathError
|
|
if errors.As(err, &pe) {
|
|
return pe.Err.Error()
|
|
}
|
|
return err.Error()
|
|
}
|
|
|
|
func (s *scanner) classifyProfile(r *Root) {
|
|
if r.Kind == KindDangling {
|
|
return
|
|
}
|
|
m := genLinkRe.FindStringSubmatch(r.Link)
|
|
if m == nil {
|
|
return
|
|
}
|
|
profile := m[1]
|
|
st, err := os.Lstat(profile)
|
|
if err != nil || st.Mode()&fs.ModeSymlink == 0 {
|
|
return
|
|
}
|
|
gen, _ := strconv.Atoi(m[2])
|
|
r.Kind = KindProfile
|
|
r.Profile = profile
|
|
r.Generation = gen
|
|
if cur, err := os.Readlink(profile); err == nil && filepath.Base(cur) == filepath.Base(r.Link) {
|
|
r.Current = true
|
|
r.Protected = true
|
|
r.Reason = "current generation of " + profile
|
|
}
|
|
}
|
|
|
|
func (s *scanner) fillOwner(r *Root) {
|
|
p := r.Link
|
|
if r.Kind == KindDangling {
|
|
p = r.Via
|
|
}
|
|
st, err := os.Lstat(p)
|
|
if err != nil {
|
|
r.UID = -1
|
|
return
|
|
}
|
|
r.ModTime = st.ModTime()
|
|
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
|
r.UID = int(sys.Uid)
|
|
r.Owner = s.userName(r.UID)
|
|
}
|
|
}
|
|
|
|
func (s *scanner) userName(uid int) string {
|
|
if n, ok := s.users[uid]; ok {
|
|
return n
|
|
}
|
|
n := strconv.Itoa(uid)
|
|
if u, err := user.LookupId(n); err == nil {
|
|
n = u.Username
|
|
}
|
|
s.users[uid] = n
|
|
return n
|
|
}
|
|
|
|
// ScanRuntime returns one root per process that holds store paths alive,
|
|
// based on `nix-store --gc --print-roots`. Roots that the persistent scan
|
|
// already found are skipped via known.
|
|
func ScanRuntime(ctx context.Context, cfg Config, known map[string]bool) ([]*Root, error) {
|
|
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc", "--print-roots").Output()
|
|
if err != nil {
|
|
return nil, cmdError(err)
|
|
}
|
|
return parseRuntime(cfg, out, known), nil
|
|
}
|
|
|
|
func parseRuntime(cfg Config, out []byte, known map[string]bool) []*Root {
|
|
type group struct {
|
|
targets map[string]bool
|
|
}
|
|
groups := map[string]*group{}
|
|
var keys []string
|
|
sc := bufio.NewScanner(bytes.NewReader(out))
|
|
sc.Buffer(make([]byte, 64*1024), 1024*1024)
|
|
for sc.Scan() {
|
|
link, target, ok := strings.Cut(sc.Text(), " -> ")
|
|
if !ok || known[link] {
|
|
continue
|
|
}
|
|
var key string
|
|
switch {
|
|
case strings.HasPrefix(link, "{"):
|
|
key = link
|
|
case strings.HasPrefix(link, "/proc/"):
|
|
rest := strings.TrimPrefix(link, "/proc/")
|
|
if i := strings.IndexByte(rest, '/'); i >= 0 {
|
|
rest = rest[:i]
|
|
}
|
|
key = "/proc/" + rest
|
|
default:
|
|
continue
|
|
}
|
|
g := groups[key]
|
|
if g == nil {
|
|
g = &group{targets: map[string]bool{}}
|
|
groups[key] = g
|
|
keys = append(keys, key)
|
|
}
|
|
g.targets[target] = true
|
|
}
|
|
sort.Strings(keys)
|
|
var res []*Root
|
|
for _, k := range keys {
|
|
g := groups[k]
|
|
r := &Root{Link: k, Kind: KindRuntime, Count: len(g.targets), ClosureSize: -1, NarSize: -1, UID: -1}
|
|
if len(g.targets) == 1 {
|
|
for t := range g.targets {
|
|
r.Target = t
|
|
}
|
|
}
|
|
if strings.HasPrefix(k, "/proc/") {
|
|
if b, err := os.ReadFile(k + "/cmdline"); err == nil {
|
|
r.Command = strings.TrimSpace(strings.ReplaceAll(string(b), "\x00", " "))
|
|
}
|
|
if st, err := os.Stat(k); err == nil {
|
|
r.ModTime = st.ModTime()
|
|
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
|
r.UID = int(sys.Uid)
|
|
if u, err := user.LookupId(strconv.Itoa(r.UID)); err == nil {
|
|
r.Owner = u.Username
|
|
} else {
|
|
r.Owner = strconv.Itoa(r.UID)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
res = append(res, r)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func cmdError(err error) error {
|
|
var ee *exec.ExitError
|
|
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
|
|
return errors.New(strings.TrimSpace(string(ee.Stderr)))
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Set is a thread safe, refreshable collection of roots shared by the TUI
|
|
// and the web server.
|
|
type Set struct {
|
|
Cfg Config
|
|
|
|
// refreshMu serialises Refresh so a scan that started before a delete
|
|
// cannot land after it and resurrect the deleted roots.
|
|
refreshMu sync.Mutex
|
|
|
|
mu sync.RWMutex
|
|
roots []*Root
|
|
scannedAt time.Time
|
|
runtime bool
|
|
sizing bool
|
|
sizesDone int
|
|
sizesTodo int
|
|
sizeCache map[string][2]int64
|
|
listeners []func()
|
|
}
|
|
|
|
func NewSet(cfg Config) *Set {
|
|
return &Set{Cfg: cfg, sizeCache: map[string][2]int64{}}
|
|
}
|
|
|
|
// OnChange registers a callback invoked (from any goroutine) when roots or
|
|
// sizes change.
|
|
func (s *Set) OnChange(f func()) {
|
|
s.mu.Lock()
|
|
s.listeners = append(s.listeners, f)
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
func (s *Set) notify() {
|
|
s.mu.RLock()
|
|
ls := append([]func(){}, s.listeners...)
|
|
s.mu.RUnlock()
|
|
for _, f := range ls {
|
|
f()
|
|
}
|
|
}
|
|
|
|
// Snapshot returns copies of the current roots so callers can sort and
|
|
// filter without locking.
|
|
func (s *Set) Snapshot() []Root {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
out := make([]Root, len(s.roots))
|
|
for i, r := range s.roots {
|
|
out[i] = *r
|
|
}
|
|
return out
|
|
}
|
|
|
|
type Status struct {
|
|
Total int
|
|
ScannedAt time.Time
|
|
Sizing bool
|
|
SizesDone int
|
|
SizesTodo int
|
|
Runtime bool
|
|
}
|
|
|
|
func (s *Set) Status() Status {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return Status{len(s.roots), s.scannedAt, s.sizing, s.sizesDone, s.sizesTodo, s.runtime}
|
|
}
|
|
|
|
// SetRuntime toggles whether the next Refresh includes runtime roots.
|
|
func (s *Set) SetRuntime(on bool) {
|
|
s.mu.Lock()
|
|
s.runtime = on
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// Refresh rescans roots and starts computing sizes in the background.
|
|
func (s *Set) Refresh(ctx context.Context) error {
|
|
s.refreshMu.Lock()
|
|
defer s.refreshMu.Unlock()
|
|
rs, err := Scan(s.Cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.mu.RLock()
|
|
wantRuntime := s.runtime
|
|
s.mu.RUnlock()
|
|
if wantRuntime {
|
|
known := map[string]bool{}
|
|
for _, r := range rs {
|
|
known[r.Link] = true
|
|
}
|
|
rt, err := ScanRuntime(ctx, s.Cfg, known)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rs = append(rs, rt...)
|
|
}
|
|
s.mu.Lock()
|
|
var todo []string
|
|
pending := map[string]bool{}
|
|
for _, r := range rs {
|
|
if r.Target == "" {
|
|
continue
|
|
}
|
|
if sz, ok := s.sizeCache[r.Target]; ok {
|
|
r.ClosureSize, r.NarSize = sz[0], sz[1]
|
|
} else if !pending[r.Target] {
|
|
pending[r.Target] = true
|
|
todo = append(todo, r.Target)
|
|
}
|
|
}
|
|
s.roots = rs
|
|
s.scannedAt = time.Now()
|
|
s.sizesDone, s.sizesTodo = 0, len(todo)
|
|
s.sizing = len(todo) > 0
|
|
s.mu.Unlock()
|
|
s.notify()
|
|
if len(todo) > 0 {
|
|
go s.computeSizes(ctx, todo)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Set) computeSizes(ctx context.Context, paths []string) {
|
|
QuerySizes(ctx, s.Cfg, paths, func(batch map[string][2]int64, n int) {
|
|
s.mu.Lock()
|
|
for p, sz := range batch {
|
|
s.sizeCache[p] = sz
|
|
}
|
|
for _, r := range s.roots {
|
|
if sz, ok := batch[r.Target]; ok {
|
|
r.ClosureSize, r.NarSize = sz[0], sz[1]
|
|
}
|
|
}
|
|
s.sizesDone += n
|
|
s.mu.Unlock()
|
|
s.notify()
|
|
})
|
|
s.mu.Lock()
|
|
s.sizing = false
|
|
s.mu.Unlock()
|
|
s.notify()
|
|
}
|
|
|
|
// Remove drops roots from the set after a successful delete, without a full
|
|
// rescan.
|
|
func (s *Set) Remove(links map[string]bool) {
|
|
s.mu.Lock()
|
|
kept := s.roots[:0:0]
|
|
for _, r := range s.roots {
|
|
if !links[r.Link] {
|
|
kept = append(kept, r)
|
|
}
|
|
}
|
|
s.roots = kept
|
|
s.mu.Unlock()
|
|
s.notify()
|
|
}
|
|
|
|
// Lookup returns the current roots with the given link paths.
|
|
func (s *Set) Lookup(links []string) []Root {
|
|
want := map[string]bool{}
|
|
for _, l := range links {
|
|
want[l] = true
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
var out []Root
|
|
for _, r := range s.roots {
|
|
if want[r.Link] {
|
|
out = append(out, *r)
|
|
}
|
|
}
|
|
return out
|
|
}
|