Skip to content

Commit 3a65e55

Browse files
committed
Improved backup system and fixed #38
1 parent 5d944ef commit 3a65e55

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

filesystem/src/backups.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
#include "backups.hpp"
66
#include "db.hpp"
77

8+
#include <algorithm>
89
#include <filesystem>
9-
#include <optional>
1010

1111
#include <pthread.h>
1212
#include <sys/signal.h>
1313
#include <time.h>
1414

1515
#include <spdlog/spdlog.h>
16+
#include <vector>
1617

1718
// better way to pass these in? handler args
1819
uint32_t _number_backups;
@@ -92,47 +93,45 @@ static auto handle_backup(sigval val) -> void {
9293
if (backup_count > _number_backups) {
9394
spdlog::info("Removing oldest backup");
9495

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();
108100

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+
};
111108

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);
117110

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();
122115

116+
if (entry.is_regular_file() && (filename.string().ends_with(".backup.db"))) {
117+
files.push_back(entry.path());
123118
}
124119
}
125120

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());
129131

130132
} 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());
132134
}
133-
134-
} else {
135-
spdlog::error("Could not remove entry, no file found");
136135
}
137136
}
138137

0 commit comments

Comments
 (0)