2004-08-25 11:43:49 +00:00
|
|
|
#include "globals.hh"
|
2005-01-19 16:39:47 +00:00
|
|
|
#include "gc.hh"
|
2005-01-27 15:21:29 +00:00
|
|
|
#include "build.hh"
|
2005-01-31 10:27:25 +00:00
|
|
|
#include "pathlocks.hh"
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2004-08-25 11:43:49 +00:00
|
|
|
|
2004-08-25 16:54:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.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>
|
|
|
|
|
|
|
|
|
2005-01-31 22:23:49 +00:00
|
|
|
static string gcLockName = "gc.lock";
|
2005-02-01 13:48:46 +00:00
|
|
|
static string tempRootsDir = "temproots";
|
|
|
|
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. */
|
|
|
|
static int openGCLock(LockType lockType)
|
2005-01-31 22:01:55 +00:00
|
|
|
{
|
2005-01-31 22:23:49 +00:00
|
|
|
Path fnGCLock = (format("%1%/%2%")
|
|
|
|
% nixStateDir % gcLockName).str();
|
2005-02-01 15:05:32 +00:00
|
|
|
|
|
|
|
debug(format("acquiring global GC lock `%1%'") % fnGCLock);
|
|
|
|
|
2005-01-31 22:23:49 +00:00
|
|
|
AutoCloseFD fdGCLock = open(fnGCLock.c_str(), O_RDWR | O_CREAT, 0600);
|
|
|
|
if (fdGCLock == -1)
|
|
|
|
throw SysError(format("opening global GC lock `%1%'") % fnGCLock);
|
|
|
|
|
|
|
|
lockFile(fdGCLock, lockType, true);
|
|
|
|
|
|
|
|
/* !!! Restrict read permission on the GC root. Otherwise any
|
|
|
|
process that can open the file for reading can DoS the
|
|
|
|
collector. */
|
|
|
|
|
|
|
|
return fdGCLock.borrow();
|
2005-01-31 22:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 13:48:46 +00:00
|
|
|
void createSymlink(const Path & link, const Path & target, bool careful)
|
|
|
|
{
|
|
|
|
/* Create directories up to `gcRoot'. */
|
|
|
|
createDirs(dirOf(link));
|
|
|
|
|
|
|
|
/* Remove the old symlink. */
|
|
|
|
if (pathExists(link)) {
|
2005-02-07 14:32:44 +00:00
|
|
|
if (careful && (!isLink(link) || !isInStore(readLink(link))))
|
2005-02-01 13:48:46 +00:00
|
|
|
throw Error(format("cannot create symlink `%1%'; already exists") % link);
|
|
|
|
unlink(link.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And create the new own. */
|
|
|
|
if (symlink(target.c_str(), link.c_str()) == -1)
|
|
|
|
throw SysError(format("symlinking `%1%' to `%2%'")
|
|
|
|
% link % target);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
|
|
|
|
bool indirect)
|
2005-02-01 12:36:25 +00:00
|
|
|
{
|
|
|
|
Path storePath(canonPath(_storePath));
|
|
|
|
Path gcRoot(canonPath(_gcRoot));
|
2005-02-01 13:48:46 +00:00
|
|
|
assertStorePath(storePath);
|
2005-02-01 12:36:25 +00:00
|
|
|
|
|
|
|
/* Grab the global GC root. This prevents the set of permanent
|
|
|
|
roots from increasing while a GC is in progress. */
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
|
|
|
|
2005-02-01 13:48:46 +00:00
|
|
|
if (indirect) {
|
|
|
|
string hash = printHash32(hashString(htSHA1, gcRoot));
|
|
|
|
Path realRoot = canonPath((format("%1%/%2%/auto/%3%")
|
|
|
|
% nixStateDir % gcRootsDir % hash).str());
|
2005-02-17 13:55:18 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SwitchToOriginalUser sw;
|
|
|
|
createSymlink(gcRoot, storePath, true);
|
|
|
|
}
|
2005-02-01 13:48:46 +00:00
|
|
|
createSymlink(realRoot, gcRoot, false);
|
|
|
|
}
|
2005-02-01 12:36:25 +00:00
|
|
|
|
2005-02-01 13:48:46 +00:00
|
|
|
else {
|
|
|
|
Path rootsDir = canonPath((format("%1%/%2%") % nixStateDir % gcRootsDir).str());
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
createSymlink(gcRoot, storePath, false);
|
|
|
|
}
|
2005-02-01 12:36:25 +00:00
|
|
|
|
|
|
|
return gcRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
/* The file to which we write our temporary roots. */
|
2005-01-31 22:01:55 +00:00
|
|
|
static Path fnTempRoots;
|
2005-01-31 10:27:25 +00:00
|
|
|
static AutoCloseFD fdTempRoots;
|
|
|
|
|
|
|
|
|
|
|
|
void addTempRoot(const Path & path)
|
|
|
|
{
|
|
|
|
/* Create the temporary roots file for this process. */
|
|
|
|
if (fdTempRoots == -1) {
|
|
|
|
|
|
|
|
while (1) {
|
2005-03-24 17:46:38 +00:00
|
|
|
Path dir = (format("%1%/%2%") % nixStateDir % tempRootsDir).str();
|
|
|
|
createDirs(dir);
|
|
|
|
|
|
|
|
fnTempRoots = (format("%1%/%2%")
|
|
|
|
% dir % getpid()).str();
|
2005-01-31 22:23:49 +00:00
|
|
|
|
|
|
|
AutoCloseFD fdGCLock = openGCLock(ltRead);
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
fdTempRoots = open(fnTempRoots.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0600);
|
|
|
|
if (fdTempRoots == -1)
|
|
|
|
throw SysError(format("opening temporary roots file `%1%'") % fnTempRoots);
|
|
|
|
|
2005-01-31 22:23:49 +00:00
|
|
|
fdGCLock.close();
|
|
|
|
|
2005-01-31 10:27:25 +00: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;
|
|
|
|
|
|
|
|
/* 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';
|
|
|
|
writeFull(fdTempRoots, (const unsigned char *) s.c_str(), s.size());
|
|
|
|
|
|
|
|
/* Downgrade to a read lock. */
|
|
|
|
debug(format("downgrading to read lock on `%1%'") % fnTempRoots);
|
|
|
|
lockFile(fdTempRoots, ltRead, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-31 21:20:59 +00:00
|
|
|
void removeTempRoots()
|
|
|
|
{
|
|
|
|
if (fdTempRoots != -1) {
|
|
|
|
fdTempRoots.close();
|
|
|
|
unlink(fnTempRoots.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-31 10:27:25 +00:00
|
|
|
typedef shared_ptr<AutoCloseFD> FDPtr;
|
|
|
|
typedef list<FDPtr> FDs;
|
|
|
|
|
|
|
|
|
|
|
|
static void readTempRoots(PathSet & tempRoots, FDs & fds)
|
|
|
|
{
|
|
|
|
/* Read the `temproots' directory for per-process temporary root
|
|
|
|
files. */
|
|
|
|
Strings tempRootFiles = readDirectory(
|
|
|
|
(format("%1%/%2%") % nixStateDir % tempRootsDir).str());
|
|
|
|
|
|
|
|
for (Strings::iterator i = tempRootFiles.begin();
|
|
|
|
i != tempRootFiles.end(); ++i)
|
|
|
|
{
|
|
|
|
Path path = (format("%1%/%2%/%3%") % nixStateDir % tempRootsDir % *i).str();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 21:20:59 +00:00
|
|
|
unlink(path.c_str());
|
|
|
|
writeFull(*fd, (const unsigned char *) "d", 1);
|
2005-01-31 10:27:25 +00: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 22:07:48 +00:00
|
|
|
string contents = readFile(*fd);
|
2005-01-31 10:27:25 +00:00
|
|
|
|
|
|
|
/* Extract the roots. */
|
|
|
|
unsigned int pos = 0, end;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 15:05:32 +00:00
|
|
|
static void findRoots(const Path & path, bool recurseSymlinks,
|
|
|
|
PathSet & roots)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
for (Strings::iterator i = names.begin(); i != names.end(); ++i)
|
|
|
|
findRoots(path + "/" + *i, recurseSymlinks, roots);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISLNK(st.st_mode)) {
|
|
|
|
string target = readLink(path);
|
|
|
|
Path target2 = absPath(target, dirOf(path));
|
|
|
|
|
2005-02-07 14:32:44 +00:00
|
|
|
if (isInStore(target2)) {
|
2005-02-01 15:05:32 +00:00
|
|
|
debug(format("found root `%1%' in `%2%'")
|
|
|
|
% target2 % path);
|
2005-05-10 14:56:10 +00:00
|
|
|
Path target3 = toStorePath(target2);
|
|
|
|
if (isValidPath(target3))
|
|
|
|
roots.insert(target3);
|
|
|
|
else
|
|
|
|
printMsg(lvlInfo, format("skipping invalid root from `%1%' to `%2%'")
|
|
|
|
% path % target3);
|
2005-02-01 15:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (recurseSymlinks) {
|
|
|
|
if (pathExists(target2))
|
|
|
|
findRoots(target2, false, roots);
|
|
|
|
else {
|
|
|
|
printMsg(lvlInfo, format("removing stale link from `%1%' to `%2%'") % path % target2);
|
|
|
|
/* 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-01-31 14:00:43 +00:00
|
|
|
static void dfsVisit(const PathSet & paths, const Path & path,
|
|
|
|
PathSet & visited, Paths & sorted)
|
|
|
|
{
|
|
|
|
if (visited.find(path) != visited.end()) return;
|
|
|
|
visited.insert(path);
|
|
|
|
|
|
|
|
PathSet references;
|
|
|
|
if (isValidPath(path))
|
2005-02-08 13:23:55 +00:00
|
|
|
queryReferences(noTxn, path, references);
|
2005-01-31 14:00:43 +00:00
|
|
|
|
|
|
|
for (PathSet::iterator i = references.begin();
|
|
|
|
i != references.end(); ++i)
|
|
|
|
/* Don't traverse into paths that don't exist. That can
|
|
|
|
happen due to substitutes for non-existent paths. */
|
|
|
|
if (*i != path && paths.find(*i) != paths.end())
|
|
|
|
dfsVisit(paths, *i, visited, sorted);
|
|
|
|
|
|
|
|
sorted.push_front(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Paths topoSort(const PathSet & paths)
|
|
|
|
{
|
|
|
|
Paths sorted;
|
|
|
|
PathSet visited;
|
|
|
|
for (PathSet::const_iterator i = paths.begin(); i != paths.end(); ++i)
|
|
|
|
dfsVisit(paths, *i, visited, sorted);
|
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-01 15:05:32 +00:00
|
|
|
void collectGarbage(GCAction action, PathSet & result)
|
2005-01-27 15:21:29 +00:00
|
|
|
{
|
|
|
|
result.clear();
|
2005-02-01 22:07:48 +00:00
|
|
|
|
2005-02-14 14:16:56 +00:00
|
|
|
bool gcKeepOutputs =
|
|
|
|
queryBoolSetting("gc-keep-outputs", false);
|
|
|
|
bool gcKeepDerivations =
|
|
|
|
queryBoolSetting("gc-keep-derivations", true);
|
2005-02-01 22:07:48 +00:00
|
|
|
|
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. */
|
|
|
|
Path rootsDir = canonPath((format("%1%/%2%") % nixStateDir % gcRootsDir).str());
|
|
|
|
PathSet roots;
|
|
|
|
findRoots(rootsDir, true, roots);
|
|
|
|
|
|
|
|
if (action == gcReturnRoots) {
|
|
|
|
result = roots;
|
|
|
|
return;
|
|
|
|
}
|
2005-02-01 12:36:25 +00:00
|
|
|
|
2005-01-27 15:21:29 +00:00
|
|
|
/* Determine the live paths which is just the closure of the
|
|
|
|
roots under the `references' relation. */
|
|
|
|
PathSet livePaths;
|
|
|
|
for (PathSet::const_iterator i = roots.begin(); i != roots.end(); ++i)
|
|
|
|
computeFSClosure(canonPath(*i), livePaths);
|
|
|
|
|
2005-02-14 14:16:56 +00:00
|
|
|
if (gcKeepDerivations) {
|
|
|
|
for (PathSet::iterator i = livePaths.begin();
|
|
|
|
i != livePaths.end(); ++i)
|
|
|
|
{
|
2005-03-25 14:31:12 +00:00
|
|
|
/* Note that the deriver need not be valid (e.g., if we
|
|
|
|
previously ran the collector with `gcKeepDerivations'
|
|
|
|
turned off). */
|
2005-02-14 14:16:56 +00:00
|
|
|
Path deriver = queryDeriver(noTxn, *i);
|
2005-03-25 14:31:12 +00:00
|
|
|
if (deriver != "" && isValidPath(deriver))
|
2005-02-14 14:16:56 +00:00
|
|
|
computeFSClosure(deriver, livePaths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-14 13:07:09 +00:00
|
|
|
if (gcKeepOutputs) {
|
2005-02-01 22:07:48 +00:00
|
|
|
/* Hmz, identical to storePathRequisites in nix-store. */
|
|
|
|
for (PathSet::iterator i = livePaths.begin();
|
|
|
|
i != livePaths.end(); ++i)
|
|
|
|
if (isDerivation(*i)) {
|
|
|
|
Derivation drv = derivationFromPath(*i);
|
|
|
|
for (DerivationOutputs::iterator j = drv.outputs.begin();
|
|
|
|
j != drv.outputs.end(); ++j)
|
|
|
|
if (isValidPath(j->second.path))
|
|
|
|
computeFSClosure(j->second.path, livePaths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-27 15:21:29 +00:00
|
|
|
if (action == gcReturnLive) {
|
|
|
|
result = livePaths;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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. */
|
|
|
|
PathSet tempRoots;
|
|
|
|
FDs fds;
|
|
|
|
readTempRoots(tempRoots, fds);
|
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
/* Close the temporary roots. Note that we *cannot* do this in
|
|
|
|
readTempRoots(), because there we may not have all locks yet,
|
|
|
|
meaning that an invalid path can become valid (and thus add to
|
|
|
|
the references graph) after we have added it to the closure
|
|
|
|
(and computeFSClosure() assumes that the presence of a path
|
|
|
|
means that it has already been closed). */
|
|
|
|
PathSet tempRootsClosed;
|
|
|
|
for (PathSet::iterator i = tempRoots.begin(); i != tempRoots.end(); ++i)
|
|
|
|
if (isValidPath(*i))
|
|
|
|
computeFSClosure(*i, tempRootsClosed);
|
|
|
|
else
|
|
|
|
tempRootsClosed.insert(*i);
|
|
|
|
|
2005-01-31 22:01:55 +00:00
|
|
|
/* For testing - see tests/gc-concurrent.sh. */
|
|
|
|
if (getenv("NIX_DEBUG_GC_WAIT"))
|
|
|
|
sleep(2);
|
|
|
|
|
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
|
|
|
|
that is not currently in in `livePaths' or `tempRootsClosed'
|
|
|
|
can be deleted. */
|
2005-01-31 10:27:25 +00:00
|
|
|
|
2005-01-27 15:21:29 +00:00
|
|
|
/* Read the Nix store directory to find all currently existing
|
|
|
|
paths. */
|
2005-01-31 14:00:43 +00:00
|
|
|
Paths storePaths = readDirectory(nixStore);
|
|
|
|
PathSet storePaths2;
|
|
|
|
for (Paths::iterator i = storePaths.begin(); i != storePaths.end(); ++i)
|
|
|
|
storePaths2.insert(canonPath(nixStore + "/" + *i));
|
|
|
|
|
|
|
|
/* Topologically sort them under the `referers' relation. That
|
|
|
|
is, a < b iff a is in referers(b). This gives us the order in
|
|
|
|
which things can be deleted safely. */
|
|
|
|
/* !!! when we have multiple output paths per derivation, this
|
|
|
|
will not work anymore because we get cycles. */
|
|
|
|
storePaths = topoSort(storePaths2);
|
2005-01-31 22:01:55 +00:00
|
|
|
|
2005-02-01 12:36:25 +00:00
|
|
|
/* Try to delete store paths in the topologically sorted order. */
|
2005-01-31 14:00:43 +00:00
|
|
|
for (Paths::iterator i = storePaths.begin(); i != storePaths.end(); ++i) {
|
2005-01-27 15:21:29 +00:00
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
debug(format("considering deletion of `%1%'") % *i);
|
|
|
|
|
|
|
|
if (livePaths.find(*i) != livePaths.end()) {
|
|
|
|
debug(format("live path `%1%'") % *i);
|
2005-01-27 15:21:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
if (tempRootsClosed.find(*i) != tempRootsClosed.end()) {
|
|
|
|
debug(format("temporary root `%1%'") % *i);
|
2005-01-31 10:27:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
debug(format("dead path `%1%'") % *i);
|
|
|
|
result.insert(*i);
|
2005-01-27 15:21:29 +00:00
|
|
|
|
2005-01-31 12:19:53 +00:00
|
|
|
AutoCloseFD fdLock;
|
|
|
|
|
2005-01-27 15:21:29 +00:00
|
|
|
if (action == gcDeleteDead) {
|
2005-01-31 12:19:53 +00:00
|
|
|
|
|
|
|
/* Only delete a lock file if we can acquire a write lock
|
|
|
|
on it. That means that it's either stale, or the
|
|
|
|
process that created it hasn't locked it yet. In the
|
|
|
|
latter case the other process will detect that we
|
|
|
|
deleted the lock, and retry (see pathlocks.cc). */
|
2005-01-31 14:00:43 +00:00
|
|
|
if (i->size() >= 5 && string(*i, i->size() - 5) == ".lock") {
|
2005-01-31 12:19:53 +00:00
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
fdLock = open(i->c_str(), O_RDWR);
|
2005-01-31 12:19:53 +00:00
|
|
|
if (fdLock == -1) {
|
|
|
|
if (errno == ENOENT) continue;
|
2005-01-31 14:00:43 +00:00
|
|
|
throw SysError(format("opening lock file `%1%'") % *i);
|
2005-01-31 12:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!lockFile(fdLock, ltWrite, false)) {
|
2005-01-31 14:00:43 +00:00
|
|
|
debug(format("skipping active lock `%1%'") % *i);
|
2005-01-31 12:19:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2005-01-31 14:00:43 +00:00
|
|
|
|
|
|
|
printMsg(lvlInfo, format("deleting `%1%'") % *i);
|
2005-01-31 12:19:53 +00:00
|
|
|
|
2005-01-31 14:00:43 +00:00
|
|
|
/* Okay, it's safe to delete. */
|
|
|
|
deleteFromStore(*i);
|
2005-01-31 12:19:53 +00:00
|
|
|
|
|
|
|
if (fdLock != -1)
|
|
|
|
/* Write token to stale (deleted) lock file. */
|
|
|
|
writeFull(fdLock, (const unsigned char *) "d", 1);
|
2005-01-27 15:21:29 +00:00
|
|
|
}
|
2004-08-25 11:43:49 +00:00
|
|
|
}
|
|
|
|
}
|