1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-13 20:41:04 +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

@ -10,27 +10,26 @@
#include <errno.h>
#include <stdio.h>
namespace nix {
/**
* Parse a generation name of the format
* `<profilename>-<number>-link'.
*/
static std::optional<GenerationNumber> parseName(const std::string & profileName, const std::string & name)
{
if (name.substr(0, profileName.size() + 1) != profileName + "-") return {};
if (name.substr(0, profileName.size() + 1) != profileName + "-")
return {};
auto s = name.substr(profileName.size() + 1);
auto p = s.find("-link");
if (p == std::string::npos) return {};
if (p == std::string::npos)
return {};
if (auto n = string2Int<unsigned int>(s.substr(0, p)))
return *n;
else
return {};
}
std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path profile)
{
Generations gens;
@ -42,28 +41,15 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro
checkInterrupt();
if (auto n = parseName(profileName, i.path().filename().string())) {
auto path = i.path().string();
gens.push_back({
.number = *n,
.path = path,
.creationTime = lstat(path).st_mtime
});
gens.push_back({.number = *n, .path = path, .creationTime = lstat(path).st_mtime});
}
}
gens.sort([](const Generation & a, const Generation & b)
{
return a.number < b.number;
});
gens.sort([](const Generation & a, const Generation & b) { return a.number < b.number; });
return {
gens,
pathExists(profile)
? parseName(profileName, readLink(profile))
: std::nullopt
};
return {gens, pathExists(profile) ? parseName(profileName, readLink(profile)) : std::nullopt};
}
/**
* Create a generation name that can be parsed by `parseName()`.
*/
@ -72,7 +58,6 @@ static Path makeName(const Path & profile, GenerationNumber num)
return fmt("%s-%s-link", profile, num);
}
Path createGeneration(LocalFSStore & store, Path profile, StorePath outPath)
{
/* The new generation number should be higher than old the
@ -110,14 +95,12 @@ Path createGeneration(LocalFSStore & store, Path profile, StorePath outPath)
return generation;
}
static void removeFile(const Path & path)
{
if (remove(path.c_str()) == -1)
throw SysError("cannot unlink '%1%'", path);
}
void deleteGeneration(const Path & profile, GenerationNumber gen)
{
Path generation = makeName(profile, gen);
@ -143,7 +126,6 @@ static void deleteGeneration2(const Path & profile, GenerationNumber gen, bool d
}
}
void deleteGenerations(const Path & profile, const std::set<GenerationNumber> & gensToDelete, bool dryRun)
{
PathLocks lock;
@ -155,7 +137,8 @@ void deleteGenerations(const Path & profile, const std::set<GenerationNumber> &
throw Error("cannot delete current version of profile %1%'", profile);
for (auto & i : gens) {
if (!gensToDelete.count(i.number)) continue;
if (!gensToDelete.count(i.number))
continue;
deleteGeneration2(profile, i.number, dryRun);
}
}
@ -165,7 +148,8 @@ void deleteGenerations(const Path & profile, const std::set<GenerationNumber> &
*/
static inline void iterDropUntil(Generations & gens, auto && i, auto && cond)
{
for (; i != gens.rend() && !cond(*i); ++i);
for (; i != gens.rend() && !cond(*i); ++i)
;
}
void deleteGenerationsGreaterThan(const Path & profile, GenerationNumber max, bool dryRun)
@ -185,7 +169,8 @@ void deleteGenerationsGreaterThan(const Path & profile, GenerationNumber max, bo
iterDropUntil(gens, i, [&](auto & g) { return g.number == curGen; });
// Skip over `max` generations, preserving them
for (GenerationNumber keep = 0; i != gens.rend() && keep < max; ++i, ++keep);
for (GenerationNumber keep = 0; i != gens.rend() && keep < max; ++i, ++keep)
;
// Delete the rest
for (; i != gens.rend(); ++i)
@ -204,7 +189,6 @@ void deleteOldGenerations(const Path & profile, bool dryRun)
deleteGeneration2(profile, i.number, dryRun);
}
void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun)
{
PathLocks lock;
@ -225,7 +209,8 @@ void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun)
We don't want delete this one yet because it
existed at the requested point in time, and
we want to be able to roll back to it. */
if (i != gens.rend()) ++i;
if (i != gens.rend())
++i;
// Delete all previous generations (unless current).
for (; i != gens.rend(); ++i) {
@ -237,7 +222,6 @@ void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun)
}
}
time_t parseOlderThanTimeSpec(std::string_view timeSpec)
{
if (timeSpec.empty() || timeSpec[timeSpec.size() - 1] != 'd')
@ -253,20 +237,16 @@ time_t parseOlderThanTimeSpec(std::string_view timeSpec)
return curTime - *days * 24 * 3600;
}
void switchLink(Path link, Path target)
{
/* Hacky. */
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
if (dirOf(target) == dirOf(link))
target = baseNameOf(target);
replaceSymlink(target, link);
}
void switchGeneration(
const Path & profile,
std::optional<GenerationNumber> dstGen,
bool dryRun)
void switchGeneration(const Path & profile, std::optional<GenerationNumber> dstGen, bool dryRun)
{
PathLocks lock;
lockProfile(lock, profile);
@ -275,8 +255,7 @@ void switchGeneration(
std::optional<Generation> dst;
for (auto & i : gens)
if ((!dstGen && i.number < curGen) ||
(dstGen && i.number == *dstGen))
if ((!dstGen && i.number < curGen) || (dstGen && i.number == *dstGen))
dst = i;
if (!dst) {
@ -288,31 +267,26 @@ void switchGeneration(
notice("switching profile from version %d to %d", curGen.value_or(0), dst->number);
if (dryRun) return;
if (dryRun)
return;
switchLink(profile, dst->path);
}
void lockProfile(PathLocks & lock, const Path & profile)
{
lock.lockPaths({profile}, fmt("waiting for lock on profile '%1%'", profile));
lock.setDeletion(true);
}
std::string optimisticLockProfile(const Path & profile)
{
return pathExists(profile) ? readLink(profile) : "";
}
Path profilesDir()
{
auto profileRoot =
isRootUser()
? rootProfilesDir()
: createNixStateDir() + "/profiles";
auto profileRoot = isRootUser() ? rootProfilesDir() : createNixStateDir() + "/profiles";
createDirs(profileRoot);
return profileRoot;
}
@ -322,7 +296,6 @@ Path rootProfilesDir()
return settings.nixStateDir + "/profiles/per-user/root";
}
Path getDefaultProfile()
{
Path profileLink = settings.useXDGBaseDirectories ? createNixStateDir() + "/profile" : getHome() + "/.nix-profile";
@ -355,4 +328,4 @@ Path rootChannelsDir()
return rootProfilesDir() + "/channels";
}
}
} // namespace nix