1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +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

@ -18,14 +18,14 @@
#include "nix/util/logging.hh"
#ifndef _WIN32 // TODO need graceful async exit support on Windows?
# include "nix/util/monitor-fd.hh"
# include "nix/util/monitor-fd.hh"
#endif
#include <sstream>
namespace nix::daemon {
Sink & operator << (Sink & sink, const Logger::Fields & fields)
Sink & operator<<(Sink & sink, const Logger::Fields & fields)
{
sink << fields.size();
for (auto & f : fields) {
@ -34,7 +34,8 @@ Sink & operator << (Sink & sink, const Logger::Fields & fields)
sink << f.i;
else if (f.type == Logger::Field::tString)
sink << f.s;
else unreachable();
else
unreachable();
}
return sink;
}
@ -57,7 +58,10 @@ struct TunnelLogger : public Logger
WorkerProto::Version clientVersion;
TunnelLogger(FdSink & to, WorkerProto::Version clientVersion)
: to(to), clientVersion(clientVersion) { }
: to(to)
, clientVersion(clientVersion)
{
}
void enqueueMsg(const std::string & s)
{
@ -80,7 +84,8 @@ struct TunnelLogger : public Logger
void log(Verbosity lvl, std::string_view s) override
{
if (lvl > verbosity) return;
if (lvl > verbosity)
return;
StringSink buf;
buf << STDERR_NEXT << (s + "\n");
@ -89,7 +94,8 @@ struct TunnelLogger : public Logger
void logEI(const ErrorInfo & ei) override
{
if (ei.level > verbosity) return;
if (ei.level > verbosity)
return;
std::ostringstream oss;
showErrorInfo(oss, ei, false);
@ -133,8 +139,13 @@ struct TunnelLogger : public Logger
}
}
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
const std::string & s, const Fields & fields, ActivityId parent) override
void startActivity(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent) override
{
if (GET_PROTOCOL_MINOR(clientVersion) < 20) {
if (!s.empty())
@ -149,7 +160,8 @@ struct TunnelLogger : public Logger
void stopActivity(ActivityId act) override
{
if (GET_PROTOCOL_MINOR(clientVersion) < 20) return;
if (GET_PROTOCOL_MINOR(clientVersion) < 20)
return;
StringSink buf;
buf << STDERR_STOP_ACTIVITY << act;
enqueueMsg(buf.s);
@ -157,7 +169,8 @@ struct TunnelLogger : public Logger
void result(ActivityId act, ResultType type, const Fields & fields) override
{
if (GET_PROTOCOL_MINOR(clientVersion) < 20) return;
if (GET_PROTOCOL_MINOR(clientVersion) < 20)
return;
StringSink buf;
buf << STDERR_RESULT << act << type << fields;
enqueueMsg(buf.s);
@ -167,8 +180,13 @@ struct TunnelLogger : public Logger
struct TunnelSink : Sink
{
Sink & to;
TunnelSink(Sink & to) : to(to) { }
void operator () (std::string_view data) override
TunnelSink(Sink & to)
: to(to)
{
}
void operator()(std::string_view data) override
{
to << STDERR_WRITE;
writeString(data, to);
@ -179,13 +197,20 @@ struct TunnelSource : BufferedSource
{
Source & from;
BufferedSink & to;
TunnelSource(Source & from, BufferedSink & to) : from(from), to(to) { }
TunnelSource(Source & from, BufferedSink & to)
: from(from)
, to(to)
{
}
size_t readUnbuffered(char * data, size_t len) override
{
to << STDERR_READ << len;
to.flush();
size_t n = readString(data, len, from);
if (n == 0) throw EndOfFile("unexpected end-of-file");
if (n == 0)
throw EndOfFile("unexpected end-of-file");
return n;
}
};
@ -233,8 +258,10 @@ struct ClientSettings
else if (!hasSuffix(s, "/") && trusted.count(s + "/"))
subs.push_back(s + "/");
else
warn("ignoring untrusted substituter '%s', you are not a trusted user.\n"
"Run `man nix.conf` for more information on the `substituters` configuration option.", s);
warn(
"ignoring untrusted substituter '%s', you are not a trusted user.\n"
"Run `man nix.conf` for more information on the `substituters` configuration option.",
s);
res = subs;
return true;
};
@ -245,23 +272,24 @@ struct ClientSettings
else if (name == experimentalFeatureSettings.experimentalFeatures.name) {
// We dont want to forward the experimental features to
// the daemon, as that could cause some pretty weird stuff
if (parseFeatures(tokenizeString<StringSet>(value)) != experimentalFeatureSettings.experimentalFeatures.get())
if (parseFeatures(tokenizeString<StringSet>(value))
!= experimentalFeatureSettings.experimentalFeatures.get())
debug("Ignoring the client-specified experimental features");
} else if (name == "plugin-files") {
warn("Ignoring the client-specified plugin-files.\n"
"The client specifying plugins to the daemon never made sense, and was removed in Nix >=2.14.");
}
else if (trusted
|| name == settings.buildTimeout.name
|| name == settings.maxSilentTime.name
|| name == settings.pollInterval.name
|| name == "connect-timeout"
warn(
"Ignoring the client-specified plugin-files.\n"
"The client specifying plugins to the daemon never made sense, and was removed in Nix >=2.14.");
} else if (
trusted || name == settings.buildTimeout.name || name == settings.maxSilentTime.name
|| name == settings.pollInterval.name || name == "connect-timeout"
|| (name == "builders" && value == ""))
settings.set(name, value);
else if (setSubstituters(settings.substituters))
;
else
warn("ignoring the client-specified setting '%s', because it is a restricted setting and you are not a trusted user", name);
warn(
"ignoring the client-specified setting '%s', because it is a restricted setting and you are not a trusted user",
name);
} catch (UsageError & e) {
warn(e.what());
}
@ -269,8 +297,11 @@ struct ClientSettings
}
};
static void performOp(TunnelLogger * logger, ref<Store> store,
TrustedFlag trusted, RecursiveFlag recursive,
static void performOp(
TunnelLogger * logger,
ref<Store> store,
TrustedFlag trusted,
RecursiveFlag recursive,
WorkerProto::BasicServerConnection & conn,
WorkerProto::Op op)
{
@ -349,7 +380,8 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
store->queryReferrers(path, paths);
else if (op == WorkerProto::Op::QueryValidDerivers)
paths = store->queryValidDerivers(path);
else paths = store->queryDerivationOutputs(path);
else
paths = store->queryDerivationOutputs(path);
logger->stopWork();
WorkerProto::write(*store, wconn, paths);
break;
@ -424,7 +456,8 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
assert(false);
}
// TODO these two steps are essentially RemoteStore::addCAToStore. Move it up to Store.
auto path = store->addToStoreFromDump(source, name, dumpMethod, contentAddressMethod, hashAlgo, refs, repair);
auto path =
store->addToStoreFromDump(source, name, dumpMethod, contentAddressMethod, hashAlgo, refs, repair);
return store->queryPathInfo(path);
}();
logger->stopWork();
@ -440,10 +473,10 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
std::string hashAlgoRaw;
conn.from >> baseName >> fixed /* obsolete */ >> recursive >> hashAlgoRaw;
if (recursive > true)
throw Error("unsupported FileIngestionMethod with value of %i; you may need to upgrade nix-daemon", recursive);
method = recursive
? ContentAddressMethod::Raw::NixArchive
: ContentAddressMethod::Raw::Flat;
throw Error(
"unsupported FileIngestionMethod with value of %i; you may need to upgrade nix-daemon",
recursive);
method = recursive ? ContentAddressMethod::Raw::NixArchive : ContentAddressMethod::Raw::Flat;
/* Compatibility hack. */
if (!fixed) {
hashAlgoRaw = "sha256";
@ -467,8 +500,8 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
parseDump(sink, savedNARSource);
});
logger->startWork();
auto path = store->addToStoreFromDump(
*dumpSource, baseName, FileSerialisationMethod::NixArchive, method, hashAlgo);
auto path =
store->addToStoreFromDump(*dumpSource, baseName, FileSerialisationMethod::NixArchive, method, hashAlgo);
logger->stopWork();
conn.to << store->printStorePath(path);
@ -485,9 +518,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
{
FramedSource source(conn.from);
store->addMultipleToStore(source,
RepairFlag{repair},
dontCheckSigs ? NoCheckSigs : CheckSigs);
store->addMultipleToStore(source, RepairFlag{repair}, dontCheckSigs ? NoCheckSigs : CheckSigs);
}
logger->stopWork();
break;
@ -499,8 +530,15 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
auto refs = WorkerProto::Serialise<StorePathSet>::read(*store, rconn);
logger->startWork();
auto path = ({
StringSource source { s };
store->addToStoreFromDump(source, suffix, FileSerialisationMethod::Flat, ContentAddressMethod::Raw::Text, HashAlgorithm::SHA256, refs, NoRepair);
StringSource source{s};
store->addToStoreFromDump(
source,
suffix,
FileSerialisationMethod::Flat,
ContentAddressMethod::Raw::Text,
HashAlgorithm::SHA256,
refs,
NoRepair);
});
logger->stopWork();
conn.to << store->printStorePath(path);
@ -521,11 +559,11 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
case WorkerProto::Op::ImportPaths: {
logger->startWork();
TunnelSource source(conn.from, conn.to);
auto paths = store->importPaths(source,
trusted ? NoCheckSigs : CheckSigs);
auto paths = store->importPaths(source, trusted ? NoCheckSigs : CheckSigs);
logger->stopWork();
Strings paths2;
for (auto & i : paths) paths2.push_back(store->printStorePath(i));
for (auto & i : paths)
paths2.push_back(store->printStorePath(i));
conn.to << paths2;
break;
}
@ -644,7 +682,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
Derivation drv2;
static_cast<BasicDerivation &>(drv2) = drv;
drvPath = writeDerivation(*store, Derivation { drv2 });
drvPath = writeDerivation(*store, Derivation{drv2});
}
auto res = store->buildDerivation(drvPath, drv, buildMode);
@ -796,11 +834,9 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
if (i == infos.end())
conn.to << 0;
else {
conn.to << 1
<< (i->second.deriver ? store->printStorePath(*i->second.deriver) : "");
conn.to << 1 << (i->second.deriver ? store->printStorePath(*i->second.deriver) : "");
WorkerProto::write(*store, wconn, i->second.references);
conn.to << i->second.downloadSize
<< i->second.narSize;
conn.to << i->second.downloadSize << i->second.narSize;
}
break;
}
@ -842,7 +878,8 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
try {
info = store->queryPathInfo(path);
} catch (InvalidPath &) {
if (GET_PROTOCOL_MINOR(conn.protoVersion) < 17) throw;
if (GET_PROTOCOL_MINOR(conn.protoVersion) < 17)
throw;
}
logger->stopWork();
if (info) {
@ -898,7 +935,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
auto path = store->parseStorePath(readString(conn.from));
auto deriver = readString(conn.from);
auto narHash = Hash::parseAny(readString(conn.from), HashAlgorithm::SHA256);
ValidPathInfo info { path, narHash };
ValidPathInfo info{path, narHash};
if (deriver != "")
info.deriver = store->parseStorePath(deriver);
info.references = WorkerProto::Serialise<StorePathSet>::read(*store, rconn);
@ -915,8 +952,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
{
FramedSource source(conn.from);
store->addToStore(info, source, (RepairFlag) repair,
dontCheckSigs ? NoCheckSigs : CheckSigs);
store->addToStore(info, source, (RepairFlag) repair, dontCheckSigs ? NoCheckSigs : CheckSigs);
}
logger->stopWork();
}
@ -927,7 +963,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
if (GET_PROTOCOL_MINOR(conn.protoVersion) >= 21)
source = std::make_unique<TunnelSource>(conn.from, conn.to);
else {
TeeSource tee { conn.from, saved };
TeeSource tee{conn.from, saved};
NullFileSystemObjectSink ether;
parseDump(ether, tee);
source = std::make_unique<StringSource>(saved.s);
@ -936,8 +972,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->startWork();
// FIXME: race if addToStore doesn't read source?
store->addToStore(info, *source, (RepairFlag) repair,
dontCheckSigs ? NoCheckSigs : CheckSigs);
store->addToStore(info, *source, (RepairFlag) repair, dontCheckSigs ? NoCheckSigs : CheckSigs);
logger->stopWork();
}
@ -962,8 +997,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
if (GET_PROTOCOL_MINOR(conn.protoVersion) < 31) {
auto outputId = DrvOutput::parse(readString(conn.from));
auto outputPath = StorePath(readString(conn.from));
store->registerDrvOutput(Realisation{
.id = outputId, .outPath = outputPath});
store->registerDrvOutput(Realisation{.id = outputId, .outPath = outputPath});
} else {
auto realisation = WorkerProto::Serialise<Realisation>::read(*store, rconn);
store->registerDrvOutput(realisation);
@ -979,11 +1013,13 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
logger->stopWork();
if (GET_PROTOCOL_MINOR(conn.protoVersion) < 31) {
std::set<StorePath> outPaths;
if (info) outPaths.insert(info->outPath);
if (info)
outPaths.insert(info->outPath);
WorkerProto::write(*store, wconn, outPaths);
} else {
std::set<Realisation> realisations;
if (info) realisations.insert(*info);
if (info)
realisations.insert(*info);
WorkerProto::write(*store, wconn, realisations);
}
break;
@ -1015,12 +1051,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
}
}
void processConnection(
ref<Store> store,
FdSource && from,
FdSink && to,
TrustedFlag trusted,
RecursiveFlag recursive)
void processConnection(ref<Store> store, FdSource && from, FdSink && to, TrustedFlag trusted, RecursiveFlag recursive)
{
#ifndef _WIN32 // TODO need graceful async exit support on Windows?
auto monitor = !recursive ? std::make_unique<MonitorFdHup>(from.fd) : nullptr;
@ -1029,8 +1060,7 @@ void processConnection(
/* Exchange the greeting. */
auto [protoVersion, features] =
WorkerProto::BasicServerConnection::handshake(
to, from, PROTOCOL_VERSION, WorkerProto::allFeatures);
WorkerProto::BasicServerConnection::handshake(to, from, PROTOCOL_VERSION, WorkerProto::allFeatures);
if (protoVersion < 0x10a)
throw Error("the Nix client version is too old");
@ -1059,14 +1089,14 @@ void processConnection(
printMsgUsing(prevLogger, lvlDebug, "%d operations", opCount);
});
conn.postHandshake(*store, {
.daemonNixVersion = nixVersion,
// We and the underlying store both need to trust the client for
// it to be trusted.
.remoteTrustsUs = trusted
? store->isTrustedClient()
: std::optional { NotTrusted },
});
conn.postHandshake(
*store,
{
.daemonNixVersion = nixVersion,
// We and the underlying store both need to trust the client for
// it to be trusted.
.remoteTrustsUs = trusted ? store->isTrustedClient() : std::optional{NotTrusted},
});
/* Send startup error messages to the client. */
tunnelLogger->startWork();
@ -1103,7 +1133,8 @@ void processConnection(
happens, just send the error message and exit. */
bool errorAllowed = tunnelLogger->state_.lock()->canSendStderr;
tunnelLogger->stopWork(&e);
if (!errorAllowed) throw;
if (!errorAllowed)
throw;
} catch (std::bad_alloc & e) {
auto ex = Error("Nix daemon out of memory");
tunnelLogger->stopWork(&ex);
@ -1127,4 +1158,4 @@ void processConnection(
}
}
}
} // namespace nix::daemon