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