From 40d9eb14dfb842c51e9f86818b43ae7711e1a5d6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 22 Nov 2003 15:58:34 +0000 Subject: [PATCH] * Fix the garbage collector. --- scripts/nix-collect-garbage.in | 21 ++++++++++++++++++--- src/libstore/store.cc | 9 +-------- src/libutil/util.cc | 11 +++++++++++ src/libutil/util.hh | 5 +++++ src/nix-env/main.cc | 4 +++- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/scripts/nix-collect-garbage.in b/scripts/nix-collect-garbage.in index 9b471d896..d0552fd2f 100755 --- a/scripts/nix-collect-garbage.in +++ b/scripts/nix-collect-garbage.in @@ -17,9 +17,24 @@ foreach my $arg (@ARGV) { else { die "unknown argument `$arg'" }; } +opendir(DIR, $linkdir) or die "cannot open directory $linkdir: $!"; +my @links = readdir DIR or die "cannot read directory $linkdir: $!"; +closedir DIR; + +my @roots; +foreach my $link (@links) { + $link = $linkdir . "/" . $link; + next if (!($link =~ /.id$/)); + open ROOT, "<$link" or die "cannot open $link: $!"; + my $root = ; + chomp $root; + close ROOT; + push @roots, $root; +} + my $extraarg = ""; if ($keepsuccessors) { $extraarg = "--include-successors"; }; -my $pid = open2(\*READ, \*WRITE, "nix --query --requisites $extraarg \$(cat $linkdir/*.id)") +my $pid = open2(\*READ, \*WRITE, "nix-store --query --requisites $extraarg @roots") or die "determining live paths"; close WRITE; while () { @@ -34,8 +49,8 @@ $? == 0 or die "determining live paths"; exit 0 if ($invert); -opendir(DIR, $storedir) or die "cannot opendir $storedir: $!"; -my @names = readdir(DIR); +opendir(DIR, $storedir) or die "cannot open directory $storedir: $!"; +my @names = readdir DIR; closedir DIR; foreach my $name (@names) { diff --git a/src/libstore/store.cc b/src/libstore/store.cc index c83316cf6..caaa293a6 100644 --- a/src/libstore/store.cc +++ b/src/libstore/store.cc @@ -1,9 +1,6 @@ #include -#include -#include #include -#include #include #include "store.hh" @@ -307,11 +304,7 @@ void addTextToStore(const Path & dstPath, const string & s) PathLocks outputLock(lockPaths); if (!isValidPath(dstPath)) { - - AutoCloseFD fd = open(dstPath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); - if (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath); - - writeFull(fd, (unsigned char *) s.c_str(), s.size()); + writeStringToFile(dstPath, s); Transaction txn(nixDB); registerValidPath(txn, dstPath); diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 6a032a3f1..60b86b162 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include "util.hh" @@ -192,6 +193,16 @@ Path createTempDir() } +void writeStringToFile(const Path & path, const string & s) +{ + AutoCloseFD fd = open(path.c_str(), + O_CREAT | O_EXCL | O_WRONLY, 0666); + if (fd == -1) + throw SysError(format("creating file `%1%'") % path); + writeFull(fd, (unsigned char *) s.c_str(), s.size()); +} + + Verbosity verbosity = lvlError; static int nestingLevel = 0; diff --git a/src/libutil/util.hh b/src/libutil/util.hh index d0e7b3ada..cca93cdc7 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -87,6 +87,11 @@ void makePathReadOnly(const Path & path); /* Create a temporary directory. */ Path createTempDir(); +/* Create a file and write the given text to it. The file is written + in binary mode (i.e., no end-of-line conversions). The path should + not already exist. */ +void writeStringToFile(const Path & path, const string & s); + /* Messages. */ diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc index 73166964f..a8377582c 100644 --- a/src/nix-env/main.cc +++ b/src/nix-env/main.cc @@ -139,6 +139,8 @@ Path createLink(Path outPath, Path drvPath) num++; } + writeStringToFile(linkPath + "-src.id", drvPath); + return linkPath; } @@ -290,7 +292,7 @@ void uninstallDerivations(EvalState & state, Strings drvNames) installedDrvs.erase(j->second); } - createUserEnv(state, installedDrvs); + createUserEnv(state, installedDrvs); }