* Open the connection to the daemon lazily (on demand) so that

read-only operations (like nix-env -qa) work properly when the
  daemon isn't running.
This commit is contained in:
Eelco Dolstra 2008-12-11 14:30:25 +00:00
parent a0766eca27
commit 07cdfb09fb
3 changed files with 38 additions and 6 deletions

View file

@ -37,6 +37,15 @@ PathSet readStorePaths(Source & from)
RemoteStore::RemoteStore() RemoteStore::RemoteStore()
{ {
initialised = false;
}
void RemoteStore::openConnection()
{
if (initialised) return;
initialised = true;
string remoteMode = getEnv("NIX_REMOTE"); string remoteMode = getEnv("NIX_REMOTE");
if (remoteMode == "slave") if (remoteMode == "slave")
@ -64,8 +73,8 @@ RemoteStore::RemoteStore()
throw Error("Nix daemon protocol version not supported"); throw Error("Nix daemon protocol version not supported");
writeInt(PROTOCOL_VERSION, to); writeInt(PROTOCOL_VERSION, to);
processStderr(); processStderr();
}
} catch (Error & e) { catch (Error & e) {
throw Error(format("cannot start worker (%1%)") throw Error(format("cannot start worker (%1%)")
% e.msg()); % e.msg());
} }
@ -194,6 +203,7 @@ void RemoteStore::setOptions()
bool RemoteStore::isValidPath(const Path & path) bool RemoteStore::isValidPath(const Path & path)
{ {
openConnection();
writeInt(wopIsValidPath, to); writeInt(wopIsValidPath, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -204,12 +214,14 @@ bool RemoteStore::isValidPath(const Path & path)
PathSet RemoteStore::queryValidPaths() PathSet RemoteStore::queryValidPaths()
{ {
openConnection();
throw Error("not implemented"); throw Error("not implemented");
} }
bool RemoteStore::hasSubstitutes(const Path & path) bool RemoteStore::hasSubstitutes(const Path & path)
{ {
openConnection();
writeInt(wopHasSubstitutes, to); writeInt(wopHasSubstitutes, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -221,6 +233,7 @@ bool RemoteStore::hasSubstitutes(const Path & path)
bool RemoteStore::querySubstitutablePathInfo(const Path & path, bool RemoteStore::querySubstitutablePathInfo(const Path & path,
SubstitutablePathInfo & info) SubstitutablePathInfo & info)
{ {
openConnection();
if (GET_PROTOCOL_MINOR(daemonVersion) < 3) return false; if (GET_PROTOCOL_MINOR(daemonVersion) < 3) return false;
writeInt(wopQuerySubstitutablePathInfo, to); writeInt(wopQuerySubstitutablePathInfo, to);
writeString(path, to); writeString(path, to);
@ -237,6 +250,7 @@ bool RemoteStore::querySubstitutablePathInfo(const Path & path,
Hash RemoteStore::queryPathHash(const Path & path) Hash RemoteStore::queryPathHash(const Path & path)
{ {
openConnection();
writeInt(wopQueryPathHash, to); writeInt(wopQueryPathHash, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -248,6 +262,7 @@ Hash RemoteStore::queryPathHash(const Path & path)
void RemoteStore::queryReferences(const Path & path, void RemoteStore::queryReferences(const Path & path,
PathSet & references) PathSet & references)
{ {
openConnection();
writeInt(wopQueryReferences, to); writeInt(wopQueryReferences, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -259,6 +274,7 @@ void RemoteStore::queryReferences(const Path & path,
void RemoteStore::queryReferrers(const Path & path, void RemoteStore::queryReferrers(const Path & path,
PathSet & referrers) PathSet & referrers)
{ {
openConnection();
writeInt(wopQueryReferrers, to); writeInt(wopQueryReferrers, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -269,6 +285,7 @@ void RemoteStore::queryReferrers(const Path & path,
Path RemoteStore::queryDeriver(const Path & path) Path RemoteStore::queryDeriver(const Path & path)
{ {
openConnection();
writeInt(wopQueryDeriver, to); writeInt(wopQueryDeriver, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -281,6 +298,8 @@ Path RemoteStore::queryDeriver(const Path & path)
Path RemoteStore::addToStore(const Path & _srcPath, Path RemoteStore::addToStore(const Path & _srcPath,
bool recursive, HashType hashAlgo, PathFilter & filter) bool recursive, HashType hashAlgo, PathFilter & filter)
{ {
openConnection();
Path srcPath(absPath(_srcPath)); Path srcPath(absPath(_srcPath));
writeInt(wopAddToStore, to); writeInt(wopAddToStore, to);
@ -298,6 +317,7 @@ Path RemoteStore::addToStore(const Path & _srcPath,
Path RemoteStore::addTextToStore(const string & name, const string & s, Path RemoteStore::addTextToStore(const string & name, const string & s,
const PathSet & references) const PathSet & references)
{ {
openConnection();
writeInt(wopAddTextToStore, to); writeInt(wopAddTextToStore, to);
writeString(name, to); writeString(name, to);
writeString(s, to); writeString(s, to);
@ -311,6 +331,7 @@ Path RemoteStore::addTextToStore(const string & name, const string & s,
void RemoteStore::exportPath(const Path & path, bool sign, void RemoteStore::exportPath(const Path & path, bool sign,
Sink & sink) Sink & sink)
{ {
openConnection();
writeInt(wopExportPath, to); writeInt(wopExportPath, to);
writeString(path, to); writeString(path, to);
writeInt(sign ? 1 : 0, to); writeInt(sign ? 1 : 0, to);
@ -321,10 +342,10 @@ void RemoteStore::exportPath(const Path & path, bool sign,
Path RemoteStore::importPath(bool requireSignature, Source & source) Path RemoteStore::importPath(bool requireSignature, Source & source)
{ {
openConnection();
writeInt(wopImportPath, to); writeInt(wopImportPath, to);
/* We ignore requireSignature, since the worker forces it to true /* We ignore requireSignature, since the worker forces it to true
anyway. */ anyway. */
processStderr(0, &source); processStderr(0, &source);
return readStorePath(from); return readStorePath(from);
} }
@ -332,6 +353,7 @@ Path RemoteStore::importPath(bool requireSignature, Source & source)
void RemoteStore::buildDerivations(const PathSet & drvPaths) void RemoteStore::buildDerivations(const PathSet & drvPaths)
{ {
openConnection();
writeInt(wopBuildDerivations, to); writeInt(wopBuildDerivations, to);
writeStringSet(drvPaths, to); writeStringSet(drvPaths, to);
processStderr(); processStderr();
@ -341,6 +363,7 @@ void RemoteStore::buildDerivations(const PathSet & drvPaths)
void RemoteStore::ensurePath(const Path & path) void RemoteStore::ensurePath(const Path & path)
{ {
openConnection();
writeInt(wopEnsurePath, to); writeInt(wopEnsurePath, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -350,6 +373,7 @@ void RemoteStore::ensurePath(const Path & path)
void RemoteStore::addTempRoot(const Path & path) void RemoteStore::addTempRoot(const Path & path)
{ {
openConnection();
writeInt(wopAddTempRoot, to); writeInt(wopAddTempRoot, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -359,6 +383,7 @@ void RemoteStore::addTempRoot(const Path & path)
void RemoteStore::addIndirectRoot(const Path & path) void RemoteStore::addIndirectRoot(const Path & path)
{ {
openConnection();
writeInt(wopAddIndirectRoot, to); writeInt(wopAddIndirectRoot, to);
writeString(path, to); writeString(path, to);
processStderr(); processStderr();
@ -368,6 +393,7 @@ void RemoteStore::addIndirectRoot(const Path & path)
void RemoteStore::syncWithGC() void RemoteStore::syncWithGC()
{ {
openConnection();
writeInt(wopSyncWithGC, to); writeInt(wopSyncWithGC, to);
processStderr(); processStderr();
readInt(from); readInt(from);
@ -376,6 +402,7 @@ void RemoteStore::syncWithGC()
Roots RemoteStore::findRoots() Roots RemoteStore::findRoots()
{ {
openConnection();
writeInt(wopFindRoots, to); writeInt(wopFindRoots, to);
processStderr(); processStderr();
unsigned int count = readInt(from); unsigned int count = readInt(from);
@ -391,6 +418,8 @@ Roots RemoteStore::findRoots()
void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results) void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
{ {
openConnection();
writeInt(wopCollectGarbage, to); writeInt(wopCollectGarbage, to);
writeInt(options.action, to); writeInt(options.action, to);
writeStringSet(options.pathsToDelete, to); writeStringSet(options.pathsToDelete, to);

View file

@ -74,6 +74,9 @@ private:
FdSource from; FdSource from;
Pid child; Pid child;
unsigned int daemonVersion; unsigned int daemonVersion;
bool initialised;
void openConnection();
void processStderr(Sink * sink = 0, Source * source = 0); void processStderr(Sink * sink = 0, Source * source = 0);

View file

@ -35,7 +35,7 @@ struct FdSink : Sink
FdSink() FdSink()
{ {
fd = 0; fd = -1;
} }
FdSink(int fd) FdSink(int fd)
@ -54,7 +54,7 @@ struct FdSource : Source
FdSource() FdSource()
{ {
fd = 0; fd = -1;
} }
FdSource(int fd) FdSource(int fd)