* Look for GC roots in @localstatedir@/nix/gcroots.

This commit is contained in:
Eelco Dolstra 2004-04-22 14:17:57 +00:00
parent d7238bc84e
commit 759c953196
2 changed files with 58 additions and 20 deletions

View file

@ -19,8 +19,12 @@ init-state:
$(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/db $(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/db
$(INSTALL) -d $(DESTDIR)$(localstatedir)/log/nix $(INSTALL) -d $(DESTDIR)$(localstatedir)/log/nix
$(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/profiles $(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/profiles
$(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/gcroots
$(INSTALL) -d $(DESTDIR)$(localstatedir)/nix/gcroots/tmp
rm -f $(DESTDIR)$(localstatedir)/nix/gcroots/profiles
ln -s $(localstatedir)/nix/profiles $(DESTDIR)$(localstatedir)/nix/gcroots/profiles
$(INSTALL) -d $(DESTDIR)$(prefix)/store $(INSTALL) -d $(DESTDIR)$(prefix)/store
# $(bindir)/nix-store --init # $(bindir)/nix-store --init
else else
init-state: init-state:
endif endif

View file

@ -3,41 +3,74 @@
use strict; use strict;
use IPC::Open2; use IPC::Open2;
my $linkdir = "@localstatedir@/nix/profiles"; my $rootsDir = "@localstatedir@/nix/gcroots";
my $storedir = "@storedir@"; my $storeDir = "@storedir@";
my %alive; my %alive;
my $keepsuccessors = 1; my $keepSuccessors = 1;
my $invert = 0; my $invert = 0;
my @roots = ();
# Parse the command line.
foreach my $arg (@ARGV) { foreach my $arg (@ARGV) {
if ($arg eq "--no-successors") { $keepsuccessors = 0; } if ($arg eq "--no-successors") { $keepSuccessors = 0; }
elsif ($arg eq "--invert") { $invert = 1; } elsif ($arg eq "--invert") { $invert = 1; }
else { die "unknown argument `$arg'" }; else { die "unknown argument `$arg'" };
} }
opendir(DIR, $linkdir) or die "cannot open directory $linkdir: $!";
my @links = readdir DIR or die "cannot read directory $linkdir: $!";
closedir DIR;
my @roots; # Read all GC roots from the given file.
foreach my $link (@links) { sub readRoots {
$link = $linkdir . "/" . $link; my $fileName = shift;
next if (!($link =~ /.gcroot$/)); open ROOT, "<$fileName" or die "cannot open `$fileName': $!";
open ROOT, "<$link" or die "cannot open $link: $!";
while (<ROOT>) { while (<ROOT>) {
chomp; chomp;
foreach my $root (split ' ') { foreach my $root (split ' ') {
die "bad root `$root' in file `$link'" unless $root =~ /^\S+$/; die "bad root `$root' in file `$fileName'"
unless $root =~ /^\S+$/;
push @roots, $root; push @roots, $root;
} }
} }
close ROOT; close ROOT;
} }
# Recursively finds all *.gcroot files in the given directory.
sub findRoots;
sub findRoots {
my $followSymlinks = shift;
my $dir = shift;
opendir(DIR, $dir) or die "cannot open directory `$dir': $!";
my @names = readdir DIR or die "cannot read directory `$dir': $!";
closedir DIR;
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;
}
}
}
}
# Find GC roots, starting at $rootsDir.
findRoots 1, $rootsDir;
# Determine all store paths reachable from the roots.
my $extraarg = ""; my $extraarg = "";
if ($keepsuccessors) { $extraarg = "--include-successors"; }; if ($keepSuccessors) { $extraarg = "--include-successors"; };
my $pid = open2(\*READ, \*WRITE, "@bindir@/nix-store --query --requisites $extraarg @roots") my $pid = open2(\*READ, \*WRITE, "@bindir@/nix-store --query --requisites $extraarg @roots")
or die "determining live paths"; or die "determining live paths";
close WRITE; close WRITE;
@ -53,14 +86,15 @@ $? == 0 or die "determining live paths";
exit 0 if ($invert); exit 0 if ($invert);
opendir(DIR, $storedir) or die "cannot open directory $storedir: $!";
my @names = readdir DIR;
closedir DIR;
foreach my $name (@names) { # Using that information, find all store paths *not* reachable from
# the roots.
opendir(DIR, $storeDir) or die "cannot open directory $storeDir: $!";
foreach my $name (readdir DIR) {
next if ($name eq "." || $name eq ".."); next if ($name eq "." || $name eq "..");
$name = "$storedir/$name"; $name = "$storeDir/$name";
if (!$alive{$name}) { if (!$alive{$name}) {
print "$name\n"; print "$name\n";
} }
} }
closedir DIR;