diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2021-10-29 14:42:26 +0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2021-10-29 14:42:26 +0200 |
commit | 6e30d9b69f19a6fb4803b48a6fa7f2646dd9af10 (patch) | |
tree | 506e4467154150630dfc765ed3884eefc2e5b01d /src/libstore/machines.cc | |
parent | 0d00dd6262bfd77aaac516410f35970f9dccbcb9 (diff) | |
parent | e989c83b44d7c4d8ffe2e5f4231e4861c5f7732f (diff) |
Merge branch 'master' of https://github.com/alekswn/nix
Diffstat (limited to 'src/libstore/machines.cc')
-rw-r--r-- | src/libstore/machines.cc | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/src/libstore/machines.cc b/src/libstore/machines.cc index 9843ccf04..910e32a76 100644 --- a/src/libstore/machines.cc +++ b/src/libstore/machines.cc @@ -83,53 +83,83 @@ ref<Store> Machine::openStore() const { return nix::openStore(storeUri, storeParams); } -void parseMachines(const std::string & s, Machines & machines) -{ - for (auto line : tokenizeString<std::vector<string>>(s, "\n;")) { +static std::vector<std::string> expandBuilderLines(const std::string& builders) { + std::vector<std::string> result; + for (auto line : tokenizeString<std::vector<string>>(builders, "\n;")) { trim(line); line.erase(std::find(line.begin(), line.end(), '#'), line.end()); if (line.empty()) continue; if (line[0] == '@') { - auto file = trim(std::string(line, 1)); + const std::string path = trim(std::string(line, 1)); + std::string text; try { - parseMachines(readFile(file), machines); + text = readFile(path); } catch (const SysError & e) { if (e.errNo != ENOENT) throw; - debug("cannot find machines file '%s'", file); + debug("cannot find machines file '%s'", path); } + + const auto lines = expandBuilderLines(text); + result.insert(end(result), begin(lines), end(lines)); continue; } - auto tokens = tokenizeString<std::vector<string>>(line); - auto sz = tokens.size(); - if (sz < 1) - throw FormatError("bad machine specification '%s'", line); + result.emplace_back(line); + } + return result; +} - auto isSet = [&](size_t n) { - return tokens.size() > n && tokens[n] != "" && tokens[n] != "-"; - }; +static Machine parseBuilderLine(const std::string& line) { + const auto tokens = tokenizeString<std::vector<string>>(line); + + auto isSet = [&](size_t fieldIndex) { + return tokens.size() > fieldIndex && tokens[fieldIndex] != "" && tokens[fieldIndex] != "-"; + }; + + auto parseUnsignedIntField = [&](size_t fieldIndex) { + const auto result = string2Int<unsigned int>(tokens[fieldIndex]); + if (!result) { + throw FormatError("bad machine specification: failed to convert column #%lu in a row: '%s' to 'unsigned int'", fieldIndex, line); + } + return result.value(); + }; + + auto ensureBase64 = [&](size_t fieldIndex) { + const auto& str = tokens[fieldIndex]; + try { + base64Decode(str); + } catch (const Error& e) { + throw FormatError("bad machine specification: a column #%lu in a row: '%s' is not valid base64 string: %s", fieldIndex, line, e.what()); + } + return str; + }; + + if (!isSet(0)) { + throw FormatError("bad machine specification: store URI was not found at the first column of a row: '%s'", line); + } - machines.emplace_back(tokens[0], + return {tokens[0], isSet(1) ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem}, isSet(2) ? tokens[2] : "", - isSet(3) ? std::stoull(tokens[3]) : 1LL, - isSet(4) ? std::stoull(tokens[4]) : 1LL, + isSet(3) ? parseUnsignedIntField(3) : 1U, + isSet(4) ? parseUnsignedIntField(4) : 1U, isSet(5) ? tokenizeString<std::set<string>>(tokens[5], ",") : std::set<string>{}, isSet(6) ? tokenizeString<std::set<string>>(tokens[6], ",") : std::set<string>{}, - isSet(7) ? tokens[7] : ""); - } + isSet(7) ? ensureBase64(7) : ""}; +} + +static Machines parseBuilderLines(const std::vector<std::string>& builders) { + Machines result; + std::transform(builders.begin(), builders.end(), std::back_inserter(result), parseBuilderLine); + return result; } Machines getMachines() { - static auto machines = [&]() { - Machines machines; - parseMachines(settings.builders, machines); - return machines; - }(); - return machines; + const auto builderLines = expandBuilderLines(settings.builders); + return parseBuilderLines(builderLines); } } |