Delete manifests in "nix-channel --remove" or when a binary cache is available

This commit is contained in:
Eelco Dolstra 2012-09-13 11:35:46 -04:00
parent 6c4ac29917
commit b14717ab90
4 changed files with 47 additions and 35 deletions

View file

@ -9,12 +9,12 @@ use Fcntl ':flock';
use Nix::Config; use Nix::Config;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw(readManifest writeManifest updateManifestDB addPatch); our @EXPORT = qw(readManifest writeManifest updateManifestDB addPatch deleteOldManifests);
sub addNAR { sub addNAR {
my ($narFiles, $storePath, $info) = @_; my ($narFiles, $storePath, $info) = @_;
$$narFiles{$storePath} = [] $$narFiles{$storePath} = []
unless defined $$narFiles{$storePath}; unless defined $$narFiles{$storePath};
@ -24,7 +24,7 @@ sub addNAR {
foreach my $narFile (@{$narFileList}) { foreach my $narFile (@{$narFileList}) {
$found = 1 if $narFile->{url} eq $info->{url}; $found = 1 if $narFile->{url} eq $info->{url};
} }
push @{$narFileList}, $info if !$found; push @{$narFileList}, $info if !$found;
} }
@ -43,7 +43,7 @@ sub addPatch {
$patch2->{url} eq $patch->{url} && $patch2->{url} eq $patch->{url} &&
$patch2->{basePath} eq $patch->{basePath}; $patch2->{basePath} eq $patch->{basePath};
} }
push @{$patchList}, $patch if !$found; push @{$patchList}, $patch if !$found;
return !$found; return !$found;
@ -93,10 +93,10 @@ sub readManifest_ {
undef $system; undef $system;
$references = ""; $references = "";
$deriver = ""; $deriver = "";
} }
} else { } else {
if (/^\}$/) { if (/^\}$/) {
$inside = 0; $inside = 0;
@ -120,7 +120,7 @@ sub readManifest_ {
} }
} }
elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) { $storePath = $1; } elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) { $storePath = $1; }
elsif (/^\s*CopyFrom:\s*(\/\S+)\s*$/) { $copyFrom = $1; } elsif (/^\s*CopyFrom:\s*(\/\S+)\s*$/) { $copyFrom = $1; }
elsif (/^\s*Hash:\s*(\S+)\s*$/) { $hash = $1; } elsif (/^\s*Hash:\s*(\S+)\s*$/) { $hash = $1; }
@ -184,7 +184,7 @@ sub writeManifest {
print MANIFEST "}\n"; print MANIFEST "}\n";
} }
} }
foreach my $storePath (sort (keys %{$patches})) { foreach my $storePath (sort (keys %{$patches})) {
my $patchList = $$patches{$storePath}; my $patchList = $$patches{$storePath};
foreach my $patch (@{$patchList}) { foreach my $patch (@{$patchList}) {
@ -201,8 +201,8 @@ sub writeManifest {
print MANIFEST "}\n"; print MANIFEST "}\n";
} }
} }
close MANIFEST; close MANIFEST;
rename("$manifest.tmp", $manifest) rename("$manifest.tmp", $manifest)
@ -211,11 +211,11 @@ sub writeManifest {
# Create a bzipped manifest. # Create a bzipped manifest.
unless (defined $noCompress) { unless (defined $noCompress) {
system("$Nix::Config::bzip2 < $manifest > $manifest.bz2.tmp") == 0 system("$Nix::Config::bzip2 < $manifest > $manifest.bz2.tmp") == 0
or die "cannot compress manifest"; or die "cannot compress manifest";
rename("$manifest.bz2.tmp", "$manifest.bz2") rename("$manifest.bz2.tmp", "$manifest.bz2")
or die "cannot rename $manifest.bz2.tmp: $!"; or die "cannot rename $manifest.bz2.tmp: $!";
} }
} }
@ -224,7 +224,7 @@ sub updateManifestDB {
my $manifestDir = $Nix::Config::manifestDir; my $manifestDir = $Nix::Config::manifestDir;
mkpath($manifestDir); mkpath($manifestDir);
my $dbPath = "$manifestDir/cache.sqlite"; my $dbPath = "$manifestDir/cache.sqlite";
# Open/create the database. # Open/create the database.
@ -245,7 +245,7 @@ sub updateManifestDB {
timestamp integer not null timestamp integer not null
); );
EOF EOF
$dbh->do(<<EOF); $dbh->do(<<EOF);
create table if not exists NARs ( create table if not exists NARs (
id integer primary key autoincrement not null, id integer primary key autoincrement not null,
@ -304,7 +304,7 @@ EOF
# Read each manifest in $manifestDir and add it to the database, # Read each manifest in $manifestDir and add it to the database,
# unless we've already done so on a previous run. # unless we've already done so on a previous run.
my %seen; my %seen;
for my $manifestLink (glob "$manifestDir/*.nixmanifest") { for my $manifestLink (glob "$manifestDir/*.nixmanifest") {
my $manifest = Cwd::abs_path($manifestLink); my $manifest = Cwd::abs_path($manifestLink);
next unless -f $manifest; next unless -f $manifest;
@ -316,9 +316,9 @@ EOF
{}, $manifest, $timestamp)} == 1; {}, $manifest, $timestamp)} == 1;
print STDERR "caching $manifest...\n"; print STDERR "caching $manifest...\n";
$dbh->do("delete from Manifests where path = ?", {}, $manifest); $dbh->do("delete from Manifests where path = ?", {}, $manifest);
$dbh->do("insert into Manifests(path, timestamp) values (?, ?)", $dbh->do("insert into Manifests(path, timestamp) values (?, ?)",
{}, $manifest, $timestamp); {}, $manifest, $timestamp);
@ -331,7 +331,7 @@ EOF
$narFile->{narHash}, $narFile->{narSize}, $narFile->{references}, $narFile->{narHash}, $narFile->{narSize}, $narFile->{references},
$narFile->{deriver}, $narFile->{system}); $narFile->{deriver}, $narFile->{system});
}; };
sub addPatchToDB { sub addPatchToDB {
my ($storePath, $patch) = @_; my ($storePath, $patch) = @_;
$insertPatch->execute( $insertPatch->execute(
@ -341,7 +341,7 @@ EOF
}; };
my $version = readManifest_($manifest, \&addNARToDB, \&addPatchToDB); my $version = readManifest_($manifest, \&addNARToDB, \&addPatchToDB);
if ($version < 3) { if ($version < 3) {
die "you have an old-style or corrupt manifest `$manifestLink'; please delete it\n"; die "you have an old-style or corrupt manifest `$manifestLink'; please delete it\n";
} }
@ -364,4 +364,22 @@ EOF
} }
# Delete all old manifests downloaded from a given URL.
sub deleteOldManifests {
my ($url, $curUrlFile) = @_;
for my $urlFile (glob "$Nix::Config::manifestDir/*.url") {
next if defined $curUrlFile && $urlFile eq $curUrlFile;
open URL, "<$urlFile" or die;
my $url2 = <URL>;
chomp $url2;
close URL;
next unless $url eq $url2;
my $base = $urlFile; $base =~ s/.url$//;
unlink "${base}.url";
unlink "${base}.nixmanifest";
}
}
return 1; return 1;

View file

@ -20,6 +20,7 @@ my $maxParallelRequests = int($Nix::Config::config{"binary-caches-parallel-conne
$maxParallelRequests = 1 if $maxParallelRequests < 1; $maxParallelRequests = 1 if $maxParallelRequests < 1;
my $debug = ($ENV{"NIX_DEBUG_SUBST"} // "") eq 1; my $debug = ($ENV{"NIX_DEBUG_SUBST"} // "") eq 1;
open(STDERR, ">>/dev/tty") if $debug;
my ($dbh, $queryCache, $insertNAR, $queryNAR, $insertNARExistence, $queryNARExistence); my ($dbh, $queryCache, $insertNAR, $queryNAR, $insertNARExistence, $queryNARExistence);

View file

@ -4,6 +4,7 @@ use strict;
use File::Basename; use File::Basename;
use File::Path qw(mkpath); use File::Path qw(mkpath);
use Nix::Config; use Nix::Config;
use Nix::Manifest;
my $manifestDir = $Nix::Config::manifestDir; my $manifestDir = $Nix::Config::manifestDir;
@ -65,6 +66,8 @@ sub addChannel {
sub removeChannel { sub removeChannel {
my ($name) = @_; my ($name) = @_;
readChannels; readChannels;
my $url = $channels{$name};
deleteOldManifests($url . "/MANIFEST", undef) if defined $url;
delete $channels{$name}; delete $channels{$name};
writeChannels; writeChannels;
@ -101,6 +104,7 @@ sub update {
my $extraAttrs = ""; my $extraAttrs = "";
if ($? == 0 && $binaryCacheURL ne "") { if ($? == 0 && $binaryCacheURL ne "") {
$extraAttrs .= "binaryCacheURL = \"$binaryCacheURL\"; "; $extraAttrs .= "binaryCacheURL = \"$binaryCacheURL\"; ";
deleteOldManifests($origUrl, undef);
} else { } else {
# No binary cache, so pull the channel manifest. # No binary cache, so pull the channel manifest.
mkdir $manifestDir, 0755 unless -e $manifestDir; mkdir $manifestDir, 0755 unless -e $manifestDir;

View file

@ -76,26 +76,15 @@ sub processURL {
open URL, ">$urlFile" or die "cannot create `$urlFile'"; open URL, ">$urlFile" or die "cannot create `$urlFile'";
print URL $origUrl; print URL $origUrl;
close URL; close URL;
my $finalPath = "$manifestDir/$baseName-$hash.nixmanifest"; my $finalPath = "$manifestDir/$baseName-$hash.nixmanifest";
unlink $finalPath if -e $finalPath; unlink $finalPath if -e $finalPath;
symlink("$manifest", "$finalPath") symlink("$manifest", "$finalPath")
or die "cannot link `$finalPath to `$manifest'"; or die "cannot link `$finalPath to `$manifest'";
# Delete all old manifests downloaded from this URL. deleteOldManifests($origUrl, $urlFile);
for my $urlFile2 (glob "$manifestDir/*.url") {
next if $urlFile eq $urlFile2;
open URL, "<$urlFile2" or die;
my $url2 = <URL>;
chomp $url2;
close URL;
next unless $origUrl eq $url2;
my $base = $urlFile2; $base =~ s/.url$//;
unlink "${base}.url";
unlink "${base}.nixmanifest";
}
} }
while (@ARGV) { while (@ARGV) {