lix/scripts/copy-from-other-stores.pl.in

104 lines
2.8 KiB
Perl
Raw Normal View History

#! @perl@ -w @perlFlags@
2014-08-20 15:00:17 +00:00
use utf8;
use strict;
use File::Basename;
use IO::Handle;
2008-11-20 15:44:59 +00:00
my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@";
STDOUT->autoflush(1);
binmode STDERR, ":encoding(utf8)";
my @remoteStoresAll = split ':', ($ENV{"NIX_OTHER_STORES"} or "");
my @remoteStores;
foreach my $dir (@remoteStoresAll) {
push @remoteStores, glob($dir);
}
exit if scalar @remoteStores == 0;
print "\n";
$ENV{"NIX_REMOTE"} = "";
sub findStorePath {
my $storePath = shift;
foreach my $store (@remoteStores) {
my $sourcePath = "$store/store/" . basename $storePath;
next unless -e $sourcePath || -l $sourcePath;
$ENV{"NIX_DB_DIR"} = "$store/var/nix/db";
return ($store, $sourcePath) if
system("$binDir/nix-store --check-validity $storePath") == 0;
}
return undef;
}
if ($ARGV[0] eq "--query") {
while (<STDIN>) {
2012-07-11 22:52:09 +00:00
chomp;
my ($cmd, @args) = split " ", $_;
if ($cmd eq "have") {
2012-07-11 22:52:09 +00:00
foreach my $storePath (@args) {
print "$storePath\n" if defined findStorePath($storePath);
}
print "\n";
}
elsif ($cmd eq "info") {
2012-07-11 22:52:09 +00:00
foreach my $storePath (@args) {
my ($store, $sourcePath) = findStorePath($storePath);
next unless defined $store;
2012-07-11 22:52:09 +00:00
$ENV{"NIX_DB_DIR"} = "$store/var/nix/db";
my $deriver = `$binDir/nix-store --query --deriver $storePath`;
2014-08-20 15:00:17 +00:00
die "cannot query deriver of $storePath" if $? != 0;
2012-07-11 22:52:09 +00:00
chomp $deriver;
$deriver = "" if $deriver eq "unknown-deriver";
my @references = split "\n",
`$binDir/nix-store --query --references $storePath`;
2014-08-20 15:00:17 +00:00
die "cannot query references of $storePath" if $? != 0;
2012-07-11 22:52:09 +00:00
my $narSize = `$binDir/nix-store --query --size $storePath`;
2014-08-20 15:00:17 +00:00
die "cannot query size of $storePath" if $? != 0;
2012-07-11 22:52:09 +00:00
chomp $narSize;
print "$storePath\n";
print "$deriver\n";
print scalar @references, "\n";
print "$_\n" foreach @references;
print "0\n";
2012-07-11 22:52:09 +00:00
print "$narSize\n";
}
print "\n";
}
2014-08-20 15:00:17 +00:00
else { die "unknown command $cmd"; }
}
}
elsif ($ARGV[0] eq "--substitute") {
die unless scalar @ARGV == 3;
my $storePath = $ARGV[1];
my $destPath = $ARGV[2];
my ($store, $sourcePath) = findStorePath $storePath;
die unless $store;
2014-08-20 15:00:17 +00:00
print STDERR "\n*** Copying $storePath from $sourcePath\n\n";
2015-06-04 12:55:40 +00:00
system("@coreutils@/cp", "-rpd", $sourcePath, $destPath) == 0
2014-08-20 15:00:17 +00:00
or die "cannot copy $sourcePath to $storePath";
print "\n"; # no hash to verify
}
else { die; }