From 7e551434659b1d5b2d4cf32b2c843fdc82871b35 Mon Sep 17 00:00:00 2001 From: Arvin Schnell Date: Tue, 4 Jun 2024 09:12:51 +0200 Subject: [PATCH] - use defines for common values --- client/installation-helper.cc | 6 +++--- snapper/Bcachefs.cc | 25 ++++++++++++------------ snapper/Btrfs.cc | 36 +++++++++++++++++------------------ snapper/Compare.cc | 3 ++- snapper/Ext4.cc | 14 +++++++------- snapper/Lvm.cc | 20 +++++++++---------- snapper/Snapper.cc | 4 ++-- snapper/SnapperDefines.h | 7 +++++++ 8 files changed, 62 insertions(+), 53 deletions(-) diff --git a/client/installation-helper.cc b/client/installation-helper.cc index 33b21e0e..1fdba8c5 100644 --- a/client/installation-helper.cc +++ b/client/installation-helper.cc @@ -149,11 +149,11 @@ step2(const string& device, const string& root_prefix, const string& default_sub string subvol_option = default_subvolume_name; if (!subvol_option.empty()) subvol_option += "/"; - subvol_option += ".snapshots"; + subvol_option += SNAPSHOTS_NAME; - mkdir((root_prefix + "/.snapshots").c_str(), 0777); + mkdir((root_prefix + "/" SNAPSHOTS_NAME).c_str(), 0777); - SDir s_dir(root_prefix + "/.snapshots"); + SDir s_dir(root_prefix + "/" SNAPSHOTS_NAME); if (!s_dir.mount(device, "btrfs", 0, "subvol=" + subvol_option)) { cerr << "mounting .snapshots failed" << endl; diff --git a/snapper/Bcachefs.cc b/snapper/Bcachefs.cc index 19e5bf3c..ab954850 100644 --- a/snapper/Bcachefs.cc +++ b/snapper/Bcachefs.cc @@ -34,6 +34,7 @@ #include "snapper/File.h" #include "snapper/Snapper.h" #include "snapper/SnapperTmpl.h" +#include "snapper/SnapperDefines.h" #include "snapper/Acls.h" #include "snapper/Exception.h" @@ -72,7 +73,7 @@ namespace snapper try { - create_subvolume(subvolume_dir.fd(), ".snapshots"); + create_subvolume(subvolume_dir.fd(), SNAPSHOTS_NAME); } catch (const runtime_error_with_errno& e) { @@ -90,7 +91,7 @@ namespace snapper } } - SFile x(subvolume_dir, ".snapshots"); + SFile x(subvolume_dir, SNAPSHOTS_NAME); #ifdef ENABLE_SELINUX try @@ -119,7 +120,7 @@ namespace snapper try { - delete_subvolume(subvolume_dir.fd(), ".snapshots"); + delete_subvolume(subvolume_dir.fd(), SNAPSHOTS_NAME); } catch (const runtime_error& e) { @@ -132,8 +133,8 @@ namespace snapper string Bcachefs::snapshotDir(unsigned int num) const { - return (subvolume == "/" ? "" : subvolume) + "/.snapshots/" + decString(num) + - "/snapshot"; + return (subvolume == "/" ? "" : subvolume) + "/" SNAPSHOTS_NAME "/" + decString(num) + + "/" SNAPSHOT_NAME; } @@ -161,7 +162,7 @@ namespace snapper Bcachefs::openInfosDir() const { SDir subvolume_dir = openSubvolumeDir(); - SDir infos_dir(subvolume_dir, ".snapshots"); + SDir infos_dir(subvolume_dir, SNAPSHOTS_NAME); struct stat stat; if (infos_dir.stat(&stat) != 0) @@ -200,7 +201,7 @@ namespace snapper Bcachefs::openSnapshotDir(unsigned int num) const { SDir info_dir = openInfoDir(num); - SDir snapshot_dir(info_dir, "snapshot"); + SDir snapshot_dir(info_dir, SNAPSHOT_NAME); return snapshot_dir; } @@ -218,9 +219,9 @@ namespace snapper try { if (empty) - create_subvolume(info_dir.fd(), "snapshot"); + create_subvolume(info_dir.fd(), SNAPSHOT_NAME); else - create_snapshot(subvolume_dir.fd(), subvolume, info_dir.fd(), "snapshot", read_only); + create_snapshot(subvolume_dir.fd(), subvolume, info_dir.fd(), SNAPSHOT_NAME, read_only); } catch (const runtime_error& e) { @@ -235,7 +236,7 @@ namespace snapper try { - create_snapshot(snapshot_dir.fd(), subvolume, info_dir.fd(), "snapshot", read_only); + create_snapshot(snapshot_dir.fd(), subvolume, info_dir.fd(), SNAPSHOT_NAME, read_only); } catch (const runtime_error& e) { @@ -253,7 +254,7 @@ namespace snapper try { - delete_subvolume(info_dir.fd(), "snapshot"); + delete_subvolume(info_dir.fd(), SNAPSHOT_NAME); } catch (const runtime_error& e) { @@ -306,7 +307,7 @@ namespace snapper SDir info_dir = openInfoDir(num); struct stat stat; - int r = info_dir.stat("snapshot", &stat, AT_SYMLINK_NOFOLLOW); + int r = info_dir.stat(SNAPSHOT_NAME, &stat, AT_SYMLINK_NOFOLLOW); return r == 0 && is_subvolume(stat); } catch (const IOErrorException& e) diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 32511adf..8ebdcf60 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -133,7 +133,7 @@ namespace snapper try { - create_subvolume(subvolume_dir.fd(), ".snapshots"); + create_subvolume(subvolume_dir.fd(), SNAPSHOTS_NAME); } catch (const runtime_error_with_errno& e) { @@ -151,7 +151,7 @@ namespace snapper } } - SFile x(subvolume_dir, ".snapshots"); + SFile x(subvolume_dir, SNAPSHOTS_NAME); #ifdef ENABLE_SELINUX try { @@ -179,7 +179,7 @@ namespace snapper #ifdef ENABLE_ROLLBACK if (subvolume == "/") { - subvolume_dir.umount(".snapshots"); + subvolume_dir.umount(SNAPSHOTS_NAME); removeFromFstab(); } @@ -187,7 +187,7 @@ namespace snapper try { - delete_subvolume(subvolume_dir.fd(), ".snapshots"); + delete_subvolume(subvolume_dir.fd(), SNAPSHOTS_NAME); } catch (const runtime_error& e) { @@ -238,8 +238,8 @@ namespace snapper string Btrfs::snapshotDir(unsigned int num) const { - return (subvolume == "/" ? "" : subvolume) + "/.snapshots/" + decString(num) + - "/snapshot"; + return (subvolume == "/" ? "" : subvolume) + "/" SNAPSHOTS_NAME "/" + decString(num) + + "/" SNAPSHOT_NAME; } @@ -267,7 +267,7 @@ namespace snapper Btrfs::openInfosDir() const { SDir subvolume_dir = openSubvolumeDir(); - SDir infos_dir(subvolume_dir, ".snapshots"); + SDir infos_dir(subvolume_dir, SNAPSHOTS_NAME); struct stat stat; if (infos_dir.stat(&stat) != 0) @@ -306,7 +306,7 @@ namespace snapper Btrfs::openSnapshotDir(unsigned int num) const { SDir info_dir = openInfoDir(num); - SDir snapshot_dir(info_dir, "snapshot"); + SDir snapshot_dir(info_dir, SNAPSHOT_NAME); return snapshot_dir; } @@ -331,9 +331,9 @@ namespace snapper try { if (empty) - create_subvolume(info_dir.fd(), "snapshot"); + create_subvolume(info_dir.fd(), SNAPSHOT_NAME); else - create_snapshot(subvolume_dir.fd(), info_dir.fd(), "snapshot", read_only, + create_snapshot(subvolume_dir.fd(), info_dir.fd(), SNAPSHOT_NAME, read_only, quota ? qgroup : no_qgroup); } catch (const runtime_error& e) @@ -349,7 +349,7 @@ namespace snapper try { - create_snapshot(snapshot_dir.fd(), info_dir.fd(), "snapshot", read_only, + create_snapshot(snapshot_dir.fd(), info_dir.fd(), SNAPSHOT_NAME, read_only, quota ? qgroup : no_qgroup); } catch (const runtime_error& e) @@ -387,7 +387,7 @@ namespace snapper try { - create_snapshot(tmp_mount_dir.fd(), info_dir.fd(), "snapshot", read_only, + create_snapshot(tmp_mount_dir.fd(), info_dir.fd(), SNAPSHOT_NAME, read_only, quota ? qgroup : no_qgroup); } catch (const runtime_error& e) @@ -419,7 +419,7 @@ namespace snapper subvolid_t subvolid = get_id(openSnapshotDir(num).fd()); #endif - delete_subvolume(info_dir.fd(), "snapshot"); + delete_subvolume(info_dir.fd(), SNAPSHOT_NAME); #if defined(HAVE_LIBBTRFS) || defined(HAVE_LIBBTRFSUTIL) deleted_subvolids.push_back(subvolid); @@ -427,7 +427,7 @@ namespace snapper } catch (const runtime_error& e) { - y2err("delete snapshot " << info_dir.fullname() << "/snapshot failed, " << e.what()); + y2err("delete snapshot " << info_dir.fullname() << "/" SNAPSHOT_NAME " failed, " << e.what()); SN_THROW(DeleteSnapshotFailedException()); } } @@ -476,7 +476,7 @@ namespace snapper SDir info_dir = openInfoDir(num); struct stat stat; - int r = info_dir.stat("snapshot", &stat, AT_SYMLINK_NOFOLLOW); + int r = info_dir.stat(SNAPSHOT_NAME, &stat, AT_SYMLINK_NOFOLLOW); return r == 0 && is_subvolume(stat); } catch (const IOErrorException& e) @@ -1614,7 +1614,7 @@ namespace snapper string subvol_option = default_subvolume_name; if (!subvol_option.empty()) subvol_option += "/"; - subvol_option += ".snapshots"; + subvol_option += SNAPSHOTS_NAME; MntTable mnt_table(root_prefix); mnt_table.parse_fstab(); @@ -1627,7 +1627,7 @@ namespace snapper if (!snapshots) throw runtime_error("mnt_copy_fs failed"); - mnt_fs_set_target(snapshots, "/.snapshots"); + mnt_fs_set_target(snapshots, "/" SNAPSHOTS_NAME); char* options = mnt_fs_strdup_options(snapshots); mnt_optstr_remove_option(&options, "defaults"); @@ -1646,7 +1646,7 @@ namespace snapper MntTable mnt_table(root_prefix); mnt_table.parse_fstab(); - string mountpoint = (subvolume == "/" ? "" : subvolume) + "/.snapshots"; + string mountpoint = (subvolume == "/" ? "" : subvolume) + "/" SNAPSHOTS_NAME; libmnt_fs* snapshots = mnt_table.find_target(mountpoint, MNT_ITER_FORWARD); if (!snapshots) return; diff --git a/snapper/Compare.cc b/snapper/Compare.cc index 9c072c87..d713b9b2 100644 --- a/snapper/Compare.cc +++ b/snapper/Compare.cc @@ -39,6 +39,7 @@ #include "snapper/Exception.h" #include "snapper/XAttributes.h" #include "snapper/Acls.h" +#include "snapper/SnapperDefines.h" namespace snapper @@ -259,7 +260,7 @@ namespace snapper bool filter(const string& name) { - if (name == "/.snapshots") + if (name == "/" SNAPSHOTS_NAME) return true; return false; diff --git a/snapper/Ext4.cc b/snapper/Ext4.cc index 6585bf61..dfb541c4 100644 --- a/snapper/Ext4.cc +++ b/snapper/Ext4.cc @@ -88,10 +88,10 @@ namespace snapper void Ext4::createConfig() const { - int r1 = mkdir((subvolume + "/.snapshots").c_str(), 0700); + int r1 = mkdir((subvolume + "/" SNAPSHOTS_NAME).c_str(), 0700); if (r1 == 0) { - SystemCmd cmd1({ CHATTRBIN, "+x", subvolume + "/.snapshots" }); + SystemCmd cmd1({ CHATTRBIN, "+x", subvolume + "/" SNAPSHOTS_NAME }); if (cmd1.retcode() != 0) throw CreateConfigFailedException("chattr failed"); } @@ -101,10 +101,10 @@ namespace snapper throw CreateConfigFailedException("mkdir failed"); } - int r2 = mkdir((subvolume + "/.snapshots/.info").c_str(), 0700); + int r2 = mkdir((subvolume + "/" SNAPSHOTS_NAME "/.info").c_str(), 0700); if (r2 == 0) { - SystemCmd cmd2({ CHATTRBIN, "-x", subvolume + "/.snapshots/.info" }); + SystemCmd cmd2({ CHATTRBIN, "-x", subvolume + "/" SNAPSHOTS_NAME "/.info" }); if (cmd2.retcode() != 0) throw CreateConfigFailedException("chattr failed"); } @@ -119,14 +119,14 @@ namespace snapper void Ext4::deleteConfig() const { - int r1 = rmdir((subvolume + "/.snapshots/.info").c_str()); + int r1 = rmdir((subvolume + "/" SNAPSHOTS_NAME "/.info").c_str()); if (r1 != 0) { y2err("rmdir failed errno:" << errno << " (" << stringerror(errno) << ")"); throw DeleteConfigFailedException("rmdir failed"); } - int r2 = rmdir((subvolume + "/.snapshots").c_str()); + int r2 = rmdir((subvolume + "/" SNAPSHOTS_NAME).c_str()); if (r2 != 0) { y2err("rmdir failed errno:" << errno << " (" << stringerror(errno) << ")"); @@ -145,7 +145,7 @@ namespace snapper string Ext4::snapshotFile(unsigned int num) const { - return (subvolume == "/" ? "" : subvolume) + "/.snapshots/" + decString(num); + return (subvolume == "/" ? "" : subvolume) + "/" SNAPSHOTS_NAME "/" + decString(num); } diff --git a/snapper/Lvm.cc b/snapper/Lvm.cc index 9caf71db..3d6fcd5a 100644 --- a/snapper/Lvm.cc +++ b/snapper/Lvm.cc @@ -109,7 +109,7 @@ namespace snapper void Lvm::createLvmConfig(const SDir& subvolume_dir, int mode) const { - int r1 = subvolume_dir.mkdir(".snapshots", mode); + int r1 = subvolume_dir.mkdir(SNAPSHOTS_NAME, mode); if (r1 != 0 && errno != EEXIST) { y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); @@ -133,7 +133,7 @@ namespace snapper try { - string path(subvolume_dir.fullname() + "/.snapshots"); + string path(subvolume_dir.fullname() + "/" SNAPSHOTS_NAME); con = selabel_handle->selabel_lookup(path, mode); if (con) @@ -182,7 +182,7 @@ namespace snapper { SDir subvolume_dir = openSubvolumeDir(); - int r1 = subvolume_dir.unlink(".snapshots", AT_REMOVEDIR); + int r1 = subvolume_dir.unlink(SNAPSHOTS_NAME, AT_REMOVEDIR); if (r1 != 0) { y2err("rmdir failed errno:" << errno << " (" << strerror(errno) << ")"); @@ -194,8 +194,8 @@ namespace snapper string Lvm::snapshotDir(unsigned int num) const { - return (subvolume == "/" ? "" : subvolume) + "/.snapshots/" + decString(num) + - "/snapshot"; + return (subvolume == "/" ? "" : subvolume) + "/" SNAPSHOTS_NAME "/" + decString(num) + + "/" SNAPSHOT_NAME; } @@ -203,7 +203,7 @@ namespace snapper Lvm::openInfosDir() const { SDir subvolume_dir = openSubvolumeDir(); - SDir infos_dir(subvolume_dir, ".snapshots"); + SDir infos_dir(subvolume_dir, SNAPSHOTS_NAME); struct stat stat; if (infos_dir.stat(&stat) != 0) @@ -237,7 +237,7 @@ namespace snapper Lvm::openSnapshotDir(unsigned int num) const { SDir info_dir = openInfoDir(num); - SDir snapshot_dir(info_dir, "snapshot"); + SDir snapshot_dir(info_dir, SNAPSHOT_NAME); return snapshot_dir; } @@ -258,7 +258,7 @@ namespace snapper SN_THROW(UnsupportedException()); SDir info_dir = openInfoDir(num); - int r1 = info_dir.mkdir("snapshot", 0755); + int r1 = info_dir.mkdir(SNAPSHOT_NAME, 0755); if (r1 != 0 && errno != EEXIST) { y2err("mkdir failed errno:" << errno << " (" << strerror(errno) << ")"); @@ -293,7 +293,7 @@ namespace snapper } SDir info_dir = openInfoDir(num); - info_dir.unlink("snapshot", AT_REMOVEDIR); + info_dir.unlink(SNAPSHOT_NAME, AT_REMOVEDIR); SDir infos_dir = openInfosDir(); infos_dir.unlink(decString(num), AT_REMOVEDIR); @@ -347,7 +347,7 @@ namespace snapper { SDir info_dir = openInfoDir(num); - if (!umount(info_dir, "snapshot")) + if (!umount(info_dir, SNAPSHOT_NAME)) SN_THROW(UmountSnapshotFailedException()); } diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index d5821c6a..7711f21f 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -1033,7 +1033,7 @@ namespace snapper try { SDir subvol_dir = openSubvolumeDir(); - SDir infos_dir(subvol_dir, ".snapshots"); + SDir infos_dir(subvol_dir, SNAPSHOTS_NAME); if (infos_dir.restorecon(selabel_handle)) { @@ -1080,7 +1080,7 @@ namespace snapper if (!skip_snapshot_dir) { - SFile snapshot_dir(info_dir, "snapshot"); + SFile snapshot_dir(info_dir, SNAPSHOT_NAME); snapshot_dir.restorecon(selabel_handle); } diff --git a/snapper/SnapperDefines.h b/snapper/SnapperDefines.h index fe2d3ee8..6285c156 100644 --- a/snapper/SnapperDefines.h +++ b/snapper/SnapperDefines.h @@ -43,6 +43,13 @@ #define DEV_MAPPER_DIR "/dev/mapper" +// parts of the path of snapshots +// "/.snapshots/42/snapshot/" = "/SNAPSHOTS_NAME/42/snapshot/" + +#define SNAPSHOTS_NAME ".snapshots" +#define SNAPSHOT_NAME "snapshot" + + // commands #define SH_BIN "/bin/sh"