diff options
author | Vincent Ambo <tazjin@google.com> | 2020-05-25T00·19+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2020-05-25T00·19+0100 |
commit | 98299da0fda612b42ab933c47f18163cfef5fa71 (patch) | |
tree | 78fc0f1127b5f2336d3e370f342f72f38d5a5ee2 /third_party/nix/src/libstore | |
parent | b371821db59d33851d521d66ba1fb126d388c00f (diff) |
refactor(3p/nix/libutil): Replace string2Int & trim functions r/843
Replaces these functions with corresponding functions from Abseil, namely absl::StripAsciiWhitespace and absl::SimpleAtoi. In the course of doing this some minor things I encountered along the way were also refactored. This also changes the signatures of the various custom readFile functions to use absl::string_view types.
Diffstat (limited to 'third_party/nix/src/libstore')
-rw-r--r-- | third_party/nix/src/libstore/binary-cache-store.cc | 13 | ||||
-rw-r--r-- | third_party/nix/src/libstore/build.cc | 6 | ||||
-rw-r--r-- | third_party/nix/src/libstore/download.cc | 16 | ||||
-rw-r--r-- | third_party/nix/src/libstore/globals.cc | 3 | ||||
-rw-r--r-- | third_party/nix/src/libstore/local-store.cc | 3 | ||||
-rw-r--r-- | third_party/nix/src/libstore/machines.cc | 5 | ||||
-rw-r--r-- | third_party/nix/src/libstore/nar-info.cc | 6 | ||||
-rw-r--r-- | third_party/nix/src/libstore/profiles.cc | 29 | ||||
-rw-r--r-- | third_party/nix/src/libstore/store-api.cc | 5 |
9 files changed, 54 insertions, 32 deletions
diff --git a/third_party/nix/src/libstore/binary-cache-store.cc b/third_party/nix/src/libstore/binary-cache-store.cc index 37d1c1a440a2..ea36078435ba 100644 --- a/third_party/nix/src/libstore/binary-cache-store.cc +++ b/third_party/nix/src/libstore/binary-cache-store.cc @@ -4,6 +4,9 @@ #include <future> #include <memory> +#include <absl/strings/ascii.h> +#include <absl/strings/numbers.h> + #include "archive.hh" #include "compression.hh" #include "derivations.hh" @@ -21,7 +24,8 @@ namespace nix { BinaryCacheStore::BinaryCacheStore(const Params& params) : Store(params) { if (secretKeyFile != "") { - secretKey = std::make_unique<SecretKey>(readFile(secretKeyFile)); + const std::string& secret_key_file = secretKeyFile; + secretKey = std::make_unique<SecretKey>(readFile(secret_key_file)); } StringSink sink; @@ -43,7 +47,8 @@ void BinaryCacheStore::init() { continue; } auto name = line.substr(0, colon); - auto value = trim(line.substr(colon + 1, std::string::npos)); + auto value = + absl::StripAsciiWhitespace(line.substr(colon + 1, std::string::npos)); if (name == "StoreDir") { if (value != storeDir) { throw Error(format("binary cache '%s' is for Nix stores with prefix " @@ -53,7 +58,9 @@ void BinaryCacheStore::init() { } else if (name == "WantMassQuery") { wantMassQuery_ = value == "1"; } else if (name == "Priority") { - string2Int(value, priority); + if (!absl::SimpleAtoi(value, &priority)) { + LOG(WARNING) << "Invalid 'Priority' value: " << value; + } } } } diff --git a/third_party/nix/src/libstore/build.cc b/third_party/nix/src/libstore/build.cc index 49204a72a8cd..c0fa0074d6ea 100644 --- a/third_party/nix/src/libstore/build.cc +++ b/third_party/nix/src/libstore/build.cc @@ -13,6 +13,7 @@ #include <thread> #include <absl/strings/ascii.h> +#include <absl/strings/numbers.h> #include <fcntl.h> #include <grp.h> #include <netdb.h> @@ -2412,7 +2413,7 @@ void DerivationGoal::startBuilder() { userNamespaceSync.readSide = -1; pid_t tmp; - if (!string2Int<pid_t>(readLine(builderOut.readSide.get()), tmp)) { + if (!absl::SimpleAtoi(readLine(builderOut.readSide.get()), &tmp)) { abort(); } pid = tmp; @@ -2805,7 +2806,8 @@ void DerivationGoal::runChild() { std::string netrcData; try { if (drv->isBuiltin() && drv->builder == "builtin:fetchurl") { - netrcData = readFile(settings.netrcFile); + const std::string& netrc_file = settings.netrcFile; + netrcData = readFile(netrc_file); } } catch (SysError&) { } diff --git a/third_party/nix/src/libstore/download.cc b/third_party/nix/src/libstore/download.cc index a83599715c1e..bebe0d1bf848 100644 --- a/third_party/nix/src/libstore/download.cc +++ b/third_party/nix/src/libstore/download.cc @@ -1,6 +1,7 @@ #include "download.hh" #include <absl/strings/ascii.h> +#include <absl/strings/numbers.h> #include "archive.hh" #include "compression.hh" @@ -174,7 +175,8 @@ struct CurlDownloader : public Downloader { size_t headerCallback(void* contents, size_t size, size_t nmemb) { size_t realSize = size * nmemb; std::string line((char*)contents, realSize); - DLOG(INFO) << "got header for '" << request.uri << "': " << trim(line); + DLOG(INFO) << "got header for '" << request.uri + << "': " << absl::StripAsciiWhitespace(line); if (line.compare(0, 5, "HTTP/") == 0) { // new response starts result.etag = ""; auto ss = tokenizeString<std::vector<std::string>>(line, " "); @@ -186,9 +188,10 @@ struct CurlDownloader : public Downloader { } else { auto i = line.find(':'); if (i != std::string::npos) { - std::string name = toLower(trim(std::string(line, 0, i))); + std::string name = absl::AsciiStrToLower( + absl::StripAsciiWhitespace(std::string(line, 0, i))); if (name == "etag") { - result.etag = trim(std::string(line, i + 1)); + result.etag = absl::StripAsciiWhitespace(std::string(line, i + 1)); /* Hack to work around a GitHub bug: it sends ETags, but ignores If-None-Match. So if we get the expected ETag on a 200 response, then shut @@ -200,9 +203,10 @@ struct CurlDownloader : public Downloader { return 0; } } else if (name == "content-encoding") { - encoding = trim(std::string(line, i + 1)); + encoding = absl::StripAsciiWhitespace(std::string(line, i + 1)); } else if (name == "accept-ranges" && - toLower(trim(std::string(line, i + 1))) == "bytes") { + absl::AsciiStrToLower(absl::StripAsciiWhitespace( + std::string(line, i + 1))) == "bytes") { acceptRanges = true; } } @@ -893,7 +897,7 @@ CachedDownloadResult Downloader::downloadCached( tokenizeString<std::vector<std::string>>(readFile(dataFile), "\n"); if (ss.size() >= 3 && ss[0] == url) { time_t lastChecked; - if (string2Int(ss[2], lastChecked) && + if (absl::SimpleAtoi(ss[2], &lastChecked) && (uint64_t)lastChecked + request.ttl >= (uint64_t)time(nullptr)) { skip = true; result.effectiveUri = request.uri; diff --git a/third_party/nix/src/libstore/globals.cc b/third_party/nix/src/libstore/globals.cc index 61293dffed50..7307f035275f 100644 --- a/third_party/nix/src/libstore/globals.cc +++ b/third_party/nix/src/libstore/globals.cc @@ -4,6 +4,7 @@ #include <map> #include <thread> +#include <absl/strings/numbers.h> #include <dlfcn.h> #include "archive.hh" @@ -163,7 +164,7 @@ void BaseSetting<SandboxMode>::convertToArg(Args& args, void MaxBuildJobsSetting::set(const std::string& str) { if (str == "auto") { value = std::max(1U, std::thread::hardware_concurrency()); - } else if (!string2Int(str, value)) { + } else if (!absl::SimpleAtoi(str, &value)) { throw UsageError( "configuration setting '%s' should be 'auto' or an integer", name); } diff --git a/third_party/nix/src/libstore/local-store.cc b/third_party/nix/src/libstore/local-store.cc index 84055740de2c..faea0fbce56a 100644 --- a/third_party/nix/src/libstore/local-store.cc +++ b/third_party/nix/src/libstore/local-store.cc @@ -7,6 +7,7 @@ #include <ctime> #include <iostream> +#include <absl/strings/numbers.h> #include <fcntl.h> #include <glog/logging.h> #include <grp.h> @@ -295,7 +296,7 @@ int LocalStore::getSchema() { int curSchema = 0; if (pathExists(schemaPath)) { std::string s = readFile(schemaPath); - if (!string2Int(s, curSchema)) { + if (!absl::SimpleAtoi(s, &curSchema)) { throw Error(format("'%1%' is corrupt") % schemaPath); } } diff --git a/third_party/nix/src/libstore/machines.cc b/third_party/nix/src/libstore/machines.cc index 2f1a7289bd84..6472e037d14c 100644 --- a/third_party/nix/src/libstore/machines.cc +++ b/third_party/nix/src/libstore/machines.cc @@ -2,6 +2,8 @@ #include <algorithm> +#include <absl/strings/ascii.h> +#include <absl/strings/string_view.h> #include <glog/logging.h> #include "globals.hh" @@ -48,14 +50,13 @@ bool Machine::mandatoryMet(const std::set<std::string>& features) const { void parseMachines(const std::string& s, Machines& machines) { for (auto line : tokenizeString<std::vector<std::string>>(s, "\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)); + auto file = absl::StripAsciiWhitespace(std::string(line, 1)); try { parseMachines(readFile(file), machines); } catch (const SysError& e) { diff --git a/third_party/nix/src/libstore/nar-info.cc b/third_party/nix/src/libstore/nar-info.cc index 5a217d1b617d..9f4c53e19b59 100644 --- a/third_party/nix/src/libstore/nar-info.cc +++ b/third_party/nix/src/libstore/nar-info.cc @@ -1,5 +1,7 @@ #include "nar-info.hh" +#include <absl/strings/numbers.h> + #include "globals.hh" namespace nix { @@ -47,13 +49,13 @@ NarInfo::NarInfo(const Store& store, const std::string& s, } else if (name == "FileHash") { fileHash = parseHashField(value); } else if (name == "FileSize") { - if (!string2Int(value, fileSize)) { + if (!absl::SimpleAtoi(value, &fileSize)) { corrupt(); } } else if (name == "NarHash") { narHash = parseHashField(value); } else if (name == "NarSize") { - if (!string2Int(value, narSize)) { + if (!absl::SimpleAtoi(value, &narSize)) { corrupt(); } } else if (name == "References") { diff --git a/third_party/nix/src/libstore/profiles.cc b/third_party/nix/src/libstore/profiles.cc index 7c802d754935..3516954b50b7 100644 --- a/third_party/nix/src/libstore/profiles.cc +++ b/third_party/nix/src/libstore/profiles.cc @@ -3,6 +3,9 @@ #include <cerrno> #include <cstdio> +#include <absl/strings/numbers.h> +#include <absl/strings/string_view.h> +#include <absl/strings/strip.h> #include <glog/logging.h> #include <sys/stat.h> #include <sys/types.h> @@ -17,22 +20,22 @@ static bool cmpGensByNumber(const Generation& a, const Generation& b) { return a.number < b.number; } -/* Parse a generation name of the format - `<profilename>-<number>-link'. */ -static int parseName(const std::string& profileName, const std::string& name) { - if (std::string(name, 0, profileName.size() + 1) != profileName + "-") { - return -1; - } - std::string s = std::string(name, profileName.size() + 1); - std::string::size_type p = s.find("-link"); - if (p == std::string::npos) { +// Parse a generation out of the format +// `<profilename>-<generation>-link'. +static int parseName(absl::string_view profileName, absl::string_view name) { + // Consume the `<profilename>-' prefix and and `-link' suffix. + if (!(absl::ConsumePrefix(&name, profileName) && + absl::ConsumePrefix(&name, "-") && + absl::ConsumeSuffix(&name, "-link"))) { return -1; } + int n; - if (string2Int(std::string(s, 0, p), n) && n >= 0) { - return n; + if (!absl::SimpleAtoi(name, &n) && n < 0) { + return -1; } - return -1; + + return n; } Generations findGenerations(const Path& profile, int& curGen) { @@ -218,7 +221,7 @@ void deleteGenerationsOlderThan(const Path& profile, std::string strDays = std::string(timeSpec, 0, timeSpec.size() - 1); int days; - if (!string2Int(strDays, days) || days < 1) { + if (!absl::SimpleAtoi(strDays, &days) || days < 1) { throw Error(format("invalid number of days specifier '%1%'") % timeSpec); } diff --git a/third_party/nix/src/libstore/store-api.cc b/third_party/nix/src/libstore/store-api.cc index e6891395cdc5..54214e6f4e94 100644 --- a/third_party/nix/src/libstore/store-api.cc +++ b/third_party/nix/src/libstore/store-api.cc @@ -3,6 +3,7 @@ #include <future> #include <utility> +#include <absl/strings/numbers.h> #include <glog/logging.h> #include "crypto.hh" @@ -718,7 +719,7 @@ ValidPathInfo decodeValidPathInfo(std::istream& str, bool hashGiven) { getline(str, s); info.narHash = Hash(s, htSHA256); getline(str, s); - if (!string2Int(s, info.narSize)) { + if (!absl::SimpleAtoi(s, &info.narSize)) { throw Error("number expected"); } } @@ -726,7 +727,7 @@ ValidPathInfo decodeValidPathInfo(std::istream& str, bool hashGiven) { std::string s; int n; getline(str, s); - if (!string2Int(s, n)) { + if (!absl::SimpleAtoi(s, &n)) { throw Error("number expected"); } while ((n--) != 0) { |