This commit is contained in:
illustris committed 2026-10-28 08:00:16 +05:30
commit 625becd03e
24 files changed
+3822

No files matched your search

+198
View File
@@ -0,0 +1,198 @@
package roots
import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
// CollectResult summarises a targeted garbage collection.
type CollectResult struct {
// Closure is the number of store paths in the closures of the targets.
Closure int `json:"closure"`
// Candidates is how many of those were not reachable from any other root.
Candidates int `json:"candidates"`
// Deleted and Freed come from nix-store's own report.
Deleted int `json:"deleted"`
Freed int64 `json:"freed"`
// Kept lists paths nix refused to delete because they turned out to be
// alive (e.g. via keep-derivations or a root created meanwhile).
Kept []string `json:"kept,omitempty"`
}
func (c CollectResult) String() string {
s := fmt.Sprintf("collected %d store paths, %s freed", c.Deleted, humanBytes(c.Freed))
if alive := c.Closure - c.Candidates + len(c.Kept); alive > 0 {
s += fmt.Sprintf(" (%d of %d closure paths still alive via other roots)", alive, c.Closure)
}
return s
}
const argChunk = 2000
// Collect deletes the parts of the targets' closures that are no longer
// reachable from any GC root, without walking the whole store the way
// `nix-store --gc` does. Liveness is pre-computed from the closure of all
// current roots (`--print-roots`); nix-store --delete re-checks it, so a
// wrong guess only costs a retry, never a live path.
func Collect(ctx context.Context, cfg Config, targets []string) (CollectResult, error) {
var res CollectResult
targets = existing(targets)
if len(targets) == 0 {
return res, nil
}
closure, err := queryClosure(ctx, cfg, targets)
if err != nil {
return res, err
}
res.Closure = len(closure)
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc", "--print-roots").Output()
if err != nil {
return res, fmt.Errorf("nix-store --print-roots: %w", cmdError(err))
}
var rootTargets []string
seen := map[string]bool{}
sc := bufio.NewScanner(bytes.NewReader(out))
sc.Buffer(make([]byte, 64*1024), 1024*1024)
for sc.Scan() {
if _, t, ok := strings.Cut(sc.Text(), " -> "); ok && !seen[t] {
seen[t] = true
rootTargets = append(rootTargets, t)
}
}
live, err := queryClosure(ctx, cfg, existing(rootTargets))
if err != nil {
return res, err
}
var cand []string
for p := range closure {
if !live[p] {
cand = append(cand, p)
}
}
res.Candidates = len(cand)
for attempt := 0; len(cand) > 0 && attempt < 50; attempt++ {
n, freed, alive, err := deletePaths(ctx, cfg, cand)
res.Deleted += n
res.Freed += freed
if err != nil && alive == "" {
return res, err
}
if alive == "" {
break
}
res.Kept = append(res.Kept, alive)
var next []string
for _, p := range existing(cand) {
if p != alive {
next = append(next, p)
}
}
cand = next
}
return res, nil
}
func existing(paths []string) []string {
var out []string
seen := map[string]bool{}
for _, p := range paths {
if p == "" || seen[p] {
continue
}
seen[p] = true
if _, err := os.Lstat(p); err == nil {
out = append(out, p)
}
}
return out
}
func queryClosure(ctx context.Context, cfg Config, paths []string) (map[string]bool, error) {
res := map[string]bool{}
for i := 0; i < len(paths); i += argChunk {
args := append([]string{"--query", "--requisites"}, paths[i:min(i+argChunk, len(paths))]...)
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, args...).Output()
if err != nil {
return nil, fmt.Errorf("nix-store --query --requisites: %w", cmdError(err))
}
for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if l != "" {
res[l] = true
}
}
}
return res, nil
}
var (
deletedRe = regexp.MustCompile(`(\d+) store paths deleted, ([0-9.]+) (\w+) freed`)
aliveRe = regexp.MustCompile(`[Cc]annot delete path '([^']+)'`)
)
// deletePaths runs nix-store --delete in chunks. If nix refuses a path
// because it is alive, that path is returned so the caller can retry
// without it.
func deletePaths(ctx context.Context, cfg Config, paths []string) (n int, freed int64, alive string, err error) {
for i := 0; i < len(paths); i += argChunk {
args := append([]string{"--delete"}, paths[i:min(i+argChunk, len(paths))]...)
out, cerr := exec.CommandContext(ctx, cfg.NixStoreBin, args...).CombinedOutput()
if m := deletedRe.FindSubmatch(out); m != nil {
c, _ := strconv.Atoi(string(m[1]))
n += c
freed += parseBytes(string(m[2]), string(m[3]))
}
if cerr != nil {
if m := aliveRe.FindSubmatch(out); m != nil {
return n, freed, string(m[1]), cerr
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
return n, freed, "", fmt.Errorf("nix-store --delete: %v: %s", cerr, lines[len(lines)-1])
}
}
return n, freed, "", nil
}
func parseBytes(num, unit string) int64 {
f, _ := strconv.ParseFloat(num, 64)
mult := map[string]float64{"bytes": 1, "KiB": 1 << 10, "MiB": 1 << 20, "GiB": 1 << 30, "TiB": 1 << 40}[unit]
if mult == 0 {
mult = 1
}
return int64(f * mult)
}
func humanBytes(n int64) string {
units := []string{"B", "KiB", "MiB", "GiB", "TiB"}
f := float64(n)
i := 0
for f >= 1024 && i < len(units)-1 {
f /= 1024
i++
}
if i == 0 {
return fmt.Sprintf("%d B", n)
}
return fmt.Sprintf("%.1f %s", f, units[i])
}
// CollectedTargets returns the store paths of roots whose deletion
// succeeded, for passing to Collect.
func CollectedTargets(results []Result) []string {
var out []string
for _, r := range results {
if r.OK() && r.Target != "" {
out = append(out, r.Target)
}
}
return out
}
+166
View File
@@ -0,0 +1,166 @@
package roots
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
type Result struct {
Link string `json:"link"`
Target string `json:"target,omitempty"`
Action string `json:"action"`
Err string `json:"error,omitempty"`
}
func (r Result) OK() bool { return r.Err == "" }
type DeleteOpts struct {
DryRun bool
// Force allows deleting protected roots (current generations,
// current-system/booted-system).
Force bool
}
// Delete removes the given roots. It only ever removes symlinks (or calls
// nix-env for profile generations); nothing inside the store is touched.
func Delete(ctx context.Context, cfg Config, rs []Root, opts DeleteOpts) []Result {
res := make([]Result, 0, len(rs))
for _, r := range rs {
res = append(res, deleteOne(ctx, cfg, r, opts))
}
return res
}
func deleteOne(ctx context.Context, cfg Config, r Root, opts DeleteOpts) Result {
res := Result{Link: r.Link, Target: r.Target}
fail := func(err error) Result {
res.Err = err.Error()
if errors.Is(err, fs.ErrPermission) {
res.Err += " (root-owned? rerun with sudo)"
}
return res
}
if !r.Deletable() {
res.Action = "skip"
return fail(errors.New("runtime roots are held by running processes and cannot be deleted"))
}
if r.Protected && !opts.Force {
res.Action = "skip"
return fail(fmt.Errorf("protected: %s", r.Reason))
}
switch r.Kind {
case KindProfile:
gen := strconv.Itoa(r.Generation)
res.Action = fmt.Sprintf("nix-env --profile %s --delete-generations %s", r.Profile, gen)
if r.Current {
// nix-env refuses to delete the current generation; with --force
// fall back to removing the link itself.
res.Action = "rm " + r.Link
if opts.DryRun {
return res
}
return orFail(res, removeSymlink(cfg, r.Link, true), fail)
}
if opts.DryRun {
return res
}
out, err := exec.CommandContext(ctx, cfg.NixEnvBin, "--profile", r.Profile, "--delete-generations", gen).CombinedOutput()
if err != nil {
msg := strings.TrimSpace(string(out))
if msg == "" {
msg = err.Error()
}
if strings.Contains(msg, "Permission denied") {
msg += " (rerun with sudo)"
}
res.Err = msg
}
return res
case KindDangling:
res.Action = "rm " + r.Via
if opts.DryRun {
return res
}
return orFail(res, removeSymlink(cfg, r.Via, false), fail)
case KindIndirect:
if r.Target == "" {
res.Action = "skip"
return fail(fmt.Errorf("cannot inspect %s: %s", r.Link, r.Reason))
}
res.Action = "rm " + r.Link
if opts.DryRun {
return res
}
if err := removeSymlink(cfg, r.Link, true); err != nil {
return fail(err)
}
// The auto/ entry is now stale; nix-store --gc prunes it, but tidy it
// up right away if we are allowed to.
if r.Via != "" {
_ = removeSymlink(cfg, r.Via, false)
}
return res
default: // KindDirect
res.Action = "rm " + r.Link
if opts.DryRun {
return res
}
return orFail(res, removeSymlink(cfg, r.Link, false), fail)
}
}
func orFail(res Result, err error, fail func(error) Result) Result {
if err != nil {
return fail(err)
}
return res
}
// removeSymlink removes p after checking it is a symlink and, if
// mustPointToStore, that it points into the store.
func removeSymlink(cfg Config, p string, mustPointToStore bool) error {
if cfg.inStore(p) || p == cfg.StoreDir {
return fmt.Errorf("refusing to remove %s: inside the store", p)
}
st, err := os.Lstat(p)
if err != nil {
return err
}
if st.Mode()&fs.ModeSymlink == 0 {
return fmt.Errorf("refusing to remove %s: not a symlink", p)
}
if mustPointToStore {
t, err := os.Readlink(p)
if err != nil {
return err
}
if !cfg.inStore(t) {
return fmt.Errorf("refusing to remove %s: points to %s, not the store", p, t)
}
}
return os.Remove(p)
}
var freedRe = regexp.MustCompile(`(\d+) store paths deleted, ([0-9.]+ \w+) freed`)
// GC runs nix-store --gc and returns its summary line.
func GC(ctx context.Context, cfg Config) (string, error) {
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc").CombinedOutput()
if m := freedRe.FindString(string(out)); m != "" {
return m, err
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
last := lines[len(lines)-1]
if err != nil {
return "", fmt.Errorf("nix-store --gc: %v: %s", err, last)
}
return last, nil
}
+78
View File
@@ -0,0 +1,78 @@
// 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
}
+209
View File
@@ -0,0 +1,209 @@
package roots
import (
"context"
"os"
"path/filepath"
"testing"
)
type fakeTree struct {
cfg Config
home string
}
func symlink(t *testing.T, target, link string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
}
func newFakeTree(t *testing.T) *fakeTree {
dir := t.TempDir()
ft := &fakeTree{
cfg: Config{
StoreDir: filepath.Join(dir, "store"),
StateDir: filepath.Join(dir, "state"),
NixStoreBin: "false",
NixBin: "false",
NixEnvBin: "true",
},
home: filepath.Join(dir, "home"),
}
store := func(name string) string {
p := filepath.Join(ft.cfg.StoreDir, name)
os.MkdirAll(p, 0o755)
return p
}
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
prof := filepath.Join(ft.cfg.StateDir, "profiles")
// indirect root: auto/h1 -> home/result -> store
symlink(t, store("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")+"/bin/hello", filepath.Join(ft.home, "result"))
symlink(t, filepath.Join(ft.home, "result"), filepath.Join(gc, "auto", "h1"))
// dangling: auto/h2 -> home/gone
symlink(t, filepath.Join(ft.home, "gone"), filepath.Join(gc, "auto", "h2"))
// auto entry pointing to a regular file is not a root
os.WriteFile(filepath.Join(ft.home, "file"), nil, 0o644)
symlink(t, filepath.Join(ft.home, "file"), filepath.Join(gc, "auto", "h3"))
// direct root
symlink(t, store("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-direct"), filepath.Join(gc, "manual", "keep"))
// gcroots/profiles -> profiles dir must not produce duplicates
symlink(t, prof, filepath.Join(gc, "profiles"))
// profile with two generations, 2 is current
symlink(t, store("cccccccccccccccccccccccccccccccc-sys1"), filepath.Join(prof, "system-1-link"))
symlink(t, store("dddddddddddddddddddddddddddddddd-sys2"), filepath.Join(prof, "system-2-link"))
symlink(t, "system-2-link", filepath.Join(prof, "system"))
// a user profile registered through auto/
symlink(t, store("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-hm"), filepath.Join(ft.home, "profiles", "hm-3-link"))
symlink(t, "hm-3-link", filepath.Join(ft.home, "profiles", "hm"))
symlink(t, filepath.Join(ft.home, "profiles", "hm-3-link"), filepath.Join(gc, "auto", "h4"))
// nix's own cache root, hidden by default
symlink(t, store("ffffffffffffffffffffffffffffffff-flake-registry.json"), filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"))
symlink(t, filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"), filepath.Join(gc, "auto", "h5"))
return ft
}
func byLink(rs []*Root) map[string]*Root {
m := map[string]*Root{}
for _, r := range rs {
m[r.Link] = r
}
return m
}
func TestScan(t *testing.T) {
ft := newFakeTree(t)
rs, err := Scan(ft.cfg)
if err != nil {
t.Fatal(err)
}
m := byLink(rs)
if len(rs) != 7 {
for _, r := range rs {
t.Logf("%s %s -> %s", r.Kind, r.Link, r.Target)
}
t.Fatalf("want 7 roots, got %d", len(rs))
}
if r := m[filepath.Join(ft.home, ".cache", "nix", "flake-registry.json")]; r == nil || !r.Hidden {
t.Errorf("cache root should be hidden: %+v", r)
}
if m[filepath.Join(ft.home, "result")].Hidden {
t.Error("result should not be hidden")
}
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
prof := filepath.Join(ft.cfg.StateDir, "profiles")
r := m[filepath.Join(ft.home, "result")]
if r == nil || r.Kind != KindIndirect || r.Via != filepath.Join(gc, "auto", "h1") ||
r.Target != filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello") {
t.Errorf("indirect root wrong: %+v", r)
}
if r.TargetName() != "hello" {
t.Errorf("TargetName = %q", r.TargetName())
}
if r := m[filepath.Join(ft.home, "gone")]; r == nil || r.Kind != KindDangling {
t.Errorf("dangling root wrong: %+v", r)
}
if r := m[filepath.Join(gc, "manual", "keep")]; r == nil || r.Kind != KindDirect {
t.Errorf("direct root wrong: %+v", r)
}
if r := m[filepath.Join(prof, "system-1-link")]; r == nil || r.Kind != KindProfile || r.Generation != 1 || r.Current {
t.Errorf("profile gen 1 wrong: %+v", r)
}
if r := m[filepath.Join(prof, "system-2-link")]; r == nil || !r.Current || !r.Protected {
t.Errorf("profile gen 2 should be current and protected: %+v", r)
}
if r := m[filepath.Join(ft.home, "profiles", "hm-3-link")]; r == nil || r.Kind != KindProfile || !r.Current || r.Via == "" {
t.Errorf("indirect profile wrong: %+v", r)
}
}
func TestDelete(t *testing.T) {
ft := newFakeTree(t)
rs, err := Scan(ft.cfg)
if err != nil {
t.Fatal(err)
}
m := byLink(rs)
ctx := context.Background()
prof := filepath.Join(ft.cfg.StateDir, "profiles")
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
result := filepath.Join(ft.home, "result")
all := []Root{*m[result], *m[filepath.Join(ft.home, "gone")], *m[filepath.Join(prof, "system-1-link")], *m[filepath.Join(prof, "system-2-link")]}
// dry run touches nothing
res := Delete(ctx, ft.cfg, all, DeleteOpts{DryRun: true})
if _, err := os.Lstat(result); err != nil {
t.Fatal("dry run removed link")
}
if !res[0].OK() || res[0].Action != "rm "+result {
t.Errorf("dry run result: %+v", res[0])
}
if res[3].OK() {
t.Errorf("current generation must be refused: %+v", res[3])
}
res = Delete(ctx, ft.cfg, all, DeleteOpts{})
for i, r := range res[:3] {
if !r.OK() {
t.Errorf("delete %d failed: %+v", i, r)
}
}
for _, p := range []string{result, filepath.Join(gc, "auto", "h1"), filepath.Join(gc, "auto", "h2")} {
if _, err := os.Lstat(p); err == nil {
t.Errorf("%s still exists", p)
}
}
if _, err := os.Stat(filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")); err != nil {
t.Error("store path must not be touched")
}
if res[2].Action != "nix-env --profile "+filepath.Join(prof, "system")+" --delete-generations 1" {
t.Errorf("profile action: %q", res[2].Action)
}
}
func TestRemoveSymlinkRefusesNonLinks(t *testing.T) {
ft := newFakeTree(t)
if err := removeSymlink(ft.cfg, filepath.Join(ft.home, "file"), false); err == nil {
t.Error("removed a regular file")
}
if err := removeSymlink(ft.cfg, filepath.Join(ft.cfg.StoreDir, "x"), false); err == nil {
t.Error("removed something in the store")
}
}
func TestParseRuntime(t *testing.T) {
cfg := DefaultConfig()
out := []byte(`/proc/1/maps -> /nix/store/a-x
/proc/1/environ -> /nix/store/b-y
/proc/1/maps -> /nix/store/a-x
/proc/22/exe -> /nix/store/c-z
{censored} -> /nix/store/d-w
/home/u/result -> /nix/store/e-v
`)
rs := parseRuntime(cfg, out, map[string]bool{})
if len(rs) != 3 {
t.Fatalf("want 3 runtime groups, got %d", len(rs))
}
m := byLink(rs)
if m["/proc/1"].Count != 2 || m["/proc/22"].Target != "/nix/store/c-z" || m["{censored}"] == nil {
t.Errorf("bad grouping: %+v %+v", m["/proc/1"], m["/proc/22"])
}
}
func TestParsePathInfo(t *testing.T) {
obj := parsePathInfo([]byte(`{"/nix/store/a-x":{"closureSize":10,"narSize":5},"/nix/store/b-y":null}`))
if obj["/nix/store/a-x"].ClosureSize != 10 || len(obj) != 1 {
t.Errorf("object format: %+v", obj)
}
arr := parsePathInfo([]byte(`[{"path":"/nix/store/a-x","closureSize":7,"narSize":3},{"path":"/nix/store/b-y","valid":false}]`))
if arr["/nix/store/a-x"].ClosureSize != 7 || len(arr) != 1 {
t.Errorf("array format: %+v", arr)
}
}
+519
View File
@@ -0,0 +1,519 @@
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
}
+82
View File
@@ -0,0 +1,82 @@
package roots
import (
"context"
"encoding/json"
"os/exec"
"sync"
)
const sizeBatch = 16
// QuerySizes asks `nix path-info -S` for closure and NAR sizes in batches,
// calling cb with each batch's results ([closure, nar]) and batch length.
// Paths that are not valid get -2 so they are not retried.
func QuerySizes(ctx context.Context, cfg Config, paths []string, cb func(map[string][2]int64, int)) {
batches := make(chan []string)
var wg sync.WaitGroup
for w := 0; w < 4; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for b := range batches {
cb(pathInfo(ctx, cfg, b), len(b))
}
}()
}
for i := 0; i < len(paths); i += sizeBatch {
end := min(i+sizeBatch, len(paths))
select {
case batches <- paths[i:end]:
case <-ctx.Done():
}
}
close(batches)
wg.Wait()
}
type pathInfoEntry struct {
Path string `json:"path"`
Valid *bool `json:"valid"`
ClosureSize int64 `json:"closureSize"`
NarSize int64 `json:"narSize"`
}
func pathInfo(ctx context.Context, cfg Config, paths []string) map[string][2]int64 {
res := map[string][2]int64{}
for _, p := range paths {
res[p] = [2]int64{-2, -2}
}
args := append([]string{"--extra-experimental-features", "nix-command", "path-info", "--json", "-S"}, paths...)
// path-info exits non-zero when some paths are invalid but still prints
// JSON for the rest, so the exit status is ignored.
out, _ := exec.CommandContext(ctx, cfg.NixBin, args...).Output()
for p, e := range parsePathInfo(out) {
res[p] = [2]int64{e.ClosureSize, e.NarSize}
}
return res
}
// parsePathInfo accepts both the object-keyed-by-path format (Nix >= 2.19)
// and the older array format.
func parsePathInfo(out []byte) map[string]pathInfoEntry {
res := map[string]pathInfoEntry{}
var obj map[string]*pathInfoEntry
if err := json.Unmarshal(out, &obj); err == nil {
for p, e := range obj {
if e != nil {
res[p] = *e
}
}
return res
}
var arr []pathInfoEntry
if err := json.Unmarshal(out, &arr); err == nil {
for _, e := range arr {
if e.Path != "" && (e.Valid == nil || *e.Valid) {
res[e.Path] = e
}
}
}
return res
}