Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
#include "derivations.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
#include "globals.hh"
|
2006-11-30 17:43:04 +00:00
|
|
|
|
#include "local-store.hh"
|
2017-09-05 18:43:42 +00:00
|
|
|
|
#include "finally.hh"
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2008-09-17 10:02:55 +00:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include <queue>
|
2009-11-24 09:53:18 +00:00
|
|
|
|
#include <algorithm>
|
2016-07-20 18:00:36 +00:00
|
|
|
|
#include <regex>
|
2018-06-13 14:56:19 +00:00
|
|
|
|
#include <random>
|
2008-09-17 10:02:55 +00:00
|
|
|
|
|
2004-08-25 16:54:08 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2017-09-05 18:43:42 +00:00
|
|
|
|
#include <sys/statvfs.h>
|
2005-01-31 10:27:25 +00:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2004-08-25 16:54:08 +00:00
|
|
|
|
#include <unistd.h>
|
2016-07-20 18:00:36 +00:00
|
|
|
|
#include <climits>
|
2004-08-25 16:54:08 +00:00
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
2005-01-31 22:23:49 +00:00
|
|
|
|
static string gcLockName = "gc.lock";
|
2005-02-01 13:48:46 +00:00
|
|
|
|
static string gcRootsDir = "gcroots";
|
2005-01-31 22:23:49 +00: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. */
|
2018-10-04 13:03:03 +00:00
|
|
|
|
AutoCloseFD LocalStore::openGCLock(LockType lockType)
|
2005-01-31 22:01:55 +00:00
|
|
|
|
{
|
2005-01-31 22:23:49 +00:00
|
|
|
|
Path fnGCLock = (format("%1%/%2%")
|
2016-06-02 11:33:49 +00:00
|
|
|
|
% stateDir % gcLockName).str();
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
|
debug(format("acquiring global GC lock '%1%'") % fnGCLock);
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2016-06-09 14:15:58 +00:00
|
|
|
|
AutoCloseFD fdGCLock = open(fnGCLock.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0600);
|
2016-07-11 19:44:44 +00:00
|
|
|
|
if (!fdGCLock)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("opening global GC lock '%1%'") % fnGCLock);
|
2005-01-31 22:23:49 +00:00
|
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
|
if (!lockFile(fdGCLock.get(), lockType, false)) {
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printError(format("waiting for the big garbage collector lock..."));
|
2016-07-11 19:44:44 +00:00
|
|
|
|
lockFile(fdGCLock.get(), lockType, true);
|
2006-09-14 22:30:33 +00:00
|
|
|
|
}
|
2005-01-31 22:23:49 +00:00
|
|
|
|
|
|
|
|
|
/* !!! Restrict read permission on the GC root. Otherwise any
|
|
|
|
|
process that can open the file for reading can DoS the
|
|
|
|
|
collector. */
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2018-10-04 13:03:03 +00:00
|
|
|
|
return fdGCLock;
|
2005-01-31 22:01:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-02-27 22:17:53 +00:00
|
|
|
|
static void makeSymlink(const Path & link, const Path & target)
|
2005-02-01 13:48:46 +00:00
|
|
|
|
{
|
|
|
|
|
/* Create directories up to `gcRoot'. */
|
|
|
|
|
createDirs(dirOf(link));
|
|
|
|
|
|
2012-04-16 16:47:01 +00:00
|
|
|
|
/* Create the new symlink. */
|
|
|
|
|
Path tempLink = (format("%1%.tmp-%2%-%3%")
|
2018-03-06 23:34:44 +00:00
|
|
|
|
% link % getpid() % random()).str();
|
2014-02-27 22:17:53 +00:00
|
|
|
|
createSymlink(target, tempLink);
|
2012-04-16 16:47:01 +00:00
|
|
|
|
|
|
|
|
|
/* Atomically replace the old one. */
|
|
|
|
|
if (rename(tempLink.c_str(), link.c_str()) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("cannot rename '%1%' to '%2%'")
|
2012-04-16 16:47:01 +00:00
|
|
|
|
% tempLink % link);
|
2005-02-01 13:48:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-02 16:41:36 +00:00
|
|
|
|
void LocalStore::syncWithGC()
|
|
|
|
|
{
|
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-04 23:29:16 +00:00
|
|
|
|
void LocalStore::addIndirectRoot(const Path & path)
|
|
|
|
|
{
|
2020-03-28 23:22:10 +00:00
|
|
|
|
string hash = hashString(HashType::SHA1, path).to_string(Base::Base32, false);
|
2006-12-04 23:29:16 +00:00
|
|
|
|
Path realRoot = canonPath((format("%1%/%2%/auto/%3%")
|
2016-06-02 11:33:49 +00:00
|
|
|
|
% stateDir % gcRootsDir % hash).str());
|
2014-02-27 22:17:53 +00:00
|
|
|
|
makeSymlink(realRoot, path);
|
2006-12-04 23:29:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
Path LocalFSStore::addPermRoot(const StorePath & storePath,
|
2011-08-31 21:11:50 +00:00
|
|
|
|
const Path & _gcRoot, bool indirect, bool allowOutsideRootsDir)
|
2005-02-01 12:36:25 +00:00
|
|
|
|
{
|
|
|
|
|
Path gcRoot(canonPath(_gcRoot));
|
|
|
|
|
|
2007-06-11 11:36:22 +00: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 13:48:46 +00:00
|
|
|
|
if (indirect) {
|
2015-03-06 15:39:48 +00:00
|
|
|
|
/* Don't clobber the link if it already exists and doesn't
|
2012-04-16 16:47:01 +00:00
|
|
|
|
point to the Nix store. */
|
|
|
|
|
if (pathExists(gcRoot) && (!isLink(gcRoot) || !isInStore(readLink(gcRoot))))
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw Error(format("cannot create symlink '%1%'; already exists") % gcRoot);
|
2019-12-05 18:11:09 +00:00
|
|
|
|
makeSymlink(gcRoot, printStorePath(storePath));
|
2016-02-11 15:14:42 +00:00
|
|
|
|
addIndirectRoot(gcRoot);
|
2005-02-01 13:48:46 +00:00
|
|
|
|
}
|
2005-02-01 12:36:25 +00:00
|
|
|
|
|
2005-02-01 13:48:46 +00:00
|
|
|
|
else {
|
2006-09-14 22:30:33 +00:00
|
|
|
|
if (!allowOutsideRootsDir) {
|
2016-06-02 11:33:49 +00:00
|
|
|
|
Path rootsDir = canonPath((format("%1%/%2%") % stateDir % gcRootsDir).str());
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2006-09-14 22:30:33 +00:00
|
|
|
|
if (string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/")
|
|
|
|
|
throw Error(format(
|
2017-07-30 11:27:57 +00:00
|
|
|
|
"path '%1%' is not a valid garbage collector root; "
|
|
|
|
|
"it's not in the directory '%2%'")
|
2006-09-14 22:30:33 +00:00
|
|
|
|
% gcRoot % rootsDir);
|
|
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
if (baseNameOf(gcRoot) == std::string(storePath.to_string()))
|
2014-08-13 15:44:41 +00:00
|
|
|
|
writeFile(gcRoot, "");
|
|
|
|
|
else
|
2019-12-05 18:11:09 +00:00
|
|
|
|
makeSymlink(gcRoot, printStorePath(storePath));
|
2005-02-01 13:48:46 +00:00
|
|
|
|
}
|
2005-02-01 12:36:25 +00:00
|
|
|
|
|
2008-06-14 16:02:31 +00: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-30 23:55:41 +00:00
|
|
|
|
if (settings.checkRootReachability) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
auto roots = findRoots(false);
|
|
|
|
|
if (roots[storePath.clone()].count(gcRoot) == 0)
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printError(
|
2019-12-05 18:11:09 +00:00
|
|
|
|
"warning: '%1%' is not in a directory where the garbage collector looks for roots; "
|
|
|
|
|
"therefore, '%2%' might be removed by the garbage collector",
|
|
|
|
|
gcRoot, printStorePath(storePath));
|
2007-03-19 09:16:47 +00:00
|
|
|
|
}
|
2010-08-30 14:53:03 +00:00
|
|
|
|
|
2006-12-02 16:41:36 +00: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. */
|
2016-02-11 15:14:42 +00:00
|
|
|
|
syncWithGC();
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2005-02-01 12:36:25 +00:00
|
|
|
|
return gcRoot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
void LocalStore::addTempRoot(const StorePath & path)
|
2005-01-31 10:27:25 +00:00
|
|
|
|
{
|
2016-04-08 16:07:13 +00:00
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
|
/* Create the temporary roots file for this process. */
|
2016-07-11 19:44:44 +00:00
|
|
|
|
if (!state->fdTempRoots) {
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
|
|
while (1) {
|
2005-01-31 22:23:49 +00:00
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2017-09-05 18:39:57 +00:00
|
|
|
|
if (pathExists(fnTempRoots))
|
2006-06-20 17:48:10 +00:00
|
|
|
|
/* It *must* be stale, since there can be no two
|
|
|
|
|
processes with the same pid. */
|
2017-09-05 18:39:57 +00:00
|
|
|
|
unlink(fnTempRoots.c_str());
|
2006-06-20 17:48:10 +00:00
|
|
|
|
|
2017-09-05 18:39:57 +00:00
|
|
|
|
state->fdTempRoots = openLockFile(fnTempRoots, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
|
fdGCLock = -1;
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2017-09-05 18:39:57 +00:00
|
|
|
|
debug(format("acquiring read lock on '%1%'") % fnTempRoots);
|
2016-07-11 19:44:44 +00:00
|
|
|
|
lockFile(state->fdTempRoots.get(), ltRead, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
|
|
/* Check whether the garbage collector didn't get in our
|
|
|
|
|
way. */
|
|
|
|
|
struct stat st;
|
2016-07-11 19:44:44 +00:00
|
|
|
|
if (fstat(state->fdTempRoots.get(), &st) == -1)
|
2017-09-05 18:39:57 +00:00
|
|
|
|
throw SysError(format("statting '%1%'") % fnTempRoots);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
if (st.st_size == 0) break;
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2005-01-31 10:27:25 +00: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. */
|
2017-09-05 18:39:57 +00:00
|
|
|
|
debug(format("acquiring write lock on '%1%'") % fnTempRoots);
|
2016-07-11 19:44:44 +00:00
|
|
|
|
lockFile(state->fdTempRoots.get(), ltWrite, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
string s = printStorePath(path) + '\0';
|
2016-07-11 19:44:44 +00:00
|
|
|
|
writeFull(state->fdTempRoots.get(), s);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
|
|
/* Downgrade to a read lock. */
|
2017-09-05 18:39:57 +00:00
|
|
|
|
debug(format("downgrading to read lock on '%1%'") % fnTempRoots);
|
2016-07-11 19:44:44 +00:00
|
|
|
|
lockFile(state->fdTempRoots.get(), ltRead, true);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
|
static std::string censored = "{censored}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::findTempRoots(FDs & fds, Roots & tempRoots, bool censor)
|
2005-01-31 10:27:25 +00:00
|
|
|
|
{
|
|
|
|
|
/* Read the `temproots' directory for per-process temporary root
|
|
|
|
|
files. */
|
2017-09-14 13:02:52 +00:00
|
|
|
|
for (auto & i : readDirectory(tempRootsDir)) {
|
2020-04-09 15:05:29 +00:00
|
|
|
|
if (i.name[0] == '.') {
|
|
|
|
|
// Ignore hidden files. Some package managers (notably portage) create
|
|
|
|
|
// those to keep the directory alive.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-09-05 18:39:57 +00:00
|
|
|
|
Path path = tempRootsDir + "/" + i.name;
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2017-09-14 13:02:52 +00:00
|
|
|
|
pid_t pid = std::stoi(i.name);
|
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
|
debug(format("reading temporary root file '%1%'") % path);
|
2016-06-09 14:15:58 +00:00
|
|
|
|
FDPtr fd(new AutoCloseFD(open(path.c_str(), O_CLOEXEC | O_RDWR, 0666)));
|
2016-07-11 19:44:44 +00:00
|
|
|
|
if (!*fd) {
|
2005-01-31 10:27:25 +00:00
|
|
|
|
/* It's okay if the file has disappeared. */
|
|
|
|
|
if (errno == ENOENT) continue;
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("opening temporary roots file '%1%'") % path);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-06-20 17:48:10 +00:00
|
|
|
|
/* This should work, but doesn't, for some reason. */
|
|
|
|
|
//FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
|
|
|
|
|
//if (*fd == -1) continue;
|
|
|
|
|
|
2019-08-02 16:37:55 +00: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->get(), ltWrite, false)) {
|
|
|
|
|
printError(format("removing stale temporary roots file '%1%'") % path);
|
|
|
|
|
unlink(path.c_str());
|
|
|
|
|
writeFull(fd->get(), "d");
|
|
|
|
|
continue;
|
2017-09-05 18:39:57 +00:00
|
|
|
|
}
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2019-08-02 16:37:55 +00:00
|
|
|
|
/* 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->get(), ltRead, true);
|
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
|
/* Read the entire file. */
|
2016-07-11 19:44:44 +00:00
|
|
|
|
string contents = readFile(fd->get());
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
|
|
/* Extract the roots. */
|
2006-05-11 02:19:43 +00:00
|
|
|
|
string::size_type pos = 0, end;
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
|
|
while ((end = contents.find((char) 0, pos)) != string::npos) {
|
|
|
|
|
Path root(contents, pos, end - pos);
|
2017-09-14 13:02:52 +00:00
|
|
|
|
debug("got temporary root '%s'", root);
|
2019-12-05 18:11:09 +00:00
|
|
|
|
tempRoots[parseStorePath(root)].emplace(censor ? censored : fmt("{temp:%d}", pid));
|
2005-01-31 10:27:25 +00:00
|
|
|
|
pos = end + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fds.push_back(fd); /* keep open */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
void LocalStore::findRoots(const Path & path, unsigned char type, Roots & roots)
|
2005-02-01 15:05:32 +00:00
|
|
|
|
{
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
auto foundRoot = [&](const Path & path, const Path & target) {
|
2020-02-28 17:07:10 +00:00
|
|
|
|
auto storePath = maybeParseStorePath(toStorePath(target));
|
|
|
|
|
if (storePath && isValidPath(*storePath))
|
|
|
|
|
roots[std::move(*storePath)].emplace(path);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
else
|
2020-02-28 17:07:10 +00:00
|
|
|
|
printInfo("skipping invalid root from '%1%' to '%2%'", path, target);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
};
|
2006-12-05 01:31:45 +00:00
|
|
|
|
|
2013-07-12 12:01:25 +00:00
|
|
|
|
try {
|
|
|
|
|
|
2014-10-03 20:37:51 +00:00
|
|
|
|
if (type == DT_UNKNOWN)
|
|
|
|
|
type = getFileType(path);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
|
if (type == DT_DIR) {
|
2014-08-01 14:37:47 +00:00
|
|
|
|
for (auto & i : readDirectory(path))
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
findRoots(path + "/" + i.name, i.type, roots);
|
2005-02-01 15:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
|
else if (type == DT_LNK) {
|
2013-07-12 12:01:25 +00:00
|
|
|
|
Path target = readLink(path);
|
|
|
|
|
if (isInStore(target))
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
foundRoot(path, target);
|
2013-07-12 12:01:25 +00:00
|
|
|
|
|
|
|
|
|
/* Handle indirect roots. */
|
|
|
|
|
else {
|
|
|
|
|
target = absPath(target, dirOf(path));
|
|
|
|
|
if (!pathExists(target)) {
|
2016-06-02 11:33:49 +00:00
|
|
|
|
if (isInDir(path, stateDir + "/" + gcRootsDir + "/auto")) {
|
2017-07-30 11:27:57 +00:00
|
|
|
|
printInfo(format("removing stale link from '%1%' to '%2%'") % path % target);
|
2013-07-12 12:01:25 +00:00
|
|
|
|
unlink(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
struct stat st2 = lstat(target);
|
|
|
|
|
if (!S_ISLNK(st2.st_mode)) return;
|
|
|
|
|
Path target2 = readLink(target);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
if (isInStore(target2)) foundRoot(target, target2);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
}
|
2005-02-01 15:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-12-05 01:31:45 +00:00
|
|
|
|
|
2014-08-01 15:20:25 +00:00
|
|
|
|
else if (type == DT_REG) {
|
2020-02-28 17:07:10 +00:00
|
|
|
|
auto storePath = maybeParseStorePath(storeDir + "/" + std::string(baseNameOf(path)));
|
|
|
|
|
if (storePath && isValidPath(*storePath))
|
|
|
|
|
roots[std::move(*storePath)].emplace(path);
|
2014-08-01 14:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-05 01:31:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (SysError & e) {
|
|
|
|
|
/* We only ignore permanent failures. */
|
|
|
|
|
if (e.errNo == EACCES || e.errNo == ENOENT || e.errNo == ENOTDIR)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printInfo("cannot read potential root '%1%'", path);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
else
|
|
|
|
|
throw;
|
2005-02-01 15:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
|
void LocalStore::findRootsNoTemp(Roots & roots, bool censor)
|
2006-12-05 00:34:42 +00:00
|
|
|
|
{
|
2018-02-01 09:39:16 +00:00
|
|
|
|
/* Process direct roots in {gcroots,profiles}. */
|
2016-06-02 11:33:49 +00:00
|
|
|
|
findRoots(stateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
|
|
|
|
|
findRoots(stateDir + "/profiles", DT_UNKNOWN, roots);
|
2006-12-05 01:31:45 +00:00
|
|
|
|
|
2019-05-01 02:43:24 +00:00
|
|
|
|
/* Add additional roots returned by different platforms-specific
|
|
|
|
|
heuristics. This is typically used to add running programs to
|
|
|
|
|
the set of roots (to prevent them from being garbage collected). */
|
2019-03-14 12:50:07 +00:00
|
|
|
|
findRuntimeRoots(roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
|
Roots LocalStore::findRoots(bool censor)
|
2017-09-14 12:38:36 +00:00
|
|
|
|
{
|
2019-03-09 23:37:52 +00:00
|
|
|
|
Roots roots;
|
2019-03-14 12:50:07 +00:00
|
|
|
|
findRootsNoTemp(roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
|
|
|
|
|
|
FDs fds;
|
2019-03-14 12:50:07 +00:00
|
|
|
|
findTempRoots(fds, roots, censor);
|
2017-09-14 12:38:36 +00:00
|
|
|
|
|
2013-07-12 12:01:25 +00:00
|
|
|
|
return roots;
|
2006-12-05 00:34:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
typedef std::unordered_map<Path, std::unordered_set<std::string>> UncheckedRoots;
|
|
|
|
|
|
|
|
|
|
static void readProcLink(const string & file, UncheckedRoots & roots)
|
2006-07-20 12:17:25 +00:00
|
|
|
|
{
|
2016-07-20 18:00:36 +00:00
|
|
|
|
/* 64 is the starting buffer size gnu readlink uses... */
|
|
|
|
|
auto bufsiz = ssize_t{64};
|
|
|
|
|
try_again:
|
|
|
|
|
char buf[bufsiz];
|
|
|
|
|
auto res = readlink(file.c_str(), buf, bufsiz);
|
|
|
|
|
if (res == -1) {
|
2018-06-16 16:04:09 +00:00
|
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 18:00:36 +00:00
|
|
|
|
return;
|
|
|
|
|
throw SysError("reading symlink");
|
|
|
|
|
}
|
|
|
|
|
if (res == bufsiz) {
|
|
|
|
|
if (SSIZE_MAX / 2 < bufsiz)
|
|
|
|
|
throw Error("stupidly long symlink");
|
|
|
|
|
bufsiz *= 2;
|
|
|
|
|
goto try_again;
|
|
|
|
|
}
|
|
|
|
|
if (res > 0 && buf[0] == '/')
|
2019-02-28 23:54:52 +00:00
|
|
|
|
roots[std::string(static_cast<char *>(buf), res)]
|
2019-03-14 12:27:16 +00:00
|
|
|
|
.emplace(file);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
}
|
2006-07-20 12:17:25 +00:00
|
|
|
|
|
2016-07-20 18:00:36 +00:00
|
|
|
|
static string quoteRegexChars(const string & raw)
|
|
|
|
|
{
|
|
|
|
|
static auto specialRegex = std::regex(R"([.^$\\*+?()\[\]{}|])");
|
|
|
|
|
return std::regex_replace(raw, specialRegex, R"(\$&)");
|
|
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
static void readFileRoots(const char * path, UncheckedRoots & roots)
|
2016-07-20 18:00:36 +00:00
|
|
|
|
{
|
|
|
|
|
try {
|
2019-02-28 23:54:52 +00:00
|
|
|
|
roots[readFile(path)].emplace(path);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
} catch (SysError & e) {
|
|
|
|
|
if (e.errNo != ENOENT && e.errNo != EACCES)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-07-20 12:17:25 +00:00
|
|
|
|
|
2019-03-14 12:50:07 +00:00
|
|
|
|
void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
|
2016-07-20 18:00:36 +00:00
|
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
|
UncheckedRoots unchecked;
|
2019-02-27 22:32:12 +00:00
|
|
|
|
|
2016-07-20 18:00:36 +00:00
|
|
|
|
auto procDir = AutoCloseDir{opendir("/proc")};
|
|
|
|
|
if (procDir) {
|
|
|
|
|
struct dirent * ent;
|
|
|
|
|
auto digitsRegex = std::regex(R"(^\d+$)");
|
|
|
|
|
auto mapRegex = std::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)");
|
|
|
|
|
auto storePathRegex = std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
|
2017-01-16 21:39:27 +00:00
|
|
|
|
while (errno = 0, ent = readdir(procDir.get())) {
|
2016-07-20 18:00:36 +00:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
if (std::regex_match(ent->d_name, digitsRegex)) {
|
2019-03-14 12:27:16 +00:00
|
|
|
|
readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked);
|
|
|
|
|
readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
|
2019-03-14 12:27:16 +00:00
|
|
|
|
auto fdStr = fmt("/proc/%s/fd", ent->d_name);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
auto fdDir = AutoCloseDir(opendir(fdStr.c_str()));
|
|
|
|
|
if (!fdDir) {
|
|
|
|
|
if (errno == ENOENT || errno == EACCES)
|
|
|
|
|
continue;
|
|
|
|
|
throw SysError(format("opening %1%") % fdStr);
|
|
|
|
|
}
|
|
|
|
|
struct dirent * fd_ent;
|
2017-01-16 21:39:27 +00:00
|
|
|
|
while (errno = 0, fd_ent = readdir(fdDir.get())) {
|
2019-03-14 12:27:16 +00:00
|
|
|
|
if (fd_ent->d_name[0] != '.')
|
|
|
|
|
readProcLink(fmt("%s/%s", fdStr, fd_ent->d_name), unchecked);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
}
|
2018-06-11 13:59:32 +00:00
|
|
|
|
if (errno) {
|
|
|
|
|
if (errno == ESRCH)
|
|
|
|
|
continue;
|
2016-07-20 18:00:36 +00:00
|
|
|
|
throw SysError(format("iterating /proc/%1%/fd") % ent->d_name);
|
|
|
|
|
}
|
2018-06-11 13:59:32 +00:00
|
|
|
|
fdDir.reset();
|
2016-07-20 18:00:36 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2019-03-14 12:27:16 +00:00
|
|
|
|
auto mapFile = fmt("/proc/%s/maps", ent->d_name);
|
2020-04-29 16:42:19 +00:00
|
|
|
|
auto mapLines = tokenizeString<std::vector<string>>(readFile(mapFile), "\n");
|
2019-03-14 12:27:16 +00:00
|
|
|
|
for (const auto & line : mapLines) {
|
2018-06-11 13:59:32 +00:00
|
|
|
|
auto match = std::smatch{};
|
|
|
|
|
if (std::regex_match(line, match, mapRegex))
|
2019-03-14 12:27:16 +00:00
|
|
|
|
unchecked[match[1]].emplace(mapFile);
|
2018-06-11 13:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 12:27:16 +00:00
|
|
|
|
auto envFile = fmt("/proc/%s/environ", ent->d_name);
|
2020-04-29 16:42:19 +00:00
|
|
|
|
auto envString = readFile(envFile);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
auto env_end = std::sregex_iterator{};
|
|
|
|
|
for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
|
2019-03-14 12:27:16 +00:00
|
|
|
|
unchecked[i->str()].emplace(envFile);
|
2016-07-20 18:00:36 +00:00
|
|
|
|
} catch (SysError & e) {
|
2018-06-11 13:59:32 +00:00
|
|
|
|
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
|
2016-07-20 18:00:36 +00:00
|
|
|
|
continue;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (errno)
|
|
|
|
|
throw SysError("iterating /proc");
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 17:11:45 +00:00
|
|
|
|
#if !defined(__linux__)
|
2019-07-30 09:29:03 +00:00
|
|
|
|
// lsof is really slow on OS X. This actually causes the gc-concurrent.sh test to fail.
|
|
|
|
|
// See: https://github.com/NixOS/nix/issues/3011
|
|
|
|
|
// Because of this we disable lsof when running the tests.
|
2020-01-04 23:41:18 +00:00
|
|
|
|
if (getEnv("_NIX_TEST_NO_LSOF") != "1") {
|
2019-07-30 09:29:03 +00:00
|
|
|
|
try {
|
|
|
|
|
std::regex lsofRegex(R"(^n(/.*)$)");
|
|
|
|
|
auto lsofLines =
|
|
|
|
|
tokenizeString<std::vector<string>>(runProgram(LSOF, true, { "-n", "-w", "-F", "n" }), "\n");
|
|
|
|
|
for (const auto & line : lsofLines) {
|
|
|
|
|
std::smatch match;
|
|
|
|
|
if (std::regex_match(line, match, lsofRegex))
|
|
|
|
|
unchecked[match[1]].emplace("{lsof}");
|
|
|
|
|
}
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
/* lsof not installed, lsof failed */
|
2016-07-20 18:00:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 17:11:45 +00:00
|
|
|
|
#endif
|
2006-07-20 12:17:25 +00:00
|
|
|
|
|
2017-04-20 17:11:45 +00:00
|
|
|
|
#if defined(__linux__)
|
2019-02-27 22:32:12 +00:00
|
|
|
|
readFileRoots("/proc/sys/kernel/modprobe", unchecked);
|
|
|
|
|
readFileRoots("/proc/sys/kernel/fbsplash", unchecked);
|
|
|
|
|
readFileRoots("/proc/sys/kernel/poweroff_cmd", unchecked);
|
2017-04-20 17:11:45 +00:00
|
|
|
|
#endif
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2019-02-28 23:54:52 +00:00
|
|
|
|
for (auto & [target, links] : unchecked) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
if (!isInStore(target)) continue;
|
|
|
|
|
Path pathS = toStorePath(target);
|
|
|
|
|
if (!isStorePath(pathS)) continue;
|
|
|
|
|
auto path = parseStorePath(pathS);
|
|
|
|
|
if (!isValidPath(path)) continue;
|
|
|
|
|
debug("got additional root '%1%'", pathS);
|
|
|
|
|
if (censor)
|
|
|
|
|
roots[path.clone()].insert(censored);
|
|
|
|
|
else
|
|
|
|
|
roots[path.clone()].insert(links.begin(), links.end());
|
2019-02-27 22:32:12 +00:00
|
|
|
|
}
|
2006-07-20 12:17:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 14:20:16 +00:00
|
|
|
|
struct GCLimitReached { };
|
|
|
|
|
|
|
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
|
struct LocalStore::GCState
|
2008-06-13 18:25:24 +00:00
|
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
|
const GCOptions & options;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
GCResults & results;
|
2019-12-05 18:11:09 +00:00
|
|
|
|
StorePathSet roots;
|
|
|
|
|
StorePathSet tempRoots;
|
|
|
|
|
StorePathSet dead;
|
|
|
|
|
StorePathSet alive;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
bool gcKeepOutputs;
|
|
|
|
|
bool gcKeepDerivations;
|
2012-03-26 18:43:33 +00:00
|
|
|
|
unsigned long long bytesInvalidated;
|
2015-06-30 19:41:26 +00:00
|
|
|
|
bool moveToTrash = true;
|
2012-12-20 16:32:15 +00:00
|
|
|
|
bool shouldDelete;
|
2019-12-05 18:11:09 +00:00
|
|
|
|
GCState(const GCOptions & options, GCResults & results)
|
|
|
|
|
: options(options), results(results), bytesInvalidated(0) { }
|
2009-11-23 16:34:24 +00:00
|
|
|
|
};
|
2008-06-18 14:20:16 +00:00
|
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
|
|
|
|
bool LocalStore::isActiveTempFile(const GCState & state,
|
|
|
|
|
const Path & path, const string & suffix)
|
2008-09-17 12:54:07 +00:00
|
|
|
|
{
|
2009-11-23 16:34:24 +00:00
|
|
|
|
return hasSuffix(path, suffix)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
&& state.tempRoots.count(parseStorePath(string(path, 0, path.size() - suffix.size())));
|
2009-11-23 16:34:24 +00:00
|
|
|
|
}
|
2008-09-17 12:54:07 +00:00
|
|
|
|
|
2012-03-26 18:00:02 +00:00
|
|
|
|
|
2012-03-26 18:43:33 +00:00
|
|
|
|
void LocalStore::deleteGarbage(GCState & state, const Path & path)
|
|
|
|
|
{
|
2012-08-02 02:34:46 +00:00
|
|
|
|
unsigned long long bytesFreed;
|
2013-11-14 10:57:37 +00:00
|
|
|
|
deletePath(path, bytesFreed);
|
2012-03-26 18:43:33 +00:00
|
|
|
|
state.results.bytesFreed += bytesFreed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
void LocalStore::deletePathRecursive(GCState & state, const Path & path)
|
2009-11-23 16:34:24 +00:00
|
|
|
|
{
|
2010-10-14 15:55:51 +00:00
|
|
|
|
checkInterrupt();
|
2012-03-26 18:56:30 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
|
2020-02-28 17:07:10 +00:00
|
|
|
|
auto storePath = maybeParseStorePath(path);
|
|
|
|
|
if (storePath && isValidPath(*storePath)) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
StorePathSet referrers;
|
2020-02-28 17:07:10 +00:00
|
|
|
|
queryReferrers(*storePath, referrers);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
for (auto & i : referrers)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
if (printStorePath(i) != path) deletePathRecursive(state, printStorePath(i));
|
2020-02-28 17:07:10 +00:00
|
|
|
|
size = queryPathInfo(*storePath)->narSize;
|
|
|
|
|
invalidatePathChecked(*storePath);
|
2012-12-20 16:32:15 +00:00
|
|
|
|
}
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
Path realPath = realStoreDir + "/" + std::string(baseNameOf(path));
|
2016-06-02 13:08:18 +00:00
|
|
|
|
|
2012-03-26 18:56:30 +00:00
|
|
|
|
struct stat st;
|
2016-06-02 13:08:18 +00:00
|
|
|
|
if (lstat(realPath.c_str(), &st)) {
|
2012-12-20 16:32:15 +00:00
|
|
|
|
if (errno == ENOENT) return;
|
2016-06-02 13:08:18 +00:00
|
|
|
|
throw SysError(format("getting status of %1%") % realPath);
|
2012-03-26 18:56:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
|
printInfo(format("deleting '%1%'") % path);
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2013-01-04 14:17:19 +00:00
|
|
|
|
state.results.paths.insert(path);
|
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
/* If the path is not a regular file or symlink, move it to the
|
|
|
|
|
trash directory. The move 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. */
|
2015-06-30 19:41:26 +00:00
|
|
|
|
if (state.moveToTrash && S_ISDIR(st.st_mode)) {
|
2012-12-20 16:32:15 +00:00
|
|
|
|
// Estimate the amount freed using the narSize field. FIXME:
|
|
|
|
|
// if the path was not valid, need to determine the actual
|
|
|
|
|
// size.
|
2015-06-30 19:41:26 +00:00
|
|
|
|
try {
|
2016-06-02 13:08:18 +00:00
|
|
|
|
if (chmod(realPath.c_str(), st.st_mode | S_IWUSR) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("making '%1%' writable") % realPath);
|
2019-12-05 18:11:09 +00:00
|
|
|
|
Path tmp = trashDir + "/" + std::string(baseNameOf(path));
|
2016-06-02 13:08:18 +00:00
|
|
|
|
if (rename(realPath.c_str(), tmp.c_str()))
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("unable to rename '%1%' to '%2%'") % realPath % tmp);
|
2015-06-30 19:41:26 +00:00
|
|
|
|
state.bytesInvalidated += size;
|
|
|
|
|
} catch (SysError & e) {
|
|
|
|
|
if (e.errNo == ENOSPC) {
|
2017-07-30 11:27:57 +00:00
|
|
|
|
printInfo(format("note: can't create move '%1%': %2%") % realPath % e.msg());
|
2016-06-02 13:08:18 +00:00
|
|
|
|
deleteGarbage(state, realPath);
|
2015-06-30 19:41:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-20 16:32:15 +00:00
|
|
|
|
} else
|
2016-06-02 13:08:18 +00:00
|
|
|
|
deleteGarbage(state, realPath);
|
2012-12-20 16:32:15 +00:00
|
|
|
|
|
|
|
|
|
if (state.results.bytesFreed + state.bytesInvalidated > state.options.maxFreed) {
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printInfo(format("deleted or invalidated more than %1% bytes; stopping") % state.options.maxFreed);
|
2012-12-20 16:32:15 +00:00
|
|
|
|
throw GCLimitReached();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
bool LocalStore::canReachRoot(GCState & state, StorePathSet & visited, const StorePath & path)
|
2012-12-20 16:32:15 +00:00
|
|
|
|
{
|
2017-09-14 12:38:36 +00:00
|
|
|
|
if (visited.count(path)) return false;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2017-09-14 12:38:36 +00:00
|
|
|
|
if (state.alive.count(path)) return true;
|
2008-09-17 12:54:07 +00:00
|
|
|
|
|
2017-09-14 12:38:36 +00:00
|
|
|
|
if (state.dead.count(path)) return false;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2017-09-14 12:38:36 +00:00
|
|
|
|
if (state.roots.count(path)) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
debug("cannot delete '%1%' because it's a root", printStorePath(path));
|
|
|
|
|
state.alive.insert(path.clone());
|
2012-12-20 16:32:15 +00:00
|
|
|
|
return true;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
}
|
2008-12-12 17:03:18 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
visited.insert(path.clone());
|
2012-03-26 18:43:33 +00:00
|
|
|
|
|
2020-02-28 17:07:10 +00:00
|
|
|
|
if (!isValidPath(path)) return false;
|
2012-09-12 22:49:35 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
StorePathSet incoming;
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
/* Don't delete this path if any of its referrers are alive. */
|
|
|
|
|
queryReferrers(path, incoming);
|
2012-09-12 22:49:35 +00:00
|
|
|
|
|
2017-08-31 12:28:25 +00:00
|
|
|
|
/* If keep-derivations is set and this is a derivation, then
|
2012-12-20 16:32:15 +00:00
|
|
|
|
don't delete the derivation if any of the outputs are alive. */
|
2019-12-05 18:11:09 +00:00
|
|
|
|
if (state.gcKeepDerivations && path.isDerivation()) {
|
|
|
|
|
for (auto & i : queryDerivationOutputs(path))
|
2016-04-19 16:50:15 +00:00
|
|
|
|
if (isValidPath(i) && queryPathInfo(i)->deriver == path)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
incoming.insert(i.clone());
|
2012-12-20 16:32:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 12:28:25 +00:00
|
|
|
|
/* If keep-outputs is set, then don't delete this path if there
|
2012-12-20 16:32:15 +00:00
|
|
|
|
are derivers of this path that are not garbage. */
|
|
|
|
|
if (state.gcKeepOutputs) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
auto derivers = queryValidDerivers(path);
|
2015-07-17 17:24:28 +00:00
|
|
|
|
for (auto & i : derivers)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
incoming.insert(i.clone());
|
2012-12-20 16:32:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
|
for (auto & i : incoming)
|
|
|
|
|
if (i != path)
|
|
|
|
|
if (canReachRoot(state, visited, i)) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
state.alive.insert(path.clone());
|
2012-12-20 16:32:15 +00:00
|
|
|
|
return true;
|
2012-09-12 22:49:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-09-12 22:49:35 +00:00
|
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
void LocalStore::tryToDelete(GCState & state, const Path & path)
|
|
|
|
|
{
|
|
|
|
|
checkInterrupt();
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
auto realPath = realStoreDir + "/" + std::string(baseNameOf(path));
|
2016-06-02 13:08:18 +00:00
|
|
|
|
if (realPath == linksDir || realPath == trashDir) return;
|
2012-12-20 16:32:15 +00:00
|
|
|
|
|
2020-03-28 23:22:10 +00:00
|
|
|
|
//Activity act(*logger, Verbosity::Debug, format("considering whether to delete '%1%'") % path);
|
2012-12-20 16:32:15 +00:00
|
|
|
|
|
2020-02-28 17:07:10 +00:00
|
|
|
|
auto storePath = maybeParseStorePath(path);
|
|
|
|
|
|
|
|
|
|
if (!storePath || !isValidPath(*storePath)) {
|
2012-12-20 16:32:15 +00:00
|
|
|
|
/* A lock file belonging to a path that we're building right
|
|
|
|
|
now isn't garbage. */
|
|
|
|
|
if (isActiveTempFile(state, path, ".lock")) return;
|
|
|
|
|
|
|
|
|
|
/* Don't delete .chroot directories for derivations that are
|
|
|
|
|
currently being built. */
|
|
|
|
|
if (isActiveTempFile(state, path, ".chroot")) return;
|
2016-12-08 20:38:58 +00:00
|
|
|
|
|
|
|
|
|
/* Don't delete .check directories for derivations that are
|
|
|
|
|
currently being built, because we may need to run
|
|
|
|
|
diff-hook. */
|
|
|
|
|
if (isActiveTempFile(state, path, ".check")) return;
|
2012-12-20 16:32:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
StorePathSet visited;
|
2012-12-20 16:32:15 +00:00
|
|
|
|
|
2020-02-28 17:07:10 +00:00
|
|
|
|
if (storePath && canReachRoot(state, visited, *storePath)) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
debug("cannot delete '%s' because it's still reachable", path);
|
2012-12-20 16:32:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* No path we visited was a root, so everything is garbage.
|
2016-11-25 23:37:43 +00:00
|
|
|
|
But we only delete ‘path’ and its referrers here so that
|
|
|
|
|
‘nix-store --delete’ doesn't have the unexpected effect of
|
2012-12-20 16:32:15 +00:00
|
|
|
|
recursing into derivations and outputs. */
|
2019-12-05 18:11:09 +00:00
|
|
|
|
for (auto & i : visited)
|
|
|
|
|
state.dead.insert(i.clone());
|
2012-12-20 16:32:15 +00:00
|
|
|
|
if (state.shouldDelete)
|
|
|
|
|
deletePathRecursive(state, path);
|
2012-09-12 22:49:35 +00:00
|
|
|
|
}
|
2008-12-12 17:03:18 +00:00
|
|
|
|
}
|
2009-11-24 09:53:18 +00:00
|
|
|
|
|
2008-12-12 17:03:18 +00:00
|
|
|
|
|
2012-07-23 19:48:30 +00: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 02:43:03 +00:00
|
|
|
|
void LocalStore::removeUnusedLinks(const GCState & state)
|
2012-07-23 19:48:30 +00:00
|
|
|
|
{
|
2017-01-16 21:39:27 +00:00
|
|
|
|
AutoCloseDir dir(opendir(linksDir.c_str()));
|
2017-07-30 11:27:57 +00:00
|
|
|
|
if (!dir) throw SysError(format("opening directory '%1%'") % linksDir);
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
2012-08-06 01:45:27 +00:00
|
|
|
|
long long actualSize = 0, unsharedSize = 0;
|
2012-08-01 23:01:50 +00:00
|
|
|
|
|
2012-07-23 19:48:30 +00:00
|
|
|
|
struct dirent * dirent;
|
2017-01-16 21:39:27 +00:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) {
|
2012-07-23 19:48:30 +00:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
|
Path path = linksDir + "/" + name;
|
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("statting '%1%'") % path);
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
2012-08-01 23:01:50 +00:00
|
|
|
|
if (st.st_nlink != 1) {
|
2019-08-29 12:49:58 +00:00
|
|
|
|
actualSize += st.st_size;
|
|
|
|
|
unsharedSize += (st.st_nlink - 1) * st.st_size;
|
2012-08-01 23:01:50 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
2020-03-28 23:22:10 +00:00
|
|
|
|
printMsg(Verbosity::Talkative, format("deleting unused link '%1%'") % path);
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
|
|
|
|
if (unlink(path.c_str()) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("deleting '%1%'") % path);
|
2012-08-02 02:43:03 +00:00
|
|
|
|
|
2019-08-29 12:49:58 +00:00
|
|
|
|
state.results.bytesFreed += st.st_size;
|
2012-07-23 19:48:30 +00:00
|
|
|
|
}
|
2012-08-01 23:01:50 +00:00
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (stat(linksDir.c_str(), &st) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
|
throw SysError(format("statting '%1%'") % linksDir);
|
2012-08-06 01:45:27 +00:00
|
|
|
|
long long overhead = st.st_blocks * 512ULL;
|
2012-08-01 23:01:50 +00:00
|
|
|
|
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printInfo(format("note: currently hard linking saves %.2f MiB")
|
2012-08-01 23:01:50 +00:00
|
|
|
|
% ((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
|
2012-07-23 19:48:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 09:34:17 +00:00
|
|
|
|
void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
|
2005-01-27 15:21:29 +00:00
|
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
|
GCState state(options, results);
|
2012-07-30 23:55:41 +00:00
|
|
|
|
state.gcKeepOutputs = settings.gcKeepOutputs;
|
|
|
|
|
state.gcKeepDerivations = settings.gcKeepDerivations;
|
2010-03-08 21:31:42 +00:00
|
|
|
|
|
|
|
|
|
/* Using `--ignore-liveness' with `--delete' can have unintended
|
2017-08-31 12:28:25 +00:00
|
|
|
|
consequences if `keep-outputs' or `keep-derivations' are true
|
|
|
|
|
(the garbage collector will recurse into deleting the outputs
|
|
|
|
|
or derivers, respectively). So disable them. */
|
2010-03-08 21:31:42 +00:00
|
|
|
|
if (options.action == GCOptions::gcDeleteSpecific && options.ignoreLiveness) {
|
|
|
|
|
state.gcKeepOutputs = false;
|
|
|
|
|
state.gcKeepDerivations = false;
|
|
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
state.shouldDelete = options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific;
|
|
|
|
|
|
2016-02-24 16:44:12 +00:00
|
|
|
|
if (state.shouldDelete)
|
2016-02-24 16:33:53 +00:00
|
|
|
|
deletePath(reservedPath);
|
|
|
|
|
|
2005-01-31 22:23:49 +00:00
|
|
|
|
/* Acquire the global GC root. This prevents
|
2005-01-31 10:27:25 +00:00
|
|
|
|
a) New roots from being added.
|
|
|
|
|
b) Processes from creating new temporary root files. */
|
2005-01-31 22:23:49 +00:00
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltWrite);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
2005-02-01 15:05:32 +00:00
|
|
|
|
/* Find the roots. Since we've grabbed the GC lock, the set of
|
|
|
|
|
permanent roots cannot increase now. */
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printError("finding garbage collector roots...");
|
2019-03-09 23:37:52 +00:00
|
|
|
|
Roots rootMap;
|
|
|
|
|
if (!options.ignoreLiveness)
|
2019-03-14 12:50:07 +00:00
|
|
|
|
findRootsNoTemp(rootMap, true);
|
2006-12-05 00:48:36 +00:00
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
|
for (auto & i : rootMap) state.roots.insert(i.first.clone());
|
2005-02-01 15:05:32 +00:00
|
|
|
|
|
2005-01-31 10:27:25 +00: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;
|
2019-03-09 23:37:52 +00:00
|
|
|
|
Roots tempRoots;
|
2019-03-14 12:50:07 +00:00
|
|
|
|
findTempRoots(fds, tempRoots, true);
|
2019-12-05 18:11:09 +00:00
|
|
|
|
for (auto & root : tempRoots) {
|
|
|
|
|
state.tempRoots.insert(root.first.clone());
|
|
|
|
|
state.roots.insert(root.first.clone());
|
|
|
|
|
}
|
2005-01-31 14:00:43 +00:00
|
|
|
|
|
|
|
|
|
/* After this point the set of roots or temporary roots cannot
|
|
|
|
|
increase, since we hold locks on everything. So everything
|
2014-08-01 14:37:47 +00:00
|
|
|
|
that is not reachable from `roots' is garbage. */
|
2009-11-23 16:34:24 +00:00
|
|
|
|
|
2013-03-08 00:24:59 +00:00
|
|
|
|
if (state.shouldDelete) {
|
2016-06-02 13:08:18 +00:00
|
|
|
|
if (pathExists(trashDir)) deleteGarbage(state, trashDir);
|
2015-06-30 19:41:26 +00:00
|
|
|
|
try {
|
2016-06-02 13:08:18 +00:00
|
|
|
|
createDirs(trashDir);
|
2015-06-30 19:41:26 +00:00
|
|
|
|
} catch (SysError & e) {
|
|
|
|
|
if (e.errNo == ENOSPC) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printInfo("note: can't create trash directory: %s", e.msg());
|
2015-06-30 19:41:26 +00:00
|
|
|
|
state.moveToTrash = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-08 00:24:59 +00:00
|
|
|
|
}
|
2012-12-20 16:32:15 +00:00
|
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
|
/* Now either delete all garbage paths, or just the specified
|
|
|
|
|
paths (for gcDeleteSpecific). */
|
|
|
|
|
|
|
|
|
|
if (options.action == GCOptions::gcDeleteSpecific) {
|
2008-09-17 10:02:55 +00:00
|
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
|
for (auto & i : options.pathsToDelete) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
tryToDelete(state, printStorePath(i));
|
2015-07-17 17:24:28 +00:00
|
|
|
|
if (state.dead.find(i) == state.dead.end())
|
2019-12-05 18:11:09 +00:00
|
|
|
|
throw Error(
|
2018-08-08 19:21:21 +00:00
|
|
|
|
"cannot delete path '%1%' since it is still alive. "
|
|
|
|
|
"To find out why use: "
|
2019-12-05 18:11:09 +00:00
|
|
|
|
"nix-store --query --roots",
|
|
|
|
|
printStorePath(i));
|
2005-12-23 21:08:42 +00:00
|
|
|
|
}
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2012-08-01 23:14:30 +00:00
|
|
|
|
} else if (options.maxFreed > 0) {
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
if (state.shouldDelete)
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printError("deleting garbage...");
|
2009-11-23 16:34:24 +00:00
|
|
|
|
else
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printError("determining live/dead paths...");
|
2012-07-30 23:55:41 +00:00
|
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
|
try {
|
2011-12-22 15:55:53 +00:00
|
|
|
|
|
2017-01-16 21:39:27 +00:00
|
|
|
|
AutoCloseDir dir(opendir(realStoreDir.c_str()));
|
2017-07-30 11:27:57 +00:00
|
|
|
|
if (!dir) throw SysError(format("opening directory '%1%'") % realStoreDir);
|
2011-12-22 15:55:53 +00: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;
|
2017-01-16 21:39:27 +00:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) {
|
2011-12-22 15:55:53 +00:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
2016-06-01 12:49:12 +00:00
|
|
|
|
Path path = storeDir + "/" + name;
|
2020-02-28 17:07:10 +00:00
|
|
|
|
auto storePath = maybeParseStorePath(path);
|
|
|
|
|
if (storePath && isValidPath(*storePath))
|
2011-12-22 15:55:53 +00:00
|
|
|
|
entries.push_back(path);
|
|
|
|
|
else
|
|
|
|
|
tryToDelete(state, path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 21:39:27 +00:00
|
|
|
|
dir.reset();
|
2011-12-22 15:55:53 +00: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());
|
2018-06-13 14:56:19 +00:00
|
|
|
|
std::mt19937 gen(1);
|
|
|
|
|
std::shuffle(entries_.begin(), entries_.end(), gen);
|
2011-12-22 15:55:53 +00:00
|
|
|
|
|
2015-07-17 17:24:28 +00:00
|
|
|
|
for (auto & i : entries_)
|
|
|
|
|
tryToDelete(state, i);
|
2011-12-22 15:55:53 +00:00
|
|
|
|
|
2009-11-23 16:34:24 +00:00
|
|
|
|
} catch (GCLimitReached & e) {
|
|
|
|
|
}
|
2011-12-22 15:55:53 +00:00
|
|
|
|
}
|
2012-03-26 18:43:33 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
if (state.options.action == GCOptions::gcReturnLive) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
for (auto & i : state.alive)
|
|
|
|
|
state.results.paths.insert(printStorePath(i));
|
2012-12-20 16:32:15 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state.options.action == GCOptions::gcReturnDead) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
for (auto & i : state.dead)
|
|
|
|
|
state.results.paths.insert(printStorePath(i));
|
2012-12-20 16:32:15 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-26 18:43:33 +00:00
|
|
|
|
/* Allow other processes to add to the store from here on. */
|
2016-07-11 19:44:44 +00:00
|
|
|
|
fdGCLock = -1;
|
2013-12-10 12:13:59 +00:00
|
|
|
|
fds.clear();
|
2012-03-26 18:43:33 +00:00
|
|
|
|
|
2012-12-20 16:32:15 +00:00
|
|
|
|
/* Delete the trash directory. */
|
2017-07-30 11:27:57 +00:00
|
|
|
|
printInfo(format("deleting '%1%'") % trashDir);
|
2016-06-02 13:08:18 +00:00
|
|
|
|
deleteGarbage(state, trashDir);
|
2012-07-23 19:48:30 +00:00
|
|
|
|
|
|
|
|
|
/* Clean up the links directory. */
|
2012-09-12 22:49:35 +00:00
|
|
|
|
if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) {
|
2019-12-05 18:11:09 +00:00
|
|
|
|
printError("deleting unused links...");
|
2012-09-12 22:49:35 +00:00
|
|
|
|
removeUnusedLinks(state);
|
|
|
|
|
}
|
2012-09-13 18:33:41 +00:00
|
|
|
|
|
|
|
|
|
/* While we're at it, vacuum the database. */
|
2014-11-19 17:02:39 +00:00
|
|
|
|
//if (options.action == GCOptions::gcDeleteDead) vacuumDB();
|
2004-08-25 11:43:49 +00:00
|
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
2008-09-17 10:02:55 +00:00
|
|
|
|
|
2017-09-05 18:43:42 +00:00
|
|
|
|
void LocalStore::autoGC(bool sync)
|
|
|
|
|
{
|
2019-11-22 15:06:44 +00:00
|
|
|
|
static auto fakeFreeSpaceFile = getEnv("_NIX_TEST_FREE_SPACE_FILE");
|
2019-08-02 15:07:33 +00:00
|
|
|
|
|
|
|
|
|
auto getAvail = [this]() -> uint64_t {
|
2019-11-22 15:06:44 +00:00
|
|
|
|
if (fakeFreeSpaceFile)
|
|
|
|
|
return std::stoll(readFile(*fakeFreeSpaceFile));
|
2019-08-02 15:07:33 +00:00
|
|
|
|
|
2017-09-05 18:43:42 +00:00
|
|
|
|
struct statvfs st;
|
|
|
|
|
if (statvfs(realStoreDir.c_str(), &st))
|
|
|
|
|
throw SysError("getting filesystem info about '%s'", realStoreDir);
|
|
|
|
|
|
2020-05-04 21:42:06 +00:00
|
|
|
|
return (uint64_t) st.f_bavail * st.f_frsize;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::shared_future<void> future;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
|
|
|
|
|
if (state->gcRunning) {
|
|
|
|
|
future = state->gcFuture;
|
|
|
|
|
debug("waiting for auto-GC to finish");
|
|
|
|
|
goto sync;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
|
2019-08-02 15:07:33 +00:00
|
|
|
|
if (now < state->lastGCCheck + std::chrono::seconds(settings.minFreeCheckInterval)) return;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
|
|
|
|
auto avail = getAvail();
|
|
|
|
|
|
|
|
|
|
state->lastGCCheck = now;
|
|
|
|
|
|
|
|
|
|
if (avail >= settings.minFree || avail >= settings.maxFree) return;
|
|
|
|
|
|
|
|
|
|
if (avail > state->availAfterGC * 0.97) return;
|
|
|
|
|
|
|
|
|
|
state->gcRunning = true;
|
|
|
|
|
|
|
|
|
|
std::promise<void> promise;
|
|
|
|
|
future = state->gcFuture = promise.get_future().share();
|
|
|
|
|
|
|
|
|
|
std::thread([promise{std::move(promise)}, this, avail, getAvail]() mutable {
|
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
|
try {
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
|
/* Wake up any threads waiting for the auto-GC to finish. */
|
|
|
|
|
Finally wakeup([&]() {
|
|
|
|
|
auto state(_state.lock());
|
|
|
|
|
state->gcRunning = false;
|
|
|
|
|
state->lastGCCheck = std::chrono::steady_clock::now();
|
|
|
|
|
promise.set_value();
|
|
|
|
|
});
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
|
GCOptions options;
|
|
|
|
|
options.maxFreed = settings.maxFree - avail;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
2019-08-29 10:09:58 +00:00
|
|
|
|
printInfo("running auto-GC to free %d bytes", options.maxFreed);
|
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
|
GCResults results;
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
2017-09-06 09:37:58 +00:00
|
|
|
|
collectGarbage(options, results);
|
|
|
|
|
|
|
|
|
|
_state.lock()->availAfterGC = getAvail();
|
|
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
|
// FIXME: we could propagate the exception to the
|
|
|
|
|
// future, but we don't really care.
|
|
|
|
|
ignoreException();
|
|
|
|
|
}
|
2017-09-05 18:43:42 +00:00
|
|
|
|
|
|
|
|
|
}).detach();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync:
|
|
|
|
|
// Wait for the future outside of the state lock.
|
|
|
|
|
if (sync) future.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
}
|