2011-10-11 00:11:08 +03:00
|
|
|
|
package Nix::Manifest;
|
|
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
use strict;
|
2011-04-11 02:22:46 +03:00
|
|
|
|
use DBI;
|
|
|
|
|
use Cwd;
|
|
|
|
|
use File::stat;
|
2011-04-11 11:06:14 +03:00
|
|
|
|
use File::Path;
|
2011-04-11 15:40:13 +03:00
|
|
|
|
use Fcntl ':flock';
|
2011-10-11 00:11:08 +03:00
|
|
|
|
use Nix::Config;
|
|
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2012-10-17 23:45:04 +03:00
|
|
|
|
our @EXPORT = qw(readManifest writeManifest updateManifestDB addPatch deleteOldManifests parseNARInfo);
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
2004-12-28 23:11:28 +02:00
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
sub addNAR {
|
|
|
|
|
my ($narFiles, $storePath, $info) = @_;
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
$$narFiles{$storePath} = []
|
|
|
|
|
unless defined $$narFiles{$storePath};
|
|
|
|
|
|
|
|
|
|
my $narFileList = $$narFiles{$storePath};
|
|
|
|
|
|
|
|
|
|
my $found = 0;
|
|
|
|
|
foreach my $narFile (@{$narFileList}) {
|
|
|
|
|
$found = 1 if $narFile->{url} eq $info->{url};
|
|
|
|
|
}
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
push @{$narFileList}, $info if !$found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-12-29 20:58:15 +02:00
|
|
|
|
sub addPatch {
|
2009-04-16 12:27:33 +03:00
|
|
|
|
my ($patches, $storePath, $patch) = @_;
|
2004-12-29 20:58:15 +02:00
|
|
|
|
|
|
|
|
|
$$patches{$storePath} = []
|
|
|
|
|
unless defined $$patches{$storePath};
|
|
|
|
|
|
|
|
|
|
my $patchList = $$patches{$storePath};
|
|
|
|
|
|
|
|
|
|
my $found = 0;
|
|
|
|
|
foreach my $patch2 (@{$patchList}) {
|
2009-03-19 11:47:34 +02:00
|
|
|
|
$found = 1 if
|
|
|
|
|
$patch2->{url} eq $patch->{url} &&
|
|
|
|
|
$patch2->{basePath} eq $patch->{basePath};
|
2004-12-29 20:58:15 +02:00
|
|
|
|
}
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2004-12-30 00:17:26 +02:00
|
|
|
|
push @{$patchList}, $patch if !$found;
|
|
|
|
|
|
|
|
|
|
return !$found;
|
2004-12-29 20:58:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
sub readManifest_ {
|
|
|
|
|
my ($manifest, $addNAR, $addPatch) = @_;
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
2011-11-16 18:25:38 +02:00
|
|
|
|
# Decompress the manifest if necessary.
|
|
|
|
|
if ($manifest =~ /\.bz2$/) {
|
|
|
|
|
open MANIFEST, "$Nix::Config::bzip2 -d < $manifest |"
|
|
|
|
|
or die "cannot decompress `$manifest': $!";
|
|
|
|
|
} else {
|
|
|
|
|
open MANIFEST, "<$manifest"
|
|
|
|
|
or die "cannot open `$manifest': $!";
|
|
|
|
|
}
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
|
|
|
|
my $inside = 0;
|
2004-12-13 15:47:38 +02:00
|
|
|
|
my $type;
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
2005-02-25 18:12:52 +02:00
|
|
|
|
my $manifestVersion = 2;
|
|
|
|
|
|
2010-11-17 16:31:42 +02:00
|
|
|
|
my ($storePath, $url, $hash, $size, $basePath, $baseHash, $patchType);
|
2012-09-20 00:33:42 +03:00
|
|
|
|
my ($narHash, $narSize, $references, $deriver, $copyFrom, $system, $compressionType);
|
2003-12-05 13:25:38 +02:00
|
|
|
|
|
|
|
|
|
while (<MANIFEST>) {
|
|
|
|
|
chomp;
|
|
|
|
|
s/\#.*$//g;
|
|
|
|
|
next if (/^$/);
|
|
|
|
|
|
|
|
|
|
if (!$inside) {
|
2004-12-28 23:11:28 +02:00
|
|
|
|
|
|
|
|
|
if (/^\s*(\w*)\s*\{$/) {
|
|
|
|
|
$type = $1;
|
|
|
|
|
$type = "narfile" if $type eq "";
|
2003-12-05 13:25:38 +02:00
|
|
|
|
$inside = 1;
|
2004-06-21 12:51:23 +03:00
|
|
|
|
undef $storePath;
|
2004-12-13 15:47:38 +02:00
|
|
|
|
undef $url;
|
2003-12-05 13:25:38 +02:00
|
|
|
|
undef $hash;
|
2004-12-28 23:11:28 +02:00
|
|
|
|
undef $size;
|
2004-12-20 18:38:50 +02:00
|
|
|
|
undef $narHash;
|
2010-11-17 16:31:42 +02:00
|
|
|
|
undef $narSize;
|
2004-12-13 15:47:38 +02:00
|
|
|
|
undef $basePath;
|
|
|
|
|
undef $baseHash;
|
|
|
|
|
undef $patchType;
|
2010-11-29 11:26:05 +02:00
|
|
|
|
undef $system;
|
2005-01-25 19:08:52 +02:00
|
|
|
|
$references = "";
|
2005-02-09 14:57:13 +02:00
|
|
|
|
$deriver = "";
|
2012-09-20 00:33:42 +03:00
|
|
|
|
$compressionType = "bzip2";
|
2012-09-13 18:35:46 +03:00
|
|
|
|
}
|
2004-12-28 23:11:28 +02:00
|
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
} else {
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
if (/^\}$/) {
|
|
|
|
|
$inside = 0;
|
|
|
|
|
|
2004-12-13 15:47:38 +02:00
|
|
|
|
if ($type eq "narfile") {
|
2011-04-11 16:16:54 +03:00
|
|
|
|
&$addNAR($storePath,
|
|
|
|
|
{ url => $url, hash => $hash, size => $size
|
|
|
|
|
, narHash => $narHash, narSize => $narSize
|
|
|
|
|
, references => $references
|
|
|
|
|
, deriver => $deriver
|
|
|
|
|
, system => $system
|
2012-09-20 00:33:42 +03:00
|
|
|
|
, compressionType => $compressionType
|
2011-04-11 16:16:54 +03:00
|
|
|
|
});
|
2004-12-13 15:47:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($type eq "patch") {
|
2011-04-11 16:16:54 +03:00
|
|
|
|
&$addPatch($storePath,
|
2004-12-29 20:58:15 +02:00
|
|
|
|
{ url => $url, hash => $hash, size => $size
|
|
|
|
|
, basePath => $basePath, baseHash => $baseHash
|
2010-11-17 16:31:42 +02:00
|
|
|
|
, narHash => $narHash, narSize => $narSize
|
2011-04-11 02:22:46 +03:00
|
|
|
|
, patchType => $patchType
|
2011-04-11 16:16:54 +03:00
|
|
|
|
});
|
2003-12-05 13:25:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2004-12-13 15:47:38 +02:00
|
|
|
|
elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) { $storePath = $1; }
|
2007-01-23 18:50:19 +02:00
|
|
|
|
elsif (/^\s*CopyFrom:\s*(\/\S+)\s*$/) { $copyFrom = $1; }
|
2004-12-13 15:47:38 +02:00
|
|
|
|
elsif (/^\s*Hash:\s*(\S+)\s*$/) { $hash = $1; }
|
|
|
|
|
elsif (/^\s*URL:\s*(\S+)\s*$/) { $url = $1; }
|
2012-09-20 00:33:42 +03:00
|
|
|
|
elsif (/^\s*Compression:\s*(\S+)\s*$/) { $compressionType = $1; }
|
2004-12-13 15:47:38 +02:00
|
|
|
|
elsif (/^\s*Size:\s*(\d+)\s*$/) { $size = $1; }
|
|
|
|
|
elsif (/^\s*BasePath:\s*(\/\S+)\s*$/) { $basePath = $1; }
|
|
|
|
|
elsif (/^\s*BaseHash:\s*(\S+)\s*$/) { $baseHash = $1; }
|
|
|
|
|
elsif (/^\s*Type:\s*(\S+)\s*$/) { $patchType = $1; }
|
2004-12-20 18:38:50 +02:00
|
|
|
|
elsif (/^\s*NarHash:\s*(\S+)\s*$/) { $narHash = $1; }
|
2010-11-17 16:31:42 +02:00
|
|
|
|
elsif (/^\s*NarSize:\s*(\d+)\s*$/) { $narSize = $1; }
|
2005-01-25 19:08:52 +02:00
|
|
|
|
elsif (/^\s*References:\s*(.*)\s*$/) { $references = $1; }
|
2005-02-09 14:57:13 +02:00
|
|
|
|
elsif (/^\s*Deriver:\s*(\S+)\s*$/) { $deriver = $1; }
|
2005-02-25 18:12:52 +02:00
|
|
|
|
elsif (/^\s*ManifestVersion:\s*(\d+)\s*$/) { $manifestVersion = $1; }
|
2010-11-29 11:26:05 +02:00
|
|
|
|
elsif (/^\s*System:\s*(\S+)\s*$/) { $system = $1; }
|
2004-12-13 15:47:38 +02:00
|
|
|
|
|
|
|
|
|
# Compatibility;
|
|
|
|
|
elsif (/^\s*NarURL:\s*(\S+)\s*$/) { $url = $1; }
|
2005-03-14 17:09:53 +02:00
|
|
|
|
elsif (/^\s*MD5:\s*(\S+)\s*$/) { $hash = "md5:$1"; }
|
2004-12-20 18:38:50 +02:00
|
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close MANIFEST;
|
2005-02-25 18:12:52 +02:00
|
|
|
|
|
|
|
|
|
return $manifestVersion;
|
2003-12-05 13:25:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
sub readManifest {
|
|
|
|
|
my ($manifest, $narFiles, $patches) = @_;
|
|
|
|
|
readManifest_($manifest,
|
|
|
|
|
sub { addNAR($narFiles, @_); },
|
|
|
|
|
sub { addPatch($patches, @_); } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-16 12:27:33 +03:00
|
|
|
|
sub writeManifest {
|
2010-11-29 17:26:28 +02:00
|
|
|
|
my ($manifest, $narFiles, $patches, $noCompress) = @_;
|
2004-12-28 23:11:28 +02:00
|
|
|
|
|
2004-12-29 21:04:21 +02:00
|
|
|
|
open MANIFEST, ">$manifest.tmp"; # !!! check exclusive
|
2004-12-28 23:11:28 +02:00
|
|
|
|
|
2005-02-25 18:12:52 +02:00
|
|
|
|
print MANIFEST "version {\n";
|
|
|
|
|
print MANIFEST " ManifestVersion: 3\n";
|
|
|
|
|
print MANIFEST "}\n";
|
|
|
|
|
|
2006-02-24 18:02:36 +02:00
|
|
|
|
foreach my $storePath (sort (keys %{$narFiles})) {
|
2004-12-28 23:11:28 +02:00
|
|
|
|
my $narFileList = $$narFiles{$storePath};
|
|
|
|
|
foreach my $narFile (@{$narFileList}) {
|
|
|
|
|
print MANIFEST "{\n";
|
|
|
|
|
print MANIFEST " StorePath: $storePath\n";
|
|
|
|
|
print MANIFEST " NarURL: $narFile->{url}\n";
|
2012-09-20 00:33:42 +03:00
|
|
|
|
print MANIFEST " Compression: $narFile->{compressionType}\n";
|
2009-04-16 12:27:33 +03:00
|
|
|
|
print MANIFEST " Hash: $narFile->{hash}\n" if defined $narFile->{hash};
|
|
|
|
|
print MANIFEST " Size: $narFile->{size}\n" if defined $narFile->{size};
|
2010-11-17 14:57:52 +02:00
|
|
|
|
print MANIFEST " NarHash: $narFile->{narHash}\n";
|
|
|
|
|
print MANIFEST " NarSize: $narFile->{narSize}\n" if $narFile->{narSize};
|
2005-01-25 19:08:52 +02:00
|
|
|
|
print MANIFEST " References: $narFile->{references}\n"
|
|
|
|
|
if defined $narFile->{references} && $narFile->{references} ne "";
|
2005-02-09 14:57:13 +02:00
|
|
|
|
print MANIFEST " Deriver: $narFile->{deriver}\n"
|
|
|
|
|
if defined $narFile->{deriver} && $narFile->{deriver} ne "";
|
2010-11-29 11:26:05 +02:00
|
|
|
|
print MANIFEST " System: $narFile->{system}\n" if defined $narFile->{system};
|
2004-12-28 23:11:28 +02:00
|
|
|
|
print MANIFEST "}\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2006-02-24 18:02:36 +02:00
|
|
|
|
foreach my $storePath (sort (keys %{$patches})) {
|
2004-12-28 23:11:28 +02:00
|
|
|
|
my $patchList = $$patches{$storePath};
|
|
|
|
|
foreach my $patch (@{$patchList}) {
|
|
|
|
|
print MANIFEST "patch {\n";
|
|
|
|
|
print MANIFEST " StorePath: $storePath\n";
|
|
|
|
|
print MANIFEST " NarURL: $patch->{url}\n";
|
2005-03-14 17:09:53 +02:00
|
|
|
|
print MANIFEST " Hash: $patch->{hash}\n";
|
2004-12-28 23:11:28 +02:00
|
|
|
|
print MANIFEST " Size: $patch->{size}\n";
|
2010-11-17 16:31:42 +02:00
|
|
|
|
print MANIFEST " NarHash: $patch->{narHash}\n";
|
|
|
|
|
print MANIFEST " NarSize: $patch->{narSize}\n" if $patch->{narSize};
|
2004-12-28 23:11:28 +02:00
|
|
|
|
print MANIFEST " BasePath: $patch->{basePath}\n";
|
|
|
|
|
print MANIFEST " BaseHash: $patch->{baseHash}\n";
|
|
|
|
|
print MANIFEST " Type: $patch->{patchType}\n";
|
|
|
|
|
print MANIFEST "}\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
|
|
|
|
|
2004-12-28 23:11:28 +02:00
|
|
|
|
close MANIFEST;
|
2004-12-29 21:04:21 +02:00
|
|
|
|
|
|
|
|
|
rename("$manifest.tmp", $manifest)
|
|
|
|
|
or die "cannot rename $manifest.tmp: $!";
|
2007-09-04 18:38:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create a bzipped manifest.
|
2010-11-29 17:26:28 +02:00
|
|
|
|
unless (defined $noCompress) {
|
2012-09-13 18:35:46 +03:00
|
|
|
|
system("$Nix::Config::bzip2 < $manifest > $manifest.bz2.tmp") == 0
|
|
|
|
|
or die "cannot compress manifest";
|
2007-09-04 18:38:09 +03:00
|
|
|
|
|
2012-09-13 18:35:46 +03:00
|
|
|
|
rename("$manifest.bz2.tmp", "$manifest.bz2")
|
|
|
|
|
or die "cannot rename $manifest.bz2.tmp: $!";
|
2010-11-29 17:26:28 +02:00
|
|
|
|
}
|
2004-12-28 23:11:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
sub updateManifestDB {
|
2011-10-11 00:11:08 +03:00
|
|
|
|
my $manifestDir = $Nix::Config::manifestDir;
|
2011-04-11 02:22:46 +03:00
|
|
|
|
|
2013-06-20 12:55:15 +03:00
|
|
|
|
my @manifests = glob "$manifestDir/*.nixmanifest";
|
|
|
|
|
return undef if scalar @manifests == 0;
|
|
|
|
|
|
2011-04-11 11:06:14 +03:00
|
|
|
|
mkpath($manifestDir);
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2012-09-20 00:33:42 +03:00
|
|
|
|
unlink "$manifestDir/cache.sqlite"; # remove obsolete cache
|
|
|
|
|
my $dbPath = "$manifestDir/cache-v2.sqlite";
|
2011-04-11 02:22:46 +03:00
|
|
|
|
|
|
|
|
|
# Open/create the database.
|
2011-04-11 16:16:54 +03:00
|
|
|
|
our $dbh = DBI->connect("dbi:SQLite:dbname=$dbPath", "", "")
|
2011-04-11 02:22:46 +03:00
|
|
|
|
or die "cannot open database `$dbPath'";
|
|
|
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
|
$dbh->{PrintError} = 0;
|
|
|
|
|
|
|
|
|
|
$dbh->do("pragma foreign_keys = on");
|
2011-07-13 17:05:54 +03:00
|
|
|
|
$dbh->do("pragma synchronous = off"); # we can always reproduce the cache
|
|
|
|
|
$dbh->do("pragma journal_mode = truncate");
|
2011-04-11 02:22:46 +03:00
|
|
|
|
|
|
|
|
|
# Initialise the database schema, if necessary.
|
|
|
|
|
$dbh->do(<<EOF);
|
|
|
|
|
create table if not exists Manifests (
|
|
|
|
|
id integer primary key autoincrement not null,
|
|
|
|
|
path text unique not null,
|
|
|
|
|
timestamp integer not null
|
|
|
|
|
);
|
|
|
|
|
EOF
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
$dbh->do(<<EOF);
|
|
|
|
|
create table if not exists NARs (
|
|
|
|
|
id integer primary key autoincrement not null,
|
|
|
|
|
manifest integer not null,
|
|
|
|
|
storePath text not null,
|
|
|
|
|
url text not null,
|
2012-09-20 00:33:42 +03:00
|
|
|
|
compressionType text not null,
|
2011-04-11 02:22:46 +03:00
|
|
|
|
hash text,
|
|
|
|
|
size integer,
|
|
|
|
|
narHash text,
|
|
|
|
|
narSize integer,
|
|
|
|
|
refs text,
|
|
|
|
|
deriver text,
|
|
|
|
|
system text,
|
|
|
|
|
foreign key (manifest) references Manifests(id) on delete cascade
|
|
|
|
|
);
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
$dbh->do("create index if not exists NARs_storePath on NARs(storePath)");
|
|
|
|
|
|
|
|
|
|
$dbh->do(<<EOF);
|
|
|
|
|
create table if not exists Patches (
|
|
|
|
|
id integer primary key autoincrement not null,
|
|
|
|
|
manifest integer not null,
|
|
|
|
|
storePath text not null,
|
|
|
|
|
basePath text not null,
|
|
|
|
|
baseHash text not null,
|
|
|
|
|
url text not null,
|
|
|
|
|
hash text,
|
|
|
|
|
size integer,
|
|
|
|
|
narHash text,
|
|
|
|
|
narSize integer,
|
|
|
|
|
patchType text not null,
|
|
|
|
|
foreign key (manifest) references Manifests(id) on delete cascade
|
|
|
|
|
);
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
$dbh->do("create index if not exists Patches_storePath on Patches(storePath)");
|
|
|
|
|
|
2011-04-11 15:40:13 +03:00
|
|
|
|
# Acquire an exclusive lock to ensure that only one process
|
|
|
|
|
# updates the DB at the same time. This isn't really necessary,
|
|
|
|
|
# but it prevents work duplication and lock contention in SQLite.
|
2011-10-11 00:11:08 +03:00
|
|
|
|
my $lockFile = "$manifestDir/cache.lock";
|
|
|
|
|
open MAINLOCK, ">>$lockFile" or die "unable to acquire lock ‘$lockFile’: $!\n";
|
2011-04-11 15:40:13 +03:00
|
|
|
|
flock(MAINLOCK, LOCK_EX) or die;
|
2011-04-11 02:22:46 +03:00
|
|
|
|
|
2011-11-16 13:37:40 +02:00
|
|
|
|
our $insertNAR = $dbh->prepare(
|
2012-09-20 00:33:42 +03:00
|
|
|
|
"insert into NARs(manifest, storePath, url, compressionType, hash, size, narHash, " .
|
|
|
|
|
"narSize, refs, deriver, system) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die;
|
2011-11-16 13:37:40 +02:00
|
|
|
|
|
|
|
|
|
our $insertPatch = $dbh->prepare(
|
|
|
|
|
"insert into Patches(manifest, storePath, basePath, baseHash, url, hash, " .
|
|
|
|
|
"size, narHash, narSize, patchType) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
|
|
|
|
2011-07-13 17:05:54 +03:00
|
|
|
|
$dbh->begin_work;
|
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
# Read each manifest in $manifestDir and add it to the database,
|
|
|
|
|
# unless we've already done so on a previous run.
|
|
|
|
|
my %seen;
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2013-06-20 12:55:15 +03:00
|
|
|
|
for my $manifestLink (@manifests) {
|
2011-11-16 18:41:48 +02:00
|
|
|
|
my $manifest = Cwd::abs_path($manifestLink);
|
2012-01-03 14:59:00 +02:00
|
|
|
|
next unless -f $manifest;
|
2011-04-11 02:22:46 +03:00
|
|
|
|
my $timestamp = lstat($manifest)->mtime;
|
|
|
|
|
$seen{$manifest} = 1;
|
|
|
|
|
|
|
|
|
|
next if scalar @{$dbh->selectcol_arrayref(
|
|
|
|
|
"select 1 from Manifests where path = ? and timestamp = ?",
|
|
|
|
|
{}, $manifest, $timestamp)} == 1;
|
|
|
|
|
|
2011-07-13 17:05:54 +03:00
|
|
|
|
print STDERR "caching $manifest...\n";
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
$dbh->do("delete from Manifests where path = ?", {}, $manifest);
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
$dbh->do("insert into Manifests(path, timestamp) values (?, ?)",
|
|
|
|
|
{}, $manifest, $timestamp);
|
|
|
|
|
|
2011-08-17 17:17:19 +03:00
|
|
|
|
our $id = $dbh->last_insert_id("", "", "", "");
|
2011-04-11 16:16:54 +03:00
|
|
|
|
|
|
|
|
|
sub addNARToDB {
|
|
|
|
|
my ($storePath, $narFile) = @_;
|
2011-11-16 13:37:40 +02:00
|
|
|
|
$insertNAR->execute(
|
2012-09-20 00:33:42 +03:00
|
|
|
|
$id, $storePath, $narFile->{url}, $narFile->{compressionType}, $narFile->{hash},
|
|
|
|
|
$narFile->{size}, $narFile->{narHash}, $narFile->{narSize}, $narFile->{references},
|
2011-04-11 16:16:54 +03:00
|
|
|
|
$narFile->{deriver}, $narFile->{system});
|
|
|
|
|
};
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
sub addPatchToDB {
|
|
|
|
|
my ($storePath, $patch) = @_;
|
2011-11-16 13:37:40 +02:00
|
|
|
|
$insertPatch->execute(
|
|
|
|
|
$id, $storePath, $patch->{basePath}, $patch->{baseHash}, $patch->{url},
|
2011-04-11 16:16:54 +03:00
|
|
|
|
$patch->{hash}, $patch->{size}, $patch->{narHash}, $patch->{narSize},
|
|
|
|
|
$patch->{patchType});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
my $version = readManifest_($manifest, \&addNARToDB, \&addPatchToDB);
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
2011-04-11 16:16:54 +03:00
|
|
|
|
if ($version < 3) {
|
2012-04-10 17:40:51 +03:00
|
|
|
|
die "you have an old-style or corrupt manifest `$manifestLink'; please delete it\n";
|
2011-04-11 02:22:46 +03:00
|
|
|
|
}
|
2011-04-11 16:16:54 +03:00
|
|
|
|
if ($version >= 10) {
|
2012-04-10 17:40:51 +03:00
|
|
|
|
die "manifest `$manifestLink' is too new; please delete it or upgrade Nix\n";
|
2011-04-11 02:22:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Removed cached information for removed manifests from the DB.
|
|
|
|
|
foreach my $manifest (@{$dbh->selectcol_arrayref("select path from Manifests")}) {
|
|
|
|
|
next if defined $seen{$manifest};
|
|
|
|
|
$dbh->do("delete from Manifests where path = ?", {}, $manifest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dbh->commit;
|
|
|
|
|
|
2011-04-11 15:40:13 +03:00
|
|
|
|
close MAINLOCK;
|
|
|
|
|
|
2011-04-11 02:22:46 +03:00
|
|
|
|
return $dbh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-09-13 18:35:46 +03:00
|
|
|
|
|
|
|
|
|
# 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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-17 23:45:04 +03:00
|
|
|
|
# Parse a NAR info file.
|
|
|
|
|
sub parseNARInfo {
|
|
|
|
|
my ($storePath, $content) = @_;
|
|
|
|
|
|
|
|
|
|
my ($storePath2, $url, $fileHash, $fileSize, $narHash, $narSize, $deriver, $system);
|
|
|
|
|
my $compression = "bzip2";
|
|
|
|
|
my @refs;
|
|
|
|
|
|
|
|
|
|
foreach my $line (split "\n", $content) {
|
|
|
|
|
return undef unless $line =~ /^(.*): (.*)$/;
|
|
|
|
|
if ($1 eq "StorePath") { $storePath2 = $2; }
|
|
|
|
|
elsif ($1 eq "URL") { $url = $2; }
|
|
|
|
|
elsif ($1 eq "Compression") { $compression = $2; }
|
|
|
|
|
elsif ($1 eq "FileHash") { $fileHash = $2; }
|
|
|
|
|
elsif ($1 eq "FileSize") { $fileSize = int($2); }
|
|
|
|
|
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; }
|
|
|
|
|
elsif ($1 eq "System") { $system = $2; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undef if $storePath ne $storePath2 || !defined $url || !defined $narHash;
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
{ url => $url
|
|
|
|
|
, compression => $compression
|
|
|
|
|
, fileHash => $fileHash
|
|
|
|
|
, fileSize => $fileSize
|
|
|
|
|
, narHash => $narHash
|
|
|
|
|
, narSize => $narSize
|
|
|
|
|
, refs => [ @refs ]
|
|
|
|
|
, deriver => $deriver
|
|
|
|
|
, system => $system
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-12-05 13:25:38 +02:00
|
|
|
|
return 1;
|