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)
{
writeInt(cmdDumpStorePath, pipes.first);
writeString(storePath, pipes.first);
pipes.first << cmdDumpStorePath << storePath;
pipes.first.flush();
restorePath(destPath, pipes.second);
std::cout << std::endl;
@ -58,17 +57,17 @@ static void query(std::pair<FdSink, FdSource> & pipes)
string cmd = tokenized.front();
tokenized.pop_front();
if (cmd == "have") {
writeInt(cmdQueryValidPaths, pipes.first);
writeInt(0, pipes.first); // don't lock
writeInt(0, pipes.first); // don't substitute
writeStrings(tokenized, pipes.first);
pipes.first
<< cmdQueryValidPaths
<< 0 // don't lock
<< 0 // don't substitute
<< tokenized;
pipes.first.flush();
PathSet paths = readStrings<PathSet>(pipes.second);
for (auto & i : paths)
std::cout << i << std::endl;
} else if (cmd == "info") {
writeInt(cmdQueryPathInfos, pipes.first);
writeStrings(tokenized, pipes.first);
pipes.first << cmdQueryPathInfos << tokenized;
pipes.first.flush();
while (1) {
Path path = readString(pipes.second);
@ -116,13 +115,13 @@ int main(int argc, char * * argv)
std::pair<FdSink, FdSource> pipes = connect(host);
/* Exchange the greeting */
writeInt(SERVE_MAGIC_1, pipes.first);
pipes.first << SERVE_MAGIC_1;
pipes.first.flush();
unsigned int magic = readInt(pipes.second);
if (magic != SERVE_MAGIC_2)
throw Error("protocol mismatch");
readInt(pipes.second); // Server version, unused for now
writeInt(SERVE_PROTOCOL_VERSION, pipes.first);
pipes.first << SERVE_PROTOCOL_VERSION;
pipes.first.flush();
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
% printHash(storedHash) % printHash(hash));
writeInt(EXPORT_MAGIC, hashAndWriteSink);
writeString(path, hashAndWriteSink);
PathSet references;
queryReferences(path, references);
writeStrings(references, hashAndWriteSink);
Path deriver = queryDeriver(path);
writeString(deriver, hashAndWriteSink);
hashAndWriteSink << EXPORT_MAGIC << path << references << queryDeriver(path);
if (sign) {
Hash hash = hashAndWriteSink.currentHash();
writeInt(1, hashAndWriteSink);
Path tmpDir = createTempDir();
AutoDelete delTmp(tmpDir);
Path hashFile = tmpDir + "/hash";
@ -1561,10 +1553,10 @@ void LocalStore::exportPath(const Path & path, bool sign,
args.push_back(hashFile);
string signature = runProgram(OPENSSL_PATH, true, args);
writeString(signature, hashAndWriteSink);
hashAndWriteSink << 1 << signature;
} 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. */
try {
writeInt(WORKER_MAGIC_1, to);
to << WORKER_MAGIC_1;
to.flush();
unsigned int magic = readInt(from);
if (magic != WORKER_MAGIC_2) throw Error("protocol mismatch");
@ -71,19 +71,18 @@ void RemoteStore::openConnection(bool reserveSpace)
daemonVersion = readInt(from);
if (GET_PROTOCOL_MAJOR(daemonVersion) != GET_PROTOCOL_MAJOR(PROTOCOL_VERSION))
throw Error("Nix daemon protocol version not supported");
writeInt(PROTOCOL_VERSION, to);
to << PROTOCOL_VERSION;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 14) {
int cpu = settings.lockCPU ? lockToCurrentCPU() : -1;
if (cpu != -1) {
writeInt(1, to);
writeInt(cpu, to);
} else
writeInt(0, to);
if (cpu != -1)
to << 1 << cpu;
else
to << 0;
}
if (GET_PROTOCOL_MINOR(daemonVersion) >= 11)
writeInt(reserveSpace, to);
to << reserveSpace;
processStderr();
}
@ -141,35 +140,31 @@ RemoteStore::~RemoteStore()
void RemoteStore::setOptions()
{
writeInt(wopSetOptions, to);
writeInt(settings.keepFailed, to);
writeInt(settings.keepGoing, to);
writeInt(settings.tryFallback, to);
writeInt(verbosity, to);
writeInt(settings.maxBuildJobs, to);
writeInt(settings.maxSilentTime, to);
to << wopSetOptions
<< settings.keepFailed
<< settings.keepGoing
<< settings.tryFallback
<< verbosity
<< settings.maxBuildJobs
<< settings.maxSilentTime;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 2)
writeInt(settings.useBuildHook, to);
if (GET_PROTOCOL_MINOR(daemonVersion) >= 4) {
writeInt(settings.buildVerbosity, to);
writeInt(logType, to);
writeInt(settings.printBuildTrace, to);
}
to << settings.useBuildHook;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 4)
to << settings.buildVerbosity
<< logType
<< settings.printBuildTrace;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 6)
writeInt(settings.buildCores, to);
to << settings.buildCores;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 10)
writeInt(settings.useSubstitutes, to);
to << settings.useSubstitutes;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 12) {
Settings::SettingsMap overrides = settings.getOverrides();
if (overrides["ssh-auth-sock"] == "")
overrides["ssh-auth-sock"] = getEnv("SSH_AUTH_SOCK");
writeInt(overrides.size(), to);
for (auto & i : overrides) {
writeString(i.first, to);
writeString(i.second, to);
}
to << overrides.size();
for (auto & i : overrides)
to << i.first << i.second;
}
processStderr();
@ -179,8 +174,7 @@ void RemoteStore::setOptions()
bool RemoteStore::isValidPath(const Path & path)
{
openConnection();
writeInt(wopIsValidPath, to);
writeString(path, to);
to << wopIsValidPath << path;
processStderr();
unsigned int reply = readInt(from);
return reply != 0;
@ -196,8 +190,7 @@ PathSet RemoteStore::queryValidPaths(const PathSet & paths)
if (isValidPath(i)) res.insert(i);
return res;
} else {
writeInt(wopQueryValidPaths, to);
writeStrings(paths, to);
to << wopQueryValidPaths << paths;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -207,7 +200,7 @@ PathSet RemoteStore::queryValidPaths(const PathSet & paths)
PathSet RemoteStore::queryAllValidPaths()
{
openConnection();
writeInt(wopQueryAllValidPaths, to);
to << wopQueryAllValidPaths;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -219,15 +212,13 @@ PathSet RemoteStore::querySubstitutablePaths(const PathSet & paths)
if (GET_PROTOCOL_MINOR(daemonVersion) < 12) {
PathSet res;
for (auto & i : paths) {
writeInt(wopHasSubstitutes, to);
writeString(i, to);
to << wopHasSubstitutes << i;
processStderr();
if (readInt(from)) res.insert(i);
}
return res;
} else {
writeInt(wopQuerySubstitutablePaths, to);
writeStrings(paths, to);
to << wopQuerySubstitutablePaths << paths;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -247,8 +238,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
for (auto & i : paths) {
SubstitutablePathInfo info;
writeInt(wopQuerySubstitutablePathInfo, to);
writeString(i, to);
to << wopQuerySubstitutablePathInfo << i;
processStderr();
unsigned int reply = readInt(from);
if (reply == 0) continue;
@ -262,8 +252,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
} else {
writeInt(wopQuerySubstitutablePathInfos, to);
writeStrings(paths, to);
to << wopQuerySubstitutablePathInfos << paths;
processStderr();
unsigned int count = readInt(from);
for (unsigned int n = 0; n < count; n++) {
@ -283,8 +272,7 @@ void RemoteStore::querySubstitutablePathInfos(const PathSet & paths,
ValidPathInfo RemoteStore::queryPathInfo(const Path & path)
{
openConnection();
writeInt(wopQueryPathInfo, to);
writeString(path, to);
to << wopQueryPathInfo << path;
processStderr();
ValidPathInfo info;
info.path = path;
@ -301,8 +289,7 @@ ValidPathInfo RemoteStore::queryPathInfo(const Path & path)
Hash RemoteStore::queryPathHash(const Path & path)
{
openConnection();
writeInt(wopQueryPathHash, to);
writeString(path, to);
to << wopQueryPathHash << path;
processStderr();
string hash = readString(from);
return parseHash(htSHA256, hash);
@ -313,8 +300,7 @@ void RemoteStore::queryReferences(const Path & path,
PathSet & references)
{
openConnection();
writeInt(wopQueryReferences, to);
writeString(path, to);
to << wopQueryReferences << path;
processStderr();
PathSet references2 = readStorePaths<PathSet>(from);
references.insert(references2.begin(), references2.end());
@ -325,8 +311,7 @@ void RemoteStore::queryReferrers(const Path & path,
PathSet & referrers)
{
openConnection();
writeInt(wopQueryReferrers, to);
writeString(path, to);
to << wopQueryReferrers << path;
processStderr();
PathSet referrers2 = readStorePaths<PathSet>(from);
referrers.insert(referrers2.begin(), referrers2.end());
@ -336,8 +321,7 @@ void RemoteStore::queryReferrers(const Path & path,
Path RemoteStore::queryDeriver(const Path & path)
{
openConnection();
writeInt(wopQueryDeriver, to);
writeString(path, to);
to << wopQueryDeriver << path;
processStderr();
Path drvPath = readString(from);
if (drvPath != "") assertStorePath(drvPath);
@ -348,8 +332,7 @@ Path RemoteStore::queryDeriver(const Path & path)
PathSet RemoteStore::queryValidDerivers(const Path & path)
{
openConnection();
writeInt(wopQueryValidDerivers, to);
writeString(path, to);
to << wopQueryValidDerivers << path;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -358,8 +341,7 @@ PathSet RemoteStore::queryValidDerivers(const Path & path)
PathSet RemoteStore::queryDerivationOutputs(const Path & path)
{
openConnection();
writeInt(wopQueryDerivationOutputs, to);
writeString(path, to);
to << wopQueryDerivationOutputs << path;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -368,8 +350,7 @@ PathSet RemoteStore::queryDerivationOutputs(const Path & path)
PathSet RemoteStore::queryDerivationOutputNames(const Path & path)
{
openConnection();
writeInt(wopQueryDerivationOutputNames, to);
writeString(path, to);
to << wopQueryDerivationOutputNames << path;
processStderr();
return readStrings<PathSet>(from);
}
@ -378,8 +359,7 @@ PathSet RemoteStore::queryDerivationOutputNames(const Path & path)
Path RemoteStore::queryPathFromHashPart(const string & hashPart)
{
openConnection();
writeInt(wopQueryPathFromHashPart, to);
writeString(hashPart, to);
to << wopQueryPathFromHashPart << hashPart;
processStderr();
Path path = readString(from);
if (!path.empty()) assertStorePath(path);
@ -396,12 +376,10 @@ Path RemoteStore::addToStore(const string & name, const Path & _srcPath,
Path srcPath(absPath(_srcPath));
writeInt(wopAddToStore, to);
writeString(name, to);
/* backwards compatibility hack */
writeInt((hashAlgo == htSHA256 && recursive) ? 0 : 1, to);
writeInt(recursive ? 1 : 0, to);
writeString(printHashType(hashAlgo), to);
to << wopAddToStore << name
<< ((hashAlgo == htSHA256 && recursive) ? 0 : 1) /* backwards compatibility hack */
<< (recursive ? 1 : 0)
<< printHashType(hashAlgo);
try {
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");
openConnection();
writeInt(wopAddTextToStore, to);
writeString(name, to);
writeString(s, to);
writeStrings(references, to);
to << wopAddTextToStore << name << s << references;
processStderr();
return readStorePath(from);
@ -443,9 +418,7 @@ void RemoteStore::exportPath(const Path & path, bool sign,
Sink & sink)
{
openConnection();
writeInt(wopExportPath, to);
writeString(path, to);
writeInt(sign ? 1 : 0, to);
to << wopExportPath << path << (sign ? 1 : 0);
processStderr(&sink); /* sink receives the actual data */
readInt(from);
}
@ -454,7 +427,7 @@ void RemoteStore::exportPath(const Path & path, bool sign,
Paths RemoteStore::importPaths(bool requireSignature, Source & source)
{
openConnection();
writeInt(wopImportPaths, to);
to << wopImportPaths;
/* We ignore requireSignature, since the worker forces it to true
anyway. */
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");
openConnection();
writeInt(wopBuildPaths, to);
to << wopBuildPaths;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 13)
writeStrings(drvPaths, to);
to << drvPaths;
else {
/* For backwards compatibility with old daemons, strip output
identifiers. */
PathSet drvPaths2;
for (auto & i : drvPaths)
drvPaths2.insert(string(i, 0, i.find('!')));
writeStrings(drvPaths2, to);
to << drvPaths2;
}
processStderr();
readInt(from);
@ -492,8 +465,7 @@ BuildResult RemoteStore::buildDerivation(const Path & drvPath, const BasicDeriva
void RemoteStore::ensurePath(const Path & path)
{
openConnection();
writeInt(wopEnsurePath, to);
writeString(path, to);
to << wopEnsurePath << path;
processStderr();
readInt(from);
}
@ -502,8 +474,7 @@ void RemoteStore::ensurePath(const Path & path)
void RemoteStore::addTempRoot(const Path & path)
{
openConnection();
writeInt(wopAddTempRoot, to);
writeString(path, to);
to << wopAddTempRoot << path;
processStderr();
readInt(from);
}
@ -512,8 +483,7 @@ void RemoteStore::addTempRoot(const Path & path)
void RemoteStore::addIndirectRoot(const Path & path)
{
openConnection();
writeInt(wopAddIndirectRoot, to);
writeString(path, to);
to << wopAddIndirectRoot << path;
processStderr();
readInt(from);
}
@ -522,7 +492,7 @@ void RemoteStore::addIndirectRoot(const Path & path)
void RemoteStore::syncWithGC()
{
openConnection();
writeInt(wopSyncWithGC, to);
to << wopSyncWithGC;
processStderr();
readInt(from);
}
@ -531,7 +501,7 @@ void RemoteStore::syncWithGC()
Roots RemoteStore::findRoots()
{
openConnection();
writeInt(wopFindRoots, to);
to << wopFindRoots;
processStderr();
unsigned int count = readInt(from);
Roots result;
@ -548,17 +518,11 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
{
openConnection(false);
writeInt(wopCollectGarbage, to);
writeInt(options.action, to);
writeStrings(options.pathsToDelete, to);
writeInt(options.ignoreLiveness, to);
writeLongLong(options.maxFreed, to);
writeInt(0, to);
if (GET_PROTOCOL_MINOR(daemonVersion) >= 5) {
to << wopCollectGarbage << options.action << options.pathsToDelete << options.ignoreLiveness
<< options.maxFreed << 0;
if (GET_PROTOCOL_MINOR(daemonVersion) >= 5)
/* removed options */
writeInt(0, to);
writeInt(0, to);
}
to << 0 << 0;
processStderr();
@ -571,7 +535,7 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
PathSet RemoteStore::queryFailedPaths()
{
openConnection();
writeInt(wopQueryFailedPaths, to);
to << wopQueryFailedPaths;
processStderr();
return readStorePaths<PathSet>(from);
}
@ -580,8 +544,7 @@ PathSet RemoteStore::queryFailedPaths()
void RemoteStore::clearFailedPaths(const PathSet & paths)
{
openConnection();
writeInt(wopClearFailedPaths, to);
writeStrings(paths, to);
to << wopClearFailedPaths << paths;
processStderr();
readInt(from);
}
@ -589,7 +552,7 @@ void RemoteStore::clearFailedPaths(const PathSet & paths)
void RemoteStore::optimiseStore()
{
openConnection();
writeInt(wopOptimiseStore, to);
to << wopOptimiseStore;
processStderr();
readInt(from);
}
@ -597,9 +560,7 @@ void RemoteStore::optimiseStore()
bool RemoteStore::verifyStore(bool checkContents, bool repair)
{
openConnection();
writeInt(wopVerifyStore, to);
writeInt(checkContents, to);
writeInt(repair, to);
to << wopVerifyStore << checkContents << repair;
processStderr();
return readInt(from) != 0;
}

View file

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

View file

@ -16,11 +16,11 @@ BufferedSink::~BufferedSink()
delete[] buffer;
}
void BufferedSink::operator () (const unsigned char * data, size_t len)
{
if (!buffer) buffer = new unsigned char[bufSize];
while (len) {
/* Optimisation: bypass the buffer if the data exceeds the
buffer size. */
@ -96,7 +96,7 @@ size_t BufferedSource::read(unsigned char * data, size_t len)
if (!buffer) buffer = new unsigned char[bufSize];
if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize);
/* Copy out the data in the buffer. */
size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
memcpy(data, buffer + bufPosOut, n);
@ -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)
{
writeInt(len, sink);
sink << len;
sink(buf, len);
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);
}
Sink & operator << (Sink & out, const string & s)
{
writeString(s, out);
return out;
return sink;
}
template<class T> void writeStrings(const T & ss, Sink & sink)
{
writeInt(ss.size(), sink);
sink << ss.size();
for (auto & i : ss)
writeString(i, sink);
sink << i;
}
template void writeStrings(const Paths & ss, Sink & sink);
template void writeStrings(const PathSet & ss, Sink & sink);
Sink & operator << (Sink & out, const Strings & s)
Sink & operator << (Sink & sink, const Strings & s)
{
writeStrings(s, out);
return out;
writeStrings(s, sink);
return sink;
}
Sink & operator << (Sink & out, const StringSet & s)
Sink & operator << (Sink & sink, const StringSet & s)
{
writeStrings(s, out);
return out;
writeStrings(s, sink);
return sink;
}
@ -271,7 +230,7 @@ size_t readString(unsigned char * buf, size_t max, Source & source)
return len;
}
string readString(Source & source)
{
size_t len = readInt(source);

View file

@ -1,13 +1,14 @@
#pragma once
#include "types.hh"
#include "util.hh"
namespace nix {
/* Abstract destination of binary data. */
struct Sink
struct Sink
{
virtual ~Sink() { }
virtual void operator () (const unsigned char * data, size_t len) = 0;
@ -25,9 +26,9 @@ struct BufferedSink : Sink
~BufferedSink();
void operator () (const unsigned char * data, size_t len);
void flush();
virtual void write(const unsigned char * data, size_t len) = 0;
};
@ -36,7 +37,7 @@ struct BufferedSink : Sink
struct Source
{
virtual ~Source() { }
/* Store exactly len bytes in the buffer pointed to by data.
It blocks until all the requested data is available, or throws
an error if it is not going to be available. */
@ -58,9 +59,9 @@ struct BufferedSource : Source
BufferedSource(size_t bufSize = 32 * 1024)
: bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { }
~BufferedSource();
size_t read(unsigned char * data, size_t len);
/* Underlying read call, to be overridden. */
virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
@ -78,7 +79,7 @@ struct FdSink : BufferedSink
FdSink() : fd(-1), warn(false), written(0) { }
FdSink(int fd) : fd(fd), warn(false), written(0) { }
~FdSink();
void write(const unsigned char * data, size_t len);
};
@ -107,21 +108,31 @@ struct StringSource : Source
const string & s;
size_t pos;
StringSource(const string & _s) : s(_s), pos(0) { }
size_t read(unsigned char * data, size_t len);
size_t read(unsigned char * data, size_t len);
};
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 string & s, Sink & sink);
template<class T> void writeStrings(const T & ss, Sink & sink);
Sink & operator << (Sink & out, unsigned int n);
Sink & operator << (Sink & out, const string & s);
Sink & operator << (Sink & out, const Strings & s);
Sink & operator << (Sink & out, const StringSet & s);
inline Sink & operator << (Sink & sink, uint64_t n)
{
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));
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);

View file

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

View file

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