From 5dd4154869f9ce32cd98e9df66fef15962facd24 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Thu, 23 Jan 2025 23:38:07 +0100 Subject: [PATCH] treewide: add async io roots to entry points it begins. Change-Id: I4b762d48180a763d2724fc635ca025f0333026b0 --- lix/legacy/build-remote.cc | 2 +- lix/legacy/nix-build.cc | 4 +- lix/legacy/nix-channel.cc | 4 +- lix/legacy/nix-collect-garbage.cc | 4 +- lix/legacy/nix-copy-closure.cc | 4 +- lix/legacy/nix-env.cc | 7 ++- lix/legacy/nix-instantiate.cc | 4 +- lix/legacy/nix-store.cc | 58 +++++++++---------- lix/libcmd/command.hh | 35 +++++++++-- lix/libcmd/legacy.hh | 3 +- lix/libcmd/meson.build | 1 + lix/libexpr/meson.build | 1 + lix/libfetchers/meson.build | 1 + lix/libmain/meson.build | 1 + lix/libmain/shared.cc | 4 +- lix/libmain/shared.hh | 5 +- lix/libstore/build/local-derivation-goal.cc | 3 +- lix/libstore/daemon.cc | 5 +- lix/libstore/daemon.hh | 2 + lix/libutil/args.cc | 4 +- lix/libutil/args.hh | 5 +- lix/libutil/meson.build | 1 + lix/nix/config.cc | 2 +- lix/nix/daemon.cc | 24 ++++---- lix/nix/derivation.cc | 2 +- lix/nix/flake.cc | 24 ++++---- lix/nix/hash.cc | 35 +++++++---- lix/nix/main.cc | 19 ++++-- lix/nix/meson.build | 1 + lix/nix/prefetch.cc | 11 ++-- lix/nix/profile.cc | 16 ++--- lix/nix/realisation.cc | 2 +- lix/nix/registry.cc | 12 ++-- lix/nix/sigs.cc | 8 ++- lix/nix/store.cc | 2 +- .../test-libstoreconsumer/meson.build | 1 + tests/unit/meson.build | 5 ++ 37 files changed, 200 insertions(+), 122 deletions(-) diff --git a/lix/legacy/build-remote.cc b/lix/legacy/build-remote.cc index e2d76d008..234e58ecd 100644 --- a/lix/legacy/build-remote.cc +++ b/lix/legacy/build-remote.cc @@ -55,7 +55,7 @@ static bool allSupportedLocally(Store & store, const std::set& requ return true; } -static int main_build_remote(std::string programName, Strings argv) +static int main_build_remote(AsyncIoRoot & aio, std::string programName, Strings argv) { { logger = makeJSONLogger(*logger); diff --git a/lix/legacy/nix-build.cc b/lix/legacy/nix-build.cc index 5423cf5b9..ddf6575f1 100644 --- a/lix/legacy/nix-build.cc +++ b/lix/legacy/nix-build.cc @@ -33,7 +33,7 @@ namespace nix { using namespace std::string_literals; -static void main_nix_build(std::string programName, Strings argv) +static void main_nix_build(AsyncIoRoot & aio, std::string programName, Strings argv) { auto dryRun = false; auto runEnv = std::regex_search(programName, std::regex("nix-shell$")); @@ -94,7 +94,7 @@ static void main_nix_build(std::string programName, Strings argv) using LegacyArgs::LegacyArgs; }; - MyArgs myArgs(myName, [&](Strings::iterator & arg, const Strings::iterator & end) { + MyArgs myArgs(aio, myName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") { showManPage(myName); } diff --git a/lix/legacy/nix-channel.cc b/lix/legacy/nix-channel.cc index ac127f080..41271ef49 100644 --- a/lix/legacy/nix-channel.cc +++ b/lix/legacy/nix-channel.cc @@ -162,7 +162,7 @@ static void update(const StringSet & channelNames) replaceSymlink(profile, channelLink); } -static int main_nix_channel(std::string programName, Strings argv) +static int main_nix_channel(AsyncIoRoot & aio, std::string programName, Strings argv) { { // Figure out the name of the `.nix-channels' file to use @@ -184,7 +184,7 @@ static int main_nix_channel(std::string programName, Strings argv) cRollback } cmd = cNone; std::vector args; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") { showManPage("nix-channel"); } else if (*arg == "--version") { diff --git a/lix/legacy/nix-collect-garbage.cc b/lix/legacy/nix-collect-garbage.cc index b818611ab..0905d3027 100644 --- a/lix/legacy/nix-collect-garbage.cc +++ b/lix/legacy/nix-collect-garbage.cc @@ -56,14 +56,14 @@ void removeOldGenerations(std::string dir) } } -static int main_nix_collect_garbage(std::string programName, Strings argv) +static int main_nix_collect_garbage(AsyncIoRoot & aio, std::string programName, Strings argv) { { bool removeOld = false; GCOptions options; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") showManPage("nix-collect-garbage"); else if (*arg == "--version") diff --git a/lix/legacy/nix-copy-closure.cc b/lix/legacy/nix-copy-closure.cc index ebed8a191..0522236fa 100644 --- a/lix/legacy/nix-copy-closure.cc +++ b/lix/legacy/nix-copy-closure.cc @@ -5,7 +5,7 @@ namespace nix { -static int main_nix_copy_closure(std::string programName, Strings argv) +static int main_nix_copy_closure(AsyncIoRoot & aio, std::string programName, Strings argv) { { auto gzip = false; @@ -16,7 +16,7 @@ static int main_nix_copy_closure(std::string programName, Strings argv) std::string sshHost; PathSet storePaths; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") showManPage("nix-copy-closure"); else if (*arg == "--version") diff --git a/lix/legacy/nix-env.cc b/lix/legacy/nix-env.cc index 1a98bedaa..ca4444db2 100644 --- a/lix/legacy/nix-env.cc +++ b/lix/legacy/nix-env.cc @@ -57,6 +57,7 @@ struct InstallSourceInfo struct Globals { + AsyncIoRoot & aio; InstallSourceInfo instSource; Path profile; std::shared_ptr state; @@ -1412,7 +1413,7 @@ static void opVersion(Globals & globals, Strings opFlags, Strings opArgs) } -static int main_nix_env(std::string programName, Strings argv) +static int main_nix_env(AsyncIoRoot & aio, std::string programName, Strings argv) { { Strings opFlags, opArgs; @@ -1421,7 +1422,7 @@ static int main_nix_env(std::string programName, Strings argv) bool showHelp = false; std::string file; - Globals globals; + Globals globals{aio, {}, {}, {}, {}, {}, {}, {}, {}}; globals.instSource.type = srcUnknown; globals.instSource.systemFilter = "*"; @@ -1451,7 +1452,7 @@ static int main_nix_env(std::string programName, Strings argv) using LegacyArgs::LegacyArgs; }; - MyArgs myArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + MyArgs myArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { Operation oldOp = op; if (*arg == "--help") diff --git a/lix/legacy/nix-instantiate.cc b/lix/legacy/nix-instantiate.cc index aaf0b1701..8578d1747 100644 --- a/lix/legacy/nix-instantiate.cc +++ b/lix/legacy/nix-instantiate.cc @@ -89,7 +89,7 @@ void processExpr(EvalState & state, const Strings & attrPaths, } -static int main_nix_instantiate(std::string programName, Strings argv) +static int main_nix_instantiate(AsyncIoRoot & aio, std::string programName, Strings argv) { { Strings files; @@ -109,7 +109,7 @@ static int main_nix_instantiate(std::string programName, Strings argv) using LegacyArgs::LegacyArgs; }; - MyArgs myArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + MyArgs myArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") showManPage("nix-instantiate"); else if (*arg == "--version") diff --git a/lix/legacy/nix-store.cc b/lix/legacy/nix-store.cc index 20dfc7412..7a38eac9a 100644 --- a/lix/legacy/nix-store.cc +++ b/lix/legacy/nix-store.cc @@ -32,7 +32,7 @@ using std::cin; using std::cout; -typedef void (* Operation) (Strings opFlags, Strings opArgs); +typedef void (* Operation) (AsyncIoRoot & aio, Strings opFlags, Strings opArgs); static Path gcRoot; @@ -119,7 +119,7 @@ static PathSet realisePath(StorePathWithOutputs path, bool build = true) /* Realise the given paths. */ -static void opRealise(Strings opFlags, Strings opArgs) +static void opRealise(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { bool dryRun = false; BuildMode buildMode = bmNormal; @@ -170,7 +170,7 @@ static void opRealise(Strings opFlags, Strings opArgs) /* Add files to the Nix store and print the resulting paths. */ -static void opAdd(Strings opFlags, Strings opArgs) +static void opAdd(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); @@ -181,7 +181,7 @@ static void opAdd(Strings opFlags, Strings opArgs) /* Preload the output of a fixed-output derivation into the Nix store. */ -static void opAddFixed(Strings opFlags, Strings opArgs) +static void opAddFixed(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { auto method = FileIngestionMethod::Flat; @@ -201,7 +201,7 @@ static void opAddFixed(Strings opFlags, Strings opArgs) /* Hack to support caching in `nix-prefetch-url'. */ -static void opPrintFixedPath(Strings opFlags, Strings opArgs) +static void opPrintFixedPath(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { auto method = FileIngestionMethod::Flat; @@ -277,7 +277,7 @@ static void printTree(const StorePath & path, /* Perform various sorts of queries. */ -static void opQuery(Strings opFlags, Strings opArgs) +static void opQuery(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { enum QueryType { qOutputs, qRequisites, qReferences, qReferrers @@ -468,7 +468,7 @@ static void opQuery(Strings opFlags, Strings opArgs) } -static void opPrintEnv(Strings opFlags, Strings opArgs) +static void opPrintEnv(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (opArgs.size() != 1) throw UsageError("'--print-env' requires one derivation store path"); @@ -494,7 +494,7 @@ static void opPrintEnv(Strings opFlags, Strings opArgs) } -static void opReadLog(Strings opFlags, Strings opArgs) +static void opReadLog(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); @@ -512,7 +512,7 @@ static void opReadLog(Strings opFlags, Strings opArgs) } -static void opDumpDB(Strings opFlags, Strings opArgs) +static void opDumpDB(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (!opArgs.empty()) { @@ -552,7 +552,7 @@ static void registerValidity(bool reregister, bool hashGiven, bool canonicalise) } -static void opLoadDB(Strings opFlags, Strings opArgs) +static void opLoadDB(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (!opArgs.empty()) @@ -561,7 +561,7 @@ static void opLoadDB(Strings opFlags, Strings opArgs) } -static void opRegisterValidity(Strings opFlags, Strings opArgs) +static void opRegisterValidity(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { bool reregister = false; // !!! maybe this should be the default bool hashGiven = false; @@ -577,7 +577,7 @@ static void opRegisterValidity(Strings opFlags, Strings opArgs) } -static void opCheckValidity(Strings opFlags, Strings opArgs) +static void opCheckValidity(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { bool printInvalid = false; @@ -597,7 +597,7 @@ static void opCheckValidity(Strings opFlags, Strings opArgs) } -static void opGC(Strings opFlags, Strings opArgs) +static void opGC(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { bool printRoots = false; GCOptions options; @@ -643,7 +643,7 @@ static void opGC(Strings opFlags, Strings opArgs) /* Remove paths from the Nix store if possible (i.e., if they do not have any remaining referrers and are not reachable from any GC roots). */ -static void opDelete(Strings opFlags, Strings opArgs) +static void opDelete(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { GCOptions options; options.action = GCOptions::gcDeleteSpecific; @@ -664,7 +664,7 @@ static void opDelete(Strings opFlags, Strings opArgs) /* Dump a path as a Nix archive. The archive is written to stdout */ -static void opDump(Strings opFlags, Strings opArgs) +static void opDump(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (opArgs.size() != 1) throw UsageError("only one argument allowed"); @@ -677,7 +677,7 @@ static void opDump(Strings opFlags, Strings opArgs) /* Restore a value from a Nix archive. The archive is read from stdin. */ -static void opRestore(Strings opFlags, Strings opArgs) +static void opRestore(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (opArgs.size() != 1) throw UsageError("only one argument allowed"); @@ -687,7 +687,7 @@ static void opRestore(Strings opFlags, Strings opArgs) } -static void opExport(Strings opFlags, Strings opArgs) +static void opExport(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { for (auto & i : opFlags) throw UsageError("unknown flag '%1%'", i); @@ -703,7 +703,7 @@ static void opExport(Strings opFlags, Strings opArgs) } -static void opImport(Strings opFlags, Strings opArgs) +static void opImport(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { for (auto & i : opFlags) throw UsageError("unknown flag '%1%'", i); @@ -719,7 +719,7 @@ static void opImport(Strings opFlags, Strings opArgs) /* Initialise the Nix databases. */ -static void opInit(Strings opFlags, Strings opArgs) +static void opInit(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); if (!opArgs.empty()) @@ -730,7 +730,7 @@ static void opInit(Strings opFlags, Strings opArgs) /* Verify the consistency of the Nix environment. */ -static void opVerify(Strings opFlags, Strings opArgs) +static void opVerify(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opArgs.empty()) throw UsageError("no arguments expected"); @@ -751,7 +751,7 @@ static void opVerify(Strings opFlags, Strings opArgs) /* Verify whether the contents of the given store path have not changed. */ -static void opVerifyPath(Strings opFlags, Strings opArgs) +static void opVerifyPath(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("no flags expected"); @@ -780,7 +780,7 @@ static void opVerifyPath(Strings opFlags, Strings opArgs) /* Repair the contents of the given path by redownloading it using a substituter (if available). */ -static void opRepairPath(Strings opFlags, Strings opArgs) +static void opRepairPath(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("no flags expected"); @@ -791,7 +791,7 @@ static void opRepairPath(Strings opFlags, Strings opArgs) /* Optimise the disk space usage of the Nix store by hard-linking files with the same contents. */ -static void opOptimise(Strings opFlags, Strings opArgs) +static void opOptimise(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { if (!opArgs.empty() || !opFlags.empty()) throw UsageError("no arguments expected"); @@ -800,7 +800,7 @@ static void opOptimise(Strings opFlags, Strings opArgs) } /* Serve the nix store in a way usable by a restricted ssh user. */ -static void opServe(Strings opFlags, Strings opArgs) +static void opServe(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { bool writeAllowed = false; for (auto & i : opFlags) @@ -1003,7 +1003,7 @@ static void opServe(Strings opFlags, Strings opArgs) } -static void opGenerateBinaryCacheKey(Strings opFlags, Strings opArgs) +static void opGenerateBinaryCacheKey(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { for (auto & i : opFlags) throw UsageError("unknown flag '%1%'", i); @@ -1022,7 +1022,7 @@ static void opGenerateBinaryCacheKey(Strings opFlags, Strings opArgs) } -static void opVersion(Strings opFlags, Strings opArgs) +static void opVersion(AsyncIoRoot & aio, Strings opFlags, Strings opArgs) { printVersion("nix-store"); } @@ -1031,7 +1031,7 @@ static void opVersion(Strings opFlags, Strings opArgs) /* Scan the arguments; find the operation, set global flags, put all other flags in a list, and put all other arguments in another list. */ -static int main_nix_store(std::string programName, Strings argv) +static int main_nix_store(AsyncIoRoot & aio, std::string programName, Strings argv) { { Strings opFlags, opArgs; @@ -1040,7 +1040,7 @@ static int main_nix_store(std::string programName, Strings argv) std::string opName; bool showHelp = false; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { Operation oldOp = op; if (*arg == "--help") @@ -1170,7 +1170,7 @@ static int main_nix_store(std::string programName, Strings argv) if (op != opDump && op != opRestore) /* !!! hack */ store = openStore(); - op(std::move(opFlags), std::move(opArgs)); + op(aio, std::move(opFlags), std::move(opArgs)); return 0; } diff --git a/lix/libcmd/command.hh b/lix/libcmd/command.hh index 90199145c..96e707c1f 100644 --- a/lix/libcmd/command.hh +++ b/lix/libcmd/command.hh @@ -263,11 +263,14 @@ struct StorePathCommand : public StorePathsCommand */ struct RegisterCommand { - typedef std::map, std::function()>> Commands; + typedef std::map< + std::vector, + std::function(AsyncIoRoot & aio)> + > Commands; static Commands * commands; RegisterCommand(std::vector && name, - std::function()> command) + std::function(AsyncIoRoot & aio)> command) { if (!commands) commands = new Commands; commands->emplace(name, command); @@ -276,16 +279,40 @@ struct RegisterCommand static nix::Commands getCommandsFor(const std::vector & prefix); }; +template +class MixAio : public Base +{ +private: + AsyncIoRoot & aio_; + +public: + template + MixAio(AsyncIoRoot & aio, Args &&... args) + : Base(std::forward(args)...) + , aio_(aio) + { + } + + AsyncIoRoot & aio() override + { + return aio_; + } +}; + template static RegisterCommand registerCommand(const std::string & name) { - return RegisterCommand({name}, [](){ return make_ref(); }); + return RegisterCommand({name}, [](AsyncIoRoot & aio) { + return make_ref>(aio); + }); } template static RegisterCommand registerCommand2(std::vector && name) { - return RegisterCommand(std::move(name), [](){ return make_ref(); }); + return RegisterCommand(std::move(name), [](AsyncIoRoot & aio) { + return make_ref>(aio); + }); } struct MixProfile : virtual StoreCommand diff --git a/lix/libcmd/legacy.hh b/lix/libcmd/legacy.hh index 9c80ae37d..ab1f1881d 100644 --- a/lix/libcmd/legacy.hh +++ b/lix/libcmd/legacy.hh @@ -1,6 +1,7 @@ #pragma once ///@file +#include "lix/libutil/async.hh" #include #include #include @@ -8,7 +9,7 @@ namespace nix { -typedef std::function)> MainFunction; +typedef std::function)> MainFunction; struct LegacyCommands { diff --git a/lix/libcmd/meson.build b/lix/libcmd/meson.build index 3a178e148..30d1f4f73 100644 --- a/lix/libcmd/meson.build +++ b/lix/libcmd/meson.build @@ -52,6 +52,7 @@ libcmd = library( lowdown, nlohmann_json, liblix_doc, + kj, ], # '../..' for self references like "lix/libcmd/*.hh" include_directories : [ '../..' ], diff --git a/lix/libexpr/meson.build b/lix/libexpr/meson.build index dcbc12e7d..35f8432a0 100644 --- a/lix/libexpr/meson.build +++ b/lix/libexpr/meson.build @@ -274,6 +274,7 @@ libexpr = library( boost, toml11, nlohmann_json, + kj, ], # for shared.hh include_directories : [ diff --git a/lix/libfetchers/meson.build b/lix/libfetchers/meson.build index dd906d7a2..980da8a2b 100644 --- a/lix/libfetchers/meson.build +++ b/lix/libfetchers/meson.build @@ -54,6 +54,7 @@ libfetchers = library( liblixstore, liblixutil, nlohmann_json, + kj, ], include_directories : [ '../..' ], cpp_pch : cpp_pch, diff --git a/lix/libmain/meson.build b/lix/libmain/meson.build index bbd6f9bb7..c71607983 100644 --- a/lix/libmain/meson.build +++ b/lix/libmain/meson.build @@ -21,6 +21,7 @@ libmain = library( dependencies : [ liblixutil, liblixstore, + kj, ], include_directories : [ '../..' ], cpp_pch : cpp_pch, diff --git a/lix/libmain/shared.cc b/lix/libmain/shared.cc index bbe3fb453..da76d90b5 100644 --- a/lix/libmain/shared.cc +++ b/lix/libmain/shared.cc @@ -175,9 +175,9 @@ void initNix() } -LegacyArgs::LegacyArgs(const std::string & programName, +LegacyArgs::LegacyArgs(AsyncIoRoot & aio, const std::string & programName, std::function parseArg) - : MixCommonArgs(programName), parseArg(parseArg) + : MixCommonArgs(programName), aio_(aio), parseArg(parseArg) { addFlag({ .longName = "no-build-output", diff --git a/lix/libmain/shared.hh b/lix/libmain/shared.hh index 20c6c4e60..2f6943216 100644 --- a/lix/libmain/shared.hh +++ b/lix/libmain/shared.hh @@ -53,9 +53,12 @@ template N getIntArg(const std::string & opt, struct LegacyArgs : public MixCommonArgs, public RootArgs { + AsyncIoRoot & aio_; + AsyncIoRoot & aio() override { return aio_; } + std::function parseArg; - LegacyArgs(const std::string & programName, + LegacyArgs(AsyncIoRoot & aio, const std::string & programName, std::function parseArg); bool processFlag(Strings::iterator & pos, Strings::iterator end) override; diff --git a/lix/libstore/build/local-derivation-goal.cc b/lix/libstore/build/local-derivation-goal.cc index ad89787ab..7e5f97f8d 100644 --- a/lix/libstore/build/local-derivation-goal.cc +++ b/lix/libstore/build/local-derivation-goal.cc @@ -1257,7 +1257,8 @@ void LocalDerivationGoal::startDaemon() FdSource from(remote.get()); FdSink to(remote.get()); try { - daemon::processConnection(store, from, to, + AsyncIoRoot aio; + daemon::processConnection(aio, store, from, to, NotTrusted, daemon::Recursive); debug("terminated daemon connection"); } catch (SysError &) { diff --git a/lix/libstore/daemon.cc b/lix/libstore/daemon.cc index 236050089..713873872 100644 --- a/lix/libstore/daemon.cc +++ b/lix/libstore/daemon.cc @@ -261,7 +261,7 @@ struct ClientSettings } }; -static void performOp(TunnelLogger * logger, ref store, +static void performOp(AsyncIoRoot & aio, TunnelLogger * logger, ref store, TrustedFlag trusted, RecursiveFlag recursive, WorkerProto::Version clientVersion, Source & from, BufferedSink & to, WorkerProto::Op op) { @@ -995,6 +995,7 @@ static void performOp(TunnelLogger * logger, ref store, } void processConnection( + AsyncIoRoot & aio, ref store, FdSource & from, FdSink & to, @@ -1073,7 +1074,7 @@ void processConnection( debug("performing daemon worker op: %d", op); try { - performOp(tunnelLogger, store, trusted, recursive, clientVersion, from, to, op); + performOp(aio, tunnelLogger, store, trusted, recursive, clientVersion, from, to, op); } catch (Error & e) { /* If we're not in a state where we can send replies, then something went wrong processing the input of the diff --git a/lix/libstore/daemon.hh b/lix/libstore/daemon.hh index 9e004ec6c..fbc7180d2 100644 --- a/lix/libstore/daemon.hh +++ b/lix/libstore/daemon.hh @@ -1,6 +1,7 @@ #pragma once ///@file +#include "lix/libutil/async.hh" #include "lix/libutil/serialise.hh" #include "lix/libstore/store-api.hh" @@ -9,6 +10,7 @@ namespace nix::daemon { enum RecursiveFlag : bool { NotRecursive = false, Recursive = true }; void processConnection( + AsyncIoRoot & aio, ref store, FdSource & from, FdSink & to, diff --git a/lix/libutil/args.cc b/lix/libutil/args.cc index 343101219..fa063f28e 100644 --- a/lix/libutil/args.cc +++ b/lix/libutil/args.cc @@ -402,7 +402,7 @@ MultiCommand::MultiCommand(const Commands & commands_) auto suggestions = Suggestions::bestMatches(commandNames, s); throw UsageError(suggestions, "'%s' is not a recognised command", s); } - command = {s, i->second()}; + command = {s, i->second(aio())}; command->second->parent = this; }}, .completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) { @@ -437,7 +437,7 @@ nlohmann::json MultiCommand::toJSON() auto cmds = nlohmann::json::object(); for (auto & [name, commandFun] : commands) { - auto command = commandFun(); + auto command = commandFun(aio()); auto j = command->toJSON(); auto cat = nlohmann::json::object(); cat["id"] = command->category(); diff --git a/lix/libutil/args.hh b/lix/libutil/args.hh index 991695772..7bca5cdc5 100644 --- a/lix/libutil/args.hh +++ b/lix/libutil/args.hh @@ -1,6 +1,7 @@ #pragma once ///@file +#include "lix/libutil/async.hh" #include "lix/libutil/experimental-features.hh" #include "lix/libutil/types.hh" #include "lix/libutil/ref.hh" @@ -41,6 +42,8 @@ public: */ virtual std::string doc() { return ""; } + virtual AsyncIoRoot & aio() = 0; + protected: /** @@ -337,7 +340,7 @@ struct Command : virtual public Args virtual Category category() { return catDefault; } }; -typedef std::map()>> Commands; +typedef std::map(AsyncIoRoot &)>> Commands; /** * An argument parser that supports multiple subcommands, diff --git a/lix/libutil/meson.build b/lix/libutil/meson.build index 4a04698cb..5154f1ae2 100644 --- a/lix/libutil/meson.build +++ b/lix/libutil/meson.build @@ -295,6 +295,7 @@ libutil = library( brotli, openssl, nlohmann_json, + kj, ], include_directories : [ '../..' ], cpp_pch : cpp_pch, diff --git a/lix/nix/config.cc b/lix/nix/config.cc index 6448af184..901422006 100644 --- a/lix/nix/config.cc +++ b/lix/nix/config.cc @@ -7,7 +7,7 @@ using namespace nix; -struct CmdConfig final : MultiCommand +struct CmdConfig : MultiCommand { CmdConfig() : MultiCommand(RegisterCommand::getCommandsFor({"config"})) { } diff --git a/lix/nix/daemon.cc b/lix/nix/daemon.cc index e67197a0a..3301695be 100644 --- a/lix/nix/daemon.cc +++ b/lix/nix/daemon.cc @@ -252,7 +252,7 @@ static std::pair authPeer(const PeerInfo & peer) * the client. Otherwise, decide based on the authentication settings * and user credentials (from the unix domain socket). */ -static void daemonLoop(std::optional forceTrustClientOpt) +static void daemonLoop(AsyncIoRoot & aio, std::optional forceTrustClientOpt) { if (chdir("/") == -1) throw SysError("cannot change current directory"); @@ -340,7 +340,7 @@ static void daemonLoop(std::optional forceTrustClientOpt) // Handle the connection. FdSource from(remote.get()); FdSink to(remote.get()); - processConnection(openUncachedStore(), from, to, trusted, NotRecursive); + processConnection(aio, openUncachedStore(), from, to, trusted, NotRecursive); exit(0); }, options).release(); @@ -403,11 +403,12 @@ static void forwardStdioConnection(RemoteStore & store) { * @param trustClient Whether to trust the client. Forwarded directly to * `processConnection()`. */ -static void processStdioConnection(ref store, TrustedFlag trustClient) +static void +processStdioConnection(AsyncIoRoot & aio, ref store, TrustedFlag trustClient) { FdSource from(STDIN_FILENO); FdSink to(STDOUT_FILENO); - processConnection(store, from, to, trustClient, NotRecursive); + processConnection(aio, store, from, to, trustClient, NotRecursive); } /** @@ -417,7 +418,8 @@ static void processStdioConnection(ref store, TrustedFlag trustClient) * @param forceTrustClientOpt See `daemonLoop()` and the parameter with * the same name over there for details. */ -static void runDaemon(bool stdio, std::optional forceTrustClientOpt) +static void +runDaemon(AsyncIoRoot & aio, bool stdio, std::optional forceTrustClientOpt) { if (stdio) { auto store = openUncachedStore(); @@ -431,18 +433,18 @@ static void runDaemon(bool stdio, std::optional forceTrustClientOpt // `Trusted` is passed in the auto (no override case) because we // cannot see who is on the other side of a plain pipe. Limiting // access to those is explicitly not `nix-daemon`'s responsibility. - processStdioConnection(store, forceTrustClientOpt.value_or(Trusted)); + processStdioConnection(aio, store, forceTrustClientOpt.value_or(Trusted)); } else - daemonLoop(forceTrustClientOpt); + daemonLoop(aio, forceTrustClientOpt); } -static int main_nix_daemon(std::string programName, Strings argv) +static int main_nix_daemon(AsyncIoRoot & aio, std::string programName, Strings argv) { { auto stdio = false; std::optional isTrustedOpt = std::nullopt; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--daemon") ; // ignored for backwards compatibility else if (*arg == "--help") @@ -464,7 +466,7 @@ static int main_nix_daemon(std::string programName, Strings argv) return true; }).parseCmdline(argv); - runDaemon(stdio, isTrustedOpt); + runDaemon(aio, stdio, isTrustedOpt); return 0; } @@ -531,7 +533,7 @@ struct CmdDaemon : StoreCommand void run(ref store) override { - runDaemon(stdio, isTrustedOpt); + runDaemon(aio(), stdio, isTrustedOpt); } }; diff --git a/lix/nix/derivation.cc b/lix/nix/derivation.cc index 5d0f4c2bb..a5620d076 100644 --- a/lix/nix/derivation.cc +++ b/lix/nix/derivation.cc @@ -2,7 +2,7 @@ using namespace nix; -struct CmdDerivation final : MultiCommand +struct CmdDerivation : MultiCommand { CmdDerivation() : MultiCommand(RegisterCommand::getCommandsFor({"derivation"})) { } diff --git a/lix/nix/flake.cc b/lix/nix/flake.cc index 747cda52a..1cba166ee 100644 --- a/lix/nix/flake.cc +++ b/lix/nix/flake.cc @@ -27,7 +27,7 @@ using namespace nix::flake; using json = nlohmann::json; struct CmdFlakeUpdate; -class FlakeCommand : virtual Args, public MixFlakeOptions +class FlakeCommand : public virtual Args, public MixFlakeOptions { protected: std::string flakeUrl = "."; @@ -1468,17 +1468,17 @@ struct CmdFlake : MultiCommand { CmdFlake() : MultiCommand({ - {"update", []() { return make_ref(); }}, - {"lock", []() { return make_ref(); }}, - {"metadata", []() { return make_ref(); }}, - {"info", []() { return make_ref(); }}, - {"check", []() { return make_ref(); }}, - {"init", []() { return make_ref(); }}, - {"new", []() { return make_ref(); }}, - {"clone", []() { return make_ref(); }}, - {"archive", []() { return make_ref(); }}, - {"show", []() { return make_ref(); }}, - {"prefetch", []() { return make_ref(); }}, + {"update", [](auto & aio) { return make_ref>(aio); }}, + {"lock", [](auto & aio) { return make_ref>(aio); }}, + {"metadata", [](auto & aio) { return make_ref>(aio); }}, + {"info", [](auto & aio) { return make_ref>(aio); }}, + {"check", [](auto & aio) { return make_ref>(aio); }}, + {"init", [](auto & aio) { return make_ref>(aio); }}, + {"new", [](auto & aio) { return make_ref>(aio); }}, + {"clone", [](auto & aio) { return make_ref>(aio); }}, + {"archive", [](auto & aio) { return make_ref>(aio); }}, + {"show", [](auto & aio) { return make_ref>(aio); }}, + {"prefetch", [](auto & aio) { return make_ref>(aio); }}, }) { } diff --git a/lix/nix/hash.cc b/lix/nix/hash.cc index 0eb89b197..2f3c40a39 100644 --- a/lix/nix/hash.cc +++ b/lix/nix/hash.cc @@ -128,14 +128,25 @@ struct CmdHash : MultiCommand { CmdHash() : MultiCommand({ - {"file", []() { return make_ref(FileIngestionMethod::Flat);; }}, - {"path", []() { return make_ref(FileIngestionMethod::Recursive); }}, - {"to-base16", []() { return make_ref(Base::Base16); }}, - {"to-base32", []() { return make_ref(Base::Base32); }}, - {"to-base64", []() { return make_ref(Base::Base64); }}, - {"to-sri", []() { return make_ref(Base::SRI); }}, - }) - { } + {"file", + [](auto & aio) { + return make_ref>(aio, FileIngestionMethod::Flat); + ; + }}, + {"path", + [](auto & aio) { + return make_ref>(aio, FileIngestionMethod::Recursive); + }}, + {"to-base16", + [](auto & aio) { return make_ref>(aio, Base::Base16); }}, + {"to-base32", + [](auto & aio) { return make_ref>(aio, Base::Base32); }}, + {"to-base64", + [](auto & aio) { return make_ref>(aio, Base::Base64); }}, + {"to-sri", [](auto & aio) { return make_ref>(aio, Base::SRI); }}, + }) + { + } std::string description() override { @@ -155,7 +166,7 @@ struct CmdHash : MultiCommand static auto rCmdHash = registerCommand("hash"); /* Legacy nix-hash command. */ -static int compatNixHash(std::string programName, Strings argv) +static int compatNixHash(AsyncIoRoot & aio, std::string programName, Strings argv) { std::optional ht; bool flat = false; @@ -164,7 +175,7 @@ static int compatNixHash(std::string programName, Strings argv) enum { opHash, opTo } op = opHash; std::vector ss; - LegacyArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + LegacyArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") showManPage("nix-hash"); else if (*arg == "--version") @@ -203,7 +214,7 @@ static int compatNixHash(std::string programName, Strings argv) }).parseCmdline(argv); if (op == opHash) { - CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive); + MixAio cmd(aio, flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive); if (!ht.has_value()) ht = HashType::MD5; cmd.ht = ht.value(); cmd.base = base; @@ -213,7 +224,7 @@ static int compatNixHash(std::string programName, Strings argv) } else { - CmdToBase cmd(base); + MixAio cmd(aio, base); cmd.args = ss; if (ht.has_value()) cmd.ht = ht; cmd.run(); diff --git a/lix/nix/main.cc b/lix/nix/main.cc index 9e1bf1376..126630ebc 100644 --- a/lix/nix/main.cc +++ b/lix/nix/main.cc @@ -111,7 +111,13 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs bool helpRequested = false; bool showVersion = false; - NixArgs() : MultiCommand(RegisterCommand::getCommandsFor({})), MixCommonArgs("nix") + AsyncIoRoot & aio_; + AsyncIoRoot & aio() override { return aio_; } + + NixArgs(AsyncIoRoot & aio) + : MultiCommand(RegisterCommand::getCommandsFor({})) + , MixCommonArgs("nix") + , aio_(aio) { categories.clear(); categories[catHelp] = "Help commands"; @@ -345,7 +351,7 @@ struct CmdHelpStores : Command static auto rCmdHelpStores = registerCommand("help-stores"); -void mainWrapped(int argc, char * * argv) +void mainWrapped(AsyncIoRoot & aio, int argc, char * * argv) { savedArgv = argv; @@ -384,7 +390,9 @@ void mainWrapped(int argc, char * * argv) { registerLegacyCommands(); auto legacy = (*LegacyCommands::commands)[programName]; - if (legacy) return legacy(std::string(baseNameOf(argv[0])), Strings(argv + 1, argv + argc)); + if (legacy) { + return legacy(aio, std::string(baseNameOf(argv[0])), Strings(argv + 1, argv + argc)); + } } evalSettings.pureEval.setDefault(true); @@ -398,7 +406,7 @@ void mainWrapped(int argc, char * * argv) verbosity = lvlInfo; } - NixArgs args; + NixArgs args(aio); if (argc == 2 && std::string(argv[1]) == "__dump-cli") { logger->cout(args.dumpCli()); @@ -543,6 +551,7 @@ int main(int argc, char * * argv) nix::setStackSize(64 * 1024 * 1024); return nix::handleExceptions(argv[0], [&]() { - nix::mainWrapped(argc, argv); + nix::AsyncIoRoot aio; + nix::mainWrapped(aio, argc, argv); }); } diff --git a/lix/nix/meson.build b/lix/nix/meson.build index 3172b2d4c..f08c5851a 100644 --- a/lix/nix/meson.build +++ b/lix/nix/meson.build @@ -133,6 +133,7 @@ nix = executable( liblixmain, boehm, nlohmann_json, + kj, ], cpp_pch : cpp_pch, install : true, diff --git a/lix/nix/prefetch.cc b/lix/nix/prefetch.cc index c45caa063..16e34adb1 100644 --- a/lix/nix/prefetch.cc +++ b/lix/nix/prefetch.cc @@ -49,6 +49,7 @@ std::string resolveMirrorUrl(EvalState & state, const std::string & url) } std::tuple prefetchFile( + AsyncIoRoot & aio, ref store, const std::string & url, std::optional name, @@ -132,7 +133,7 @@ std::tuple prefetchFile( return {storePath.value(), hash.value()}; } -static int main_nix_prefetch_url(std::string programName, Strings argv) +static int main_nix_prefetch_url(AsyncIoRoot & aio, std::string programName, Strings argv) { { HashType ht = HashType::SHA256; @@ -149,7 +150,7 @@ static int main_nix_prefetch_url(std::string programName, Strings argv) using LegacyArgs::LegacyArgs; }; - MyArgs myArgs(programName, [&](Strings::iterator & arg, const Strings::iterator & end) { + MyArgs myArgs(aio, programName, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--help") showManPage("nix-prefetch-url"); else if (*arg == "--version") @@ -236,7 +237,8 @@ static int main_nix_prefetch_url(std::string programName, Strings argv) expectedHash = Hash::parseAny(args[1], ht); auto [storePath, hash] = prefetchFile( - store, resolveMirrorUrl(*state, url), name, ht, expectedHash, unpack, executable); + aio, store, resolveMirrorUrl(*state, url), name, ht, expectedHash, unpack, executable + ); logger->pause(); @@ -316,7 +318,8 @@ struct CmdStorePrefetchFile : StoreCommand, MixJSON } void run(ref store) override { - auto [storePath, hash] = prefetchFile(store, url, name, hashType, expectedHash, unpack, executable); + auto [storePath, hash] = + prefetchFile(aio(), store, url, name, hashType, expectedHash, unpack, executable); if (json) { auto res = nlohmann::json::object(); diff --git a/lix/nix/profile.cc b/lix/nix/profile.cc index 37e9c8a95..2fdd63860 100644 --- a/lix/nix/profile.cc +++ b/lix/nix/profile.cc @@ -607,14 +607,14 @@ struct CmdProfile : MultiCommand { CmdProfile() : MultiCommand({ - {"install", []() { return make_ref(); }}, - {"remove", []() { return make_ref(); }}, - {"upgrade", []() { return make_ref(); }}, - {"list", []() { return make_ref(); }}, - {"diff-closures", []() { return make_ref(); }}, - {"history", []() { return make_ref(); }}, - {"rollback", []() { return make_ref(); }}, - {"wipe-history", []() { return make_ref(); }}, + {"install", [](auto & aio) { return make_ref>(aio); }}, + {"remove", [](auto & aio) { return make_ref>(aio); }}, + {"upgrade", [](auto & aio) { return make_ref>(aio); }}, + {"list", [](auto & aio) { return make_ref>(aio); }}, + {"diff-closures", [](auto & aio) { return make_ref>(aio); }}, + {"history", [](auto & aio) { return make_ref>(aio); }}, + {"rollback", [](auto & aio) { return make_ref>(aio); }}, + {"wipe-history", [](auto & aio) { return make_ref>(aio); }}, }) { } diff --git a/lix/nix/realisation.cc b/lix/nix/realisation.cc index b48e3eda1..08f924ee2 100644 --- a/lix/nix/realisation.cc +++ b/lix/nix/realisation.cc @@ -5,7 +5,7 @@ using namespace nix; -struct CmdRealisation final : MultiCommand +struct CmdRealisation : MultiCommand { CmdRealisation() : MultiCommand(RegisterCommand::getCommandsFor({"realisation"})) { } diff --git a/lix/nix/registry.cc b/lix/nix/registry.cc index 80fd83760..324af5000 100644 --- a/lix/nix/registry.cc +++ b/lix/nix/registry.cc @@ -12,7 +12,7 @@ using namespace nix; using namespace nix::flake; -class RegistryCommand : virtual Args +class RegistryCommand : public virtual Args { std::string registry_path; @@ -204,14 +204,14 @@ struct CmdRegistryPin : RegistryCommand, EvalCommand } }; -struct CmdRegistry final : MultiCommand +struct CmdRegistry : MultiCommand { CmdRegistry() : MultiCommand({ - {"list", []() { return make_ref(); }}, - {"add", []() { return make_ref(); }}, - {"remove", []() { return make_ref(); }}, - {"pin", []() { return make_ref(); }}, + {"list", [](auto & aio) { return make_ref>(aio); }}, + {"add", [](auto & aio) { return make_ref>(aio); }}, + {"remove", [](auto & aio) { return make_ref>(aio); }}, + {"pin", [](auto & aio) { return make_ref>(aio); }}, }) { } diff --git a/lix/nix/sigs.cc b/lix/nix/sigs.cc index ae729172e..1d7a8e016 100644 --- a/lix/nix/sigs.cc +++ b/lix/nix/sigs.cc @@ -197,9 +197,11 @@ struct CmdKey : MultiCommand { CmdKey() : MultiCommand({ - {"generate-secret", []() { return make_ref(); }}, - {"convert-secret-to-public", []() { return make_ref(); }}, - }) + {"generate-secret", + [](auto & aio) { return make_ref>(aio); }}, + {"convert-secret-to-public", + [](auto & aio) { return make_ref>(aio); }}, + }) { } diff --git a/lix/nix/store.cc b/lix/nix/store.cc index 599f544d1..ca9c4b961 100644 --- a/lix/nix/store.cc +++ b/lix/nix/store.cc @@ -2,7 +2,7 @@ using namespace nix; -struct CmdStore final : MultiCommand +struct CmdStore : MultiCommand { CmdStore() : MultiCommand(RegisterCommand::getCommandsFor({"store"})) { } diff --git a/tests/functional/test-libstoreconsumer/meson.build b/tests/functional/test-libstoreconsumer/meson.build index 63d0c97ac..5cc04ba90 100644 --- a/tests/functional/test-libstoreconsumer/meson.build +++ b/tests/functional/test-libstoreconsumer/meson.build @@ -9,6 +9,7 @@ libstoreconsumer_tester = executable( editline, boost, lowdown, + kj, ], build_by_default : false, ) diff --git a/tests/unit/meson.build b/tests/unit/meson.build index bea2d48b2..c839e5498 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -114,6 +114,7 @@ libstore_test_support = library( liblixstore, rapidcheck, boehm, + kj, ], include_directories : include_directories( 'libstore-support', @@ -152,6 +153,7 @@ libstore_tester = executable( rapidcheck, gtest, nlohmann_json, + kj, ], cpp_pch : cpp_pch, ) @@ -220,6 +222,7 @@ libexpr_tester = executable( boehm, gtest, nlohmann_json, + kj, ], cpp_pch : cpp_pch, ) @@ -248,6 +251,7 @@ libcmd_tester = executable( liblixstore_mstatic, gtest, boost, + kj, ], cpp_pch : cpp_pch, ) @@ -284,6 +288,7 @@ libmain_tester = executable( liblixstore, gtest, boost, + kj, ], cpp_pch : cpp_pch, )