2009-04-16 09:25:42 +00:00
|
|
|
use strict;
|
|
|
|
use readmanifest;
|
|
|
|
use File::Basename;
|
|
|
|
use File::stat;
|
|
|
|
use File::Temp qw/tempfile/;
|
2010-01-26 09:38:13 +00:00
|
|
|
use Fcntl ':flock';
|
2009-04-16 09:25:42 +00:00
|
|
|
|
2010-06-23 14:07:47 +00:00
|
|
|
if (scalar @ARGV != 6 && scalar @ARGV != 7) {
|
|
|
|
print STDERR "Syntax: perl mirror-channel.pl <src-channel-url> <dst-channel-dir> <nar-dir> <nar-url> <patches-dir> <patches-url> [<nix-exprs-url>]\n";
|
2009-04-16 09:25:42 +00:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2010-01-26 09:17:50 +00:00
|
|
|
my $curl = "curl --location --silent --show-error --fail";
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
my $srcChannelURL = $ARGV[0];
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstChannelPath = $ARGV[1];
|
|
|
|
my $narPath = $ARGV[2];
|
|
|
|
my $narURL = $ARGV[3];
|
|
|
|
my $patchesPath = $ARGV[4];
|
|
|
|
my $patchesURL = $ARGV[5];
|
|
|
|
my $nixexprsURL = $ARGV[6] || "$srcChannelURL/nixexprs.tar.bz2";
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
die "$dstChannelPath doesn't exist\n" unless -d $dstChannelPath;
|
2010-06-23 14:07:47 +00:00
|
|
|
die "$narPath doesn't exist\n" unless -d $narPath;
|
|
|
|
die "$patchesPath doesn't exist\n" unless -d $patchesPath;
|
2009-04-16 09:25:42 +00:00
|
|
|
|
2010-01-26 09:38:13 +00:00
|
|
|
open LOCK, ">$dstChannelPath/.lock" or die;
|
|
|
|
flock LOCK, LOCK_EX;
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
my ($fh, $tmpManifest) = tempfile(UNLINK => 1);
|
2010-01-26 09:17:50 +00:00
|
|
|
system("$curl '$srcChannelURL/MANIFEST' > $tmpManifest") == 0 or die;
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
# Read the manifest.
|
|
|
|
my %narFiles;
|
|
|
|
my %localPaths;
|
|
|
|
my %patches;
|
|
|
|
|
|
|
|
my $version = readManifest($tmpManifest, \%narFiles, \%localPaths, \%patches);
|
|
|
|
|
|
|
|
%localPaths = ();
|
|
|
|
%patches = (); # not supported yet
|
|
|
|
|
|
|
|
my $size = scalar (keys %narFiles);
|
|
|
|
print "$size store paths in manifest\n";
|
|
|
|
|
2010-01-26 09:51:05 +00:00
|
|
|
# Protect against Hydra problems that leave the channel empty.
|
|
|
|
die "cowardly refusing to mirror an empty channel" if $size == 0;
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
# Download every file that we don't already have, and update every URL
|
|
|
|
# to point to the mirror. Also fill in the size and hash fields in
|
|
|
|
# the manifest in order to be compatible with Nix < 0.13.
|
|
|
|
|
|
|
|
while (my ($storePath, $files) = each %narFiles) {
|
|
|
|
foreach my $file (@{$files}) {
|
2009-10-16 11:33:48 +00:00
|
|
|
my $narHash = $file->{narHash};
|
2009-04-16 09:25:42 +00:00
|
|
|
my $srcURL = $file->{url};
|
2009-10-16 11:33:48 +00:00
|
|
|
my $dstName = $narHash;
|
|
|
|
$dstName =~ s/:/_/; # `:' in filenames might cause problems
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstFile = "$narPath/$dstName";
|
|
|
|
my $dstURL = "$narURL/$dstName";
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
$file->{url} = $dstURL;
|
|
|
|
if (! -e $dstFile) {
|
|
|
|
print "downloading $srcURL\n";
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstFileTmp = "$narPath/.tmp.$$.nar.$dstName";
|
|
|
|
system("$curl '$srcURL' > $dstFileTmp") == 0 or next;
|
2009-04-16 09:25:42 +00:00
|
|
|
rename($dstFileTmp, $dstFile) or die "cannot rename $dstFileTmp";
|
|
|
|
}
|
|
|
|
|
|
|
|
$file->{size} = stat($dstFile)->size or die;
|
|
|
|
|
2010-06-23 14:07:47 +00:00
|
|
|
my $hashFile = "$narPath/.hash.$dstName";
|
2009-04-16 09:25:42 +00:00
|
|
|
my $hash;
|
|
|
|
if (-e $hashFile) {
|
|
|
|
open HASH, "<$hashFile" or die;
|
|
|
|
$hash = <HASH>;
|
|
|
|
close HASH;
|
|
|
|
} else {
|
|
|
|
$hash = `nix-hash --flat --type sha256 --base32 '$dstFile'` or die;
|
|
|
|
chomp $hash;
|
|
|
|
open HASH, ">$hashFile" or die;
|
|
|
|
print HASH $hash;
|
|
|
|
close HASH;
|
|
|
|
}
|
|
|
|
$file->{hash} = "sha256:$hash";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Write the new manifest.
|
|
|
|
writeManifest("$dstChannelPath/MANIFEST", \%narFiles, \%patches);
|
2009-04-17 13:48:30 +00:00
|
|
|
|
|
|
|
# Mirror nixexprs.tar.bz2.
|
|
|
|
my $tmpFile = "$dstChannelPath/.tmp.$$.nixexprs.tar.bz2";
|
2010-06-23 14:07:47 +00:00
|
|
|
system("$curl '$nixexprsURL' > $tmpFile") == 0 or die "cannot download `$nixexprsURL'";
|
2009-04-17 13:48:30 +00:00
|
|
|
rename($tmpFile, "$dstChannelPath/nixexprs.tar.bz2") or die "cannot rename $tmpFile";
|
2010-06-23 14:07:47 +00:00
|
|
|
|
|
|
|
# Remove ".hash.*" files corresponding to NARs that have been removed.
|
|
|
|
foreach my $fn (glob "$narPath/.hash.*") {
|
|
|
|
my $fn2 = $fn;
|
|
|
|
$fn2 =~ s/\.hash\.//;
|
|
|
|
if (! -e "$fn2") {
|
|
|
|
print STDERR "removing hash $fn\n";
|
|
|
|
unlink "$fn";
|
|
|
|
}
|
|
|
|
}
|