From e4d8011896a393500146d6564a381202fd5b8baa Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Thu, 23 May 2024 14:46:24 +0200 Subject: [PATCH] tukit: move getBindDir as protected Signed-off-by: Alberto Planas --- lib/Plugins.cpp | 10 +++++++--- lib/Plugins.hpp | 6 ++++-- lib/Transaction.hpp | 8 ++------ tukit/tukit.cpp | 31 +++++++++++++++++++------------ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/lib/Plugins.cpp b/lib/Plugins.cpp index 847fed0..b7776a9 100644 --- a/lib/Plugins.cpp +++ b/lib/Plugins.cpp @@ -14,7 +14,7 @@ namespace TransactionalUpdate { using namespace std; -Plugins::Plugins() { +Plugins::Plugins(TransactionalUpdate::Transaction* transaction): transaction(transaction) { set plugins_set{}; const filesystem::path plugins_dir{filesystem::path(CONFDIR)/"tukit"/"plugins"}; @@ -40,6 +40,7 @@ Plugins::Plugins() { if (!(filesystem::is_regular_file(path) && (access(path.c_str(), X_OK) == 0))) continue; + tulog.info("Regiter plugin ", path); plugins.push_back(path); plugins_set.insert(filename); } @@ -68,8 +69,11 @@ void Plugins::run(string stage, string args) { } } -void Plugins::run(string stage, std::filesystem::path path, std::string id, char* argv[]) { - std::string args = path.string() + " " + id; +void Plugins::run(string stage, char* argv[]) { + std::string args; + + if (transaction != nullptr) + args.append(transaction->getBindDir().string() + " " + transaction->getSnapshot()); int i = 0; while (argv != nullptr && argv[i]) { diff --git a/lib/Plugins.hpp b/lib/Plugins.hpp index ca7d489..64907ff 100644 --- a/lib/Plugins.hpp +++ b/lib/Plugins.hpp @@ -6,6 +6,7 @@ #ifndef T_U_PLUGINS_H #define T_U_PLUGINS_H +#include "Transaction.hpp" #include #include #include @@ -14,11 +15,12 @@ namespace TransactionalUpdate { class Plugins { public: - Plugins(); + Plugins(TransactionalUpdate::Transaction* transaction); virtual ~Plugins(); void run(std::string stage, std::string args); - void run(std::string stage, std::filesystem::path path, std::string id, char* argv[]); + void run(std::string stage, char* argv[]); protected: + TransactionalUpdate::Transaction* transaction; std::vector plugins; }; diff --git a/lib/Transaction.hpp b/lib/Transaction.hpp index b7a85ad..3e13b82 100644 --- a/lib/Transaction.hpp +++ b/lib/Transaction.hpp @@ -168,12 +168,8 @@ class Transaction { */ std::filesystem::path getRoot(); - /** - * @brief Return the bindDir path of the snapshot - * @return bindDir path - * - * The bindDir is the live snapshot, that includes auxiliary mounts as /etc or /run. - */ + friend class Plugins; +protected: std::filesystem::path getBindDir(); private: class impl; diff --git a/tukit/tukit.cpp b/tukit/tukit.cpp index c08dd15..dceb153 100644 --- a/tukit/tukit.cpp +++ b/tukit/tukit.cpp @@ -133,21 +133,21 @@ int TUKit::parseOptions(int argc, char *argv[]) { } int TUKit::processCommand(char *argv[]) { - TransactionalUpdate::Plugins plugins{}; if (argv[0] == nullptr) { throw invalid_argument{"Missing command. See --help for usage information."}; } string arg = argv[0]; if (arg == "execute") { TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; if (discardSnapshot) { transaction.setDiscardIfUnchanged(true); } transaction.init(baseSnapshot); - plugins.run(arg + "-pre", transaction.getBindDir(), transaction.getSnapshot(), &argv[1]); + plugins.run(arg + "-pre", &argv[1]); int status = transaction.execute(&argv[1]); // All remaining arguments if (status == 0) { - plugins.run(arg + "-post", transaction.getBindDir(), transaction.getSnapshot(), &argv[1]); + plugins.run(arg + "-post", &argv[1]); transaction.finalize(); } else { throw runtime_error{"Application returned with exit status " + to_string(status)}; @@ -156,11 +156,12 @@ int TUKit::processCommand(char *argv[]) { } else if (arg == "open") { TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; if (discardSnapshot) { transaction.setDiscardIfUnchanged(true); } transaction.init(baseSnapshot, description); - plugins.run(arg + "-post", transaction.getBindDir(), transaction.getSnapshot(), nullptr); + plugins.run(arg + "-post", nullptr); cout << "ID: " << transaction.getSnapshot() << endl; transaction.keep(); return 0; @@ -171,10 +172,11 @@ int TUKit::processCommand(char *argv[]) { throw invalid_argument{"Missing argument for 'call'"}; } TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; transaction.resume(argv[1]); - plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], &argv[2]); + plugins.run(arg + "-pre", &argv[2]); int status = transaction.execute(&argv[2]); // All remaining arguments - plugins.run(arg + "-post", transaction.getBindDir(), argv[1], &argv[2]); + plugins.run(arg + "-post", &argv[2]); transaction.keep(); return status; } @@ -184,10 +186,11 @@ int TUKit::processCommand(char *argv[]) { throw invalid_argument{"Missing argument for 'callext'"}; } TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; transaction.resume(argv[1]); - plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], &argv[2]); + plugins.run(arg + "-pre", &argv[2]); int status = transaction.callExt(&argv[2]); // All remaining arguments - plugins.run(arg + "-post", transaction.getBindDir(), argv[1], &argv[1]); + plugins.run(arg + "-post", &argv[2]); transaction.keep(); return status; } @@ -197,8 +200,9 @@ int TUKit::processCommand(char *argv[]) { throw invalid_argument{"Missing argument for 'close'"}; } TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; transaction.resume(argv[1]); - plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], nullptr); + plugins.run(arg + "-pre", nullptr); transaction.finalize(); return 0; } @@ -208,8 +212,9 @@ int TUKit::processCommand(char *argv[]) { throw invalid_argument{"Missing argument for 'abort'"}; } TransactionalUpdate::Transaction transaction{}; + TransactionalUpdate::Plugins plugins{&transaction}; transaction.resume(argv[1]); - plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], nullptr); + plugins.run(arg + "-pre", nullptr); return 0; } else if (arg == "rollback") { @@ -218,9 +223,10 @@ int TUKit::processCommand(char *argv[]) { throw invalid_argument{"Missing argument for 'rollback'"}; } unique_ptr snapshotMgr = TransactionalUpdate::SnapshotFactory::get(); - plugins.run(arg + "-pre", argv[1]); + TransactionalUpdate::Plugins plugins{nullptr}; + plugins.run(arg + "-pre", &argv[1]); snapshotMgr->rollbackTo(argv[1]); - plugins.run(arg + "-post", argv[1]); + plugins.run(arg + "-post", &argv[1]); return 0; } else if (arg == "snapshots") { @@ -244,6 +250,7 @@ int TUKit::processCommand(char *argv[]) { } else { method = "auto"; } + TransactionalUpdate::Plugins plugins{nullptr}; plugins.run(arg + "-pre", method); TransactionalUpdate::Reboot rebootmgr{method}; rebootmgr.reboot();