From 04a4b159916dcd33e5708fa6c84b1a49b9d07248 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 19 Nov 2025 13:58:48 +0100 Subject: [PATCH] libcmd/copy: Check the validity of the options if required This is done at the start of the command call, which allows not building expensive things if the arguments are incorrect anyway. Fixes #687 Change-Id: Ic924fe2115cf760684c6fdf7987279e96ab00286 --- lix/libcmd/command.cc | 12 +++++++++--- lix/libcmd/command.hh | 2 ++ lix/nix/make-content-addressed.cc | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lix/libcmd/command.cc b/lix/libcmd/command.cc index 812f8b446..4082ddb0d 100644 --- a/lix/libcmd/command.cc +++ b/lix/libcmd/command.cc @@ -75,6 +75,15 @@ CopyCommand::CopyCommand() }); } +void CopyCommand::run() +{ + if (requireStore && srcUri.empty() && dstUri.empty()) { + throw UsageError("you must pass '--from' and/or '--to'"); + } + + StoreCommand::run(); +} + ref CopyCommand::createStore(AsyncIoRoot & in) { return srcUri.empty() ? StoreCommand::createStore(in) : in.blockOn(openStore(srcUri)); @@ -82,9 +91,6 @@ ref CopyCommand::createStore(AsyncIoRoot & in) ref CopyCommand::getDstStore() { - if (srcUri.empty() && dstUri.empty()) - throw UsageError("you must pass '--from' and/or '--to'"); - return aio().blockOn(dstUri.empty() ? openStore() : openStore(dstUri)); } diff --git a/lix/libcmd/command.hh b/lix/libcmd/command.hh index da8cc4934..9b242e159 100644 --- a/lix/libcmd/command.hh +++ b/lix/libcmd/command.hh @@ -56,9 +56,11 @@ private: struct CopyCommand : virtual StoreCommand { std::string srcUri, dstUri; + bool requireStore = true; CopyCommand(); + void run() override; ref createStore(AsyncIoRoot & in) override; ref getDstStore(); diff --git a/lix/nix/make-content-addressed.cc b/lix/nix/make-content-addressed.cc index 9074e6f3b..4912a6556 100644 --- a/lix/nix/make-content-addressed.cc +++ b/lix/nix/make-content-addressed.cc @@ -12,6 +12,7 @@ struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand, CmdMakeContentAddressed() { realiseMode = Realise::Outputs; + requireStore = false; } std::string description() override @@ -28,7 +29,7 @@ struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand, void run(ref srcStore, StorePaths && storePaths) override { - auto dstStore = aio().blockOn(dstUri.empty() ? openStore() : openStore(dstUri)); + auto dstStore = getDstStore(); auto remappings = aio().blockOn(makeContentAddressed(*srcStore, *dstStore, StorePathSet(storePaths.begin(), storePaths.end())));