1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-21 09:49:36 +01:00

Apply clang-format universally.

* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
This commit is contained in:
Graham Christensen 2025-07-18 12:47:27 -04:00
parent 41bf87ec70
commit e4f62e4608
587 changed files with 23258 additions and 23135 deletions

View file

@ -40,7 +40,6 @@ namespace nix {
static std::string gcSocketPath = "/gc-socket/socket";
static std::string gcRootsDir = "gcroots";
void LocalStore::addIndirectRoot(const Path & path)
{
std::string hash = hashString(HashAlgorithm::SHA1, path).to_string(HashFormat::Nix32, false);
@ -48,13 +47,13 @@ void LocalStore::addIndirectRoot(const Path & path)
makeSymlink(realRoot, path);
}
void LocalStore::createTempRootsFile()
{
auto fdTempRoots(_fdTempRoots.lock());
/* Create the temporary roots file for this process. */
if (*fdTempRoots) return;
if (*fdTempRoots)
return;
while (1) {
if (pathExists(fnTempRoots))
@ -72,7 +71,8 @@ void LocalStore::createTempRootsFile()
struct stat st;
if (fstat(fromDescriptorReadOnly(fdTempRoots->get()), &st) == -1)
throw SysError("statting '%1%'", fnTempRoots);
if (st.st_size == 0) break;
if (st.st_size == 0)
break;
/* The garbage collector deleted this file before we could get
a lock. (It won't delete the file after we get a lock.)
@ -80,12 +80,12 @@ void LocalStore::createTempRootsFile()
}
}
void LocalStore::addTempRoot(const StorePath & path)
{
if (config->readOnly) {
debug("Read-only store doesn't support creating lock files for temp roots, but nothing can be deleted anyways.");
return;
debug(
"Read-only store doesn't support creating lock files for temp roots, but nothing can be deleted anyways.");
return;
}
createTempRootsFile();
@ -97,7 +97,7 @@ void LocalStore::addTempRoot(const StorePath & path)
*fdGCLock = openGCLock();
}
restart:
restart:
/* Try to acquire a shared global GC lock (non-blocking). This
only succeeds if the garbage collector is not currently
running. */
@ -157,10 +157,8 @@ void LocalStore::addTempRoot(const StorePath & path)
writeFull(_fdTempRoots.lock()->get(), s);
}
static std::string censored = "{censored}";
void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
{
/* Read the `temproots' directory for per-process temporary root
@ -178,14 +176,17 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
pid_t pid = std::stoi(name);
debug("reading temporary root file '%1%'", path);
AutoCloseFD fd(toDescriptor(open(path.c_str(),
AutoCloseFD fd(toDescriptor(open(
path.c_str(),
#ifndef _WIN32
O_CLOEXEC |
#endif
O_RDWR, 0666)));
O_RDWR,
0666)));
if (!fd) {
/* It's okay if the file has disappeared. */
if (errno == ENOENT) continue;
if (errno == ENOENT)
continue;
throw SysError("opening temporary roots file '%1%'", path);
}
@ -214,7 +215,6 @@ void LocalStore::findTempRoots(Roots & tempRoots, bool censor)
}
}
void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, Roots & roots)
{
auto foundRoot = [&](const Path & path, const Path & target) {
@ -224,7 +224,8 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
roots[std::move(storePath)].emplace(path);
else
printInfo("skipping invalid root from '%1%' to '%2%'", path, target);
} catch (BadStorePath &) { }
} catch (BadStorePath &) {
}
};
try {
@ -253,9 +254,11 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
unlink(path.c_str());
}
} else {
if (!std::filesystem::is_symlink(target)) return;
if (!std::filesystem::is_symlink(target))
return;
Path target2 = readLink(target);
if (isInStore(target2)) foundRoot(target, target2);
if (isInStore(target2))
foundRoot(target, target2);
}
}
}
@ -270,7 +273,8 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
catch (std::filesystem::filesystem_error & e) {
/* We only ignore permanent failures. */
if (e.code() == std::errc::permission_denied || e.code() == std::errc::no_such_file_or_directory || e.code() == std::errc::not_a_directory)
if (e.code() == std::errc::permission_denied || e.code() == std::errc::no_such_file_or_directory
|| e.code() == std::errc::not_a_directory)
printInfo("cannot read potential root '%1%'", path);
else
throw;
@ -285,7 +289,6 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
}
}
void LocalStore::findRootsNoTemp(Roots & roots, bool censor)
{
/* Process direct roots in {gcroots,profiles}. */
@ -298,7 +301,6 @@ void LocalStore::findRootsNoTemp(Roots & roots, bool censor)
findRuntimeRoots(roots, censor);
}
Roots LocalStore::findRoots(bool censor)
{
Roots roots;
@ -320,9 +322,8 @@ static void readProcLink(const std::filesystem::path & file, UncheckedRoots & ro
try {
buf = std::filesystem::read_symlink(file);
} catch (std::filesystem::filesystem_error & e) {
if (e.code() == std::errc::no_such_file_or_directory
|| e.code() == std::errc::permission_denied
|| e.code() == std::errc::no_such_process)
if (e.code() == std::errc::no_such_file_or_directory || e.code() == std::errc::permission_denied
|| e.code() == std::errc::no_such_process)
return;
throw;
}
@ -362,7 +363,7 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
checkInterrupt();
if (boost::regex_match(ent->d_name, digitsRegex)) {
try {
readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked);
readProcLink(fmt("/proc/%s/exe", ent->d_name), unchecked);
readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked);
auto fdStr = fmt("/proc/%s/fd", ent->d_name);
@ -395,7 +396,9 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
auto envFile = fmt("/proc/%s/environ", ent->d_name);
auto envString = readFile(envFile);
auto env_end = boost::sregex_iterator{};
for (auto i = boost::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
for (auto i = boost::sregex_iterator{envString.begin(), envString.end(), storePathRegex};
i != env_end;
++i)
unchecked[i->str()].emplace(envFile);
} catch (SystemError & e) {
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
@ -416,7 +419,7 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
try {
boost::regex lsofRegex(R"(^n(/.*)$)");
auto lsofLines =
tokenizeString<std::vector<std::string>>(runProgram(LSOF, true, { "-n", "-w", "-F", "n" }), "\n");
tokenizeString<std::vector<std::string>>(runProgram(LSOF, true, {"-n", "-w", "-F", "n"}), "\n");
for (const auto & line : lsofLines) {
boost::smatch match;
if (boost::regex_match(line, match, lsofRegex))
@ -435,22 +438,24 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
#endif
for (auto & [target, links] : unchecked) {
if (!isInStore(target)) continue;
if (!isInStore(target))
continue;
try {
auto path = toStorePath(target).first;
if (!isValidPath(path)) continue;
if (!isValidPath(path))
continue;
debug("got additional root '%1%'", printStorePath(path));
if (censor)
roots[path].insert(censored);
else
roots[path].insert(links.begin(), links.end());
} catch (BadStorePath &) { }
} catch (BadStorePath &) {
}
}
}
struct GCLimitReached { };
struct GCLimitReached
{};
void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
{
@ -521,7 +526,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
fdServer.close();
while (true) {
auto item = remove_begin(*connections.lock());
if (!item) break;
if (!item)
break;
auto & [fd, thread] = *item;
shutdown(fd, SHUT_RDWR);
thread.join();
@ -543,7 +549,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
/* Accept a new connection. */
assert(fds[1].revents & POLLIN);
AutoCloseFD fdClient = accept(fdServer.get(), nullptr, nullptr);
if (!fdClient) continue;
if (!fdClient)
continue;
debug("GC roots server accepted new client");
@ -604,7 +611,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
Finally stopServer([&]() {
writeFull(shutdownPipe.writeSide.get(), "x", false);
wakeup.notify_all();
if (serverThread.joinable()) serverThread.join();
if (serverThread.joinable())
serverThread.join();
});
#endif
@ -616,7 +624,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
if (!options.ignoreLiveness)
findRootsNoTemp(rootMap, true);
for (auto & i : rootMap) roots.insert(i.first);
for (auto & i : rootMap)
roots.insert(i.first);
/* Read the temporary roots created before we acquired the global
GC root. Any new roots will be sent to our socket. */
@ -633,8 +642,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
/* Helper function that deletes a path from the store and throws
GCLimitReached if we've deleted enough garbage. */
auto deleteFromStore = [&](std::string_view baseName)
{
auto deleteFromStore = [&](std::string_view baseName) {
Path path = storeDir + "/" + std::string(baseName);
Path realPath = config->realStoreDir + "/" + std::string(baseName);
@ -701,19 +709,24 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
/* If we've previously deleted this path, we don't have to
handle it again. */
if (dead.count(*path)) continue;
if (dead.count(*path))
continue;
auto markAlive = [&]()
{
auto markAlive = [&]() {
alive.insert(*path);
alive.insert(start);
try {
StorePathSet closure;
computeFSClosure(*path, closure,
/* flipDirection */ false, gcKeepOutputs, gcKeepDerivations);
computeFSClosure(
*path,
closure,
/* flipDirection */ false,
gcKeepOutputs,
gcKeepDerivations);
for (auto & p : closure)
alive.insert(p);
} catch (InvalidPath &) { }
} catch (InvalidPath &) {
}
};
/* If this is a root, bail out. */
@ -722,8 +735,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
return markAlive();
}
if (options.action == GCOptions::gcDeleteSpecific
&& !options.pathsToDelete.count(*path))
if (options.action == GCOptions::gcDeleteSpecific && !options.pathsToDelete.count(*path))
return;
{
@ -753,9 +765,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
derivation, then visit the derivation outputs. */
if (gcKeepDerivations && path->isDerivation()) {
for (auto & [name, maybeOutPath] : queryPartialDerivationOutputMap(*path))
if (maybeOutPath &&
isValidPath(*maybeOutPath) &&
queryPathInfo(*maybeOutPath)->deriver == *path)
if (maybeOutPath && isValidPath(*maybeOutPath)
&& queryPathInfo(*maybeOutPath)->deriver == *path)
enqueue(*maybeOutPath);
}
@ -768,13 +779,14 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
}
}
for (auto & path : topoSortPaths(visited)) {
if (!dead.insert(path).second) continue;
if (!dead.insert(path).second)
continue;
if (shouldDelete) {
try {
invalidatePathChecked(path);
deleteFromStore(path.to_string());
referrersCache.erase(path);
} catch (PathInUse &e) {
} catch (PathInUse & e) {
// If we end up here, it's likely a new occurrence
// of https://github.com/NixOS/nix/issues/11923
printError("BUG: %s", e.what());
@ -806,7 +818,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
try {
AutoCloseDir dir(opendir(config->realStoreDir.get().c_str()));
if (!dir) throw SysError("opening directory '%1%'", config->realStoreDir);
if (!dir)
throw SysError("opening directory '%1%'", config->realStoreDir);
/* Read the store and delete all paths that are invalid or
unreachable. We don't use readDirectory() here so that
@ -817,13 +830,13 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
while (errno = 0, dirent = readdir(dir.get())) {
checkInterrupt();
std::string name = dirent->d_name;
if (name == "." || name == ".." || name == linksName) continue;
if (name == "." || name == ".." || name == linksName)
continue;
if (auto storePath = maybeParseStorePath(storeDir + "/" + name))
deleteReferrersClosure(*storePath);
else
deleteFromStore(name);
}
} catch (GCLimitReached & e) {
}
@ -850,7 +863,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
printInfo("deleting unused links...");
AutoCloseDir dir(opendir(linksDir.c_str()));
if (!dir) throw SysError("opening directory '%1%'", linksDir);
if (!dir)
throw SysError("opening directory '%1%'", linksDir);
int64_t actualSize = 0, unsharedSize = 0;
@ -858,7 +872,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
while (errno = 0, dirent = readdir(dir.get())) {
checkInterrupt();
std::string name = dirent->d_name;
if (name == "." || name == "..") continue;
if (name == "." || name == "..")
continue;
Path path = linksDir + "/" + name;
auto st = lstat(path);
@ -889,15 +904,15 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
#endif
;
printInfo("note: currently hard linking saves %.2f MiB",
printInfo(
"note: currently hard linking saves %.2f MiB",
((unsharedSize - actualSize - overhead) / (1024.0 * 1024.0)));
}
/* While we're at it, vacuum the database. */
//if (options.action == GCOptions::gcDeleteDead) vacuumDB();
// if (options.action == GCOptions::gcDeleteDead) vacuumDB();
}
void LocalStore::autoGC(bool sync)
{
#if HAVE_STATVFS
@ -927,15 +942,18 @@ void LocalStore::autoGC(bool sync)
auto now = std::chrono::steady_clock::now();
if (now < state->lastGCCheck + std::chrono::seconds(settings.minFreeCheckInterval)) return;
if (now < state->lastGCCheck + std::chrono::seconds(settings.minFreeCheckInterval))
return;
auto avail = getAvail();
state->lastGCCheck = now;
if (avail >= settings.minFree || avail >= settings.maxFree) return;
if (avail >= settings.minFree || avail >= settings.maxFree)
return;
if (avail > state->availAfterGC * 0.97) return;
if (avail > state->availAfterGC * 0.97)
return;
state->gcRunning = true;
@ -943,7 +961,6 @@ void LocalStore::autoGC(bool sync)
future = state->gcFuture = promise.get_future().share();
std::thread([promise{std::move(promise)}, this, avail, getAvail]() mutable {
try {
/* Wake up any threads waiting for the auto-GC to finish. */
@ -970,15 +987,14 @@ void LocalStore::autoGC(bool sync)
// future, but we don't really care. (what??)
ignoreExceptionInDestructor();
}
}).detach();
}
sync:
sync:
// Wait for the future outside of the state lock.
if (sync) future.get();
if (sync)
future.get();
#endif
}
}
} // namespace nix