From 6eb9a8d2a9c9ccdcccef8d1f15b5d6aa7c9e6568 Mon Sep 17 00:00:00 2001 From: Arvin Schnell Date: Thu, 2 Jun 2022 13:05:55 +0200 Subject: [PATCH] extended generic plugin support --- LIBVERSION | 2 +- client/installation-helper.cc | 2 +- doc/snapper.xml.in | 28 +++++++++++++++----- package/snapper.changes | 5 ++++ snapper/Btrfs.cc | 4 +-- snapper/Hooks.cc | 50 ++++++++++++++++++++--------------- snapper/Hooks.h | 5 +++- snapper/SnapperDefines.h | 2 ++ snapper/Snapshot.cc | 6 ++--- 9 files changed, 68 insertions(+), 36 deletions(-) diff --git a/LIBVERSION b/LIBVERSION index 5fe60723..dfda3e0b 100644 --- a/LIBVERSION +++ b/LIBVERSION @@ -1 +1 @@ -6.0.1 +6.1.0 diff --git a/client/installation-helper.cc b/client/installation-helper.cc index e3eed4fe..874c6c99 100644 --- a/client/installation-helper.cc +++ b/client/installation-helper.cc @@ -120,7 +120,7 @@ step1(const string& device, const string& description, const string& cleanup, cout << "setting default subvolume" << endl; - snapper.getFilesystem()->setDefault(snapshot->getNum()); + snapshot->setDefault(); cout << "done" << endl; } diff --git a/doc/snapper.xml.in b/doc/snapper.xml.in index c9d1aa3b..8f567f18 100644 --- a/doc/snapper.xml.in +++ b/doc/snapper.xml.in @@ -2,13 +2,13 @@ - 2021-09-21 + 2022-06-02 snapper 8 - 2021-09-21 + 2022-06-02 @VERSION@ Filesystem Snapshot Management @@ -845,33 +845,47 @@ have to be placed in /usr/lib/snapper/plugins. The name has to start with a digit, execution order is alphabetical. The first argument of a script is the action snapper executed. The - following actions are defined: + following actions are defined: - + + + Executed when a new config gets created + + + + + + Executed when a config gets deleted + + + + Executed when a new snapshot gets created - + Executed when a snapshot is removed - + Executed when a snapshot gets modified - + Executed when the default snapshot gets changed + More arguments may be passed in the future. Using snapper in + the plugins is not allowed. diff --git a/package/snapper.changes b/package/snapper.changes index f831feeb..223d5a7f 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Thu Jun 02 13:01:49 CEST 2022 - lnussel@suse.com + +- added generic plugin supprot (gh#openSUSE/snapper#727) + ------------------------------------------------------------------- Tue May 03 08:46:28 CEST 2022 - aschnell@suse.com diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 5dc1e267..0f85b2df 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -1531,9 +1531,9 @@ namespace snapper SDir snapshot_dir = openSnapshotDir(num); subvolid_t id = get_id(snapshot_dir.fd()); set_default_id(general_dir.fd(), id); - - Hooks::set_default_snapshot(subvolume, this, num); } + + Hooks::set_default_snapshot(subvolume, this, num); } catch (const runtime_error& e) { diff --git a/snapper/Hooks.cc b/snapper/Hooks.cc index 54dec216..b4924dba 100644 --- a/snapper/Hooks.cc +++ b/snapper/Hooks.cc @@ -1,5 +1,6 @@ /* * Copyright (c) [2011-2015] Novell, Inc. + * Copyright (c) 2022 SUSE LLC * * All Rights Reserved. * @@ -22,50 +23,54 @@ #include "config.h" -#include - -#include - #include "snapper/FileUtils.h" #include "snapper/Hooks.h" #include "snapper/SystemCmd.h" -#include "snapper/Log.h" +#include "snapper/SnapperDefines.h" namespace snapper { using namespace std; + static bool - _plugins_filter_entries(unsigned char type, const char* name) + plugins_filter_entries(unsigned char type, const char* name) { // must start with digit - if (*name >= '0' && *name <= '9') - return true; - return false; + return *name >= '0' && *name <= '9'; } + void - Hooks::run_scripts(const list& args) + Hooks::run_scripts(const vector& args) { - SDir dir("/usr/lib/snapper/plugins"); + try + { + SDir dir(PLUGINS_DIR); - vector scripts = dir.entries(_plugins_filter_entries); + vector scripts = dir.entries(plugins_filter_entries); std::sort(scripts.begin(), scripts.end()); for (const string& script : scripts) { - string cmdln = dir.fullname(script); - for (const string& arg : args) { - cmdln += " " + quote(arg); - } - SystemCmd cmd(cmdln); + string cmd_line = dir.fullname(script); + for (const string& arg : args) + cmd_line += " " + quote(arg); + SystemCmd cmd(cmd_line); } + } + catch (const Exception& e) + { + SN_CAUGHT(e); + } } + void Hooks::create_config(const string& subvolume, const Filesystem* filesystem) { grub(subvolume, filesystem, "--enable"); + run_scripts({ "create-config", subvolume, filesystem->fstype() }); } @@ -73,6 +78,7 @@ namespace snapper Hooks::delete_config(const string& subvolume, const Filesystem* filesystem) { grub(subvolume, filesystem, "--disable"); + run_scripts({ "delete-config", subvolume, filesystem->fstype() }); } @@ -80,7 +86,7 @@ namespace snapper Hooks::create_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { grub(subvolume, filesystem, "--refresh"); - run_scripts(std::list({"create-snapshot", subvolume, std::to_string(snapshot.getNum())})); + run_scripts({ "create-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); } @@ -88,7 +94,7 @@ namespace snapper Hooks::modify_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { grub(subvolume, filesystem, "--refresh"); - run_scripts(std::list({"modify-snapshot", subvolume, std::to_string(snapshot.getNum())})); + run_scripts({ "modify-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); } @@ -96,15 +102,17 @@ namespace snapper Hooks::delete_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { grub(subvolume, filesystem, "--refresh"); - run_scripts(std::list({"delete-snapshot", subvolume, std::to_string(snapshot.getNum())})); + run_scripts({ "delete-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); } + void Hooks::set_default_snapshot(const string& subvolume, const Filesystem* filesystem, unsigned int num) { - run_scripts(std::list({"set-default-snapshot", subvolume, std::to_string(num)})); + run_scripts({ "set-default-snapshot", subvolume, filesystem->fstype(), std::to_string(num) }); } + void Hooks::grub(const string& subvolume, const Filesystem* filesystem, const char* option) { diff --git a/snapper/Hooks.h b/snapper/Hooks.h index a81fa31c..3b31857d 100644 --- a/snapper/Hooks.h +++ b/snapper/Hooks.h @@ -1,5 +1,6 @@ /* * Copyright (c) [2011-2015] Novell, Inc. + * Copyright (c) 2022 SUSE LLC * * All Rights Reserved. * @@ -37,13 +38,13 @@ namespace snapper { public: - static void run_scripts(const list& args); static void create_config(const string& subvolume, const Filesystem* filesystem); static void delete_config(const string& subvolume, const Filesystem* filesystem); static void create_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); static void modify_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); static void delete_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); + static void set_default_snapshot(const string& subvolume, const Filesystem* filesystem, unsigned int num); static void rollback(const string& old_root, const string& new_root); @@ -53,6 +54,8 @@ namespace snapper static void grub(const string& subvolume, const Filesystem* filesystem, const char* option); + static void run_scripts(const vector& args); + }; } diff --git a/snapper/SnapperDefines.h b/snapper/SnapperDefines.h index 46f0c6f1..cf218ab0 100644 --- a/snapper/SnapperDefines.h +++ b/snapper/SnapperDefines.h @@ -37,6 +37,8 @@ #define ETC_FILTERS_DIR "/etc/snapper/filters" #define USR_FILTERS_DIR "/usr/share/snapper/filters" +#define PLUGINS_DIR "/usr/lib/snapper/plugins" + #define DEV_DIR "/dev" #define DEV_MAPPER_DIR "/dev/mapper" diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 384701a2..abf17aa8 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -144,7 +144,7 @@ namespace snapper void Snapshot::setDefault() const { - return snapper->getFilesystem()->setDefault(num); + snapper->getFilesystem()->setDefault(num); } @@ -792,9 +792,9 @@ namespace snapper SDir infos_dir = snapper->openInfosDir(); infos_dir.unlink(decString(snapshot->getNum()), AT_REMOVEDIR); - entries.erase(snapshot); - Hooks::delete_snapshot(snapper->subvolumeDir(), snapper->getFilesystem(), *snapshot); + + entries.erase(snapshot); } -- 2.47.3