Skip to content

Commit

Permalink
improve: delete backups after X days (#113)
Browse files Browse the repository at this point in the history
This add the function to delete old backups, with an option to configure the age of the backups to be deleted.
  • Loading branch information
lamonato29 authored Aug 23, 2024
1 parent 7b137f7 commit b69d445
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
#include "live_client.h"
#include "live_action.h"

#include <filesystem>
#include <chrono>
#include <iostream>

namespace fs = std::filesystem;

Editor::Editor(CopyBuffer &copybuffer) :
live_server(nullptr),
live_client(nullptr),
Expand Down Expand Up @@ -475,6 +481,8 @@ void Editor::saveMap(FileName filename, bool showdialog) {
std::remove(backup_zones.c_str());
}

deleteOldBackups(map_path + "backups/");

clearChanges();
}

Expand Down Expand Up @@ -2090,3 +2098,28 @@ void Editor::SendNodeRequests() {
live_client->sendNodeRequests();
}
}

void Editor::deleteOldBackups(const std::string &backup_path) {
int days_to_delete = g_settings.getInteger(Config::DELETE_BACKUP_DAYS);
if (days_to_delete <= 0) {
return; // Se o valor é zero ou negativo, não deletar backups
}

try {
auto now = std::chrono::system_clock::now();
for (const auto &entry : fs::directory_iterator(backup_path)) {
if (fs::is_regular_file(entry.status())) {
auto file_time = fs::last_write_time(entry);
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(file_time - fs::file_time_type::clock::now() + now);
auto file_age = std::chrono::duration_cast<std::chrono::hours>(now - sctp).count() / 24;

if (file_age > days_to_delete) {
fs::remove(entry);
std::cout << "Deleted old backup: " << entry.path() << std::endl;
}
}
}
} catch (const fs::filesystem_error &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
1 change: 1 addition & 0 deletions source/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class Editor {
void undraw(const PositionVector &todraw, PositionVector &toborder, bool alt);

void ensureBackupDirectoryExists(const std::string &backup_path);
void deleteOldBackups(const std::string &backup_path);

protected:
void drawInternal(const Position offset, bool alt, bool dodraw);
Expand Down
6 changes: 6 additions & 0 deletions source/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ wxNotebookPage* PreferencesWindow::CreateGeneralPage() {
grid_sizer->Add(replace_size_spin, 0);
SetWindowToolTip(tmptext, replace_size_spin, "How many items you can replace on the map using the Replace Item tool.");

grid_sizer->Add(tmptext = newd wxStaticText(general_page, wxID_ANY, "Delete backup after X days: "), 0);
delete_backup_days_spin = newd wxSpinCtrl(general_page, wxID_ANY, i2ws(g_settings.getInteger(Config::DELETE_BACKUP_DAYS)), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 365);
grid_sizer->Add(delete_backup_days_spin, 0);
SetWindowToolTip(tmptext, delete_backup_days_spin, "Configure the number of days after which backups will be automatically deleted.");

sizer->Add(grid_sizer, 0, wxALL, 5);
sizer->AddSpacer(10);

Expand Down Expand Up @@ -616,6 +621,7 @@ void PreferencesWindow::Apply() {
g_settings.setInteger(Config::UNDO_MEM_SIZE, undo_mem_size_spin->GetValue());
g_settings.setInteger(Config::WORKER_THREADS, worker_threads_spin->GetValue());
g_settings.setInteger(Config::REPLACE_SIZE, replace_size_spin->GetValue());
g_settings.setInteger(Config::DELETE_BACKUP_DAYS, delete_backup_days_spin->GetValue());
g_settings.setInteger(Config::COPY_POSITION_FORMAT, position_format->GetSelection());
g_settings.setInteger(Config::COPY_AREA_FORMAT, area_format->GetSelection());
if (g_settings.getBoolean(Config::SHOW_TILESET_EDITOR) != enable_tileset_editing_chkbox->GetValue()) {
Expand Down
1 change: 1 addition & 0 deletions source/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PreferencesWindow : public wxDialog {
wxSpinCtrl* undo_mem_size_spin;
wxSpinCtrl* worker_threads_spin;
wxSpinCtrl* replace_size_spin;
wxSpinCtrl* delete_backup_days_spin;
wxRadioBox* position_format;
wxRadioBox* area_format;

Expand Down
1 change: 1 addition & 0 deletions source/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ void Settings::IO(IOMode mode) {
Int(USE_OTGZ, 1);
Int(SAVE_WITH_OTB_MAGIC_NUMBER, 0);
Int(REPLACE_SIZE, 500);
Int(DELETE_BACKUP_DAYS, 0);
Int(COPY_POSITION_FORMAT, 0);
Int(COPY_AREA_FORMAT, 0);

Expand Down
1 change: 1 addition & 0 deletions source/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace Config {
USE_OTGZ,
SAVE_WITH_OTB_MAGIC_NUMBER,
REPLACE_SIZE,
DELETE_BACKUP_DAYS,

USE_LARGE_CONTAINER_ICONS,
USE_LARGE_CHOOSE_ITEM_ICONS,
Expand Down

0 comments on commit b69d445

Please sign in to comment.