feat: Add option to create auto backups of files before they're modified

This commit is contained in:
WerWolv
2025-12-07 21:37:14 +01:00
parent c2e07bf7b2
commit 855e4c4913
8 changed files with 86 additions and 10 deletions

View File

@@ -7,12 +7,14 @@
#include <cmath>
#include <cstring>
#include <optional>
#include <hex/api/content_registry/settings.hpp>
#include <hex/helpers/magic.hpp>
#include <wolv/io/file.hpp>
#include <wolv/literals.hpp>
#include <nlohmann/json.hpp>
#include <wolv/utils/string.hpp>
namespace hex::prv {
@@ -24,6 +26,28 @@ namespace hex::prv {
}
IProviderDataBackupable::IProviderDataBackupable(Provider* provider) : m_provider(provider) {
m_shouldCreateBackups = ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.enable", true);
m_maxSize = ContentRegistry::Settings::read<u32>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.max_size", 1_MiB);
m_backupExtension = ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.extension", ".bak");
}
void IProviderDataBackupable::createBackupIfNeeded(const std::fs::path &inputFilePath) {
if (!m_shouldCreateBackups || m_backupCreated)
return;
if (m_provider->getActualSize() > m_maxSize)
return;
const std::fs::path backupFilePath = wolv::util::toUTF8String(inputFilePath) + m_backupExtension;
if (wolv::io::fs::copyFile(inputFilePath, backupFilePath, std::fs::copy_options::overwrite_existing)) {
if (wolv::io::fs::exists(backupFilePath)) {
m_backupCreated = true;
log::info("Created backup of provider data at '{}'", backupFilePath.string());
}
}
}
Provider::Provider() : m_undoRedoStack(this), m_id(s_idCounter++) {