More cleanup

This commit is contained in:
Eelco Dolstra 2015-07-20 01:16:16 +02:00
parent 6bd2c7bb38
commit b3491c781c
9 changed files with 184 additions and 296 deletions

View file

@ -43,8 +43,7 @@ static std::pair<FdSink, FdSource> connect(const string & conn)
static void substitute(std::pair<FdSink, FdSource> & pipes, Path storePath, Path destPath) static void substitute(std::pair<FdSink, FdSource> & pipes, Path storePath, Path destPath)
{ {
writeInt(cmdDumpStorePath, pipes.first); pipes.first << cmdDumpStorePath << storePath;
writeString(storePath, pipes.first);
pipes.first.flush(); pipes.first.flush();
restorePath(destPath, pipes.second); restorePath(destPath, pipes.second);
std::cout << std::endl; std::cout << std::endl;
@ -58,17 +57,17 @@ static void query(std::pair<FdSink, FdSource> & pipes)
string cmd = tokenized.front(); string cmd = tokenized.front();
tokenized.pop_front(); tokenized.pop_front();
if (cmd == "have") { if (cmd == "have") {
writeInt(cmdQueryValidPaths, pipes.first); pipes.first
writeInt(0, pipes.first); // don't lock << cmdQueryValidPaths
writeInt(0, pipes.first); // don't substitute << 0 // don't lock
writeStrings(tokenized, pipes.first); << 0 // don't substitute
<< tokenized;
pipes.first.flush(); pipes.first.flush();
PathSet paths = readStrings<PathSet>(pipes.second); PathSet paths = readStrings<PathSet>(pipes.second);
for (auto & i : paths) for (auto & i : paths)
std::cout << i << std::endl; std::cout << i << std::endl;
} else if (cmd == "info") { } else if (cmd == "info") {
writeInt(cmdQueryPathInfos, pipes.first); pipes.first << cmdQueryPathInfos << tokenized;
writeStrings(tokenized, pipes.first);
pipes.first.flush(); pipes.first.flush();
while (1) { while (1) {
Path path = readString(pipes.second); Path path = readString(pipes.second);
@ -116,13 +115,13 @@ int main(int argc, char * * argv)
std::pair<FdSink, FdSource> pipes = connect(host); std::pair<FdSink, FdSource> pipes = connect(host);
/* Exchange the greeting */ /* Exchange the greeting */
writeInt(SERVE_MAGIC_1, pipes.first); pipes.first << SERVE_MAGIC_1;
pipes.first.flush(); pipes.first.flush();
unsigned int magic = readInt(pipes.second); unsigned int magic = readInt(pipes.second);
if (magic != SERVE_MAGIC_2) if (magic != SERVE_MAGIC_2)
throw Error("protocol mismatch"); throw Error("protocol mismatch");
readInt(pipes.second); // Server version, unused for now readInt(pipes.second); // Server version, unused for now
writeInt(SERVE_PROTOCOL_VERSION, pipes.first); pipes.first << SERVE_PROTOCOL_VERSION;
pipes.first.flush(); pipes.first.flush();
string arg = argv[1]; string arg = argv[1];

View file

@ -1528,22 +1528,14 @@ void LocalStore::exportPath(const Path & path, bool sign,
throw Error(format("hash of path %1% has changed from %2% to %3%!") % path throw Error(format("hash of path %1% has changed from %2% to %3%!") % path
% printHash(storedHash) % printHash(hash)); % printHash(storedHash) % printHash(hash));
writeInt(EXPORT_MAGIC, hashAndWriteSink);
writeString(path, hashAndWriteSink);
PathSet references; PathSet references;
queryReferences(path, references); queryReferences(path, references);
writeStrings(references, hashAndWriteSink);
Path deriver = queryDeriver(path); hashAndWriteSink << EXPORT_MAGIC << path << references << queryDeriver(path);
writeString(deriver, hashAndWriteSink);
if (sign) { if (sign) {
Hash hash = hashAndWriteSink.currentHash(); Hash hash = hashAndWriteSink.currentHash();
writeInt(1, hashAndWriteSink);
Path tmpDir = createTempDir(); Path tmpDir = createTempDir();
AutoDelete delTmp(tmpDir); AutoDelete delTmp(tmpDir);
Path hashFile = tmpDir + "/hash"; Path hashFile = tmpDir + "/hash";
@ -1561,10 +1553,10 @@ void LocalStore::exportPath(const Path & path, bool sign,
args.push_back(hashFile); args.push_back(hashFile);
string signature = runProgram(OPENSSL_PATH, true, args); string signature = runProgram(OPENSSL_PATH, true, args);
writeString(signature, hashAndWriteSink); hashAndWriteSink << 1 << signature;
} else } else
writeInt(0, hashAndWriteSink); hashAndWriteSink << 0;
} }

View file

@ -63,7 +63,7 @@ void RemoteStore::openConnection(bool reserveSpace)
/* Send the magic greeting, check for the reply. */ /* Send the magic greeting, check for the reply. */
try { try {
writeInt(WORKER_MAGIC_1, to); to << WORKER_MAGIC_1;
to.flush(); to.flush();
unsigned int magic = readInt(from); unsigned int magic = readInt(from);
if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch"); if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch");
@ -71,19 +71,18 @@ void RemoteStore::openConnection(bool reserveSpace)
daemonVersion = readInt(from); daemonVersion = readInt(from);
if (GET_PROTOCOL_MAJOR(daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION)) if (GET_PROTOCOL_MAJOR(daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION))
throw Error("Nix daemon protocol version not supported"); throw Error("Nix daemon protocol version not supported");
writeInt(PROTOCOL_VERSION, to); to << PROTOCOL_VERSION;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 14) { if (GET_PROTOCOL_MINOR(daemonVersion) >= 14) {
int cpu = settings.lockCPU ? lockToCurrentCPU() : -1; int cpu = settings.lockCPU ? lockToCurrentCPU() : -1;
if (cpu != -1) { if (cpu != -1)
writeInt(1, to); to << 1 << cpu;
writeInt(cpu, to); else
} else to << 0;
writeInt(0, to);
} }
if (GET_PROTOCOL_MINOR(daemonVersion) >= 11) if (GET_PROTOCOL_MINOR(daemonVersion) >= 11)
writeInt(reserveSpace, to); to << reserveSpace;
processStderr(); processStderr();
} }
@ -141,35 +140,31 @@ RemoteStore::~RemoteStore()
void RemoteStore::setOptions() void RemoteStore::setOptions()
{ {
writeInt(wopSetOptions, to); to << wopSetOptions
<< settings.keepFailed
writeInt(settings.keepFailed, to); << settings.keepGoing
writeInt(settings.keepGoing, to); << settings.tryFallback
writeInt(settings.tryFallback, to); << verbosity
writeInt(verbosity, to); << settings.maxBuildJobs
writeInt(settings.maxBuildJobs, to); << settings.maxSilentTime;
writeInt(settings.maxSilentTime, to);
if (GET_PROTOCOL_MINOR(daemonVersion) >= 2) if (GET_PROTOCOL_MINOR(daemonVersion) >= 2)
writeInt(settings.useBuildHook, to); to << settings.useBuildHook;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 4) { if (GET_PROTOCOL_MINOR(daemonVersion) >= 4)
writeInt(settings.buildVerbosity, to); to << settings.buildVerbosity
writeInt(logType, to); << logType
writeInt(settings.printBuildTrace, to); << settings.printBuildTrace;
}
if (GET_PROTOCOL_MINOR(daemonVersion) >= 6) if (GET_PROTOCOL_MINOR(daemonVersion) >= 6)
writeInt(settings.buildCores, to); to << settings.buildCores;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 10) if (GET_PROTOCOL_MINOR(daemonVersion) >= 10)
writeInt(settings.useSubstitutes, to); to << settings.useSubstitutes;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 12) { if (GET_PROTOCOL_MINOR(daemonVersion) >= 12) {
Settings::SettingsMap overrides = settings.getOverrides(); Settings::SettingsMap overrides = settings.getOverrides();
if (overrides["ssh-auth-sock"] == "") if (overrides["ssh-auth-sock"] == "")
overrides["ssh-auth-sock"] = getEnv("SSH_AUTH_SOCK"); overrides["ssh-auth-sock"] = getEnv("SSH_AUTH_SOCK");
writeInt(overrides.size(), to); to << overrides.size();
for (auto & i : overrides) { for (auto & i : overrides)
writeString(i.first, to); to << i.first << i.second;
writeString(i.second, to);
}
} }
processStderr(); processStderr();
@ -179,8 +174,7 @@ void RemoteStore::setOptions()
bool RemoteStore::isValidPath(const Path & path) bool RemoteStore::isValidPath(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopIsValidPath, to); to << wopIsValidPath << path;
writeString(path, to);
processStderr(); processStderr();
unsigned int reply = readInt(from); unsigned int reply = readInt(from);
return reply != 0; return reply != 0;
@ -196,8 +190,7 @@ PathSet RemoteStore::queryValidPaths(const PathSet & paths)
if (isValidPath(i)) res.insert(i); if (isValidPath(i)) res.insert(i);
return res; return res;
} else { } else {
writeInt(wopQueryValidPaths, to); to << wopQueryValidPaths << paths;
writeStrings(paths, to);
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -207,7 +200,7 @@ PathSet RemoteStore::queryValidPaths(const PathSet & paths)
PathSet RemoteStore::queryAllValidPaths() PathSet RemoteStore::queryAllValidPaths()
{ {
openConnection(); openConnection();
writeInt(wopQueryAllValidPaths, to); to << wopQueryAllValidPaths;
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -219,15 +212,13 @@ PathSet RemoteStore::querySubstitutablePaths(const PathSet & paths)
if (GET_PROTOCOL_MINOR(daemonVersion) < 12) { if (GET_PROTOCOL_MINOR(daemonVersion) < 12) {
PathSet res; PathSet res;
for (auto & i : paths) { for (auto & i : paths) {
writeInt(wopHasSubstitutes, to); to << wopHasSubstitutes << i;
writeString(i, to);
processStderr(); processStderr();
if (readInt(from)) res.insert(i); if (readInt(from)) res.insert(i);
} }
return res; return res;
} else { } else {
writeInt(wopQuerySubstitutablePaths, to); to << wopQuerySubstitutablePaths << paths;
writeStrings(paths, to);
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -247,8 +238,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
for (auto & i : paths) { for (auto & i : paths) {
SubstitutablePathInfo info; SubstitutablePathInfo info;
writeInt(wopQuerySubstitutablePathInfo, to); to << wopQuerySubstitutablePathInfo << i;
writeString(i, to);
processStderr(); processStderr();
unsigned int reply = readInt(from); unsigned int reply = readInt(from);
if (reply == 0) continue; if (reply == 0) continue;
@ -262,8 +252,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
} else { } else {
writeInt(wopQuerySubstitutablePathInfos, to); to << wopQuerySubstitutablePathInfos << paths;
writeStrings(paths, to);
processStderr(); processStderr();
unsigned int count = readInt(from); unsigned int count = readInt(from);
for (unsigned int n = 0; n < count; n++) { for (unsigned int n = 0; n < count; n++) {
@ -283,8 +272,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
ValidPathInfo RemoteStore::queryPathInfo(const Path & path) ValidPathInfo RemoteStore::queryPathInfo(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryPathInfo, to); to << wopQueryPathInfo << path;
writeString(path, to);
processStderr(); processStderr();
ValidPathInfo info; ValidPathInfo info;
info.path = path; info.path = path;
@ -301,8 +289,7 @@ ValidPathInfo RemoteStore::queryPathInfo(const Path & path)
Hash RemoteStore::queryPathHash(const Path & path) Hash RemoteStore::queryPathHash(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryPathHash, to); to << wopQueryPathHash << path;
writeString(path, to);
processStderr(); processStderr();
string hash = readString(from); string hash = readString(from);
return parseHash(htSHA256, hash); return parseHash(htSHA256, hash);
@ -313,8 +300,7 @@ void RemoteStore::queryReferences(const Path & path,
PathSet & references) PathSet & references)
{ {
openConnection(); openConnection();
writeInt(wopQueryReferences, to); to << wopQueryReferences << path;
writeString(path, to);
processStderr(); processStderr();
PathSet references2 = readStorePaths<PathSet>(from); PathSet references2 = readStorePaths<PathSet>(from);
references.insert(references2.begin(), references2.end()); references.insert(references2.begin(), references2.end());
@ -325,8 +311,7 @@ void RemoteStore::queryReferrers(const Path & path,
PathSet & referrers) PathSet & referrers)
{ {
openConnection(); openConnection();
writeInt(wopQueryReferrers, to); to << wopQueryReferrers << path;
writeString(path, to);
processStderr(); processStderr();
PathSet referrers2 = readStorePaths<PathSet>(from); PathSet referrers2 = readStorePaths<PathSet>(from);
referrers.insert(referrers2.begin(), referrers2.end()); referrers.insert(referrers2.begin(), referrers2.end());
@ -336,8 +321,7 @@ void RemoteStore::queryReferrers(const Path & path,
Path RemoteStore::queryDeriver(const Path & path) Path RemoteStore::queryDeriver(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryDeriver, to); to << wopQueryDeriver << path;
writeString(path, to);
processStderr(); processStderr();
Path drvPath = readString(from); Path drvPath = readString(from);
if (drvPath != "") assertStorePath(drvPath); if (drvPath != "") assertStorePath(drvPath);
@ -348,8 +332,7 @@ Path RemoteStore::queryDeriver(const Path & path)
PathSet RemoteStore::queryValidDerivers(const Path & path) PathSet RemoteStore::queryValidDerivers(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryValidDerivers, to); to << wopQueryValidDerivers << path;
writeString(path, to);
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -358,8 +341,7 @@ PathSet RemoteStore::queryValidDerivers(const Path & path)
PathSet RemoteStore::queryDerivationOutputs(const Path & path) PathSet RemoteStore::queryDerivationOutputs(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryDerivationOutputs, to); to << wopQueryDerivationOutputs << path;
writeString(path, to);
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -368,8 +350,7 @@ PathSet RemoteStore::queryDerivationOutputs(const Path & path)
PathSet RemoteStore::queryDerivationOutputNames(const Path & path) PathSet RemoteStore::queryDerivationOutputNames(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopQueryDerivationOutputNames, to); to << wopQueryDerivationOutputNames << path;
writeString(path, to);
processStderr(); processStderr();
return readStrings<PathSet>(from); return readStrings<PathSet>(from);
} }
@ -378,8 +359,7 @@ PathSet RemoteStore::queryDerivationOutputNames(const Path & path)
Path RemoteStore::queryPathFromHashPart(const string & hashPart) Path RemoteStore::queryPathFromHashPart(const string & hashPart)
{ {
openConnection(); openConnection();
writeInt(wopQueryPathFromHashPart, to); to << wopQueryPathFromHashPart << hashPart;
writeString(hashPart, to);
processStderr(); processStderr();
Path path = readString(from); Path path = readString(from);
if (!path.empty()) assertStorePath(path); if (!path.empty()) assertStorePath(path);
@ -396,12 +376,10 @@ Path RemoteStore::addToStore(const string & name, const Path & _srcPath,
Path srcPath(absPath(_srcPath)); Path srcPath(absPath(_srcPath));
writeInt(wopAddToStore, to); to << wopAddToStore << name
writeString(name, to); << ((hashAlgo == htSHA256 && recursive) ? 0 : 1) /* backwards compatibility hack */
/* backwards compatibility hack */ << (recursive ? 1 : 0)
writeInt((hashAlgo == htSHA256 && recursive) ? 0 : 1, to); << printHashType(hashAlgo);
writeInt(recursive ? 1 : 0, to);
writeString(printHashType(hashAlgo), to);
try { try {
to.written = 0; to.written = 0;
@ -429,10 +407,7 @@ Path RemoteStore::addTextToStore(const string & name, const string & s,
if (repair) throw Error("repairing is not supported when building through the Nix daemon"); if (repair) throw Error("repairing is not supported when building through the Nix daemon");
openConnection(); openConnection();
writeInt(wopAddTextToStore, to); to << wopAddTextToStore << name << s << references;
writeString(name, to);
writeString(s, to);
writeStrings(references, to);
processStderr(); processStderr();
return readStorePath(from); return readStorePath(from);
@ -443,9 +418,7 @@ void RemoteStore::exportPath(const Path & path, bool sign,
Sink & sink) Sink & sink)
{ {
openConnection(); openConnection();
writeInt(wopExportPath, to); to << wopExportPath << path << (sign ? 1 : 0);
writeString(path, to);
writeInt(sign ? 1 : 0, to);
processStderr(&sink); /* sink receives the actual data */ processStderr(&sink); /* sink receives the actual data */
readInt(from); readInt(from);
} }
@ -454,7 +427,7 @@ void RemoteStore::exportPath(const Path & path, bool sign,
Paths RemoteStore::importPaths(bool requireSignature, Source & source) Paths RemoteStore::importPaths(bool requireSignature, Source & source)
{ {
openConnection(); openConnection();
writeInt(wopImportPaths, to); to << wopImportPaths;
/* 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);
@ -466,16 +439,16 @@ void RemoteStore::buildPaths(const PathSet & drvPaths, BuildMode buildMode)
{ {
if (buildMode != bmNormal) throw Error("repairing or checking is not supported when building through the Nix daemon"); if (buildMode != bmNormal) throw Error("repairing or checking is not supported when building through the Nix daemon");
openConnection(); openConnection();
writeInt(wopBuildPaths, to); to << wopBuildPaths;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 13) if (GET_PROTOCOL_MINOR(daemonVersion) >= 13)
writeStrings(drvPaths, to); to << drvPaths;
else { else {
/* For backwards compatibility with old daemons, strip output /* For backwards compatibility with old daemons, strip output
identifiers. */ identifiers. */
PathSet drvPaths2; PathSet drvPaths2;
for (auto & i : drvPaths) for (auto & i : drvPaths)
drvPaths2.insert(string(i, 0, i.find('!'))); drvPaths2.insert(string(i, 0, i.find('!')));
writeStrings(drvPaths2, to); to << drvPaths2;
} }
processStderr(); processStderr();
readInt(from); readInt(from);
@ -492,8 +465,7 @@ BuildResult RemoteStore::buildDerivation(const Path & drvPath, const BasicDeriva
void RemoteStore::ensurePath(const Path & path) void RemoteStore::ensurePath(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopEnsurePath, to); to << wopEnsurePath << path;
writeString(path, to);
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -502,8 +474,7 @@ void RemoteStore::ensurePath(const Path & path)
void RemoteStore::addTempRoot(const Path & path) void RemoteStore::addTempRoot(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopAddTempRoot, to); to << wopAddTempRoot << path;
writeString(path, to);
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -512,8 +483,7 @@ void RemoteStore::addTempRoot(const Path & path)
void RemoteStore::addIndirectRoot(const Path & path) void RemoteStore::addIndirectRoot(const Path & path)
{ {
openConnection(); openConnection();
writeInt(wopAddIndirectRoot, to); to << wopAddIndirectRoot << path;
writeString(path, to);
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -522,7 +492,7 @@ void RemoteStore::addIndirectRoot(const Path & path)
void RemoteStore::syncWithGC() void RemoteStore::syncWithGC()
{ {
openConnection(); openConnection();
writeInt(wopSyncWithGC, to); to << wopSyncWithGC;
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -531,7 +501,7 @@ void RemoteStore::syncWithGC()
Roots RemoteStore::findRoots() Roots RemoteStore::findRoots()
{ {
openConnection(); openConnection();
writeInt(wopFindRoots, to); to << wopFindRoots;
processStderr(); processStderr();
unsigned int count = readInt(from); unsigned int count = readInt(from);
Roots result; Roots result;
@ -548,17 +518,11 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
{ {
openConnection(false); openConnection(false);
writeInt(wopCollectGarbage, to); to << wopCollectGarbage << options.action << options.pathsToDelete << options.ignoreLiveness
writeInt(options.action, to); << options.maxFreed << 0;
writeStrings(options.pathsToDelete, to); if (GET_PROTOCOL_MINOR(daemonVersion) >= 5)
writeInt(options.ignoreLiveness, to);
writeLongLong(options.maxFreed, to);
writeInt(0, to);
if (GET_PROTOCOL_MINOR(daemonVersion) >= 5) {
/* removed options */ /* removed options */
writeInt(0, to); to << 0 << 0;
writeInt(0, to);
}
processStderr(); processStderr();
@ -571,7 +535,7 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
PathSet RemoteStore::queryFailedPaths() PathSet RemoteStore::queryFailedPaths()
{ {
openConnection(); openConnection();
writeInt(wopQueryFailedPaths, to); to << wopQueryFailedPaths;
processStderr(); processStderr();
return readStorePaths<PathSet>(from); return readStorePaths<PathSet>(from);
} }
@ -580,8 +544,7 @@ PathSet RemoteStore::queryFailedPaths()
void RemoteStore::clearFailedPaths(const PathSet & paths) void RemoteStore::clearFailedPaths(const PathSet & paths)
{ {
openConnection(); openConnection();
writeInt(wopClearFailedPaths, to); to << wopClearFailedPaths << paths;
writeStrings(paths, to);
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -589,7 +552,7 @@ void RemoteStore::clearFailedPaths(const PathSet & paths)
void RemoteStore::optimiseStore() void RemoteStore::optimiseStore()
{ {
openConnection(); openConnection();
writeInt(wopOptimiseStore, to); to << wopOptimiseStore;
processStderr(); processStderr();
readInt(from); readInt(from);
} }
@ -597,9 +560,7 @@ void RemoteStore::optimiseStore()
bool RemoteStore::verifyStore(bool checkContents, bool repair) bool RemoteStore::verifyStore(bool checkContents, bool repair)
{ {
openConnection(); openConnection();
writeInt(wopVerifyStore, to); to << wopVerifyStore << checkContents << repair;
writeInt(checkContents, to);
writeInt(repair, to);
processStderr(); processStderr();
return readInt(from) != 0; return readInt(from) != 0;
} }

View file

@ -298,10 +298,10 @@ void exportPaths(StoreAPI & store, const Paths & paths,
bool sign, Sink & sink) bool sign, Sink & sink)
{ {
for (auto & i : paths) { for (auto & i : paths) {
writeInt(1, sink); sink << 1;
store.exportPath(i, sign, sink); store.exportPath(i, sign, sink);
} }
writeInt(0, sink); sink << 0;
} }

View file

@ -39,8 +39,7 @@ PathFilter defaultPathFilter;
static void dumpContents(const Path & path, size_t size, static void dumpContents(const Path & path, size_t size,
Sink & sink) Sink & sink)
{ {
writeString("contents", sink); sink << "contents" << size;
writeLongLong(size, sink);
AutoCloseFD fd = open(path.c_str(), O_RDONLY); AutoCloseFD fd = open(path.c_str(), O_RDONLY);
if (fd == -1) throw SysError(format("opening file %1%") % path); if (fd == -1) throw SysError(format("opening file %1%") % path);
@ -65,21 +64,17 @@ static void dump(const Path & path, Sink & sink, PathFilter & filter)
if (lstat(path.c_str(), &st)) if (lstat(path.c_str(), &st))
throw SysError(format("getting attributes of path %1%") % path); throw SysError(format("getting attributes of path %1%") % path);
writeString("(", sink); sink << "(";
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
writeString("type", sink); sink << "type" << "regular";
writeString("regular", sink); if (st.st_mode & S_IXUSR)
if (st.st_mode & S_IXUSR) { sink << "executable" << "";
writeString("executable", sink);
writeString("", sink);
}
dumpContents(path, (size_t) st.st_size, sink); dumpContents(path, (size_t) st.st_size, sink);
} }
else if (S_ISDIR(st.st_mode)) { else if (S_ISDIR(st.st_mode)) {
writeString("type", sink); sink << "type" << "directory";
writeString("directory", sink);
/* If we're on a case-insensitive system like Mac OS X, undo /* If we're on a case-insensitive system like Mac OS X, undo
the case hack applied by restorePath(). */ the case hack applied by restorePath(). */
@ -101,32 +96,24 @@ static void dump(const Path & path, Sink & sink, PathFilter & filter)
for (auto & i : unhacked) for (auto & i : unhacked)
if (filter(path + "/" + i.first)) { if (filter(path + "/" + i.first)) {
writeString("entry", sink); sink << "entry" << "(" << "name" << i.first << "node";
writeString("(", sink);
writeString("name", sink);
writeString(i.first, sink);
writeString("node", sink);
dump(path + "/" + i.second, sink, filter); dump(path + "/" + i.second, sink, filter);
writeString(")", sink); sink << ")";
} }
} }
else if (S_ISLNK(st.st_mode)) { else if (S_ISLNK(st.st_mode))
writeString("type", sink); sink << "type" << "symlink" << "target" << readLink(path);
writeString("symlink", sink);
writeString("target", sink);
writeString(readLink(path), sink);
}
else throw Error(format("file %1% has an unsupported type") % path); else throw Error(format("file %1% has an unsupported type") % path);
writeString(")", sink); sink << ")";
} }
void dumpPath(const Path & path, Sink & sink, PathFilter & filter) void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
{ {
writeString(archiveVersion1, sink); sink << archiveVersion1;
dump(path, sink, filter); dump(path, sink, filter);
} }

View file

@ -144,79 +144,38 @@ void writePadding(size_t len, Sink & sink)
} }
void writeInt(unsigned int n, Sink & sink)
{
unsigned char buf[8];
memset(buf, 0, sizeof(buf));
buf[0] = n & 0xff;
buf[1] = (n >> 8) & 0xff;
buf[2] = (n >> 16) & 0xff;
buf[3] = (n >> 24) & 0xff;
sink(buf, sizeof(buf));
}
Sink & operator << (Sink & out, unsigned int n)
{
writeInt(n, out);
return out;
}
void writeLongLong(unsigned long long n, Sink & sink)
{
unsigned char buf[8];
buf[0] = n & 0xff;
buf[1] = (n >> 8) & 0xff;
buf[2] = (n >> 16) & 0xff;
buf[3] = (n >> 24) & 0xff;
buf[4] = (n >> 32) & 0xff;
buf[5] = (n >> 40) & 0xff;
buf[6] = (n >> 48) & 0xff;
buf[7] = (n >> 56) & 0xff;
sink(buf, sizeof(buf));
}
void writeString(const unsigned char * buf, size_t len, Sink & sink) void writeString(const unsigned char * buf, size_t len, Sink & sink)
{ {
writeInt(len, sink); sink << len;
sink(buf, len); sink(buf, len);
writePadding(len, sink); writePadding(len, sink);
} }
void writeString(const string & s, Sink & sink) Sink & operator << (Sink & sink, const string & s)
{ {
writeString((const unsigned char *) s.data(), s.size(), sink); writeString((const unsigned char *) s.data(), s.size(), sink);
} return sink;
Sink & operator << (Sink & out, const string & s)
{
writeString(s, out);
return out;
} }
template<class T> void writeStrings(const T & ss, Sink & sink) template<class T> void writeStrings(const T & ss, Sink & sink)
{ {
writeInt(ss.size(), sink); sink << ss.size();
for (auto & i : ss) for (auto & i : ss)
writeString(i, sink); sink << i;
} }
template void writeStrings(const Paths & ss, Sink & sink); Sink & operator << (Sink & sink, const Strings & s)
template void writeStrings(const PathSet & ss, Sink & sink);
Sink & operator << (Sink & out, const Strings & s)
{ {
writeStrings(s, out); writeStrings(s, sink);
return out; return sink;
} }
Sink & operator << (Sink & out, const StringSet & s) Sink & operator << (Sink & sink, const StringSet & s)
{ {
writeStrings(s, out); writeStrings(s, sink);
return out; return sink;
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "types.hh" #include "types.hh"
#include "util.hh"
namespace nix { namespace nix {
@ -112,16 +113,26 @@ struct StringSource : Source
void writePadding(size_t len, Sink & sink); void writePadding(size_t len, Sink & sink);
void writeInt(unsigned int n, Sink & sink);
void writeLongLong(unsigned long long n, Sink & sink);
void writeString(const unsigned char * buf, size_t len, Sink & sink); void writeString(const unsigned char * buf, size_t len, Sink & sink);
void writeString(const string & s, Sink & sink);
template<class T> void writeStrings(const T & ss, Sink & sink);
Sink & operator << (Sink & out, unsigned int n); inline Sink & operator << (Sink & sink, uint64_t n)
Sink & operator << (Sink & out, const string & s); {
Sink & operator << (Sink & out, const Strings & s); unsigned char buf[8];
Sink & operator << (Sink & out, const StringSet & s); buf[0] = n & 0xff;
buf[1] = (n >> 8) & 0xff;
buf[2] = (n >> 16) & 0xff;
buf[3] = (n >> 24) & 0xff;
buf[4] = (n >> 32) & 0xff;
buf[5] = (n >> 40) & 0xff;
buf[6] = (n >> 48) & 0xff;
buf[7] = (n >> 56) & 0xff;
sink(buf, sizeof(buf));
return sink;
}
Sink & operator << (Sink & sink, const string & s);
Sink & operator << (Sink & sink, const Strings & s);
Sink & operator << (Sink & sink, const StringSet & s);
void readPadding(size_t len, Source & source); void readPadding(size_t len, Source & source);

View file

@ -43,7 +43,7 @@ static void tunnelStderr(const unsigned char * buf, size_t count)
{ {
if (canSendStderr) { if (canSendStderr) {
try { try {
writeInt(STDERR_NEXT, to); to << STDERR_NEXT;
writeString(buf, count, to); writeString(buf, count, to);
to.flush(); to.flush();
} catch (...) { } catch (...) {
@ -72,11 +72,10 @@ static void stopWork(bool success = true, const string & msg = "", unsigned int
canSendStderr = false; canSendStderr = false;
if (success) if (success)
writeInt(STDERR_LAST, to); to << STDERR_LAST;
else { else {
writeInt(STDERR_ERROR, to); to << STDERR_ERROR << msg;
writeString(msg, to); if (status != 0) to << status;
if (status != 0) writeInt(status, to);
} }
} }
@ -87,7 +86,7 @@ struct TunnelSink : Sink
TunnelSink(Sink & to) : to(to) { } TunnelSink(Sink & to) : to(to) { }
virtual void operator () (const unsigned char * data, size_t len) virtual void operator () (const unsigned char * data, size_t len)
{ {
writeInt(STDERR_WRITE, to); to << STDERR_WRITE;
writeString(data, len, to); writeString(data, len, to);
} }
}; };
@ -99,8 +98,7 @@ struct TunnelSource : BufferedSource
TunnelSource(Source & from) : from(from) { } TunnelSource(Source & from) : from(from) { }
size_t readUnbuffered(unsigned char * data, size_t len) size_t readUnbuffered(unsigned char * data, size_t len)
{ {
writeInt(STDERR_READ, to); to << STDERR_READ << len;
writeInt(len, to);
to.flush(); to.flush();
size_t n = readString(data, len, from); size_t n = readString(data, len, from);
if (n == 0) throw EndOfFile("unexpected end-of-file"); if (n == 0) throw EndOfFile("unexpected end-of-file");
@ -166,7 +164,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
assertStorePath(path); assertStorePath(path);
bool result = store->isValidPath(path); bool result = store->isValidPath(path);
stopWork(); stopWork();
writeInt(result, to); to << result;
break; break;
} }
@ -175,7 +173,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
PathSet res = store->queryValidPaths(paths); PathSet res = store->queryValidPaths(paths);
stopWork(); stopWork();
writeStrings(res, to); to << res;
break; break;
} }
@ -184,7 +182,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
PathSet res = store->querySubstitutablePaths(singleton<PathSet>(path)); PathSet res = store->querySubstitutablePaths(singleton<PathSet>(path));
stopWork(); stopWork();
writeInt(res.find(path) != res.end(), to); to << (res.find(path) != res.end());
break; break;
} }
@ -193,7 +191,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
PathSet res = store->querySubstitutablePaths(paths); PathSet res = store->querySubstitutablePaths(paths);
stopWork(); stopWork();
writeStrings(res, to); to << res;
break; break;
} }
@ -202,7 +200,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
Hash hash = store->queryPathHash(path); Hash hash = store->queryPathHash(path);
stopWork(); stopWork();
writeString(printHash(hash), to); to << printHash(hash);
break; break;
} }
@ -221,7 +219,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
paths = store->queryValidDerivers(path); paths = store->queryValidDerivers(path);
else paths = store->queryDerivationOutputs(path); else paths = store->queryDerivationOutputs(path);
stopWork(); stopWork();
writeStrings(paths, to); to << paths;
break; break;
} }
@ -231,7 +229,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
StringSet names; StringSet names;
names = store->queryDerivationOutputNames(path); names = store->queryDerivationOutputNames(path);
stopWork(); stopWork();
writeStrings(names, to); to << names;
break; break;
} }
@ -240,7 +238,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
Path deriver = store->queryDeriver(path); Path deriver = store->queryDeriver(path);
stopWork(); stopWork();
writeString(deriver, to); to << deriver;
break; break;
} }
@ -249,7 +247,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
Path path = store->queryPathFromHashPart(hashPart); Path path = store->queryPathFromHashPart(hashPart);
stopWork(); stopWork();
writeString(path, to); to << path;
break; break;
} }
@ -283,7 +281,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
->addToStoreFromDump(recursive ? savedNAR.s : savedRegular.s, baseName, recursive, hashAlgo); ->addToStoreFromDump(recursive ? savedNAR.s : savedRegular.s, baseName, recursive, hashAlgo);
stopWork(); stopWork();
writeString(path, to); to << path;
break; break;
} }
@ -294,7 +292,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
Path path = store->addTextToStore(suffix, s, refs); Path path = store->addTextToStore(suffix, s, refs);
stopWork(); stopWork();
writeString(path, to); to << path;
break; break;
} }
@ -305,7 +303,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
TunnelSink sink(to); TunnelSink sink(to);
store->exportPath(path, sign, sink); store->exportPath(path, sign, sink);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -314,7 +312,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
TunnelSource source(from); TunnelSource source(from);
Paths paths = store->importPaths(!trusted, source); Paths paths = store->importPaths(!trusted, source);
stopWork(); stopWork();
writeStrings(paths, to); to << paths;
break; break;
} }
@ -323,7 +321,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->buildPaths(drvs); store->buildPaths(drvs);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -332,7 +330,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->ensurePath(path); store->ensurePath(path);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -341,7 +339,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->addTempRoot(path); store->addTempRoot(path);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -350,7 +348,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->addIndirectRoot(path); store->addIndirectRoot(path);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -358,7 +356,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->syncWithGC(); store->syncWithGC();
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -366,11 +364,9 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
Roots roots = store->findRoots(); Roots roots = store->findRoots();
stopWork(); stopWork();
writeInt(roots.size(), to); to << roots.size();
for (auto & i : roots) { for (auto & i : roots)
writeString(i.first, to); to << i.first << i.second;
writeString(i.second, to);
}
break; break;
} }
@ -395,9 +391,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
store->collectGarbage(options, results); store->collectGarbage(options, results);
stopWork(); stopWork();
writeStrings(results.paths, to); to << results.paths << results.bytesFreed << 0 /* obsolete */;
writeLongLong(results.bytesFreed, to);
writeLongLong(0, to); // obsolete
break; break;
} }
@ -445,14 +439,11 @@ static void performOp(bool trusted, unsigned int clientVersion,
stopWork(); stopWork();
SubstitutablePathInfos::iterator i = infos.find(path); SubstitutablePathInfos::iterator i = infos.find(path);
if (i == infos.end()) if (i == infos.end())
writeInt(0, to); to << 0;
else { else {
writeInt(1, to); to << 1 << i->second.deriver << i->second.references << i->second.downloadSize;
writeString(i->second.deriver, to);
writeStrings(i->second.references, to);
writeLongLong(i->second.downloadSize, to);
if (GET_PROTOCOL_MINOR(clientVersion) >= 7) if (GET_PROTOCOL_MINOR(clientVersion) >= 7)
writeLongLong(i->second.narSize, to); to << i->second.narSize;
} }
break; break;
} }
@ -463,13 +454,10 @@ static void performOp(bool trusted, unsigned int clientVersion,
SubstitutablePathInfos infos; SubstitutablePathInfos infos;
store->querySubstitutablePathInfos(paths, infos); store->querySubstitutablePathInfos(paths, infos);
stopWork(); stopWork();
writeInt(infos.size(), to); to << infos.size();
for (auto & i : infos) { for (auto & i : infos) {
writeString(i.first, to); to << i.first << i.second.deriver << i.second.references
writeString(i.second.deriver, to); << i.second.downloadSize << i.second.narSize;
writeStrings(i.second.references, to);
writeLongLong(i.second.downloadSize, to);
writeLongLong(i.second.narSize, to);
} }
break; break;
} }
@ -478,7 +466,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
PathSet paths = store->queryAllValidPaths(); PathSet paths = store->queryAllValidPaths();
stopWork(); stopWork();
writeStrings(paths, to); to << paths;
break; break;
} }
@ -486,7 +474,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
PathSet paths = store->queryFailedPaths(); PathSet paths = store->queryFailedPaths();
stopWork(); stopWork();
writeStrings(paths, to); to << paths;
break; break;
} }
@ -495,7 +483,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->clearFailedPaths(paths); store->clearFailedPaths(paths);
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
} }
@ -504,11 +492,8 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
ValidPathInfo info = store->queryPathInfo(path); ValidPathInfo info = store->queryPathInfo(path);
stopWork(); stopWork();
writeString(info.deriver, to); to << info.deriver << printHash(info.hash) << info.references
writeString(printHash(info.hash), to); << info.registrationTime << info.narSize;
writeStrings(info.references, to);
writeInt(info.registrationTime, to);
writeLongLong(info.narSize, to);
break; break;
} }
@ -516,7 +501,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
startWork(); startWork();
store->optimiseStore(); store->optimiseStore();
stopWork(); stopWork();
writeInt(1, to); to << 1;
break; break;
case wopVerifyStore: { case wopVerifyStore: {
@ -527,7 +512,7 @@ static void performOp(bool trusted, unsigned int clientVersion,
throw Error("you are not privileged to repair paths"); throw Error("you are not privileged to repair paths");
bool errors = store->verifyStore(checkContents, repair); bool errors = store->verifyStore(checkContents, repair);
stopWork(); stopWork();
writeInt(errors, to); to << errors;
break; break;
} }
@ -547,8 +532,7 @@ static void processConnection(bool trusted)
/* Exchange the greeting. */ /* Exchange the greeting. */
unsigned int magic = readInt(from); unsigned int magic = readInt(from);
if (magic != WORKER_MAGIC_1) throw Error("protocol mismatch"); if (magic != WORKER_MAGIC_1) throw Error("protocol mismatch");
writeInt(WORKER_MAGIC_2, to); to << WORKER_MAGIC_2 << PROTOCOL_VERSION;
writeInt(PROTOCOL_VERSION, to);
to.flush(); to.flush();
unsigned int clientVersion = readInt(from); unsigned int clientVersion = readInt(from);

View file

@ -851,8 +851,7 @@ static void opServe(Strings opFlags, Strings opArgs)
/* Exchange the greeting. */ /* Exchange the greeting. */
unsigned int magic = readInt(in); unsigned int magic = readInt(in);
if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch"); if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch");
writeInt(SERVE_MAGIC_2, out); out << SERVE_MAGIC_2 << SERVE_PROTOCOL_VERSION;
writeInt(SERVE_PROTOCOL_VERSION, out);
out.flush(); out.flush();
readInt(in); // Client version, unused for now readInt(in); // Client version, unused for now
@ -906,7 +905,7 @@ static void opServe(Strings opFlags, Strings opArgs)
} }
} }
writeStrings(store->queryValidPaths(paths), out); out << store->queryValidPaths(paths);
break; break;
} }
@ -917,14 +916,12 @@ static void opServe(Strings opFlags, Strings opArgs)
if (!store->isValidPath(i)) if (!store->isValidPath(i))
continue; continue;
ValidPathInfo info = store->queryPathInfo(i); ValidPathInfo info = store->queryPathInfo(i);
writeString(info.path, out); out << info.path << info.deriver << info.references;
writeString(info.deriver, out);
writeStrings(info.references, out);
// !!! Maybe we want compression? // !!! Maybe we want compression?
writeLongLong(info.narSize, out); // downloadSize out << info.narSize // downloadSize
writeLongLong(info.narSize, out); << info.narSize;
} }
writeString("", out); out << "";
break; break;
} }
@ -935,7 +932,7 @@ static void opServe(Strings opFlags, Strings opArgs)
case cmdImportPaths: { case cmdImportPaths: {
if (!writeAllowed) throw Error("importing paths is not allowed"); if (!writeAllowed) throw Error("importing paths is not allowed");
store->importPaths(false, in); store->importPaths(false, in);
writeInt(1, out); // indicate success out << 1; // indicate success
break; break;
} }
@ -957,11 +954,10 @@ static void opServe(Strings opFlags, Strings opArgs)
try { try {
MonitorFdHup monitor(in.fd); MonitorFdHup monitor(in.fd);
store->buildPaths(paths); store->buildPaths(paths);
writeInt(0, out); out << 0;
} catch (Error & e) { } catch (Error & e) {
assert(e.status); assert(e.status);
writeInt(e.status, out); out << e.status << e.msg();
writeString(e.msg(), out);
} }
break; break;
} }
@ -979,8 +975,7 @@ static void opServe(Strings opFlags, Strings opArgs)
MonitorFdHup monitor(in.fd); MonitorFdHup monitor(in.fd);
auto status = store->buildDerivation(drvPath, drv); auto status = store->buildDerivation(drvPath, drv);
writeInt(status.status, out); out << status.status << status.errorMsg;
writeString(status.errorMsg, out);
break; break;
} }
@ -991,7 +986,7 @@ static void opServe(Strings opFlags, Strings opArgs)
PathSet closure; PathSet closure;
for (auto & i : paths) for (auto & i : paths)
computeFSClosure(*store, i, closure, false, includeOutputs); computeFSClosure(*store, i, closure, false, includeOutputs);
writeStrings(closure, out); out << closure;
break; break;
} }