1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 22:42:41 +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

@ -34,4 +34,4 @@ void commonChildInit()
close(fdDevNull);
}
}
} // namespace nix

View file

@ -206,6 +206,6 @@ struct DarwinDerivationBuilder : DerivationBuilderImpl
}
};
}
} // namespace nix
#endif

File diff suppressed because it is too large Load diff

View file

@ -46,13 +46,13 @@ HookInstance::HookInstance()
/* Fork the hook. */
pid = startProcess([&]() {
if (dup2(fromHook.writeSide.get(), STDERR_FILENO) == -1)
throw SysError("cannot pipe standard error into log file");
commonChildInit();
if (chdir("/") == -1) throw SysError("changing into /");
if (chdir("/") == -1)
throw SysError("changing into /");
/* Dup the communication pipes. */
if (dup2(toHook.readSide.get(), STDIN_FILENO) == -1)
@ -84,15 +84,15 @@ HookInstance::HookInstance()
sink << 0;
}
HookInstance::~HookInstance()
{
try {
toHook.writeSide = -1;
if (pid != -1) pid.kill();
if (pid != -1)
pid.kill();
} catch (...) {
ignoreExceptionInDestructor();
}
}
}
} // namespace nix

View file

@ -878,6 +878,6 @@ struct ChrootLinuxDerivationBuilder : LinuxDerivationBuilder
}
};
}
} // namespace nix
#endif

View file

@ -1,4 +1,5 @@
#pragma once
///@file
namespace nix {
@ -8,4 +9,4 @@ namespace nix {
*/
void commonChildInit();
}
} // namespace nix

View file

@ -75,7 +75,8 @@ struct DerivationBuilderParams
, inputPaths{inputPaths}
, initialOutputs{initialOutputs}
, buildMode{buildMode}
{ }
{
}
DerivationBuilderParams(DerivationBuilderParams &&) = default;
};
@ -189,8 +190,6 @@ struct DerivationBuilder : RestrictionContext
};
std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
Store & store,
std::unique_ptr<DerivationBuilderCallbacks> miscMethods,
DerivationBuilderParams params);
Store & store, std::unique_ptr<DerivationBuilderCallbacks> miscMethods, DerivationBuilderParams params);
}
} // namespace nix

View file

@ -38,4 +38,4 @@ struct HookInstance
~HookInstance();
};
}
} // namespace nix

View file

@ -9,7 +9,7 @@ namespace nix {
struct UserLock
{
virtual ~UserLock() { }
virtual ~UserLock() {}
/**
* Get the first and last UID.
@ -40,4 +40,4 @@ std::unique_ptr<UserLock> acquireUserLock(uid_t nrIds, bool useUserNamespace);
bool useBuildUsers();
}
} // namespace nix

View file

@ -11,7 +11,6 @@
#include <sys/stat.h>
#include <sys/file.h>
namespace nix {
AutoCloseFD openLockFile(const Path & path, bool create)
@ -25,7 +24,6 @@ AutoCloseFD openLockFile(const Path & path, bool create)
return fd;
}
void deleteLockFile(const Path & path, Descriptor desc)
{
/* Get rid of the lock file. Have to be careful not to introduce
@ -38,14 +36,17 @@ void deleteLockFile(const Path & path, Descriptor desc)
file is an optimisation, not a necessity. */
}
bool lockFile(Descriptor desc, LockType lockType, bool wait)
{
int type;
if (lockType == ltRead) type = LOCK_SH;
else if (lockType == ltWrite) type = LOCK_EX;
else if (lockType == ltNone) type = LOCK_UN;
else unreachable();
if (lockType == ltRead)
type = LOCK_SH;
else if (lockType == ltWrite)
type = LOCK_EX;
else if (lockType == ltNone)
type = LOCK_UN;
else
unreachable();
if (wait) {
while (flock(desc, type) != 0) {
@ -58,7 +59,8 @@ bool lockFile(Descriptor desc, LockType lockType, bool wait)
} else {
while (flock(desc, type | LOCK_NB) != 0) {
checkInterrupt();
if (errno == EWOULDBLOCK) return false;
if (errno == EWOULDBLOCK)
return false;
if (errno != EINTR)
throw SysError("acquiring/releasing lock");
}
@ -67,9 +69,7 @@ bool lockFile(Descriptor desc, LockType lockType, bool wait)
return true;
}
bool PathLocks::lockPaths(const PathSet & paths,
const std::string & waitMsg, bool wait)
bool PathLocks::lockPaths(const PathSet & paths, const std::string & waitMsg, bool wait)
{
assert(fds.empty());
@ -95,7 +95,8 @@ bool PathLocks::lockPaths(const PathSet & paths,
/* Acquire an exclusive lock. */
if (!lockFile(fd.get(), ltWrite, false)) {
if (wait) {
if (waitMsg != "") printError(waitMsg);
if (waitMsg != "")
printError(waitMsg);
lockFile(fd.get(), ltWrite, true);
} else {
/* Failed to lock this path; release all other
@ -129,16 +130,14 @@ bool PathLocks::lockPaths(const PathSet & paths,
return true;
}
void PathLocks::unlock()
{
for (auto & i : fds) {
if (deletePaths) deleteLockFile(i.second, i.first);
if (deletePaths)
deleteLockFile(i.second, i.first);
if (close(i.first) == -1)
printError(
"error (ignored): cannot close lock file on '%1%'",
i.second);
printError("error (ignored): cannot close lock file on '%1%'", i.second);
debug("lock released on '%1%'", i.second);
}
@ -146,7 +145,6 @@ void PathLocks::unlock()
fds.clear();
}
FdLock::FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg)
: desc(desc)
{
@ -159,5 +157,4 @@ FdLock::FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view w
acquired = lockFile(desc, lockType, false);
}
}
} // namespace nix

View file

@ -13,12 +13,12 @@ namespace nix {
#ifdef __linux__
static std::vector<gid_t> get_group_list(const char *username, gid_t group_id)
static std::vector<gid_t> get_group_list(const char * username, gid_t group_id)
{
std::vector<gid_t> gids;
gids.resize(32); // Initial guess
auto getgroupl_failed {[&] {
auto getgroupl_failed{[&] {
int ngroups = gids.size();
int err = getgrouplist(username, group_id, gids.data(), &ngroups);
gids.resize(ngroups);
@ -35,7 +35,6 @@ static std::vector<gid_t> get_group_list(const char *username, gid_t group_id)
}
#endif
struct SimpleUserLock : UserLock
{
AutoCloseFD fdUserLock;
@ -43,11 +42,27 @@ struct SimpleUserLock : UserLock
gid_t gid;
std::vector<gid_t> supplementaryGIDs;
uid_t getUID() override { assert(uid); return uid; }
uid_t getUIDCount() override { return 1; }
gid_t getGID() override { assert(gid); return gid; }
uid_t getUID() override
{
assert(uid);
return uid;
}
std::vector<gid_t> getSupplementaryGIDs() override { return supplementaryGIDs; }
uid_t getUIDCount() override
{
return 1;
}
gid_t getGID() override
{
assert(gid);
return gid;
}
std::vector<gid_t> getSupplementaryGIDs() override
{
return supplementaryGIDs;
}
static std::unique_ptr<UserLock> acquire()
{
@ -61,7 +76,7 @@ struct SimpleUserLock : UserLock
/* Copy the result of getgrnam. */
Strings users;
for (char * * p = gr->gr_mem; *p; ++p) {
for (char ** p = gr->gr_mem; *p; ++p) {
debug("found build user '%s'", *p);
users.push_back(*p);
}
@ -78,7 +93,7 @@ struct SimpleUserLock : UserLock
if (!pw)
throw Error("the user '%s' in the group '%s' does not exist", i, settings.buildUsersGroup);
auto fnUserLock = fmt("%s/userpool/%s", settings.nixStateDir,pw->pw_uid);
auto fnUserLock = fmt("%s/userpool/%s", settings.nixStateDir, pw->pw_uid);
AutoCloseFD fd = open(fnUserLock.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (!fd)
@ -95,7 +110,7 @@ struct SimpleUserLock : UserLock
if (lock->uid == getuid() || lock->uid == geteuid())
throw Error("the Nix user should not be a member of '%s'", settings.buildUsersGroup);
#ifdef __linux__
#ifdef __linux__
/* Get the list of supplementary groups of this user. This is
* usually either empty or contains a group such as "kvm". */
@ -104,7 +119,7 @@ struct SimpleUserLock : UserLock
if (gid != lock->gid)
lock->supplementaryGIDs.push_back(gid);
}
#endif
#endif
return lock;
}
@ -121,19 +136,33 @@ struct AutoUserLock : UserLock
gid_t firstGid = 0;
uid_t nrIds = 1;
uid_t getUID() override { assert(firstUid); return firstUid; }
uid_t getUID() override
{
assert(firstUid);
return firstUid;
}
gid_t getUIDCount() override { return nrIds; }
gid_t getUIDCount() override
{
return nrIds;
}
gid_t getGID() override { assert(firstGid); return firstGid; }
gid_t getGID() override
{
assert(firstGid);
return firstGid;
}
std::vector<gid_t> getSupplementaryGIDs() override { return {}; }
std::vector<gid_t> getSupplementaryGIDs() override
{
return {};
}
static std::unique_ptr<UserLock> acquire(uid_t nrIds, bool useUserNamespace)
{
#if !defined(__linux__)
#if !defined(__linux__)
useUserNamespace = false;
#endif
#endif
experimentalFeatureSettings.require(Xp::AutoAllocateUids);
assert(settings.startId > 0);
@ -172,7 +201,8 @@ struct AutoUserLock : UserLock
else {
struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());
if (!gr)
throw Error("the group '%s' specified in 'build-users-group' does not exist", settings.buildUsersGroup);
throw Error(
"the group '%s' specified in 'build-users-group' does not exist", settings.buildUsersGroup);
lock->firstGid = gr->gr_gid;
}
lock->nrIds = nrIds;
@ -194,15 +224,15 @@ std::unique_ptr<UserLock> acquireUserLock(uid_t nrIds, bool useUserNamespace)
bool useBuildUsers()
{
#ifdef __linux__
#ifdef __linux__
static bool b = (settings.buildUsersGroup != "" || settings.autoAllocateUids) && isRootUser();
return b;
#elif defined(__APPLE__) || defined(__FreeBSD__)
#elif defined(__APPLE__) || defined(__FreeBSD__)
static bool b = settings.buildUsersGroup != "" && isRootUser();
return b;
#else
#else
return false;
#endif
#endif
}
}
} // namespace nix