about summary refs log tree commit diff
path: root/third_party/nix/src/build-remote/build-remote.cc
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-17T15·31+0100
committerVincent Ambo <tazjin@google.com>2020-05-17T15·31+0100
commit0f2cf531f705d370321843e5ba9135b2ebdb5d19 (patch)
tree256feb13963a849ed96e89228fa05454c2a22363 /third_party/nix/src/build-remote/build-remote.cc
parent65a1aae98ce5a237c9643e639e550c8b0c0be7f1 (diff)
style(3p/nix): Reformat project in Google C++ style r/740
Reformatted with:

    fd . -e hh -e cc | xargs clang-format -i
Diffstat (limited to 'third_party/nix/src/build-remote/build-remote.cc')
-rw-r--r--third_party/nix/src/build-remote/build-remote.cc411
1 files changed, 210 insertions, 201 deletions
diff --git a/third_party/nix/src/build-remote/build-remote.cc b/third_party/nix/src/build-remote/build-remote.cc
index 279ae62f69cc..42a93bae8a0d 100644
--- a/third_party/nix/src/build-remote/build-remote.cc
+++ b/third_party/nix/src/build-remote/build-remote.cc
@@ -1,266 +1,275 @@
+#include <algorithm>
 #include <cstdlib>
 #include <cstring>
-#include <algorithm>
-#include <set>
+#include <iomanip>
 #include <memory>
+#include <set>
 #include <tuple>
-#include <iomanip>
 #if __APPLE__
 #include <sys/time.h>
 #endif
 
+#include "derivations.hh"
+#include "globals.hh"
+#include "legacy.hh"
+#include "local-store.hh"
 #include "machines.hh"
-#include "shared.hh"
 #include "pathlocks.hh"
-#include "globals.hh"
 #include "serialise.hh"
+#include "shared.hh"
 #include "store-api.hh"
-#include "derivations.hh"
-#include "local-store.hh"
-#include "legacy.hh"
 
 using namespace nix;
 using std::cin;
 
-static void handleAlarm(int sig) {
-}
+static void handleAlarm(int sig) {}
 
-std::string escapeUri(std::string uri)
-{
-    std::replace(uri.begin(), uri.end(), '/', '_');
-    return uri;
+std::string escapeUri(std::string uri) {
+  std::replace(uri.begin(), uri.end(), '/', '_');
+  return uri;
 }
 
 static string currentLoad;
 
-static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
-{
-    return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true);
+static AutoCloseFD openSlotLock(const Machine& m, unsigned long long slot) {
+  return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot),
+                      true);
 }
 
 static bool allSupportedLocally(const std::set<std::string>& requiredFeatures) {
-    for (auto & feature : requiredFeatures)
-        if (!settings.systemFeatures.get().count(feature)) return false;
-    return true;
+  for (auto& feature : requiredFeatures)
+    if (!settings.systemFeatures.get().count(feature)) return false;
+  return true;
 }
 
-static int _main(int argc, char * * argv)
-{
-    {
-        logger = makeJSONLogger(*logger);
+static int _main(int argc, char** argv) {
+  {
+    logger = makeJSONLogger(*logger);
 
-        /* Ensure we don't get any SSH passphrase or host key popups. */
-        unsetenv("DISPLAY");
-        unsetenv("SSH_ASKPASS");
+    /* Ensure we don't get any SSH passphrase or host key popups. */
+    unsetenv("DISPLAY");
+    unsetenv("SSH_ASKPASS");
 
-        if (argc != 2)
-            throw UsageError("called without required arguments");
+    if (argc != 2) throw UsageError("called without required arguments");
 
-        verbosity = (Verbosity) std::stoll(argv[1]);
+    verbosity = (Verbosity)std::stoll(argv[1]);
 
-        FdSource source(STDIN_FILENO);
+    FdSource source(STDIN_FILENO);
 
-        /* Read the parent's settings. */
-        while (readInt(source)) {
-            auto name = readString(source);
-            auto value = readString(source);
-            settings.set(name, value);
-        }
+    /* Read the parent's settings. */
+    while (readInt(source)) {
+      auto name = readString(source);
+      auto value = readString(source);
+      settings.set(name, value);
+    }
 
-        settings.maxBuildJobs.set("1"); // hack to make tests with local?root= work
+    settings.maxBuildJobs.set("1");  // hack to make tests with local?root= work
 
-        initPlugins();
+    initPlugins();
 
-        auto store = openStore().cast<LocalStore>();
+    auto store = openStore().cast<LocalStore>();
 
-        /* It would be more appropriate to use $XDG_RUNTIME_DIR, since
-           that gets cleared on reboot, but it wouldn't work on macOS. */
-        currentLoad = store->stateDir + "/current-load";
+    /* It would be more appropriate to use $XDG_RUNTIME_DIR, since
+       that gets cleared on reboot, but it wouldn't work on macOS. */
+    currentLoad = store->stateDir + "/current-load";
 
-        std::shared_ptr<Store> sshStore;
-        AutoCloseFD bestSlotLock;
+    std::shared_ptr<Store> sshStore;
+    AutoCloseFD bestSlotLock;
 
-        auto machines = getMachines();
-        debug("got %d remote builders", machines.size());
+    auto machines = getMachines();
+    debug("got %d remote builders", machines.size());
 
-        if (machines.empty()) {
-            std::cerr << "# decline-permanently\n";
-            return 0;
-        }
+    if (machines.empty()) {
+      std::cerr << "# decline-permanently\n";
+      return 0;
+    }
 
-        string drvPath;
-        string storeUri;
-
-        while (true) {
-
-            try {
-                auto s = readString(source);
-                if (s != "try") return 0;
-            } catch (EndOfFile &) { return 0; }
-
-            auto amWilling = readInt(source);
-            auto neededSystem = readString(source);
-            source >> drvPath;
-            auto requiredFeatures = readStrings<std::set<std::string>>(source);
-
-             auto canBuildLocally = amWilling
-                 &&  (  neededSystem == settings.thisSystem
-                     || settings.extraPlatforms.get().count(neededSystem) > 0)
-                 &&  allSupportedLocally(requiredFeatures);
-
-            /* Error ignored here, will be caught later */
-            mkdir(currentLoad.c_str(), 0777);
-
-            while (true) {
-                bestSlotLock = -1;
-                AutoCloseFD lock = openLockFile(currentLoad + "/main-lock", true);
-                lockFile(lock.get(), ltWrite, true);
-
-                bool rightType = false;
-
-                Machine * bestMachine = nullptr;
-                unsigned long long bestLoad = 0;
-                for (auto & m : machines) {
-                    debug("considering building on remote machine '%s'", m.storeUri);
-
-                    if (m.enabled && std::find(m.systemTypes.begin(),
-                            m.systemTypes.end(),
-                            neededSystem) != m.systemTypes.end() &&
-                        m.allSupported(requiredFeatures) &&
-                        m.mandatoryMet(requiredFeatures)) {
-                        rightType = true;
-                        AutoCloseFD free;
-                        unsigned long long load = 0;
-                        for (unsigned long long slot = 0; slot < m.maxJobs; ++slot) {
-                            auto slotLock = openSlotLock(m, slot);
-                            if (lockFile(slotLock.get(), ltWrite, false)) {
-                                if (!free) {
-                                    free = std::move(slotLock);
-                                }
-                            } else {
-                                ++load;
-                            }
-                        }
-                        if (!free) {
-                            continue;
-                        }
-                        bool best = false;
-                        if (!bestSlotLock) {
-                            best = true;
-                        } else if (load / m.speedFactor < bestLoad / bestMachine->speedFactor) {
-                            best = true;
-                        } else if (load / m.speedFactor == bestLoad / bestMachine->speedFactor) {
-                            if (m.speedFactor > bestMachine->speedFactor) {
-                                best = true;
-                            } else if (m.speedFactor == bestMachine->speedFactor) {
-                                if (load < bestLoad) {
-                                    best = true;
-                                }
-                            }
-                        }
-                        if (best) {
-                            bestLoad = load;
-                            bestSlotLock = std::move(free);
-                            bestMachine = &m;
-                        }
-                    }
-                }
+    string drvPath;
+    string storeUri;
 
-                if (!bestSlotLock) {
-                    if (rightType && !canBuildLocally)
-                        std::cerr << "# postpone\n";
-                    else
-                        std::cerr << "# decline\n";
-                    break;
+    while (true) {
+      try {
+        auto s = readString(source);
+        if (s != "try") return 0;
+      } catch (EndOfFile&) {
+        return 0;
+      }
+
+      auto amWilling = readInt(source);
+      auto neededSystem = readString(source);
+      source >> drvPath;
+      auto requiredFeatures = readStrings<std::set<std::string>>(source);
+
+      auto canBuildLocally =
+          amWilling &&
+          (neededSystem == settings.thisSystem ||
+           settings.extraPlatforms.get().count(neededSystem) > 0) &&
+          allSupportedLocally(requiredFeatures);
+
+      /* Error ignored here, will be caught later */
+      mkdir(currentLoad.c_str(), 0777);
+
+      while (true) {
+        bestSlotLock = -1;
+        AutoCloseFD lock = openLockFile(currentLoad + "/main-lock", true);
+        lockFile(lock.get(), ltWrite, true);
+
+        bool rightType = false;
+
+        Machine* bestMachine = nullptr;
+        unsigned long long bestLoad = 0;
+        for (auto& m : machines) {
+          debug("considering building on remote machine '%s'", m.storeUri);
+
+          if (m.enabled &&
+              std::find(m.systemTypes.begin(), m.systemTypes.end(),
+                        neededSystem) != m.systemTypes.end() &&
+              m.allSupported(requiredFeatures) &&
+              m.mandatoryMet(requiredFeatures)) {
+            rightType = true;
+            AutoCloseFD free;
+            unsigned long long load = 0;
+            for (unsigned long long slot = 0; slot < m.maxJobs; ++slot) {
+              auto slotLock = openSlotLock(m, slot);
+              if (lockFile(slotLock.get(), ltWrite, false)) {
+                if (!free) {
+                  free = std::move(slotLock);
                 }
+              } else {
+                ++load;
+              }
+            }
+            if (!free) {
+              continue;
+            }
+            bool best = false;
+            if (!bestSlotLock) {
+              best = true;
+            } else if (load / m.speedFactor <
+                       bestLoad / bestMachine->speedFactor) {
+              best = true;
+            } else if (load / m.speedFactor ==
+                       bestLoad / bestMachine->speedFactor) {
+              if (m.speedFactor > bestMachine->speedFactor) {
+                best = true;
+              } else if (m.speedFactor == bestMachine->speedFactor) {
+                if (load < bestLoad) {
+                  best = true;
+                }
+              }
+            }
+            if (best) {
+              bestLoad = load;
+              bestSlotLock = std::move(free);
+              bestMachine = &m;
+            }
+          }
+        }
+
+        if (!bestSlotLock) {
+          if (rightType && !canBuildLocally)
+            std::cerr << "# postpone\n";
+          else
+            std::cerr << "# decline\n";
+          break;
+        }
 
 #if __APPLE__
-                futimes(bestSlotLock.get(), NULL);
+        futimes(bestSlotLock.get(), NULL);
 #else
-                futimens(bestSlotLock.get(), NULL);
+        futimens(bestSlotLock.get(), NULL);
 #endif
 
-                lock = -1;
-
-                try {
-
-                    Activity act(*logger, lvlTalkative, actUnknown, fmt("connecting to '%s'", bestMachine->storeUri));
-
-                    Store::Params storeParams;
-                    if (hasPrefix(bestMachine->storeUri, "ssh://")) {
-                        storeParams["max-connections"] ="1";
-                        storeParams["log-fd"] = "4";
-                        if (bestMachine->sshKey != "")
-                            storeParams["ssh-key"] = bestMachine->sshKey;
-                    }
-
-                    sshStore = openStore(bestMachine->storeUri, storeParams);
-                    sshStore->connect();
-                    storeUri = bestMachine->storeUri;
-
-                } catch (std::exception & e) {
-                    auto msg = chomp(drainFD(5, false));
-                    printError("cannot build on '%s': %s%s",
-                        bestMachine->storeUri, e.what(),
-                        (msg.empty() ? "" : ": " + msg));
-                    bestMachine->enabled = false;
-                    continue;
-                }
-
-                goto connected;
-            }
+        lock = -1;
+
+        try {
+          Activity act(*logger, lvlTalkative, actUnknown,
+                       fmt("connecting to '%s'", bestMachine->storeUri));
+
+          Store::Params storeParams;
+          if (hasPrefix(bestMachine->storeUri, "ssh://")) {
+            storeParams["max-connections"] = "1";
+            storeParams["log-fd"] = "4";
+            if (bestMachine->sshKey != "")
+              storeParams["ssh-key"] = bestMachine->sshKey;
+          }
+
+          sshStore = openStore(bestMachine->storeUri, storeParams);
+          sshStore->connect();
+          storeUri = bestMachine->storeUri;
+
+        } catch (std::exception& e) {
+          auto msg = chomp(drainFD(5, false));
+          printError("cannot build on '%s': %s%s", bestMachine->storeUri,
+                     e.what(), (msg.empty() ? "" : ": " + msg));
+          bestMachine->enabled = false;
+          continue;
         }
 
-connected:
-        close(5);
-
-        std::cerr << "# accept\n" << storeUri << "\n";
+        goto connected;
+      }
+    }
 
-        auto inputs = readStrings<PathSet>(source);
-        auto outputs = readStrings<PathSet>(source);
+  connected:
+    close(5);
 
-        AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
+    std::cerr << "# accept\n" << storeUri << "\n";
 
-        {
-            Activity act(*logger, lvlTalkative, actUnknown, fmt("waiting for the upload lock to '%s'", storeUri));
+    auto inputs = readStrings<PathSet>(source);
+    auto outputs = readStrings<PathSet>(source);
 
-            auto old = signal(SIGALRM, handleAlarm);
-            alarm(15 * 60);
-            if (!lockFile(uploadLock.get(), ltWrite, true))
-                printError("somebody is hogging the upload lock for '%s', continuing...");
-            alarm(0);
-            signal(SIGALRM, old);
-        }
+    AutoCloseFD uploadLock = openLockFile(
+        currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true);
 
-        auto substitute = settings.buildersUseSubstitutes ? Substitute : NoSubstitute;
+    {
+      Activity act(*logger, lvlTalkative, actUnknown,
+                   fmt("waiting for the upload lock to '%s'", storeUri));
+
+      auto old = signal(SIGALRM, handleAlarm);
+      alarm(15 * 60);
+      if (!lockFile(uploadLock.get(), ltWrite, true))
+        printError(
+            "somebody is hogging the upload lock for '%s', continuing...");
+      alarm(0);
+      signal(SIGALRM, old);
+    }
 
-        {
-            Activity act(*logger, lvlTalkative, actUnknown, fmt("copying dependencies to '%s'", storeUri));
-            copyPaths(store, ref<Store>(sshStore), inputs, NoRepair, NoCheckSigs, substitute);
-        }
+    auto substitute =
+        settings.buildersUseSubstitutes ? Substitute : NoSubstitute;
 
-        uploadLock = -1;
+    {
+      Activity act(*logger, lvlTalkative, actUnknown,
+                   fmt("copying dependencies to '%s'", storeUri));
+      copyPaths(store, ref<Store>(sshStore), inputs, NoRepair, NoCheckSigs,
+                substitute);
+    }
 
-        BasicDerivation drv(readDerivation(store->realStoreDir + "/" + baseNameOf(drvPath)));
-        drv.inputSrcs = inputs;
+    uploadLock = -1;
 
-        auto result = sshStore->buildDerivation(drvPath, drv);
+    BasicDerivation drv(
+        readDerivation(store->realStoreDir + "/" + baseNameOf(drvPath)));
+    drv.inputSrcs = inputs;
 
-        if (!result.success())
-            throw Error("build of '%s' on '%s' failed: %s", drvPath, storeUri, result.errorMsg);
+    auto result = sshStore->buildDerivation(drvPath, drv);
 
-        PathSet missing;
-        for (auto & path : outputs)
-            if (!store->isValidPath(path)) missing.insert(path);
+    if (!result.success())
+      throw Error("build of '%s' on '%s' failed: %s", drvPath, storeUri,
+                  result.errorMsg);
 
-        if (!missing.empty()) {
-            Activity act(*logger, lvlTalkative, actUnknown, fmt("copying outputs from '%s'", storeUri));
-            store->locksHeld.insert(missing.begin(), missing.end()); /* FIXME: ugly */
-            copyPaths(ref<Store>(sshStore), store, missing, NoRepair, NoCheckSigs, NoSubstitute);
-        }
+    PathSet missing;
+    for (auto& path : outputs)
+      if (!store->isValidPath(path)) missing.insert(path);
 
-        return 0;
+    if (!missing.empty()) {
+      Activity act(*logger, lvlTalkative, actUnknown,
+                   fmt("copying outputs from '%s'", storeUri));
+      store->locksHeld.insert(missing.begin(), missing.end()); /* FIXME: ugly */
+      copyPaths(ref<Store>(sshStore), store, missing, NoRepair, NoCheckSigs,
+                NoSubstitute);
     }
+
+    return 0;
+  }
 }
 
 static RegisterLegacyCommand s1("build-remote", _main);