2006-09-04 23:06:23 +02:00
|
|
|
|
#include "globals.hh"
|
2006-03-06 12:21:15 +01:00
|
|
|
|
#include "misc.hh"
|
2006-11-30 18:43:04 +01:00
|
|
|
|
#include "local-store.hh"
|
2012-03-26 20:43:33 +02:00
|
|
|
|
#include "immutable.hh"
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2004-08-25 13:43:49 +02:00
|
|
|
|
|
2008-09-17 12:02:55 +02:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include <queue>
|
2009-11-24 10:53:18 +01:00
|
|
|
|
#include <algorithm>
|
2008-09-17 12:02:55 +02:00
|
|
|
|
|
2004-08-25 18:54:08 +02:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2005-01-31 11:27:25 +01:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2004-08-25 18:54:08 +02:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
static string gcLockName = "gc.lock";
|
2005-02-01 14:48:46 +01:00
|
|
|
|
static string tempRootsDir = "temproots";
|
|
|
|
|
static string gcRootsDir = "gcroots";
|
2005-01-31 23:23:49 +01:00
|
|
|
|
|
2007-11-29 17:18:24 +01:00
|
|
|
|
static const int defaultGcLevel = 1000;
|
2007-11-15 16:07:27 +01:00
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
|
|
|
|
|
/* Acquire the global GC lock. This is used to prevent new Nix
|
|
|
|
|
processes from starting after the temporary root files have been
|
|
|
|
|
read. To be precise: when they try to create a new temporary root
|
|
|
|
|
file, they will block until the garbage collector has finished /
|
|
|
|
|
yielded the GC lock. */
|
2010-08-31 13:47:31 +02:00
|
|
|
|
int LocalStore::openGCLock(LockType lockType)
|
2005-01-31 23:01:55 +01:00
|
|
|
|
{
|
2005-01-31 23:23:49 +01:00
|
|
|
|
Path fnGCLock = (format("%1%/%2%")
|
2012-07-31 01:55:41 +02:00
|
|
|
|
% settings.nixStateDir % gcLockName).str();
|
|
|
|
|
|
2005-02-01 16:05:32 +01:00
|
|
|
|
debug(format("acquiring global GC lock `%1%'") % fnGCLock);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
AutoCloseFD fdGCLock = open(fnGCLock.c_str(), O_RDWR | O_CREAT, 0600);
|
|
|
|
|
if (fdGCLock == -1)
|
|
|
|
|
throw SysError(format("opening global GC lock `%1%'") % fnGCLock);
|
2012-03-05 20:29:00 +01:00
|
|
|
|
closeOnExec(fdGCLock);
|
2005-01-31 23:23:49 +01:00
|
|
|
|
|
2006-09-15 00:30:33 +02:00
|
|
|
|
if (!lockFile(fdGCLock, lockType, false)) {
|
|
|
|
|
printMsg(lvlError, format("waiting for the big garbage collector lock..."));
|
|
|
|
|
lockFile(fdGCLock, lockType, true);
|
|
|
|
|
}
|
2005-01-31 23:23:49 +01:00
|
|
|
|
|
|
|
|
|
/* !!! Restrict read permission on the GC root. Otherwise any
|
|
|
|
|
process that can open the file for reading can DoS the
|
|
|
|
|
collector. */
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
return fdGCLock.borrow();
|
2005-01-31 23:01:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-04-16 18:47:01 +02:00
|
|
|
|
void createSymlink(const Path & link, const Path & target)
|
2005-02-01 14:48:46 +01:00
|
|
|
|
{
|
|
|
|
|
/* Create directories up to `gcRoot'. */
|
|
|
|
|
createDirs(dirOf(link));
|
|
|
|
|
|
2012-04-16 18:47:01 +02:00
|
|
|
|
/* Create the new symlink. */
|
|
|
|
|
Path tempLink = (format("%1%.tmp-%2%-%3%")
|
|
|
|
|
% link % getpid() % rand()).str();
|
|
|
|
|
if (symlink(target.c_str(), tempLink.c_str()) == -1)
|
2005-02-01 14:48:46 +01:00
|
|
|
|
throw SysError(format("symlinking `%1%' to `%2%'")
|
2012-04-16 18:47:01 +02:00
|
|
|
|
% tempLink % target);
|
|
|
|
|
|
|
|
|
|
/* Atomically replace the old one. */
|
|
|
|
|
if (rename(tempLink.c_str(), link.c_str()) == -1)
|
|
|
|
|
throw SysError(format("cannot rename `%1%' to `%2%'")
|
|
|
|
|
% tempLink % link);
|
2005-02-01 14:48:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-02 17:41:36 +01:00
|
|
|
|
void LocalStore::syncWithGC()
|
|
|
|
|
{
|
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-05 00:29:16 +01:00
|
|
|
|
void LocalStore::addIndirectRoot(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
string hash = printHash32(hashString(htSHA1, path));
|
|
|
|
|
Path realRoot = canonPath((format("%1%/%2%/auto/%3%")
|
2012-07-31 01:55:41 +02:00
|
|
|
|
% settings.nixStateDir % gcRootsDir % hash).str());
|
2012-04-16 18:47:01 +02:00
|
|
|
|
createSymlink(realRoot, path);
|
2006-12-05 00:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-31 23:11:50 +02:00
|
|
|
|
Path addPermRoot(StoreAPI & store, const Path & _storePath,
|
|
|
|
|
const Path & _gcRoot, bool indirect, bool allowOutsideRootsDir)
|
2005-02-01 13:36:25 +01:00
|
|
|
|
{
|
|
|
|
|
Path storePath(canonPath(_storePath));
|
|
|
|
|
Path gcRoot(canonPath(_gcRoot));
|
2005-02-01 14:48:46 +01:00
|
|
|
|
assertStorePath(storePath);
|
2005-02-01 13:36:25 +01:00
|
|
|
|
|
2007-06-11 13:36:22 +02:00
|
|
|
|
if (isInStore(gcRoot))
|
|
|
|
|
throw Error(format(
|
|
|
|
|
"creating a garbage collector root (%1%) in the Nix store is forbidden "
|
|
|
|
|
"(are you running nix-build inside the store?)") % gcRoot);
|
|
|
|
|
|
2005-02-01 14:48:46 +01:00
|
|
|
|
if (indirect) {
|
2012-04-16 18:47:01 +02:00
|
|
|
|
/* Don't clobber the the link if it already exists and doesn't
|
|
|
|
|
point to the Nix store. */
|
|
|
|
|
if (pathExists(gcRoot) && (!isLink(gcRoot) || !isInStore(readLink(gcRoot))))
|
|
|
|
|
throw Error(format("cannot create symlink `%1%'; already exists") % gcRoot);
|
|
|
|
|
createSymlink(gcRoot, storePath);
|
2011-08-31 23:11:50 +02:00
|
|
|
|
store.addIndirectRoot(gcRoot);
|
2005-02-01 14:48:46 +01:00
|
|
|
|
}
|
2005-02-01 13:36:25 +01:00
|
|
|
|
|
2005-02-01 14:48:46 +01:00
|
|
|
|
else {
|
2006-09-15 00:30:33 +02:00
|
|
|
|
if (!allowOutsideRootsDir) {
|
2012-07-31 01:55:41 +02:00
|
|
|
|
Path rootsDir = canonPath((format("%1%/%2%") % settings.nixStateDir % gcRootsDir).str());
|
|
|
|
|
|
2006-09-15 00:30:33 +02:00
|
|
|
|
if (string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/")
|
|
|
|
|
throw Error(format(
|
|
|
|
|
"path `%1%' is not a valid garbage collector root; "
|
|
|
|
|
"it's not in the directory `%2%'")
|
|
|
|
|
% gcRoot % rootsDir);
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-04-16 18:47:01 +02:00
|
|
|
|
createSymlink(gcRoot, storePath);
|
2005-02-01 14:48:46 +01:00
|
|
|
|
}
|
2005-02-01 13:36:25 +01:00
|
|
|
|
|
2008-06-14 18:02:31 +02:00
|
|
|
|
/* Check that the root can be found by the garbage collector.
|
|
|
|
|
!!! This can be very slow on machines that have many roots.
|
|
|
|
|
Instead of reading all the roots, it would be more efficient to
|
|
|
|
|
check if the root is in a directory in or linked from the
|
|
|
|
|
gcroots directory. */
|
2012-07-31 01:55:41 +02:00
|
|
|
|
if (settings.checkRootReachability) {
|
2011-08-31 23:11:50 +02:00
|
|
|
|
Roots roots = store.findRoots();
|
2007-03-19 10:16:47 +01:00
|
|
|
|
if (roots.find(gcRoot) == roots.end())
|
2012-07-31 01:55:41 +02:00
|
|
|
|
printMsg(lvlError,
|
2007-03-19 10:16:47 +01:00
|
|
|
|
format(
|
|
|
|
|
"warning: `%1%' is not in a directory where the garbage collector looks for roots; "
|
|
|
|
|
"therefore, `%2%' might be removed by the garbage collector")
|
|
|
|
|
% gcRoot % storePath);
|
|
|
|
|
}
|
2010-08-30 16:53:03 +02:00
|
|
|
|
|
2006-12-02 17:41:36 +01:00
|
|
|
|
/* Grab the global GC root, causing us to block while a GC is in
|
|
|
|
|
progress. This prevents the set of permanent roots from
|
|
|
|
|
increasing while a GC is in progress. */
|
2011-08-31 23:11:50 +02:00
|
|
|
|
store.syncWithGC();
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-02-01 13:36:25 +01:00
|
|
|
|
return gcRoot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-01-31 11:27:25 +01:00
|
|
|
|
/* The file to which we write our temporary roots. */
|
2005-01-31 23:01:55 +01:00
|
|
|
|
static Path fnTempRoots;
|
2005-01-31 11:27:25 +01:00
|
|
|
|
static AutoCloseFD fdTempRoots;
|
|
|
|
|
|
|
|
|
|
|
2006-12-02 17:41:36 +01:00
|
|
|
|
void LocalStore::addTempRoot(const Path & path)
|
2005-01-31 11:27:25 +01:00
|
|
|
|
{
|
|
|
|
|
/* Create the temporary roots file for this process. */
|
|
|
|
|
if (fdTempRoots == -1) {
|
|
|
|
|
|
|
|
|
|
while (1) {
|
2012-07-31 01:55:41 +02:00
|
|
|
|
Path dir = (format("%1%/%2%") % settings.nixStateDir % tempRootsDir).str();
|
2005-03-24 18:46:38 +01:00
|
|
|
|
createDirs(dir);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-03-24 18:46:38 +01:00
|
|
|
|
fnTempRoots = (format("%1%/%2%")
|
|
|
|
|
% dir % getpid()).str();
|
2005-01-31 23:23:49 +01:00
|
|
|
|
|
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2006-06-20 19:48:10 +02:00
|
|
|
|
if (pathExists(fnTempRoots))
|
|
|
|
|
/* It *must* be stale, since there can be no two
|
|
|
|
|
processes with the same pid. */
|
2012-02-15 00:28:01 +01:00
|
|
|
|
unlink(fnTempRoots.c_str());
|
2006-06-20 19:48:10 +02:00
|
|
|
|
|
2012-07-31 01:55:41 +02:00
|
|
|
|
fdTempRoots = openLockFile(fnTempRoots, true);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
fdGCLock.close();
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-01-31 11:27:25 +01:00
|
|
|
|
debug(format("acquiring read lock on `%1%'") % fnTempRoots);
|
|
|
|
|
lockFile(fdTempRoots, ltRead, true);
|
|
|
|
|
|
|
|
|
|
/* Check whether the garbage collector didn't get in our
|
|
|
|
|
way. */
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (fstat(fdTempRoots, &st) == -1)
|
|
|
|
|
throw SysError(format("statting `%1%'") % fnTempRoots);
|
|
|
|
|
if (st.st_size == 0) break;
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-01-31 11:27:25 +01:00
|
|
|
|
/* The garbage collector deleted this file before we could
|
|
|
|
|
get a lock. (It won't delete the file after we get a
|
|
|
|
|
lock.) Try again. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Upgrade the lock to a write lock. This will cause us to block
|
|
|
|
|
if the garbage collector is holding our lock. */
|
|
|
|
|
debug(format("acquiring write lock on `%1%'") % fnTempRoots);
|
|
|
|
|
lockFile(fdTempRoots, ltWrite, true);
|
|
|
|
|
|
|
|
|
|
string s = path + '\0';
|
2012-02-09 18:27:45 +01:00
|
|
|
|
writeFull(fdTempRoots, (const unsigned char *) s.data(), s.size());
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
|
|
|
|
/* Downgrade to a read lock. */
|
|
|
|
|
debug(format("downgrading to read lock on `%1%'") % fnTempRoots);
|
|
|
|
|
lockFile(fdTempRoots, ltRead, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-01-31 22:20:59 +01:00
|
|
|
|
void removeTempRoots()
|
|
|
|
|
{
|
|
|
|
|
if (fdTempRoots != -1) {
|
|
|
|
|
fdTempRoots.close();
|
|
|
|
|
unlink(fnTempRoots.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-03-13 17:07:49 +01:00
|
|
|
|
/* Automatically clean up the temporary roots file when we exit. */
|
2012-07-31 01:55:41 +02:00
|
|
|
|
struct RemoveTempRoots
|
2012-03-13 17:07:49 +01:00
|
|
|
|
{
|
|
|
|
|
~RemoveTempRoots()
|
|
|
|
|
{
|
|
|
|
|
removeTempRoots();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static RemoveTempRoots autoRemoveTempRoots __attribute__((unused));
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
typedef boost::shared_ptr<AutoCloseFD> FDPtr;
|
2005-01-31 11:27:25 +01:00
|
|
|
|
typedef list<FDPtr> FDs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void readTempRoots(PathSet & tempRoots, FDs & fds)
|
|
|
|
|
{
|
|
|
|
|
/* Read the `temproots' directory for per-process temporary root
|
|
|
|
|
files. */
|
|
|
|
|
Strings tempRootFiles = readDirectory(
|
2012-07-31 01:55:41 +02:00
|
|
|
|
(format("%1%/%2%") % settings.nixStateDir % tempRootsDir).str());
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
2009-04-21 13:52:16 +02:00
|
|
|
|
foreach (Strings::iterator, i, tempRootFiles) {
|
2012-07-31 01:55:41 +02:00
|
|
|
|
Path path = (format("%1%/%2%/%3%") % settings.nixStateDir % tempRootsDir % *i).str();
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
|
|
|
|
debug(format("reading temporary root file `%1%'") % path);
|
|
|
|
|
FDPtr fd(new AutoCloseFD(open(path.c_str(), O_RDWR, 0666)));
|
|
|
|
|
if (*fd == -1) {
|
|
|
|
|
/* It's okay if the file has disappeared. */
|
|
|
|
|
if (errno == ENOENT) continue;
|
|
|
|
|
throw SysError(format("opening temporary roots file `%1%'") % path);
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-20 19:48:10 +02:00
|
|
|
|
/* This should work, but doesn't, for some reason. */
|
|
|
|
|
//FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
|
|
|
|
|
//if (*fd == -1) continue;
|
|
|
|
|
|
2005-01-31 11:27:25 +01:00
|
|
|
|
/* Try to acquire a write lock without blocking. This can
|
|
|
|
|
only succeed if the owning process has died. In that case
|
|
|
|
|
we don't care about its temporary roots. */
|
|
|
|
|
if (lockFile(*fd, ltWrite, false)) {
|
|
|
|
|
printMsg(lvlError, format("removing stale temporary roots file `%1%'")
|
|
|
|
|
% path);
|
2005-01-31 22:20:59 +01:00
|
|
|
|
unlink(path.c_str());
|
|
|
|
|
writeFull(*fd, (const unsigned char *) "d", 1);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Acquire a read lock. This will prevent the owning process
|
|
|
|
|
from upgrading to a write lock, therefore it will block in
|
|
|
|
|
addTempRoot(). */
|
|
|
|
|
debug(format("waiting for read lock on `%1%'") % path);
|
|
|
|
|
lockFile(*fd, ltRead, true);
|
|
|
|
|
|
|
|
|
|
/* Read the entire file. */
|
2005-02-01 23:07:48 +01:00
|
|
|
|
string contents = readFile(*fd);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
|
|
|
|
/* Extract the roots. */
|
2006-05-11 04:19:43 +02:00
|
|
|
|
string::size_type pos = 0, end;
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
|
|
|
|
while ((end = contents.find((char) 0, pos)) != string::npos) {
|
|
|
|
|
Path root(contents, pos, end - pos);
|
|
|
|
|
debug(format("got temporary root `%1%'") % root);
|
|
|
|
|
assertStorePath(root);
|
|
|
|
|
tempRoots.insert(root);
|
|
|
|
|
pos = end + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fds.push_back(fd); /* keep open */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-31 23:11:50 +02:00
|
|
|
|
static void findRoots(StoreAPI & store, const Path & path,
|
|
|
|
|
bool recurseSymlinks, bool deleteStale, Roots & roots)
|
2005-02-01 16:05:32 +01:00
|
|
|
|
{
|
2006-12-05 02:31:45 +01:00
|
|
|
|
try {
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2006-12-05 02:31:45 +01:00
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st) == -1)
|
|
|
|
|
throw SysError(format("statting `%1%'") % path);
|
|
|
|
|
|
|
|
|
|
printMsg(lvlVomit, format("looking at `%1%'") % path);
|
|
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
|
Strings names = readDirectory(path);
|
2009-04-21 13:52:16 +02:00
|
|
|
|
foreach (Strings::iterator, i, names)
|
2011-08-31 23:11:50 +02:00
|
|
|
|
findRoots(store, path + "/" + *i, recurseSymlinks, deleteStale, roots);
|
2005-02-01 16:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-05 02:31:45 +01:00
|
|
|
|
else if (S_ISLNK(st.st_mode)) {
|
|
|
|
|
Path target = absPath(readLink(path), dirOf(path));
|
|
|
|
|
|
|
|
|
|
if (isInStore(target)) {
|
|
|
|
|
debug(format("found root `%1%' in `%2%'")
|
|
|
|
|
% target % path);
|
|
|
|
|
Path storePath = toStorePath(target);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
if (store.isValidPath(storePath))
|
2006-12-05 02:31:45 +01:00
|
|
|
|
roots[path] = storePath;
|
|
|
|
|
else
|
|
|
|
|
printMsg(lvlInfo, format("skipping invalid root from `%1%' to `%2%'")
|
|
|
|
|
% path % storePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (recurseSymlinks) {
|
|
|
|
|
if (pathExists(target))
|
2011-08-31 23:11:50 +02:00
|
|
|
|
findRoots(store, target, false, deleteStale, roots);
|
2006-12-05 02:31:45 +01:00
|
|
|
|
else if (deleteStale) {
|
|
|
|
|
printMsg(lvlInfo, format("removing stale link from `%1%' to `%2%'") % path % target);
|
|
|
|
|
/* Note that we only delete when recursing, i.e.,
|
|
|
|
|
when we are still in the `gcroots' tree. We
|
|
|
|
|
never delete stuff outside that tree. */
|
|
|
|
|
unlink(path.c_str());
|
|
|
|
|
}
|
2005-02-01 16:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-12-05 02:31:45 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (SysError & e) {
|
|
|
|
|
/* We only ignore permanent failures. */
|
|
|
|
|
if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
|
|
|
|
|
printMsg(lvlInfo, format("cannot read potential root `%1%'") % path);
|
|
|
|
|
else
|
|
|
|
|
throw;
|
2005-02-01 16:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-31 23:11:50 +02:00
|
|
|
|
static Roots findRoots(StoreAPI & store, bool deleteStale)
|
2006-12-05 01:34:42 +01:00
|
|
|
|
{
|
2006-12-05 02:31:45 +01:00
|
|
|
|
Roots roots;
|
2012-07-31 01:55:41 +02:00
|
|
|
|
Path rootsDir = canonPath((format("%1%/%2%") % settings.nixStateDir % gcRootsDir).str());
|
2011-08-31 23:11:50 +02:00
|
|
|
|
findRoots(store, rootsDir, true, deleteStale, roots);
|
2006-12-05 02:31:45 +01:00
|
|
|
|
return roots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Roots LocalStore::findRoots()
|
|
|
|
|
{
|
2011-08-31 23:11:50 +02:00
|
|
|
|
return nix::findRoots(*this, false);
|
2006-12-05 01:34:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-31 23:11:50 +02:00
|
|
|
|
static void addAdditionalRoots(StoreAPI & store, PathSet & roots)
|
2006-07-20 14:17:25 +02:00
|
|
|
|
{
|
|
|
|
|
Path rootFinder = getEnv("NIX_ROOT_FINDER",
|
2012-07-31 01:55:41 +02:00
|
|
|
|
settings.nixLibexecDir + "/nix/find-runtime-roots.pl");
|
2006-07-20 14:17:25 +02:00
|
|
|
|
|
|
|
|
|
if (rootFinder.empty()) return;
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2006-07-20 14:19:55 +02:00
|
|
|
|
debug(format("executing `%1%' to find additional roots") % rootFinder);
|
2006-07-20 14:17:25 +02:00
|
|
|
|
|
|
|
|
|
string result = runProgram(rootFinder);
|
|
|
|
|
|
|
|
|
|
Strings paths = tokenizeString(result, "\n");
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2009-04-21 13:52:16 +02:00
|
|
|
|
foreach (Strings::iterator, i, paths) {
|
2006-07-20 14:17:25 +02:00
|
|
|
|
if (isInStore(*i)) {
|
|
|
|
|
Path path = toStorePath(*i);
|
2011-08-31 23:11:50 +02:00
|
|
|
|
if (roots.find(path) == roots.end() && store.isValidPath(path)) {
|
2009-11-23 17:34:24 +01:00
|
|
|
|
debug(format("got additional root `%1%'") % path);
|
2006-07-20 14:17:25 +02:00
|
|
|
|
roots.insert(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 16:20:16 +02:00
|
|
|
|
struct GCLimitReached { };
|
|
|
|
|
|
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
struct LocalStore::GCState
|
2008-06-13 20:25:24 +02:00
|
|
|
|
{
|
2009-11-23 17:34:24 +01:00
|
|
|
|
GCOptions options;
|
|
|
|
|
GCResults & results;
|
|
|
|
|
PathSet roots;
|
|
|
|
|
PathSet tempRoots;
|
|
|
|
|
PathSet deleted;
|
|
|
|
|
PathSet live;
|
|
|
|
|
PathSet busy;
|
2012-03-26 20:43:33 +02:00
|
|
|
|
PathSet invalidated;
|
2009-11-23 17:34:24 +01:00
|
|
|
|
bool gcKeepOutputs;
|
|
|
|
|
bool gcKeepDerivations;
|
2012-03-26 20:43:33 +02:00
|
|
|
|
unsigned long long bytesInvalidated;
|
|
|
|
|
GCState(GCResults & results_) : results(results_), bytesInvalidated(0) { }
|
2009-11-23 17:34:24 +01:00
|
|
|
|
};
|
2008-06-18 16:20:16 +02:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
|
2010-10-14 17:55:51 +02:00
|
|
|
|
static bool shouldDelete(GCOptions::GCAction action)
|
2009-11-23 17:34:24 +01:00
|
|
|
|
{
|
|
|
|
|
return action == GCOptions::gcDeleteDead
|
|
|
|
|
|| action == GCOptions::gcDeleteSpecific;
|
2008-06-13 20:25:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
bool LocalStore::isActiveTempFile(const GCState & state,
|
|
|
|
|
const Path & path, const string & suffix)
|
2008-09-17 14:54:07 +02:00
|
|
|
|
{
|
2009-11-23 17:34:24 +01:00
|
|
|
|
return hasSuffix(path, suffix)
|
|
|
|
|
&& state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end();
|
|
|
|
|
}
|
2008-09-17 14:54:07 +02:00
|
|
|
|
|
2012-03-26 20:00:02 +02:00
|
|
|
|
|
2012-03-26 20:43:33 +02:00
|
|
|
|
void LocalStore::deleteGarbage(GCState & state, const Path & path)
|
|
|
|
|
{
|
|
|
|
|
printMsg(lvlInfo, format("deleting `%1%'") % path);
|
2012-08-02 04:34:46 +02:00
|
|
|
|
unsigned long long bytesFreed;
|
|
|
|
|
deletePathWrapped(path, bytesFreed);
|
2012-03-26 20:43:33 +02:00
|
|
|
|
state.results.bytesFreed += bytesFreed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
bool LocalStore::tryToDelete(GCState & state, const Path & path)
|
|
|
|
|
{
|
2010-10-14 17:55:51 +02:00
|
|
|
|
checkInterrupt();
|
2012-03-26 20:56:30 +02:00
|
|
|
|
|
2012-07-23 21:48:30 +02:00
|
|
|
|
if (path == linksDir) return true;
|
|
|
|
|
|
2012-03-26 20:56:30 +02:00
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st)) {
|
|
|
|
|
if (errno == ENOENT) return true;
|
|
|
|
|
throw SysError(format("getting status of %1%") % path);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
if (state.deleted.find(path) != state.deleted.end()) return true;
|
|
|
|
|
if (state.live.find(path) != state.live.end()) return false;
|
|
|
|
|
|
|
|
|
|
startNest(nest, lvlDebug, format("considering whether to delete `%1%'") % path);
|
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* If gc-keep-outputs and gc-keep-derivations are both set, we can
|
|
|
|
|
have cycles in the liveness graph, so we need to treat such
|
|
|
|
|
strongly connected components as a single unit (‘paths’). That
|
|
|
|
|
is, we can delete the elements of ‘paths’ only if all referrers
|
|
|
|
|
of ‘paths’ are garbage. */
|
|
|
|
|
PathSet paths, referrers;
|
2012-09-13 19:08:27 +02:00
|
|
|
|
Paths pathsSorted;
|
2009-11-23 17:34:24 +01:00
|
|
|
|
|
|
|
|
|
if (isValidPath(path)) {
|
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* Add derivers and outputs of ‘path’ to ‘paths’. */
|
|
|
|
|
PathSet todo;
|
|
|
|
|
todo.insert(path);
|
|
|
|
|
while (!todo.empty()) {
|
|
|
|
|
Path p = *todo.begin();
|
|
|
|
|
assertStorePath(p);
|
|
|
|
|
todo.erase(p);
|
|
|
|
|
if (paths.find(p) != paths.end()) continue;
|
|
|
|
|
paths.insert(p);
|
|
|
|
|
/* If gc-keep-derivations is set and this is a derivation,
|
|
|
|
|
then don't delete the derivation if any of the outputs
|
|
|
|
|
are live. */
|
|
|
|
|
if (state.gcKeepDerivations && isDerivation(p)) {
|
|
|
|
|
PathSet outputs = queryDerivationOutputs(p);
|
|
|
|
|
foreach (PathSet::iterator, i, outputs)
|
|
|
|
|
if (isValidPath(*i)) todo.insert(*i);
|
2009-11-23 17:34:24 +01:00
|
|
|
|
}
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* If gc-keep-outputs is set, then don't delete this path
|
|
|
|
|
if there are derivers of this path that are not
|
|
|
|
|
garbage. */
|
|
|
|
|
if (state.gcKeepOutputs) {
|
|
|
|
|
PathSet derivers = queryValidDerivers(p);
|
|
|
|
|
foreach (PathSet::iterator, i, derivers) todo.insert(*i);
|
2009-11-23 17:34:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-17 14:54:07 +02:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
else {
|
|
|
|
|
/* A lock file belonging to a path that we're building right
|
|
|
|
|
now isn't garbage. */
|
|
|
|
|
if (isActiveTempFile(state, path, ".lock")) return false;
|
2008-12-12 18:03:18 +01:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
/* Don't delete .chroot directories for derivations that are
|
|
|
|
|
currently being built. */
|
|
|
|
|
if (isActiveTempFile(state, path, ".chroot")) return false;
|
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
paths.insert(path);
|
2009-11-23 17:34:24 +01:00
|
|
|
|
}
|
2008-12-12 18:03:18 +01:00
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* Check if any path in ‘paths’ is a root. */
|
|
|
|
|
foreach (PathSet::iterator, i, paths)
|
|
|
|
|
if (state.roots.find(*i) != state.roots.end()) {
|
|
|
|
|
printMsg(lvlDebug, format("cannot delete `%1%' because it's a root") % *i);
|
|
|
|
|
goto isLive;
|
|
|
|
|
}
|
2012-03-26 20:43:33 +02:00
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* Recursively try to delete the referrers of this strongly
|
|
|
|
|
connected component. If any referrer can't be deleted, then
|
|
|
|
|
these paths can't be deleted either. */
|
|
|
|
|
foreach (PathSet::iterator, i, paths)
|
|
|
|
|
if (isValidPath(*i)) queryReferrers(*i, referrers);
|
|
|
|
|
|
|
|
|
|
foreach (PathSet::iterator, i, referrers)
|
|
|
|
|
if (paths.find(*i) == paths.end() && !tryToDelete(state, *i)) {
|
|
|
|
|
printMsg(lvlDebug, format("cannot delete `%1%' because it has live referrers") % *i);
|
|
|
|
|
goto isLive;
|
2009-11-23 17:34:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-13 00:49:35 +02:00
|
|
|
|
/* The paths are garbage, so delete them. */
|
2012-09-13 19:08:27 +02:00
|
|
|
|
pathsSorted = topoSortPaths(*this, paths);
|
|
|
|
|
foreach (Paths::iterator, i, pathsSorted) {
|
2012-09-13 00:49:35 +02:00
|
|
|
|
if (shouldDelete(state.options.action)) {
|
|
|
|
|
|
|
|
|
|
/* If it's a valid path that's not a regular file or
|
|
|
|
|
symlink, invalidate it, rename it, and schedule it for
|
|
|
|
|
deletion. The renaming is to ensure that later (when
|
|
|
|
|
we're not holding the global GC lock) we can delete the
|
|
|
|
|
path without being afraid that the path has become
|
|
|
|
|
alive again. Otherwise delete it right away. */
|
|
|
|
|
if (isValidPath(*i)) {
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
|
printMsg(lvlInfo, format("invalidating `%1%'") % *i);
|
|
|
|
|
// Estimate the amount freed using the narSize field.
|
|
|
|
|
state.bytesInvalidated += queryPathInfo(*i).narSize;
|
|
|
|
|
invalidatePathChecked(*i);
|
|
|
|
|
makeMutable(i->c_str());
|
|
|
|
|
// Mac OS X cannot rename directories if they are read-only.
|
|
|
|
|
if (chmod(i->c_str(), st.st_mode | S_IWUSR) == -1)
|
|
|
|
|
throw SysError(format("making `%1%' writable") % *i);
|
|
|
|
|
Path tmp = (format("%1%-gc-%2%") % *i % getpid()).str();
|
|
|
|
|
if (rename(i->c_str(), tmp.c_str()))
|
|
|
|
|
throw SysError(format("unable to rename `%1%' to `%2%'") % *i % tmp);
|
|
|
|
|
state.invalidated.insert(tmp);
|
|
|
|
|
} else {
|
|
|
|
|
invalidatePathChecked(*i);
|
|
|
|
|
deleteGarbage(state, *i);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
deleteGarbage(state, *i);
|
|
|
|
|
|
|
|
|
|
if (state.results.bytesFreed + state.bytesInvalidated > state.options.maxFreed) {
|
|
|
|
|
printMsg(lvlInfo, format("deleted or invalidated more than %1% bytes; stopping") % state.options.maxFreed);
|
|
|
|
|
throw GCLimitReached();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
printMsg(lvlTalkative, format("would delete `%1%'") % *i);
|
|
|
|
|
|
|
|
|
|
state.deleted.insert(*i);
|
|
|
|
|
if (state.options.action != GCOptions::gcReturnLive)
|
|
|
|
|
state.results.paths.insert(*i);
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
isLive:
|
2012-09-13 00:49:35 +02:00
|
|
|
|
foreach (PathSet::iterator, i, paths) {
|
|
|
|
|
state.live.insert(*i);
|
|
|
|
|
if (state.options.action == GCOptions::gcReturnLive)
|
|
|
|
|
state.results.paths.insert(*i);
|
|
|
|
|
}
|
2008-12-12 18:03:18 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-11-24 10:53:18 +01:00
|
|
|
|
|
2008-12-12 18:03:18 +01:00
|
|
|
|
|
2012-07-23 21:48:30 +02:00
|
|
|
|
/* Unlink all files in /nix/store/.links that have a link count of 1,
|
|
|
|
|
which indicates that there are no other links and so they can be
|
|
|
|
|
safely deleted. FIXME: race condition with optimisePath(): we
|
|
|
|
|
might see a link count of 1 just before optimisePath() increases
|
|
|
|
|
the link count. */
|
2012-08-02 04:43:03 +02:00
|
|
|
|
void LocalStore::removeUnusedLinks(const GCState & state)
|
2012-07-23 21:48:30 +02:00
|
|
|
|
{
|
|
|
|
|
AutoCloseDir dir = opendir(linksDir.c_str());
|
|
|
|
|
if (!dir) throw SysError(format("opening directory `%1%'") % linksDir);
|
|
|
|
|
|
2012-08-06 03:45:27 +02:00
|
|
|
|
long long actualSize = 0, unsharedSize = 0;
|
2012-08-02 01:01:50 +02:00
|
|
|
|
|
2012-07-23 21:48:30 +02:00
|
|
|
|
struct dirent * dirent;
|
|
|
|
|
while (errno = 0, dirent = readdir(dir)) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
|
Path path = linksDir + "/" + name;
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st) == -1)
|
|
|
|
|
throw SysError(format("statting `%1%'") % path);
|
|
|
|
|
|
2012-08-02 01:01:50 +02:00
|
|
|
|
if (st.st_nlink != 1) {
|
|
|
|
|
unsigned long long size = st.st_blocks * 512ULL;
|
|
|
|
|
actualSize += size;
|
|
|
|
|
unsharedSize += (st.st_nlink - 1) * size;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-07-23 21:48:30 +02:00
|
|
|
|
|
|
|
|
|
printMsg(lvlTalkative, format("deleting unused link `%1%'") % path);
|
|
|
|
|
|
|
|
|
|
if (unlink(path.c_str()) == -1)
|
|
|
|
|
throw SysError(format("deleting `%1%'") % path);
|
2012-08-02 04:43:03 +02:00
|
|
|
|
|
|
|
|
|
state.results.bytesFreed += st.st_blocks * 512;
|
2012-07-23 21:48:30 +02:00
|
|
|
|
}
|
2012-08-02 01:01:50 +02:00
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (stat(linksDir.c_str(), &st) == -1)
|
|
|
|
|
throw SysError(format("statting `%1%'") % linksDir);
|
2012-08-06 03:45:27 +02:00
|
|
|
|
long long overhead = st.st_blocks * 512ULL;
|
2012-08-02 01:01:50 +02:00
|
|
|
|
|
|
|
|
|
printMsg(lvlInfo, format("note: currently hard linking saves %.2f MiB")
|
|
|
|
|
% ((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
|
2012-07-23 21:48:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
|
void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
|
2005-01-27 16:21:29 +01:00
|
|
|
|
{
|
2009-11-23 17:34:24 +01:00
|
|
|
|
GCState state(results);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
state.options = options;
|
|
|
|
|
|
|
|
|
|
state.gcKeepOutputs = settings.gcKeepOutputs;
|
|
|
|
|
state.gcKeepDerivations = settings.gcKeepDerivations;
|
2010-03-08 22:31:42 +01:00
|
|
|
|
|
|
|
|
|
/* Using `--ignore-liveness' with `--delete' can have unintended
|
|
|
|
|
consequences if `gc-keep-outputs' or `gc-keep-derivations' are
|
|
|
|
|
true (the garbage collector will recurse into deleting the
|
|
|
|
|
outputs or derivers, respectively). So disable them. */
|
|
|
|
|
if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
|
|
|
|
|
state.gcKeepOutputs = false;
|
|
|
|
|
state.gcKeepDerivations = false;
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2005-01-31 23:23:49 +01:00
|
|
|
|
/* Acquire the global GC root. This prevents
|
2005-01-31 11:27:25 +01:00
|
|
|
|
a) New roots from being added.
|
|
|
|
|
b) Processes from creating new temporary root files. */
|
2005-01-31 23:23:49 +01:00
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltWrite);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
|
2005-02-01 16:05:32 +01:00
|
|
|
|
/* Find the roots. Since we've grabbed the GC lock, the set of
|
|
|
|
|
permanent roots cannot increase now. */
|
2008-06-13 19:21:20 +02:00
|
|
|
|
printMsg(lvlError, format("finding garbage collector roots..."));
|
2011-08-31 23:11:50 +02:00
|
|
|
|
Roots rootMap = options.ignoreLiveness ? Roots() : nix::findRoots(*this, true);
|
2006-12-05 01:48:36 +01:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
foreach (Roots::iterator, i, rootMap) state.roots.insert(i->second);
|
2005-02-01 16:05:32 +01:00
|
|
|
|
|
2006-07-20 14:17:25 +02:00
|
|
|
|
/* Add additional roots returned by the program specified by the
|
|
|
|
|
NIX_ROOT_FINDER environment variable. This is typically used
|
|
|
|
|
to add running programs to the set of roots (to prevent them
|
|
|
|
|
from being garbage collected). */
|
2008-06-18 11:34:17 +02:00
|
|
|
|
if (!options.ignoreLiveness)
|
2011-08-31 23:11:50 +02:00
|
|
|
|
addAdditionalRoots(*this, state.roots);
|
2006-07-20 14:17:25 +02:00
|
|
|
|
|
2005-01-31 11:27:25 +01:00
|
|
|
|
/* Read the temporary roots. This acquires read locks on all
|
|
|
|
|
per-process temporary root files. So after this point no paths
|
|
|
|
|
can be added to the set of temporary roots. */
|
|
|
|
|
FDs fds;
|
2009-11-23 17:34:24 +01:00
|
|
|
|
readTempRoots(state.tempRoots, fds);
|
|
|
|
|
state.roots.insert(state.tempRoots.begin(), state.tempRoots.end());
|
2005-01-31 15:00:43 +01:00
|
|
|
|
|
|
|
|
|
/* After this point the set of roots or temporary roots cannot
|
|
|
|
|
increase, since we hold locks on everything. So everything
|
2009-11-23 17:34:24 +01:00
|
|
|
|
that is not reachable from `roots'. */
|
|
|
|
|
|
|
|
|
|
/* Now either delete all garbage paths, or just the specified
|
|
|
|
|
paths (for gcDeleteSpecific). */
|
|
|
|
|
|
|
|
|
|
if (options.action == GCOptions::gcDeleteSpecific) {
|
2008-09-17 12:02:55 +02:00
|
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
|
foreach (PathSet::iterator, i, options.pathsToDelete) {
|
2005-12-23 22:08:42 +01:00
|
|
|
|
assertStorePath(*i);
|
2009-11-23 17:34:24 +01:00
|
|
|
|
if (!tryToDelete(state, *i))
|
2008-09-17 12:02:55 +02:00
|
|
|
|
throw Error(format("cannot delete path `%1%' since it is still alive") % *i);
|
2005-12-23 22:08:42 +01:00
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-08-02 01:14:30 +02:00
|
|
|
|
} else if (options.maxFreed > 0) {
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2010-10-14 17:55:51 +02:00
|
|
|
|
if (shouldDelete(state.options.action))
|
2009-11-23 17:34:24 +01:00
|
|
|
|
printMsg(lvlError, format("deleting garbage..."));
|
|
|
|
|
else
|
|
|
|
|
printMsg(lvlError, format("determining live/dead paths..."));
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
try {
|
2011-12-22 16:55:53 +01:00
|
|
|
|
|
2012-07-31 01:55:41 +02:00
|
|
|
|
AutoCloseDir dir = opendir(settings.nixStore.c_str());
|
|
|
|
|
if (!dir) throw SysError(format("opening directory `%1%'") % settings.nixStore);
|
2011-12-22 16:55:53 +01:00
|
|
|
|
|
|
|
|
|
/* Read the store and immediately delete all paths that
|
|
|
|
|
aren't valid. When using --max-freed etc., deleting
|
|
|
|
|
invalid paths is preferred over deleting unreachable
|
|
|
|
|
paths, since unreachable paths could become reachable
|
|
|
|
|
again. We don't use readDirectory() here so that GCing
|
|
|
|
|
can start faster. */
|
|
|
|
|
Paths entries;
|
|
|
|
|
struct dirent * dirent;
|
|
|
|
|
while (errno = 0, dirent = readdir(dir)) {
|
|
|
|
|
checkInterrupt();
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
2012-07-31 01:55:41 +02:00
|
|
|
|
Path path = settings.nixStore + "/" + name;
|
2011-12-22 16:55:53 +01:00
|
|
|
|
if (isValidPath(path))
|
|
|
|
|
entries.push_back(path);
|
|
|
|
|
else
|
|
|
|
|
tryToDelete(state, path);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 01:55:41 +02:00
|
|
|
|
dir.close();
|
2011-12-22 16:55:53 +01:00
|
|
|
|
|
|
|
|
|
/* Now delete the unreachable valid paths. Randomise the
|
|
|
|
|
order in which we delete entries to make the collector
|
|
|
|
|
less biased towards deleting paths that come
|
|
|
|
|
alphabetically first (e.g. /nix/store/000...). This
|
|
|
|
|
matters when using --max-freed etc. */
|
|
|
|
|
vector<Path> entries_(entries.begin(), entries.end());
|
|
|
|
|
random_shuffle(entries_.begin(), entries_.end());
|
|
|
|
|
|
2009-11-24 10:53:18 +01:00
|
|
|
|
foreach (vector<Path>::iterator, i, entries_)
|
2011-12-22 17:27:03 +01:00
|
|
|
|
tryToDelete(state, *i);
|
2011-12-22 16:55:53 +01:00
|
|
|
|
|
2009-11-23 17:34:24 +01:00
|
|
|
|
} catch (GCLimitReached & e) {
|
|
|
|
|
}
|
2011-12-22 16:55:53 +01:00
|
|
|
|
}
|
2012-03-26 20:43:33 +02:00
|
|
|
|
|
|
|
|
|
/* Allow other processes to add to the store from here on. */
|
|
|
|
|
fdGCLock.close();
|
|
|
|
|
|
|
|
|
|
/* Delete the invalidated paths now that the lock has been
|
|
|
|
|
released. */
|
|
|
|
|
foreach (PathSet::iterator, i, state.invalidated)
|
|
|
|
|
deleteGarbage(state, *i);
|
2012-07-23 21:48:30 +02:00
|
|
|
|
|
|
|
|
|
/* Clean up the links directory. */
|
2012-09-13 00:49:35 +02:00
|
|
|
|
if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) {
|
|
|
|
|
printMsg(lvlError, format("deleting unused links..."));
|
|
|
|
|
removeUnusedLinks(state);
|
|
|
|
|
}
|
2004-08-25 13:43:49 +02:00
|
|
|
|
}
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
2008-09-17 12:02:55 +02:00
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
}
|