2012-06-30 00:28:52 +02:00
|
|
|
|
#! @perl@ -w @perlFlags@
|
|
|
|
|
|
|
|
|
|
use strict;
|
2012-07-02 18:42:58 +02:00
|
|
|
|
use File::Basename;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
use Nix::Config;
|
|
|
|
|
use Nix::Store;
|
|
|
|
|
|
2012-07-02 03:55:36 +02:00
|
|
|
|
|
|
|
|
|
my @binaryCacheUrls = split / /, ($ENV{"NIX_BINARY_CACHES"} || "");
|
|
|
|
|
|
2012-06-30 00:28:52 +02:00
|
|
|
|
|
|
|
|
|
sub getInfoFrom {
|
|
|
|
|
my ($storePath, $pathHash, $binaryCacheUrl) = @_;
|
|
|
|
|
my $infoUrl = "$binaryCacheUrl/$pathHash.narinfo";
|
|
|
|
|
#print STDERR "checking $infoUrl...\n";
|
2012-07-02 18:42:58 +02:00
|
|
|
|
my $s = `$Nix::Config::curl --fail --silent --location $infoUrl`;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
if ($? != 0) {
|
2012-07-02 18:42:58 +02:00
|
|
|
|
my $status = $? >> 8;
|
|
|
|
|
print STDERR "could not download ‘$infoUrl’ (curl returned status ", $? >> 8, ")\n"
|
|
|
|
|
if $status != 22 && $status != 37;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
return undef;
|
|
|
|
|
}
|
2012-07-02 00:46:38 +02:00
|
|
|
|
my ($storePath2, $url, $compression, $fileHash, $fileSize, $narHash, $narSize, $deriver);
|
2012-06-30 00:28:52 +02:00
|
|
|
|
my @refs;
|
|
|
|
|
foreach my $line (split "\n", $s) {
|
|
|
|
|
$line =~ /^(.*): (.*)$/ or return undef;
|
|
|
|
|
if ($1 eq "StorePath") { $storePath2 = $2; }
|
|
|
|
|
elsif ($1 eq "URL") { $url = $2; }
|
2012-07-02 00:46:38 +02:00
|
|
|
|
elsif ($1 eq "Compression") { $compression = $2; }
|
|
|
|
|
elsif ($1 eq "FileHash") { $fileHash = $2; }
|
|
|
|
|
elsif ($1 eq "FileSize") { $fileSize = int($2); }
|
2012-06-30 00:28:52 +02:00
|
|
|
|
elsif ($1 eq "NarHash") { $narHash = $2; }
|
|
|
|
|
elsif ($1 eq "NarSize") { $narSize = int($2); }
|
|
|
|
|
elsif ($1 eq "References") { @refs = split / /, $2; }
|
|
|
|
|
elsif ($1 eq "Deriver") { $deriver = $2; }
|
|
|
|
|
}
|
2012-07-03 00:53:04 +02:00
|
|
|
|
if ($storePath ne $storePath2 || !defined $url || !defined $narHash) {
|
2012-06-30 00:28:52 +02:00
|
|
|
|
print STDERR "bad NAR info file ‘$infoUrl’\n";
|
2012-07-03 00:53:04 +02:00
|
|
|
|
return undef;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
{ url => $url
|
2012-07-02 00:46:38 +02:00
|
|
|
|
, compression => ($compression || "bzip2")
|
2012-06-30 00:28:52 +02:00
|
|
|
|
, fileHash => $fileHash
|
|
|
|
|
, fileSize => $fileSize
|
|
|
|
|
, narHash => $narHash
|
|
|
|
|
, narSize => $narSize
|
|
|
|
|
, refs => [ map { "$Nix::Config::storeDir/$_" } @refs ]
|
2012-07-03 00:53:04 +02:00
|
|
|
|
, deriver => defined $deriver ? "$Nix::Config::storeDir/$deriver" : undef
|
2012-06-30 00:28:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 03:55:36 +02:00
|
|
|
|
|
2012-06-30 00:28:52 +02:00
|
|
|
|
sub getInfo {
|
|
|
|
|
my ($storePath) = @_;
|
|
|
|
|
|
2012-07-02 18:42:58 +02:00
|
|
|
|
my $pathHash = substr(basename($storePath), 0, 32);
|
2012-06-30 00:28:52 +02:00
|
|
|
|
|
|
|
|
|
cache: foreach my $binaryCacheUrl (@binaryCacheUrls) {
|
|
|
|
|
my $info = getInfoFrom($storePath, $pathHash, $binaryCacheUrl);
|
|
|
|
|
return $info if defined $info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 03:55:36 +02:00
|
|
|
|
|
2012-06-30 00:28:52 +02:00
|
|
|
|
sub downloadBinary {
|
|
|
|
|
my ($storePath) = @_;
|
|
|
|
|
|
2012-07-02 18:42:58 +02:00
|
|
|
|
my $pathHash = substr(basename($storePath), 0, 32);
|
2012-06-30 00:28:52 +02:00
|
|
|
|
|
|
|
|
|
cache: foreach my $binaryCacheUrl (@binaryCacheUrls) {
|
|
|
|
|
my $info = getInfoFrom($storePath, $pathHash, $binaryCacheUrl);
|
|
|
|
|
if (defined $info) {
|
2012-07-02 00:46:38 +02:00
|
|
|
|
my $decompressor;
|
|
|
|
|
if ($info->{compression} eq "bzip2") { $decompressor = "$Nix::Config::bzip2 -d"; }
|
|
|
|
|
elsif ($info->{compression} eq "xz") { $decompressor = "$Nix::Config::xz -d"; }
|
|
|
|
|
else {
|
|
|
|
|
print STDERR "unknown compression method ‘$info->{compression}’\n";
|
|
|
|
|
next;
|
|
|
|
|
}
|
2012-07-03 00:53:04 +02:00
|
|
|
|
if (system("$Nix::Config::curl --fail --location $binaryCacheUrl/$info->{url} | $decompressor | $Nix::Config::binDir/nix-store --restore $storePath") != 0) {
|
|
|
|
|
die "download of `$info->{url}' failed" . ($! ? ": $!" : "") . "\n" unless $? == 0;
|
|
|
|
|
next;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
}
|
2012-07-03 00:53:04 +02:00
|
|
|
|
# The hash in the manifest can be either in base-16 or
|
|
|
|
|
# base-32. Handle both.
|
|
|
|
|
$info->{narHash} =~ /^sha256:(.*)$/ or die "invalid hash";
|
|
|
|
|
my $hash = $1;
|
|
|
|
|
my $hash2 = hashPath("sha256", 1, $storePath);
|
|
|
|
|
die "hash mismatch in downloaded path ‘$storePath’; expected $hash, got $hash2\n"
|
|
|
|
|
if $hash ne $hash2;
|
|
|
|
|
return 1;
|
2012-06-30 00:28:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 03:55:36 +02:00
|
|
|
|
|
2012-06-30 00:28:52 +02:00
|
|
|
|
if ($ARGV[0] eq "--query") {
|
|
|
|
|
|
|
|
|
|
while (<STDIN>) {
|
|
|
|
|
my $cmd = $_; chomp $cmd;
|
|
|
|
|
|
|
|
|
|
if ($cmd eq "have") {
|
|
|
|
|
my $storePath = <STDIN>; chomp $storePath;
|
|
|
|
|
# FIXME: want to give correct info here, but it's too slow.
|
|
|
|
|
print "0\n";
|
|
|
|
|
#my $info = getInfo($storePath);
|
|
|
|
|
#if (defined $info) { print "1\n"; } else { print "0\n"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($cmd eq "info") {
|
|
|
|
|
my $storePath = <STDIN>; chomp $storePath;
|
|
|
|
|
my $info = getInfo($storePath);
|
|
|
|
|
if (defined $info) {
|
|
|
|
|
print "1\n";
|
|
|
|
|
print $info->{deriver} || "", "\n";
|
|
|
|
|
print scalar @{$info->{refs}}, "\n";
|
|
|
|
|
print "$_\n" foreach @{$info->{refs}};
|
|
|
|
|
print $info->{fileSize} || 0, "\n";
|
2012-07-03 00:53:04 +02:00
|
|
|
|
print $info->{narSize} || 0, "\n";
|
2012-06-30 00:28:52 +02:00
|
|
|
|
} else {
|
|
|
|
|
print "0\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else { die "unknown command `$cmd'"; }
|
|
|
|
|
|
|
|
|
|
flush STDOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($ARGV[0] eq "--substitute") {
|
|
|
|
|
my $storePath = $ARGV[1] or die;
|
2012-07-02 18:42:58 +02:00
|
|
|
|
if (!downloadBinary($storePath)) {
|
|
|
|
|
print STDERR "could not download ‘$storePath’ from any binary cache\n";
|
|
|
|
|
}
|
2012-06-30 00:28:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
die;
|
|
|
|
|
}
|