* Move addTempRoot() to the store API, and add another function

syncWithGC() to allow clients to register GC roots without needing
  write access to the global roots directory or the GC lock.
This commit is contained in:
Eelco Dolstra 2006-12-02 16:41:36 +00:00
parent 30bf547f4f
commit e25fad691a
11 changed files with 81 additions and 17 deletions

View file

@ -642,7 +642,7 @@ void DerivationGoal::haveStoreExpr()
for (DerivationOutputs::iterator i = drv.outputs.begin();
i != drv.outputs.end(); ++i)
addTempRoot(i->second.path);
store->addTempRoot(i->second.path);
/* Check what outputs paths are not already valid. */
PathSet invalidOutputs = checkPathValidity(false);
@ -1714,7 +1714,7 @@ void SubstitutionGoal::init()
{
trace("init");
addTempRoot(storePath);
store->addTempRoot(storePath);
/* If the path already exists we're done. */
if (store->isValidPath(storePath)) {

View file

@ -76,6 +76,12 @@ void createSymlink(const Path & link, const Path & target, bool careful)
}
void LocalStore::syncWithGC()
{
AutoCloseFD fdGCLock = openGCLock(ltRead);
}
Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
bool indirect, bool allowOutsideRootsDir)
{
@ -83,10 +89,6 @@ Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
Path gcRoot(canonPath(_gcRoot));
assertStorePath(storePath);
/* Grab the global GC root. This prevents the set of permanent
roots from increasing while a GC is in progress. */
AutoCloseFD fdGCLock = openGCLock(ltRead);
if (indirect) {
string hash = printHash32(hashString(htSHA1, gcRoot));
Path realRoot = canonPath((format("%1%/%2%/auto/%3%")
@ -110,6 +112,11 @@ Path addPermRoot(const Path & _storePath, const Path & _gcRoot,
createSymlink(gcRoot, storePath, false);
}
/* 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. */
store->syncWithGC();
return gcRoot;
}
@ -119,7 +126,7 @@ static Path fnTempRoots;
static AutoCloseFD fdTempRoots;
void addTempRoot(const Path & path)
void LocalStore::addTempRoot(const Path & path)
{
/* Create the temporary roots file for this process. */
if (fdTempRoots == -1) {

View file

@ -26,12 +26,6 @@ typedef enum {
void collectGarbage(GCAction action, const PathSet & pathsToDelete,
bool ignoreLiveness, PathSet & result, unsigned long long & bytesFreed);
/* Register a temporary GC root. This root will automatically
disappear when this process exits. WARNING: this function should
not be called inside a BDB transaction, otherwise we can
deadlock. */
void addTempRoot(const Path & path);
/* Remove the temporary roots file for this process. Any temporary
root becomes garbage after this point unless it has been registered
as a (permanent) root. */

View file

@ -58,6 +58,10 @@ public:
void buildDerivations(const PathSet & drvPaths);
void ensurePath(const Path & path);
void addTempRoot(const Path & path);
void syncWithGC();
};

View file

@ -170,4 +170,19 @@ void RemoteStore::ensurePath(const Path & path)
}
void RemoteStore::addTempRoot(const Path & path)
{
writeInt(wopAddTempRoot, to);
writeString(path, to);
readInt(from);
}
void RemoteStore::syncWithGC()
{
writeInt(wopSyncWithGC, to);
readInt(from);
}
}

View file

@ -47,6 +47,10 @@ public:
void ensurePath(const Path & path);
void addTempRoot(const Path & path);
void syncWithGC();
private:
Pipe toChild;
Pipe fromChild;

View file

@ -86,6 +86,31 @@ public:
may be made valid by running a substitute (if defined for the
path). */
virtual void ensurePath(const Path & path) = 0;
/* Add a store path as a temporary root of the garbage collector.
The root disappears as soon as we exit. */
virtual void addTempRoot(const Path & path) = 0;
/* Acquire the global GC lock, then immediately release it. This
function must be called after registering a new permanent root,
but before exiting. Otherwise, it is possible that a running
garbage collector doesn't see the new root and deletes the
stuff we've just built. By acquiring the lock briefly, we
ensure that either:
- The collector is already running, and so we block until the
collector is finished. The collector will know about our
*temporary* locks, which should include whatever it is we
want to register as a permanent lock.
- The collector isn't running, or it's just started but hasn't
acquired the GC lock yet. In that case we get and release
the lock right away, then exit. The collector scans the
permanent root and sees our's.
In either case the permanent root is seen by the collector. */
virtual void syncWithGC() = 0;
};

View file

@ -18,6 +18,8 @@ typedef enum {
wopAddTextToStore,
wopBuildDerivations,
wopEnsurePath,
wopAddTempRoot,
wopSyncWithGC
} WorkerOp;

View file

@ -122,7 +122,7 @@ Path dirOf(const Path & path)
{
Path::size_type pos = path.rfind('/');
if (pos == string::npos)
throw Error(format("invalid file name: %1%") % path);
throw Error(format("invalid file name `%1%'") % path);
return pos == 0 ? "/" : Path(path, 0, pos);
}
@ -131,7 +131,7 @@ string baseNameOf(const Path & path)
{
Path::size_type pos = path.rfind('/');
if (pos == string::npos)
throw Error(format("invalid file name %1% ") % path);
throw Error(format("invalid file name `%1%'") % path);
return string(path, pos + 1);
}

View file

@ -182,7 +182,7 @@ static void createUserEnv(EvalState & state, const DrvInfos & elems,
/* This is only necessary when installing store paths, e.g.,
`nix-env -i /nix/store/abcd...-foo'. */
addTempRoot(i->queryOutPath(state));
store->addTempRoot(i->queryOutPath(state));
store->ensurePath(i->queryOutPath(state));
references.insert(i->queryOutPath(state));
@ -940,7 +940,7 @@ static void opSwitchProfile(Globals & globals,
if (opArgs.size() != 1)
throw UsageError(format("exactly one argument expected"));
Path profile = opArgs.front();
Path profile = absPath(opArgs.front());
Path profileLink = getHomeDir() + "/.nix-profile";
switchLink(profileLink, profile);

View file

@ -127,6 +127,19 @@ void processConnection(Source & from, Sink & to)
break;
}
case wopAddTempRoot: {
Path path = readStorePath(from);
store->addTempRoot(path);
writeInt(1, to);
break;
}
case wopSyncWithGC: {
store->syncWithGC();
writeInt(1, to);
break;
}
default:
throw Error(format("invalid operation %1%") % op);
}