2004-03-15 15:23:53 +00:00
|
|
|
#! @perl@ -w
|
2003-07-14 10:45:04 +00:00
|
|
|
|
2003-08-25 14:36:04 +00:00
|
|
|
use strict;
|
|
|
|
use IPC::Open2;
|
|
|
|
|
2004-04-22 14:17:57 +00:00
|
|
|
my $rootsDir = "@localstatedir@/nix/gcroots";
|
|
|
|
my $storeDir = "@storedir@";
|
2003-07-14 10:45:04 +00:00
|
|
|
|
|
|
|
my %alive;
|
|
|
|
|
2004-08-25 15:39:13 +00:00
|
|
|
my $gcOper = "--delete";
|
2005-01-31 10:27:25 +00:00
|
|
|
my $extraArgs = "";
|
2003-07-29 14:28:17 +00:00
|
|
|
|
2004-04-22 14:17:57 +00:00
|
|
|
my @roots = ();
|
|
|
|
|
|
|
|
|
|
|
|
# Parse the command line.
|
2004-08-25 16:54:08 +00:00
|
|
|
for (my $i = 0; $i < scalar @ARGV; $i++) {
|
|
|
|
my $arg = $ARGV[$i];
|
2004-08-25 15:39:13 +00:00
|
|
|
if ($arg eq "--delete" || $arg eq "--print-live" || $arg eq "--print-dead") {
|
|
|
|
$gcOper = $arg;
|
2004-08-25 16:54:08 +00:00
|
|
|
}
|
2005-01-31 10:27:25 +00:00
|
|
|
elsif ($arg =~ /^-v+$/) {
|
|
|
|
$extraArgs = "$extraArgs $arg";
|
|
|
|
}
|
2004-08-25 16:54:08 +00:00
|
|
|
else { die "unknown argument `$arg'" };
|
2003-07-29 14:28:17 +00:00
|
|
|
}
|
|
|
|
|
2003-11-22 15:58:34 +00:00
|
|
|
|
2005-01-27 15:21:29 +00:00
|
|
|
# Recursively finds all symlinks to the store in the given directory.
|
2004-04-22 14:17:57 +00:00
|
|
|
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 "..";
|
2005-01-27 15:21:29 +00:00
|
|
|
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;
|
2004-04-22 14:17:57 +00:00
|
|
|
}
|
2005-01-27 15:21:29 +00:00
|
|
|
|
|
|
|
elsif ($followSymlinks && -d $path) {
|
|
|
|
findRoots 0, $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif (-d $path) {
|
|
|
|
findRoots $followSymlinks, $path;
|
2004-04-22 14:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Find GC roots, starting at $rootsDir.
|
|
|
|
findRoots 1, $rootsDir;
|
|
|
|
|
|
|
|
|
2004-08-25 15:39:13 +00:00
|
|
|
# Run the collector with the roots we found.
|
2005-01-31 10:27:25 +00:00
|
|
|
my $pid = open2(">&1", \*WRITE, "@bindir@/nix-store --gc $gcOper $extraArgs")
|
2004-08-25 15:39:13 +00:00
|
|
|
or die "cannot run `nix-store --gc'";
|
2003-08-25 14:36:04 +00:00
|
|
|
|
2004-08-25 15:39:13 +00:00
|
|
|
foreach my $root (@roots) {
|
|
|
|
print WRITE "$root\n";
|
|
|
|
}
|
2003-07-29 14:28:17 +00:00
|
|
|
|
2004-08-25 15:39:13 +00:00
|
|
|
close WRITE;
|
2003-07-14 10:45:04 +00:00
|
|
|
|
2004-08-25 15:39:13 +00:00
|
|
|
waitpid $pid, 0;
|
|
|
|
$? == 0 or die "`nix-store --gc' failed";
|