about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2005-01-25T17·08+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2005-01-25T17·08+0000
commit066da4ab852ebe4288536149824ea175dc36cad4 (patch)
treed9258a2d224a0574c0135cd799cb704fc8c25433 /scripts
parentc6290e42bc8890e2036013773a98e3551352c91a (diff)
* Really fix the substitute mechanism, i.e., ensure the closure
  invariant by registering references through the manifest.
* Added a test for nix-pull.

Diffstat (limited to 'scripts')
-rw-r--r--scripts/download-using-manifests.pl.in6
-rw-r--r--scripts/nix-prefetch-url.in4
-rw-r--r--scripts/nix-pull.in36
-rw-r--r--scripts/nix-push.in117
-rw-r--r--scripts/readmanifest.pm.in9
5 files changed, 118 insertions, 54 deletions
diff --git a/scripts/download-using-manifests.pl.in b/scripts/download-using-manifests.pl.in
index 5d4193590b75..5698f49ae4aa 100644
--- a/scripts/download-using-manifests.pl.in
+++ b/scripts/download-using-manifests.pl.in
@@ -13,7 +13,7 @@ open LOGFILE, ">>$logFile" or die "cannot open log file $logFile";
 die unless scalar @ARGV == 1;
 my $targetPath = $ARGV[0];
 
-my $date = `date`;
+my $date = `date` or die;
 chomp $date;
 print LOGFILE "$$ get $targetPath $date\n";
 
@@ -180,7 +180,7 @@ sub downloadFile {
     my ($hash2, $path) = `@bindir@/nix-prefetch-url '$url' '$hash'`;
     chomp $hash2;
     chomp $path;
-    die "hash mismatch" if $hash ne $hash2;
+    die "hash mismatch, expected $hash, got $hash2" if $hash ne $hash2;
     return $path;
 }
 
@@ -236,7 +236,7 @@ while (scalar @path > 0) {
 
         # Unpack the archive into the target path.
         print "  unpacking archive...\n";
-        system "@bunzip2@ < '$narFilePath' | nix-store --restore '$v'";
+        system "@bunzip2@ < '$narFilePath' | @bindir@/nix-store --restore '$v'";
         die "cannot unpack `$narFilePath' into `$v'" if ($? != 0);
     }
 }
diff --git a/scripts/nix-prefetch-url.in b/scripts/nix-prefetch-url.in
index c4731f3f627b..409c3f992d91 100644
--- a/scripts/nix-prefetch-url.in
+++ b/scripts/nix-prefetch-url.in
@@ -46,10 +46,8 @@ storeExpr=$( \
         {url = $url; outputHashAlgo = \"$hashType\"; outputHash = \"$hash\"; system = \"@system@\";}" \
     | @bindir@/nix-instantiate -)
 
-echo "$storeExpr"    
-
 # Realise it.
-finalPath=$(@bindir@/nix-store -qnB --force-realise $storeExpr)
+finalPath=$(@bindir@/nix-store -q --force-realise $storeExpr)
     
 if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
 
diff --git a/scripts/nix-pull.in b/scripts/nix-pull.in
index 1a8e2a91127c..ee90a06e27e7 100644
--- a/scripts/nix-pull.in
+++ b/scripts/nix-pull.in
@@ -13,6 +13,15 @@ my $manifest = "$tmpdir/manifest";
 
 END { unlink $manifest; rmdir $tmpdir; }
 
+my $binDir = $ENV{"NIX_BIN_DIR"};
+$binDir = "@bindir@" unless defined $binDir;
+
+my $libexecDir = $ENV{"NIX_LIBEXEC_DIR"};
+$libexecDir = "@libexecdir@" unless defined $libexecDir;
+
+my $localStateDir = $ENV{"NIX_LOCALSTATE_DIR"};
+$localStateDir = "@localstatedir@" unless defined $localStateDir;
+
 
 # Obtain URLs either from the command line or from a configuration file.
 my %narFiles;
@@ -36,11 +45,11 @@ sub processURL {
         $baseName = $1;
     }
 
-    my $hash = `@bindir@/nix-hash --flat '$manifest'`
+    my $hash = `$binDir/nix-hash --flat '$manifest'`
         or die "cannot hash `$manifest'";
     chomp $hash;
     
-    my $finalPath = "@localstatedir@/nix/manifests/$baseName-$hash.nixmanifest";
+    my $finalPath = "$localStateDir/nix/manifests/$baseName-$hash.nixmanifest";
     
     system("mv '$manifest' '$finalPath'") == 0
         or die "cannot move `$manifest' to `$finalPath";
@@ -59,7 +68,7 @@ print "$size store paths in manifest\n";
 # Register all substitutes.
 print STDERR "registering substitutes...\n";
 
-my $pid = open2(\*READ, \*WRITE, "@bindir@/nix-store --substitute")
+my $pid = open2(\*READ, \*WRITE, "$binDir/nix-store --substitute")
     or die "cannot run nix-store";
 
 close READ;
@@ -68,8 +77,14 @@ foreach my $storePath (keys %narFiles) {
     my $narFileList = $narFiles{$storePath};
     foreach my $narFile (@{$narFileList}) {
         print WRITE "$storePath\n";
-        print WRITE "@libexecdir@/nix/download-using-manifests.pl\n";
+        print WRITE "$libexecDir/nix/download-using-manifests.pl\n";
         print WRITE "0\n";
+        my @references = split " ", $narFile->{references};
+        my $count = scalar @references;
+        print WRITE "$count\n";
+        foreach my $reference (@references) {
+            print WRITE "$reference\n";
+        }
     }
 }
 
@@ -77,16 +92,3 @@ close WRITE;
 
 waitpid $pid, 0;
 $? == 0 or die "nix-store failed";
-
-
-# Register all successors.
-print STDERR "registering successors...\n";
-my @sucs = %successors;
-while (scalar @sucs > 0) {
-    my $n = scalar @sucs;
-    if ($n > 256) { $n = 256 };
-    my @sucs2 = @sucs[0..$n - 1];
-    @sucs = @sucs[$n..scalar @sucs - 1];
-    system "@bindir@/nix-store --successor @sucs2";
-    if ($?) { die "`nix-store --successor' failed"; }
-}
diff --git a/scripts/nix-push.in b/scripts/nix-push.in
index 1c51778f1850..60ccce4edacf 100644
--- a/scripts/nix-push.in
+++ b/scripts/nix-push.in
@@ -1,6 +1,7 @@
 #! @perl@ -w -I@libexecdir@/nix
 
 use strict;
+use IPC::Open2;
 use POSIX qw(tmpnam);
 use readmanifest;
 
@@ -17,32 +18,62 @@ my $curl = "@curl@ --fail --silent";
 my $extraCurlFlags = ${ENV{'CURL_FLAGS'}};
 $curl = "$curl $extraCurlFlags" if defined $extraCurlFlags;
 
+my $binDir = $ENV{"NIX_BIN_DIR"};
+$binDir = "@bindir@" unless defined $binDir;
+
+my $dataDir = $ENV{"NIX_DATA_DIR"};
+$dataDir = "@datadir@" unless defined $dataDir;
+
 
 # Parse the command line.
-my $archives_put_url = shift @ARGV;
-my $archives_get_url = shift @ARGV;
-my $manifest_put_url = shift @ARGV;
+my $localCopy;
+my $localArchivesDir;
+my $localManifestFile;
+
+my $archives_put_url;
+my $archives_get_url;
+my $manifest_put_url;
+
+if ($ARGV[0] eq "--copy") {
+    die "syntax: nix-push --copy ARCHIVES_DIR MANIFEST_FILE PATHS...\n" if scalar @ARGV < 3;
+    $localCopy = 1;
+    shift @ARGV;
+    $localArchivesDir = shift @ARGV;
+    $localManifestFile = shift @ARGV;
+}
+else {
+    die "syntax: nix-push ARCHIVES_PUT_URL ARCHIVES_GET_URL " .
+        "MANIFEST_PUT_URL PATHS...\n" if scalar @ARGV < 3;
+    $localCopy = 0;
+    $archives_put_url = shift @ARGV;
+    $archives_get_url = shift @ARGV;
+    $manifest_put_url = shift @ARGV;
+}
 
 
-# From the given store expressions, determine the requisite store
-# paths.
+# From the given store paths, determine the set of requisite store
+# paths, i.e, the paths required to realise them.
 my %storePaths;
 
-foreach my $storeexpr (@ARGV) {
-    die unless $storeexpr =~ /^\//;
+foreach my $path (@ARGV) {
+    die unless $path =~ /^\//;
 
     # Get all paths referenced by the normalisation of the given 
     # Nix expression.
-    system "@bindir@/nix-store --realise $storeexpr > /dev/null";
-    die if ($?);
-
-    open PATHS, "@bindir@/nix-store --query --requisites --include-successors $storeexpr 2> /dev/null |" or die;
-    while (<PATHS>) {
+    my $pid = open2(\*READ, \*WRITE,
+        "$binDir/nix-store --query --requisites --force-realise " .
+        "--include-outputs '$path'") or die;
+    close WRITE;
+    
+    while (<READ>) {
         chomp;
         die "bad: $_" unless /^\//;
         $storePaths{$_} = "";
     }
-    close PATHS;
+    close READ;
+    
+    waitpid $pid, 0;
+    $? == 0 or die "nix-store failed";
 }
 
 my @storePaths = keys %storePaths;
@@ -58,8 +89,7 @@ foreach my $storePath (@storePaths) {
 
     # Construct a Nix expression that creates a Nix archive.
     my $nixexpr = 
-        "((import @datadir@/nix/corepkgs/nar/nar.nix) " .
-        # !!! $storePath should be represented as a closure
+        "((import $dataDir/nix/corepkgs/nar/nar.nix) " .
         "{path = \"$storePath\"; system = \"@system@\";}) ";
     
     print NIX $nixexpr;
@@ -72,7 +102,7 @@ close NIX;
 # Instantiate store expressions from the Nix expression.
 my @storeexprs;
 print STDERR "instantiating store expressions...\n";
-open STOREEXPRS, "@bindir@/nix-instantiate $nixfile |" or die "cannot run nix-instantiate";
+open STOREEXPRS, "$binDir/nix-instantiate $nixfile |" or die "cannot run nix-instantiate";
 while (<STOREEXPRS>) {
     chomp;
     die unless /^\//;
@@ -93,10 +123,7 @@ while (scalar @tmp > 0) {
     my @tmp2 = @tmp[0..$n - 1];
     @tmp = @tmp[$n..scalar @tmp - 1];
 
-    system "@bindir@/nix-store --realise @tmp2 > /dev/null";
-    if ($?) { die "`nix-store --realise' failed"; }
-
-    open NARPATHS, "@bindir@/nix-store --query --list @tmp2 |" or die "cannot run nix";
+    open NARPATHS, "$binDir/nix-store --realise @tmp2 |" or die "cannot run nix-store";
     while (<NARPATHS>) {
         chomp;
         die unless (/^\//);
@@ -142,16 +169,26 @@ for (my $n = 0; $n < scalar @storePaths; $n++) {
     
     my $narbz2Size = (stat $narfile)[7];
 
+    my $references = join(" ", split(" ", `$binDir/nix-store --query --references '$storePath'`));
+
+    my $url;
+    if ($localCopy) {
+        $url = "file://$localArchivesDir/$narname";
+    } else {
+        $url = "$archives_get_url/$narname";
+    }
     $narFiles{$storePath} = [
-        { url => $archives_get_url/$narname
+        { url => $url
         , hash => $narbz2Hash
         , size => $narbz2Size
         , narHash => $narHash
+        , hashAlgo => "sha1"
+        , references => $references
         }
     ];
                             
     if ($storePath =~ /\.store$/) {
-        open PREDS, "@bindir@/nix-store --query --predecessors $storePath |" or die "cannot run nix";
+        open PREDS, "$binDir/nix-store --query --predecessors $storePath |" or die "cannot run nix";
         while (<PREDS>) {
             chomp;
             die unless (/^\//);
@@ -170,6 +207,14 @@ for (my $n = 0; $n < scalar @storePaths; $n++) {
 writeManifest $manifest, \%narFiles, \%patches, \%successors;
 
 
+sub copyFile {
+    my $src = shift;
+    my $dst = shift;
+    system("cp '$src' '$dst.tmp'") == 0 or die "cannot copy file";
+    rename("$dst.tmp", "$dst") or die "cannot rename file";
+}
+
+
 # Upload the archives.
 print STDERR "uploading archives...\n";
 foreach my $nararchive (@nararchives) {
@@ -177,17 +222,29 @@ foreach my $nararchive (@nararchives) {
     $nararchive =~ /\/([^\/]*)$/;
     my $basename = $1;
 
-    if (system("$curl --head $archives_get_url/$basename > /dev/null") != 0) {
-        print STDERR "  $nararchive\n";
-        system("$curl --show-error --upload-file " .
-               "'$nararchive' '$archives_put_url/$basename' > /dev/null") == 0 or
-            die "curl failed on $nararchive: $?";
+    if ($localCopy) {
+        if (! -f "$localArchivesDir/$basename") {
+            print STDERR "  $nararchive\n";
+            copyFile $nararchive, "$localArchivesDir/$basename";
+        }
+    }
+    else {
+        if (system("$curl --head $archives_get_url/$basename > /dev/null") != 0) {
+            print STDERR "  $nararchive\n";
+            system("$curl --show-error --upload-file " .
+                   "'$nararchive' '$archives_put_url/$basename' > /dev/null") == 0 or
+                   die "curl failed on $nararchive: $?";
+        }
     }
 }
 
 
 # Upload the manifest.
 print STDERR "uploading manifest...\n";
-system("$curl  --show-error --upload-file " .
-       "'$manifest' '$manifest_put_url' > /dev/null") == 0 or
-    die "curl failed on $manifest: $?";
+if ($localCopy) {
+    copyFile $manifest, $localManifestFile;
+} else {
+    system("$curl  --show-error --upload-file " .
+           "'$manifest' '$manifest_put_url' > /dev/null") == 0 or
+           die "curl failed on $manifest: $?";
+}
diff --git a/scripts/readmanifest.pm.in b/scripts/readmanifest.pm.in
index 8b9920b798d5..3e74c88f98b7 100644
--- a/scripts/readmanifest.pm.in
+++ b/scripts/readmanifest.pm.in
@@ -49,6 +49,7 @@ sub readManifest {
     my $baseHash;
     my $patchType;
     my $narHash;
+    my $references;
 
     while (<MANIFEST>) {
         chomp;
@@ -70,6 +71,7 @@ sub readManifest {
                 undef $basePath;
                 undef $baseHash;
                 undef $patchType;
+                $references = "";
 	    }
 
         } else {
@@ -98,7 +100,7 @@ sub readManifest {
                     if (!$found) {
                         push @{$narFileList},
                             { url => $url, hash => $hash, size => $size
-                            , narHash => $narHash
+                            , narHash => $narHash, references => $references
                             };
                     }
                 
@@ -127,6 +129,7 @@ sub readManifest {
             elsif (/^\s*BaseHash:\s*(\S+)\s*$/) { $baseHash = $1; }
             elsif (/^\s*Type:\s*(\S+)\s*$/) { $patchType = $1; }
             elsif (/^\s*NarHash:\s*(\S+)\s*$/) { $narHash = $1; }
+            elsif (/^\s*References:\s*(.*)\s*$/) { $references = $1; }
 
             # Compatibility;
             elsif (/^\s*NarURL:\s*(\S+)\s*$/) { $url = $1; }
@@ -153,10 +156,13 @@ sub writeManifest
         foreach my $narFile (@{$narFileList}) {
             print MANIFEST "{\n";
             print MANIFEST "  StorePath: $storePath\n";
+            print MANIFEST "  HashAlgo: $narFile->{hashAlgo}\n";
             print MANIFEST "  NarURL: $narFile->{url}\n";
             print MANIFEST "  MD5: $narFile->{hash}\n";
             print MANIFEST "  NarHash: $narFile->{narHash}\n";
             print MANIFEST "  Size: $narFile->{size}\n";
+            print MANIFEST "  References: $narFile->{references}\n"
+                if defined $narFile->{references} && $narFile->{references} ne "";
             foreach my $p (keys %{$successors}) { # !!! quadratic
                 if ($$successors{$p} eq $storePath) {
                     print MANIFEST "  SuccOf: $p\n";
@@ -171,6 +177,7 @@ sub writeManifest
         foreach my $patch (@{$patchList}) {
             print MANIFEST "patch {\n";
             print MANIFEST "  StorePath: $storePath\n";
+            print MANIFEST "  HashAlgo: $patch->{hashAlgo}\n";
             print MANIFEST "  NarURL: $patch->{url}\n";
             print MANIFEST "  MD5: $patch->{hash}\n";
             print MANIFEST "  NarHash: $patch->{narHash}\n";