build-remote: Don't use C++ streams to read the conf file

This commit is contained in:
Shea Levy 2017-01-10 10:36:26 -05:00
parent d771c28613
commit 28db297862

View file

@ -1,8 +1,6 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
#include <fstream>
#include <sstream>
#include <set> #include <set>
#include <memory> #include <memory>
#include <tuple> #include <tuple>
@ -71,35 +69,37 @@ static std::vector<machine> read_conf() {
auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines"); auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines");
auto machines = std::vector<machine>{}; auto machines = std::vector<machine>{};
auto confFile = std::ifstream{conf}; auto lines = std::vector<string>{};
if (confFile.good()) { try {
confFile.exceptions(std::ifstream::badbit); lines = tokenizeString<std::vector<string>>(readFile(conf), "\n");
for (string line; getline(confFile, line);) { } catch (const SysError & e) {
chomp(line); if (e.errNo != ENOENT)
line.erase(std::find(line.begin(), line.end(), '#'), line.end()); throw;
if (line.empty()) { }
continue; for (auto line : lines) {
} chomp(line);
auto tokens = tokenizeString<std::vector<string>>(line); line.erase(std::find(line.begin(), line.end(), '#'), line.end());
auto sz = tokens.size(); if (line.empty()) {
if (sz < 4) { continue;
throw new FormatError(format("Bad machines.conf file %1%") }
% conf); auto tokens = tokenizeString<std::vector<string>>(line);
} auto sz = tokens.size();
machines.emplace_back(tokens[0], if (sz < 4) {
tokenizeString<std::vector<string>>(tokens[1], ","), throw new FormatError(format("Bad machines.conf file %1%")
tokens[2], % conf);
stoull(tokens[3]), }
sz >= 5 ? stoull(tokens[4]) : 1LL, machines.emplace_back(tokens[0],
sz >= 6 ? tokenizeString<std::vector<string>>(tokens[1], ","),
tokenizeString<std::set<string>>(tokens[5], ",") : tokens[2],
std::set<string>{}, stoull(tokens[3]),
sz >= 7 ? sz >= 5 ? stoull(tokens[4]) : 1LL,
tokenizeString<std::set<string>>(tokens[6], ",") : sz >= 6 ?
std::set<string>{}); tokenizeString<std::set<string>>(tokens[5], ",") :
} std::set<string>{},
sz >= 7 ?
tokenizeString<std::set<string>>(tokens[6], ",") :
std::set<string>{});
} }
confFile.close();
return machines; return machines;
} }