]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- remove empty directories while deleting configs 1119/head
authorArvin Schnell <aschnell@suse.de>
Tue, 24 Mar 2026 17:55:00 +0000 (18:55 +0100)
committerArvin Schnell <aschnell@suse.de>
Tue, 24 Mar 2026 17:55:00 +0000 (18:55 +0100)
snapper/FileUtils.cc
snapper/FileUtils.h
snapper/Snapper.cc
snapper/Snapshot.cc

index 55c7724affe06edfb5476fc41f14c074bd415b2f..d7951ee164c8424e550c5fb3f24beffd2f93384a 100644 (file)
@@ -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 <cstdlib>
 #include <cassert>
 #include <algorithm>
+#include <regex>
 
 #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<string>
     SDir::entries() const
     {
index 088ab0aa754dcfcd5008dba33b90f2e3e7188b90..bdbd5c0f3e534e7f28d53434959bf89a35899f95 100644 (file)
@@ -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<bool(unsigned char type, const char* name)> 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<string> entries() const;
        vector<string> entries(entries_pred_t pred) const;
index 706afbc2c957059591f5990144e85f533d7acb5d..d1789e357ea290e0f31d455d9c5c1e72c0ad37a5 100644 (file)
@@ -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();
index 727777f3aac5206c398df7cfc6544ca2c5778906..71b8ea67bc62faaa4acb08398be88e925c70c6ef 100644 (file)
@@ -28,7 +28,6 @@
 #include <fcntl.h>
 #include <cerrno>
 #include <cstring>
-#include <regex>
 
 #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<unsigned int> 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