From b14717ab9003452fda7afe0f9627673b9f331569 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Sep 2012 11:35:46 -0400 Subject: Delete manifests in "nix-channel --remove" or when a binary cache is available --- perl/lib/Nix/Manifest.pm | 60 +++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'perl/lib/Nix/Manifest.pm') diff --git a/perl/lib/Nix/Manifest.pm b/perl/lib/Nix/Manifest.pm index 532a900978f2..7a7263c5a5b1 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; -- cgit 1.4.1