From: Arvin Schnell Date: Tue, 24 Mar 2026 17:55:00 +0000 (+0100) Subject: - remove empty directories while deleting configs X-Git-Tag: v0.13.1~8^2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=163671e9ea545dfdebe82d0e3b237cdfd8096b05;p=thirdparty%2Fsnapper.git - remove empty directories while deleting configs --- diff --git a/snapper/FileUtils.cc b/snapper/FileUtils.cc index 55c7724a..d7951ee1 100644 --- a/snapper/FileUtils.cc +++ b/snapper/FileUtils.cc @@ -1,6 +1,6 @@ /* * Copyright (c) [2011-2014] Novell, Inc. - * Copyright (c) [2018-2025] SUSE LLC + * Copyright (c) [2018-2026] SUSE LLC * * All Rights Reserved. * @@ -37,6 +37,7 @@ #include #include #include +#include #include "snapper/FileUtils.h" #include "snapper/AppUtil.h" @@ -165,13 +166,25 @@ namespace snapper } - static bool - all_entries(unsigned char type, const char* name) + bool + SDir::all_entries(unsigned char type, const char* name) { return true; } + bool + SDir::number_entries(unsigned char type, const char* name) + { + // Snapshot '0' is internal and not saved on disks. So simply ignore directories + // starting with '0'. + + static const regex rx_num("[1-9][0-9]*", regex::extended); + + return regex_match(name, rx_num); + } + + vector SDir::entries() const { diff --git a/snapper/FileUtils.h b/snapper/FileUtils.h index 088ab0aa..bdbd5c0f 100644 --- a/snapper/FileUtils.h +++ b/snapper/FileUtils.h @@ -1,6 +1,6 @@ /* * Copyright (c) [2011-2014] Novell, Inc. - * Copyright (c) [2020-2025] SUSE LLC + * Copyright (c) [2020-2026] SUSE LLC * * All Rights Reserved. * @@ -75,6 +75,9 @@ namespace snapper // Type is not supported by all file system types, see readdir(3). typedef std::function entries_pred_t; + static bool all_entries(unsigned char type, const char* name); + static bool number_entries(unsigned char type, const char* name); + // The order of the result of the entries functions is undefined. vector entries() const; vector entries(entries_pred_t pred) const; diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 706afbc2..d1789e35 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -467,6 +467,14 @@ namespace snapper } } + // Since we want all-time unique snapshot numbers there might be empty + // directories. + + SDir infos_dir = snapper->openInfosDir(); + + for (const string& tmp : infos_dir.entries(SDir::number_entries)) + infos_dir.rmdir(tmp); + try { snapper->getFilesystem()->deleteConfig(); diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 727777f3..71b8ea67 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -28,7 +28,6 @@ #include #include #include -#include #include "snapper/Snapshot.h" #include "snapper/Snapper.h" @@ -49,7 +48,6 @@ namespace snapper { using std::list; - using std::regex; std::ostream& operator<<(std::ostream& s, const Snapshot& snapshot) @@ -236,24 +234,12 @@ namespace snapper Snapshots::~Snapshots() = default; - static bool - number_entries(unsigned char type, const char* name) - { - // Snapshot '0' is internal and not saved on disks. So simply ignore directories - // starting with '0'. - - static const regex rx_num("[1-9][0-9]*", regex::extended); - - return regex_match(name, rx_num); - } - - void Snapshots::read() { SDir infos_dir = snapper->openInfosDir(); - for (const string& info : infos_dir.entries(number_entries)) + for (const string& info : infos_dir.entries(SDir::number_entries)) { try { @@ -505,7 +491,7 @@ namespace snapper // Numbers of directories (and files) found in infos-dir. vector nums; - for (const string& tmp : infos_dir.entries(number_entries)) + for (const string& tmp : infos_dir.entries(SDir::number_entries)) nums.push_back(stoi(tmp)); // Set num to next available free number. All entries should also be included in