refactor(tvix): Make static strings constexpr string_views
Make all static std::strings constexpr std::string_views, and replace concatenation with absl::StrCat where necessary. Technically all of these are constant, so they really don't need to be top-level statics - and since I'm trying to get rid of as much global state as possible in preparation for making the nix daemon properly multithreaded I figured I'd knock these out while I was at it. Change-Id: Ibd3ad9ef68f0a0eacb135541b39fdb13dae042e1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1939 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
parent
31b06516f3
commit
381ce8a666
8 changed files with 46 additions and 42 deletions
25
third_party/nix/src/libstore/gc.cc
vendored
25
third_party/nix/src/libstore/gc.cc
vendored
|
|
@ -7,6 +7,7 @@
|
|||
#include <regex>
|
||||
|
||||
#include <absl/strings/match.h>
|
||||
#include <absl/strings/str_cat.h>
|
||||
#include <absl/strings/str_split.h>
|
||||
#include <fcntl.h>
|
||||
#include <glog/logging.h>
|
||||
|
|
@ -22,8 +23,8 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
static std::string gcLockName = "gc.lock";
|
||||
static std::string gcRootsDir = "gcroots";
|
||||
constexpr std::string_view kGcLockName = "gc.lock";
|
||||
constexpr std::string_view kGcRootsDir = "gcroots";
|
||||
|
||||
/* Acquire the global GC lock. This is used to prevent new Nix
|
||||
processes from starting after the temporary root files have been
|
||||
|
|
@ -31,7 +32,7 @@ static std::string gcRootsDir = "gcroots";
|
|||
file, they will block until the garbage collector has finished /
|
||||
yielded the GC lock. */
|
||||
AutoCloseFD LocalStore::openGCLock(LockType lockType) {
|
||||
Path fnGCLock = (format("%1%/%2%") % stateDir % gcLockName).str();
|
||||
Path fnGCLock = absl::StrCat(stateDir.get(), "/", kGcLockName);
|
||||
|
||||
DLOG(INFO) << "acquiring global GC lock " << fnGCLock;
|
||||
|
||||
|
|
@ -73,8 +74,8 @@ void LocalStore::syncWithGC() { AutoCloseFD fdGCLock = openGCLock(ltRead); }
|
|||
|
||||
void LocalStore::addIndirectRoot(const Path& path) {
|
||||
std::string hash = hashString(htSHA1, path).to_string(Base32, false);
|
||||
Path realRoot = canonPath(
|
||||
(format("%1%/%2%/auto/%3%") % stateDir % gcRootsDir % hash).str());
|
||||
Path realRoot =
|
||||
canonPath(absl::StrCat(stateDir.get(), "/", kGcRootsDir, "/auto/", hash));
|
||||
makeSymlink(realRoot, path);
|
||||
}
|
||||
|
||||
|
|
@ -105,8 +106,7 @@ Path LocalFSStore::addPermRoot(const Path& _storePath, const Path& _gcRoot,
|
|||
|
||||
else {
|
||||
if (!allowOutsideRootsDir) {
|
||||
Path rootsDir =
|
||||
canonPath((format("%1%/%2%") % stateDir % gcRootsDir).str());
|
||||
Path rootsDir = canonPath(absl::StrCat(stateDir.get(), "/", kGcRootsDir));
|
||||
|
||||
if (std::string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/") {
|
||||
throw Error(format("path '%1%' is not a valid garbage collector root; "
|
||||
|
|
@ -195,7 +195,7 @@ void LocalStore::addTempRoot(const Path& path) {
|
|||
lockFile(state->fdTempRoots.get(), ltRead, true);
|
||||
}
|
||||
|
||||
static std::string censored = "{censored}";
|
||||
constexpr std::string_view kCensored = "{censored}";
|
||||
|
||||
void LocalStore::findTempRoots(FDs& fds, Roots& tempRoots, bool censor) {
|
||||
/* Read the `temproots' directory for per-process temporary root
|
||||
|
|
@ -247,7 +247,7 @@ void LocalStore::findTempRoots(FDs& fds, Roots& tempRoots, bool censor) {
|
|||
Path root(contents, pos, end - pos);
|
||||
DLOG(INFO) << "got temporary root " << root;
|
||||
assertStorePath(root);
|
||||
tempRoots[root].emplace(censor ? censored : fmt("{temp:%d}", pid));
|
||||
tempRoots[root].emplace(censor ? kCensored : fmt("{temp:%d}", pid));
|
||||
pos = end + 1;
|
||||
}
|
||||
|
||||
|
|
@ -287,7 +287,8 @@ void LocalStore::findRoots(const Path& path, unsigned char type, Roots& roots) {
|
|||
else {
|
||||
target = absPath(target, dirOf(path));
|
||||
if (!pathExists(target)) {
|
||||
if (isInDir(path, stateDir + "/" + gcRootsDir + "/auto")) {
|
||||
if (isInDir(path, absl::StrCat(stateDir.get(), "/", kGcRootsDir,
|
||||
"/auto"))) {
|
||||
LOG(INFO) << "removing stale link from '" << path << "' to '"
|
||||
<< target << "'";
|
||||
unlink(path.c_str());
|
||||
|
|
@ -326,7 +327,7 @@ void LocalStore::findRoots(const Path& path, unsigned char type, Roots& roots) {
|
|||
|
||||
void LocalStore::findRootsNoTemp(Roots& roots, bool censor) {
|
||||
/* Process direct roots in {gcroots,profiles}. */
|
||||
findRoots(stateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
|
||||
findRoots(absl::StrCat(stateDir.get(), "/", kGcRootsDir), DT_UNKNOWN, roots);
|
||||
findRoots(stateDir + "/profiles", DT_UNKNOWN, roots);
|
||||
|
||||
/* Add additional roots returned by different platforms-specific
|
||||
|
|
@ -465,7 +466,7 @@ void LocalStore::findRuntimeRoots(Roots& roots, bool censor) {
|
|||
if (isStorePath(path) && isValidPath(path)) {
|
||||
DLOG(INFO) << "got additional root " << path;
|
||||
if (censor) {
|
||||
roots[path].insert(censored);
|
||||
roots[path].insert(std::string(kCensored));
|
||||
} else {
|
||||
roots[path].insert(links.begin(), links.end());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue