|
5 | 5 | #include "backups.hpp" |
6 | 6 | #include "db.hpp" |
7 | 7 |
|
| 8 | +#include <algorithm> |
8 | 9 | #include <filesystem> |
9 | | -#include <optional> |
10 | 10 |
|
11 | 11 | #include <pthread.h> |
12 | 12 | #include <sys/signal.h> |
13 | 13 | #include <time.h> |
14 | 14 |
|
15 | 15 | #include <spdlog/spdlog.h> |
| 16 | +#include <vector> |
16 | 17 |
|
17 | 18 | // better way to pass these in? handler args |
18 | 19 | uint32_t _number_backups; |
@@ -92,47 +93,45 @@ static auto handle_backup(sigval val) -> void { |
92 | 93 | if (backup_count > _number_backups) { |
93 | 94 | spdlog::info("Removing oldest backup"); |
94 | 95 |
|
95 | | - dir_iter = std::filesystem::directory_iterator(_backup_dir); |
96 | | - auto oldest_entry = std::make_optional<std::filesystem::directory_entry>(); |
97 | | - std::tm oldest_entry_date; |
98 | | - |
99 | | - for (auto entry : dir_iter) { |
100 | | - |
101 | | - if (entry.is_regular_file()) { |
102 | | - |
103 | | - // Get time from filename |
104 | | - const auto new_entry_name = entry.path().stem(); |
105 | | - |
106 | | - // TODO: if this is removed, the backup fails! |
107 | | - spdlog::debug("Looking at file {0} {1}", entry.path().c_str(), entry.path().stem().c_str()); |
| 96 | + // Derives the time from the file name |
| 97 | + const auto get_time = [] (const std::filesystem::path entry) -> time_t |
| 98 | + { |
| 99 | + const auto file_stem = entry.stem(); |
108 | 100 |
|
109 | | - std::tm new_entry_date; |
110 | | - strptime(new_entry_name.c_str(), "%Y-%m-%d.%X", &new_entry_date); |
| 101 | + spdlog::debug("Looking at file {0} {1}", entry.c_str(), entry.stem().c_str()); |
| 102 | + |
| 103 | + std::tm file_date; |
| 104 | + strptime(file_stem.c_str(), "%Y-%m-%d.%X", &file_date); |
| 105 | + |
| 106 | + return mktime(&file_date); |
| 107 | + }; |
111 | 108 |
|
112 | | - if (!oldest_entry.has_value()) { |
113 | | - oldest_entry = entry; |
114 | | - oldest_entry_date = new_entry_date; |
115 | | - continue; |
116 | | - } |
| 109 | + dir_iter = std::filesystem::directory_iterator(_backup_dir); |
117 | 110 |
|
118 | | - if (difftime(mktime(&new_entry_date), mktime(&oldest_entry_date)) < 0) { |
119 | | - oldest_entry = entry; |
120 | | - oldest_entry_date = new_entry_date; |
121 | | - } |
| 111 | + // Placing the iterator into a vector so its more straightforward to work with |
| 112 | + std::vector<std::filesystem::path> files{}; |
| 113 | + for (auto entry : dir_iter) { |
| 114 | + const auto filename = entry.path().filename(); |
122 | 115 |
|
| 116 | + if (entry.is_regular_file() && (filename.string().ends_with(".backup.db"))) { |
| 117 | + files.push_back(entry.path()); |
123 | 118 | } |
124 | 119 | } |
125 | 120 |
|
126 | | - if (oldest_entry.has_value()) { |
127 | | - if (std::filesystem::remove(oldest_entry->path())) { |
128 | | - spdlog::info("Removed file at {0}", oldest_entry->path().c_str()); |
| 121 | + std::sort(files.begin(), files.end(), |
| 122 | + [get_time](const std::filesystem::path& entry_a, const std::filesystem::path& entry_b) -> bool { |
| 123 | + return get_time(entry_a) < get_time(entry_b); |
| 124 | + }); |
| 125 | + |
| 126 | + // remove files |
| 127 | + for (int i = 0; i < (backup_count - _number_backups); i++) |
| 128 | + { |
| 129 | + if (std::filesystem::remove(files[i])) { |
| 130 | + spdlog::info("Removed file at {0}", files[i].c_str()); |
129 | 131 |
|
130 | 132 | } else { |
131 | | - spdlog::error("Could not remove file at {0}", oldest_entry->path().c_str()); |
| 133 | + spdlog::error("Could not remove file at {0}",files[i].c_str()); |
132 | 134 | } |
133 | | - |
134 | | - } else { |
135 | | - spdlog::error("Could not remove entry, no file found"); |
136 | 135 | } |
137 | 136 | } |
138 | 137 |
|
|
0 commit comments