From b14717ab9003452fda7afe0f9627673b9f331569 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Sep 2012 11:35:46 -0400 Subject: [PATCH] Delete manifests in "nix-channel --remove" or when a binary cache is available --- perl/lib/Nix/Manifest.pm | 60 +++++++++++++++--------- scripts/download-from-binary-cache.pl.in | 1 + scripts/nix-channel.in | 4 ++ scripts/nix-pull.in | 17 ++----- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/perl/lib/Nix/Manifest.pm b/perl/lib/Nix/Manifest.pm index 532a90097..7a7263c5a 100644 --- a/perl/lib/Nix/Manifest.pm +++ b/perl/lib/Nix/Manifest.pm @@ -9,12 +9,12 @@ use Fcntl ':flock'; use Nix::Config; our @ISA = qw(Exporter); -our @EXPORT = qw(readManifest writeManifest updateManifestDB addPatch); +our @EXPORT = qw(readManifest writeManifest updateManifestDB addPatch deleteOldManifests); sub addNAR { my ($narFiles, $storePath, $info) = @_; - + $$narFiles{$storePath} = [] unless defined $$narFiles{$storePath}; @@ -24,7 +24,7 @@ sub addNAR { foreach my $narFile (@{$narFileList}) { $found = 1 if $narFile->{url} eq $info->{url}; } - + push @{$narFileList}, $info if !$found; } @@ -43,7 +43,7 @@ sub addPatch { $patch2->{url} eq $patch->{url} && $patch2->{basePath} eq $patch->{basePath}; } - + push @{$patchList}, $patch if !$found; return !$found; @@ -93,10 +93,10 @@ sub readManifest_ { undef $system; $references = ""; $deriver = ""; - } + } } else { - + if (/^\}$/) { $inside = 0; @@ -120,7 +120,7 @@ sub readManifest_ { } } - + elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) { $storePath = $1; } elsif (/^\s*CopyFrom:\s*(\/\S+)\s*$/) { $copyFrom = $1; } elsif (/^\s*Hash:\s*(\S+)\s*$/) { $hash = $1; } @@ -184,7 +184,7 @@ sub writeManifest { print MANIFEST "}\n"; } } - + foreach my $storePath (sort (keys %{$patches})) { my $patchList = $$patches{$storePath}; foreach my $patch (@{$patchList}) { @@ -201,8 +201,8 @@ sub writeManifest { print MANIFEST "}\n"; } } - - + + close MANIFEST; rename("$manifest.tmp", $manifest) @@ -211,11 +211,11 @@ sub writeManifest { # Create a bzipped manifest. unless (defined $noCompress) { - system("$Nix::Config::bzip2 < $manifest > $manifest.bz2.tmp") == 0 - or die "cannot compress manifest"; + system("$Nix::Config::bzip2 < $manifest > $manifest.bz2.tmp") == 0 + or die "cannot compress manifest"; - rename("$manifest.bz2.tmp", "$manifest.bz2") - or die "cannot rename $manifest.bz2.tmp: $!"; + rename("$manifest.bz2.tmp", "$manifest.bz2") + or die "cannot rename $manifest.bz2.tmp: $!"; } } @@ -224,7 +224,7 @@ sub updateManifestDB { my $manifestDir = $Nix::Config::manifestDir; mkpath($manifestDir); - + my $dbPath = "$manifestDir/cache.sqlite"; # Open/create the database. @@ -245,7 +245,7 @@ sub updateManifestDB { timestamp integer not null ); EOF - + $dbh->do(<do("delete from Manifests where path = ?", {}, $manifest); - + $dbh->do("insert into Manifests(path, timestamp) values (?, ?)", {}, $manifest, $timestamp); @@ -331,7 +331,7 @@ EOF $narFile->{narHash}, $narFile->{narSize}, $narFile->{references}, $narFile->{deriver}, $narFile->{system}); }; - + sub addPatchToDB { my ($storePath, $patch) = @_; $insertPatch->execute( @@ -341,7 +341,7 @@ EOF }; my $version = readManifest_($manifest, \&addNARToDB, \&addPatchToDB); - + if ($version < 3) { 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 = ; + chomp $url2; + close URL; + next unless $url eq $url2; + my $base = $urlFile; $base =~ s/.url$//; + unlink "${base}.url"; + unlink "${base}.nixmanifest"; + } +} + + return 1; diff --git a/scripts/download-from-binary-cache.pl.in b/scripts/download-from-binary-cache.pl.in index f16434d0a..cbac17762 100644 --- a/scripts/download-from-binary-cache.pl.in +++ b/scripts/download-from-binary-cache.pl.in @@ -20,6 +20,7 @@ my $maxParallelRequests = int($Nix::Config::config{"binary-caches-parallel-conne $maxParallelRequests = 1 if $maxParallelRequests < 1; my $debug = ($ENV{"NIX_DEBUG_SUBST"} // "") eq 1; +open(STDERR, ">>/dev/tty") if $debug; my ($dbh, $queryCache, $insertNAR, $queryNAR, $insertNARExistence, $queryNARExistence); diff --git a/scripts/nix-channel.in b/scripts/nix-channel.in index e057cc916..7e50dac1e 100755 --- a/scripts/nix-channel.in +++ b/scripts/nix-channel.in @@ -4,6 +4,7 @@ use strict; use File::Basename; use File::Path qw(mkpath); use Nix::Config; +use Nix::Manifest; my $manifestDir = $Nix::Config::manifestDir; @@ -65,6 +66,8 @@ sub addChannel { sub removeChannel { my ($name) = @_; readChannels; + my $url = $channels{$name}; + deleteOldManifests($url . "/MANIFEST", undef) if defined $url; delete $channels{$name}; writeChannels; @@ -101,6 +104,7 @@ sub update { my $extraAttrs = ""; if ($? == 0 && $binaryCacheURL ne "") { $extraAttrs .= "binaryCacheURL = \"$binaryCacheURL\"; "; + deleteOldManifests($origUrl, undef); } else { # No binary cache, so pull the channel manifest. mkdir $manifestDir, 0755 unless -e $manifestDir; diff --git a/scripts/nix-pull.in b/scripts/nix-pull.in index e59a38eec..fbd90c2b2 100755 --- a/scripts/nix-pull.in +++ b/scripts/nix-pull.in @@ -76,26 +76,15 @@ sub processURL { open URL, ">$urlFile" or die "cannot create `$urlFile'"; print URL $origUrl; close URL; - + my $finalPath = "$manifestDir/$baseName-$hash.nixmanifest"; unlink $finalPath if -e $finalPath; - + symlink("$manifest", "$finalPath") or die "cannot link `$finalPath to `$manifest'"; - # Delete all old manifests downloaded from this URL. - for my $urlFile2 (glob "$manifestDir/*.url") { - next if $urlFile eq $urlFile2; - open URL, "<$urlFile2" or die; - my $url2 = ; - chomp $url2; - close URL; - next unless $origUrl eq $url2; - my $base = $urlFile2; $base =~ s/.url$//; - unlink "${base}.url"; - unlink "${base}.nixmanifest"; - } + deleteOldManifests($origUrl, $urlFile); } while (@ARGV) {