From c505702265833a762d681952bcc72562d64a242e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 27 Jan 2005 15:21:29 +0000 Subject: * Fix and simplify the garbage collector (it's still not concurrent, though). In particular it's now much easier to register a GC root. Just place a symlink to whatever store path it is that you want to keep in /nix/var/nix/gcroots. --- scripts/nix-build.in | 2 +- scripts/nix-collect-garbage.in | 53 +++++++++++++++++------------------------- 2 files changed, 22 insertions(+), 33 deletions(-) (limited to 'scripts') diff --git a/scripts/nix-build.in b/scripts/nix-build.in index a5d747686a..5ae591f351 100644 --- a/scripts/nix-build.in +++ b/scripts/nix-build.in @@ -23,7 +23,7 @@ for i in "$@"; do for j in $storeExprs; do echo "store expression is $j" >&2 done - outPaths=$(@bindir@/nix-store -qnfv $extraArgs $storeExprs) + outPaths=$(@bindir@/nix-store -rv $extraArgs $storeExprs) for j in $outPaths; do echo "$j" if test -z "$noLink"; then diff --git a/scripts/nix-collect-garbage.in b/scripts/nix-collect-garbage.in index 44bcc16bbc..f11ed2cb69 100644 --- a/scripts/nix-collect-garbage.in +++ b/scripts/nix-collect-garbage.in @@ -9,7 +9,6 @@ my $storeDir = "@storedir@"; my %alive; my $gcOper = "--delete"; -my $minAge = 0; my @roots = (); @@ -20,33 +19,11 @@ for (my $i = 0; $i < scalar @ARGV; $i++) { if ($arg eq "--delete" || $arg eq "--print-live" || $arg eq "--print-dead") { $gcOper = $arg; } - elsif ($arg eq "--min-age") { - $i++; - $minAge = undef; - $minAge = $ARGV[$i]; - die "invalid minimum age" unless defined $minAge && $minAge =~ /^\d*$/; - } else { die "unknown argument `$arg'" }; } -# Read all GC roots from the given file. -sub readRoots { - my $fileName = shift; - open ROOT, "<$fileName" or die "cannot open `$fileName': $!"; - while () { - chomp; - foreach my $root (split ' ') { - die "bad root `$root' in file `$fileName'" - unless $root =~ /^\S+$/; - push @roots, $root; - } - } - close ROOT; -} - - -# Recursively finds all *.gcroot files in the given directory. +# Recursively finds all symlinks to the store in the given directory. sub findRoots; sub findRoots { my $followSymlinks = shift; @@ -58,14 +35,26 @@ sub findRoots { foreach my $name (@names) { next if $name eq "." || $name eq ".."; - $name = $dir . "/" . $name; - if ($name =~ /.gcroot$/ && -f $name) { - readRoots $name; - } - elsif (-d $name) { - if ($followSymlinks || !-l $name) { - findRoots 0, $name; + my $path = $dir . "/" . $name; + + if (-l $path) { + my $target = readlink $path + or die "cannot read symlink `$path': $!"; + + if (substr($target, 0, length $storeDir) eq $storeDir) { + # We're only interested in the store-level part. + $target = substr($target, length $storeDir); + $target = "$storeDir/$target"; + push @roots, $target; } + + elsif ($followSymlinks && -d $path) { + findRoots 0, $path; + } + } + + elsif (-d $path) { + findRoots $followSymlinks, $path; } } @@ -77,7 +66,7 @@ findRoots 1, $rootsDir; # Run the collector with the roots we found. -my $pid = open2(">&1", \*WRITE, "@bindir@/nix-store --gc $gcOper --min-age $minAge") +my $pid = open2(">&1", \*WRITE, "@bindir@/nix-store --gc $gcOper") or die "cannot run `nix-store --gc'"; foreach my $root (@roots) { -- cgit 1.4.1