From 6a313c691b15a1f338e0885c5c593eae4f5509eb Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <edolstra@gmail.com>
Date: Thu, 6 Oct 2016 15:24:09 +0200
Subject: [PATCH] hydra-queue-runner: Fix build

---
 src/hydra-queue-runner/build-remote.cc       | 28 +++++++++---------
 src/hydra-queue-runner/build-result.cc       |  2 +-
 src/hydra-queue-runner/hydra-queue-runner.cc | 31 ++++++++------------
 3 files changed, 27 insertions(+), 34 deletions(-)

diff --git a/src/hydra-queue-runner/build-remote.cc b/src/hydra-queue-runner/build-remote.cc
index a1ddb5fe..58e26176 100644
--- a/src/hydra-queue-runner/build-remote.cc
+++ b/src/hydra-queue-runner/build-remote.cc
@@ -35,10 +35,10 @@ static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Chil
 
     child.pid = startProcess([&]() {
 
-        if (dup2(to.readSide, STDIN_FILENO) == -1)
+        if (dup2(to.readSide.get(), STDIN_FILENO) == -1)
             throw SysError("cannot dup input pipe to stdin");
 
-        if (dup2(from.writeSide, STDOUT_FILENO) == -1)
+        if (dup2(from.writeSide.get(), STDOUT_FILENO) == -1)
             throw SysError("cannot dup output pipe to stdout");
 
         if (dup2(stderrFD, STDERR_FILENO) == -1)
@@ -67,11 +67,11 @@ static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Chil
         throw SysError("cannot start ssh");
     });
 
-    to.readSide.close();
-    from.writeSide.close();
+    to.readSide = -1;
+    from.writeSide = -1;
 
-    child.to = to.writeSide.borrow();
-    child.from = from.readSide.borrow();
+    child.to = to.writeSide.release();
+    child.from = from.readSide.release();
 }
 
 
@@ -93,7 +93,7 @@ static void copyClosureTo(ref<Store> destStore,
 
     /* Get back the set of paths that are already valid on the remote
        host. */
-    auto present = readStorePaths<PathSet>(from);
+    auto present = readStorePaths<PathSet>(*destStore, from);
 
     if (present.size() == closure.size()) return;
 
@@ -125,8 +125,8 @@ void State::buildRemote(ref<Store> destStore,
 
     createDirs(dirOf(result.logFile));
 
-    AutoCloseFD logFD(open(result.logFile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666));
-    if (logFD == -1) throw SysError(format("creating log file ‘%1%’") % result.logFile);
+    AutoCloseFD logFD = open(result.logFile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
+    if (!logFD) throw SysError(format("creating log file ‘%1%’") % result.logFile);
 
     nix::Path tmpDir = createTempDir();
     AutoDelete tmpDirDel(tmpDir, true);
@@ -134,12 +134,12 @@ void State::buildRemote(ref<Store> destStore,
     try {
 
         Child child;
-        openConnection(machine, tmpDir, logFD, child);
+        openConnection(machine, tmpDir, logFD.get(), child);
 
-        logFD.close();
+        logFD = -1;
 
-        FdSource from(child.from);
-        FdSink to(child.to);
+        FdSource from(child.from.get());
+        FdSink to(child.to.get());
 
         Finally updateStats([&]() {
             bytesReceived += from.read;
@@ -368,7 +368,7 @@ void State::buildRemote(ref<Store> destStore,
         }
 
         /* Shut down the connection. */
-        child.to.close();
+        child.to = -1;
         child.pid.wait(true);
 
     } catch (Error & e) {
diff --git a/src/hydra-queue-runner/build-result.cc b/src/hydra-queue-runner/build-result.cc
index 5a5177ce..a39cf612 100644
--- a/src/hydra-queue-runner/build-result.cc
+++ b/src/hydra-queue-runner/build-result.cc
@@ -65,7 +65,7 @@ BuildOutput getBuildOutput(nix::ref<Store> store,
             // store paths, or that are outside the input closure?
             if (product.path == "" || product.path[0] != '/') continue;
             product.path = canonPath(product.path);
-            if (!isInStore(product.path)) continue;
+            if (!store->isInStore(product.path)) continue;
 
             auto st = accessor->stat(product.path);
             if (st.type == FSAccessor::Type::tMissing) continue;
diff --git a/src/hydra-queue-runner/hydra-queue-runner.cc b/src/hydra-queue-runner/hydra-queue-runner.cc
index 6c7fc14b..27d2a023 100644
--- a/src/hydra-queue-runner/hydra-queue-runner.cc
+++ b/src/hydra-queue-runner/hydra-queue-runner.cc
@@ -11,7 +11,8 @@
 
 #include "shared.hh"
 #include "globals.hh"
-#include "value-to-json.hh"
+#include "json.hh"
+#include "s3-binary-cache-store.hh"
 
 using namespace nix;
 
@@ -451,7 +452,7 @@ void State::logCompressor()
             // FIXME: use libbz2
 
             Pid pid = startProcess([&]() {
-                if (dup2(fd, STDOUT_FILENO) == -1)
+                if (dup2(fd.get(), STDOUT_FILENO) == -1)
                     throw SysError("cannot dup output pipe to stdout");
                 execlp("bzip2", "bzip2", "-c", item.logPath.c_str(), nullptr);
                 throw SysError("cannot start bzip2");
@@ -582,7 +583,7 @@ void State::dumpStatus(Connection & conn, bool log)
         root.attr("bytesReceived", bytesReceived);
         root.attr("nrBuildsRead", nrBuildsRead);
         root.attr("buildReadTimeMs", buildReadTimeMs);
-        root.attr("buildReadTimeAvgMs", nrBuildsRead == 0 ? 0.0 :  (float) buildReadTimeMs / nrBuildsRead);
+        root.attr("buildReadTimeAvgMs", nrBuildsRead == 0 ? 0.0 : (float) buildReadTimeMs / nrBuildsRead);
         root.attr("nrBuildsDone", nrBuildsDone);
         root.attr("nrStepsStarted", nrStepsStarted);
         root.attr("nrStepsDone", nrStepsDone);
@@ -603,14 +604,12 @@ void State::dumpStatus(Connection & conn, bool log)
         root.attr("memoryTokensInUse", memoryTokens.currentUse());
 
         {
-            root.attr("machines");
-            JSONObject nested(out);
+            auto nested = root.object("machines");
             auto machines_(machines.lock());
             for (auto & i : *machines_) {
                 auto & m(i.second);
                 auto & s(m->state);
-                nested.attr(m->sshName);
-                JSONObject nested2(out);
+                auto nested2 = nested.object(m->sshName);
                 nested2.attr("enabled", m->enabled);
                 nested2.attr("currentJobs", s->currentJobs);
                 if (s->currentJobs == 0)
@@ -631,24 +630,20 @@ void State::dumpStatus(Connection & conn, bool log)
         }
 
         {
-            root.attr("jobsets");
-            JSONObject nested(out);
+            auto nested = root.object("jobsets");
             auto jobsets_(jobsets.lock());
             for (auto & jobset : *jobsets_) {
-                nested.attr(jobset.first.first + ":" + jobset.first.second);
-                JSONObject nested2(out);
+                auto nested2 = nested.object(jobset.first.first + ":" + jobset.first.second);
                 nested2.attr("shareUsed", jobset.second->shareUsed());
                 nested2.attr("seconds", jobset.second->getSeconds());
             }
         }
 
         {
-            root.attr("machineTypes");
-            JSONObject nested(out);
+            auto nested = root.object("machineTypes");
             auto machineTypes_(machineTypes.lock());
             for (auto & i : *machineTypes_) {
-                nested.attr(i.first);
-                JSONObject nested2(out);
+                auto nested2 = nested.object(i.first);
                 nested2.attr("runnable", i.second.runnable);
                 nested2.attr("running", i.second.running);
                 if (i.second.runnable > 0)
@@ -661,8 +656,7 @@ void State::dumpStatus(Connection & conn, bool log)
 
         auto store = getDestStore();
 
-        root.attr("store");
-        JSONObject nested(out);
+        auto nested = root.object("store");
 
         auto & stats = store->getStats();
         nested.attr("narInfoRead", stats.narInfoRead);
@@ -689,8 +683,7 @@ void State::dumpStatus(Connection & conn, bool log)
 
         auto s3Store = dynamic_cast<S3BinaryCacheStore *>(&*store);
         if (s3Store) {
-            nested.attr("s3");
-            JSONObject nested2(out);
+            auto nested2 = nested.object("s3");
             auto & s3Stats = s3Store->getS3Stats();
             nested2.attr("put", s3Stats.put);
             nested2.attr("putBytes", s3Stats.putBytes);