From: Arvin Schnell Date: Fri, 25 Jul 2025 08:07:39 +0000 (+0200) Subject: - use unique_ptr for filesystem X-Git-Tag: v0.13.0~24^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=707d5bbbee57f1ecc482458552792c7fff66a279;p=thirdparty%2Fsnapper.git - use unique_ptr for filesystem --- diff --git a/client/misc.cc b/client/misc.cc index a06e76aa..61c5eb1c 100644 --- a/client/misc.cc +++ b/client/misc.cc @@ -168,7 +168,7 @@ username(uid_t uid) } -const Filesystem* + unique_ptr get_filesystem(const ProxyConfig& config, const string& target_root) { const map& raw = config.getAllValues(); diff --git a/client/misc.h b/client/misc.h index 5a62deb8..d4f4b73a 100644 --- a/client/misc.h +++ b/client/misc.h @@ -51,8 +51,8 @@ read_configdata(const vector& v, const map& old = map + get_filesystem(const ProxyConfig& config, const string& target_root); struct Differ diff --git a/client/snapper/cmd-modify.cc b/client/snapper/cmd-modify.cc index f1066863..e9304c55 100644 --- a/client/snapper/cmd-modify.cc +++ b/client/snapper/cmd-modify.cc @@ -141,7 +141,7 @@ namespace snapper if (set_default) { ProxyConfig config = snapper->getConfig(); - const Filesystem* filesystem = get_filesystem(config, global_options.root()); + unique_ptr filesystem = get_filesystem(config, global_options.root()); filesystem->setDefault(snapshot->getNum(), report); } } diff --git a/client/snapper/cmd-rollback.cc b/client/snapper/cmd-rollback.cc index 80447fba..319ba66a 100644 --- a/client/snapper/cmd-rollback.cc +++ b/client/snapper/cmd-rollback.cc @@ -111,7 +111,7 @@ namespace snapper ProxyConfig config = snapper->getConfig(); - const Filesystem* filesystem = get_filesystem(config, global_options.root()); + unique_ptr filesystem = get_filesystem(config, global_options.root()); if (filesystem->fstype() != "btrfs") { cerr << _("Command 'rollback' only available for btrfs.") << endl; @@ -222,8 +222,8 @@ namespace snapper Plugins::rollback(filesystem->snapshotDir(snapshot1->getNum()), filesystem->snapshotDir(snapshot2->getNum()), report); - Plugins::rollback(Plugins::Stage::POST_ACTION, subvolume, filesystem, snapshot1->getNum(), - snapshot2->getNum(), report); + Plugins::rollback(Plugins::Stage::POST_ACTION, subvolume, filesystem.get(), + snapshot1->getNum(), snapshot2->getNum(), report); if (print_number) cout << snapshot2->getNum() << endl; @@ -268,8 +268,8 @@ namespace snapper Plugins::rollback(filesystem->snapshotDir(previous_default->getNum()), filesystem->snapshotDir(snapshot->getNum()), report); - Plugins::rollback(Plugins::Stage::POST_ACTION, subvolume, filesystem, previous_default->getNum(), - snapshot->getNum(), report); + Plugins::rollback(Plugins::Stage::POST_ACTION, subvolume, filesystem.get(), + previous_default->getNum(), snapshot->getNum(), report); } break; diff --git a/snapper/Bcachefs.cc b/snapper/Bcachefs.cc index c0f7cc40..6ea886dc 100644 --- a/snapper/Bcachefs.cc +++ b/snapper/Bcachefs.cc @@ -50,11 +50,11 @@ namespace snapper using namespace BcachefsUtils; - Filesystem* + std::unique_ptr Bcachefs::create(const string& fstype, const string& subvolume, const string& root_prefix) { if (fstype == "bcachefs") - return new Bcachefs(subvolume, root_prefix); + return std::make_unique(subvolume, root_prefix); return nullptr; } diff --git a/snapper/Bcachefs.h b/snapper/Bcachefs.h index 17be882d..30696ece 100644 --- a/snapper/Bcachefs.h +++ b/snapper/Bcachefs.h @@ -34,8 +34,8 @@ namespace snapper { public: - static Filesystem* create(const string& fstype, const string& subvolume, - const string& root_prefix); + static std::unique_ptr create(const string& fstype, const string& subvolume, + const string& root_prefix); Bcachefs(const string& subvolume, const string& root_prefix); diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 71029394..355305cb 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -69,11 +69,11 @@ namespace snapper using namespace std; - Filesystem* + std::unique_ptr Btrfs::create(const string& fstype, const string& subvolume, const string& root_prefix) { if (fstype == "btrfs") - return new Btrfs(subvolume, root_prefix); + return std::make_unique(subvolume, root_prefix); return nullptr; } diff --git a/snapper/Btrfs.h b/snapper/Btrfs.h index d67d7015..4357ef04 100644 --- a/snapper/Btrfs.h +++ b/snapper/Btrfs.h @@ -39,8 +39,8 @@ namespace snapper { public: - static Filesystem* create(const string& fstype, const string& subvolume, - const string& root_prefix); + static std::unique_ptr create(const string& fstype, const string& subvolume, + const string& root_prefix); Btrfs(const string& subvolume, const string& root_prefix); diff --git a/snapper/Ext4.cc b/snapper/Ext4.cc index 8b4b259c..7fe4a68a 100644 --- a/snapper/Ext4.cc +++ b/snapper/Ext4.cc @@ -44,11 +44,11 @@ namespace snapper { - Filesystem* + std::unique_ptr Ext4::create(const string& fstype, const string& subvolume, const string& root_prefix) { if (fstype == "ext4") - return new Ext4(subvolume, root_prefix); + return std::make_unique(subvolume, root_prefix); return nullptr; } diff --git a/snapper/Ext4.h b/snapper/Ext4.h index 9aafda24..62ed67e0 100644 --- a/snapper/Ext4.h +++ b/snapper/Ext4.h @@ -34,8 +34,8 @@ namespace snapper { public: - static Filesystem* create(const string& fstype, const string& subvolume, - const string& root_prefix); + static std::unique_ptr create(const string& fstype, const string& subvolume, + const string& root_prefix); Ext4(const string& subvolume, const string& root_prefix); diff --git a/snapper/Filesystem.cc b/snapper/Filesystem.cc index 7cb1bc04..cf54c78d 100644 --- a/snapper/Filesystem.cc +++ b/snapper/Filesystem.cc @@ -94,11 +94,11 @@ namespace snapper } - Filesystem* + std::unique_ptr Filesystem::create(const string& fstype, const string& subvolume, const string& root_prefix) { - typedef Filesystem* (*func_t)(const string& fstype, const string& subvolume, - const string& root_prefix); + typedef std::unique_ptr (*func_t)(const string& fstype, const string& subvolume, + const string& root_prefix); static const func_t funcs[] = { #ifdef ENABLE_BTRFS @@ -118,7 +118,7 @@ namespace snapper for (const func_t* func = funcs; *func != nullptr; ++func) { - Filesystem* fs = (*func)(fstype, subvolume, root_prefix); + std::unique_ptr fs = (*func)(fstype, subvolume, root_prefix); if (fs) return fs; } @@ -129,13 +129,13 @@ namespace snapper } - Filesystem* + std::unique_ptr Filesystem::create(const ConfigInfo& config_info, const string& root_prefix) { string fstype = "btrfs"; config_info.get_value(KEY_FSTYPE, fstype); - Filesystem* fs = create(fstype, config_info.get_subvolume(), root_prefix); + std::unique_ptr fs = create(fstype, config_info.get_subvolume(), root_prefix); fs->evalConfigInfo(config_info); diff --git a/snapper/Filesystem.h b/snapper/Filesystem.h index c587a35d..42ebd6ef 100644 --- a/snapper/Filesystem.h +++ b/snapper/Filesystem.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "snapper/FileUtils.h" #include "snapper/Compare.h" @@ -52,8 +53,9 @@ namespace snapper : subvolume(subvolume), root_prefix(root_prefix) {} virtual ~Filesystem() {} - static Filesystem* create(const string& fstype, const string& subvolume, const string& root_prefix); - static Filesystem* create(const ConfigInfo& config_info, const string& root_prefix); + static std::unique_ptr create(const string& fstype, const string& subvolume, + const string& root_prefix); + static std::unique_ptr create(const ConfigInfo& config_info, const string& root_prefix); virtual void evalConfigInfo(const ConfigInfo& config_info) {} diff --git a/snapper/Lvm.cc b/snapper/Lvm.cc index 79973b16..c6276a7f 100644 --- a/snapper/Lvm.cc +++ b/snapper/Lvm.cc @@ -53,14 +53,14 @@ namespace snapper using namespace std; - Filesystem* + std::unique_ptr Lvm::create(const string& fstype, const string& subvolume, const string& root_prefix) { static const regex rx("lvm\\(([_a-z0-9]+)\\)", regex::extended); smatch match; if (regex_match(fstype, match, rx)) - return new Lvm(subvolume, root_prefix, match[1]); + return std::make_unique(subvolume, root_prefix, match[1]); return nullptr; } diff --git a/snapper/Lvm.h b/snapper/Lvm.h index e67009c7..bb6da1c8 100644 --- a/snapper/Lvm.h +++ b/snapper/Lvm.h @@ -82,8 +82,8 @@ namespace snapper { public: - static Filesystem* create(const string& fstype, const string& subvolume, - const string& root_prefix); + static std::unique_ptr create(const string& fstype, const string& subvolume, + const string& root_prefix); Lvm(const string& subvolume, const string& root_prefix, const string& mount_type); diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 6d86cb6a..61b974cb 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -123,9 +123,6 @@ namespace snapper SN_CAUGHT(e); } } - - delete filesystem; - filesystem = nullptr; } @@ -343,7 +340,7 @@ namespace snapper unique_ptr filesystem; try { - filesystem.reset(Filesystem::create(fstype, subvolume, "")); + filesystem = Filesystem::create(fstype, subvolume, ""); } catch (const InvalidConfigException& e) { @@ -441,7 +438,7 @@ namespace snapper y2mil("Snapper delete-config"); y2mil("libsnapper version " VERSION); - unique_ptr snapper(new Snapper(config_name, root_prefix)); + unique_ptr snapper = make_unique(config_name, root_prefix); Plugins::delete_config(Plugins::Stage::PRE_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), report); diff --git a/snapper/Snapper.h b/snapper/Snapper.h index df4e841f..cc3354d7 100644 --- a/snapper/Snapper.h +++ b/snapper/Snapper.h @@ -144,11 +144,11 @@ namespace snapper static bool detectFstype(const string& subvolume, string& fstype); - const Filesystem* getFilesystem() const { return filesystem; } - const ConfigInfo& getConfigInfo() { return *config_info; } void setConfigInfo(const map& raw); + const Filesystem* getFilesystem() const { return filesystem.get(); } + void syncAcl() const; void syncFilesystem() const; @@ -198,7 +198,7 @@ namespace snapper std::unique_ptr config_info; - Filesystem* filesystem = nullptr; + std::unique_ptr filesystem; vector ignore_patterns; diff --git a/testsuite-cmp/cmp.cc b/testsuite-cmp/cmp.cc index c6159a0a..d01f866c 100644 --- a/testsuite-cmp/cmp.cc +++ b/testsuite-cmp/cmp.cc @@ -27,7 +27,7 @@ struct helper bool cmp(const string& fstype, const string& subvolume, unsigned int num1, unsigned int num2) { - Filesystem* filesystem = Filesystem::create(fstype, subvolume, "/"); + unique_ptr filesystem = Filesystem::create(fstype, subvolume, "/"); SDir dir1 = filesystem->openSnapshotDir(num1); SDir dir2 = filesystem->openSnapshotDir(num2);