Skip to content

Commit

Permalink
Walletdb: unify sort backups file by last write.
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed May 7, 2021
1 parent c9abf3c commit 7dbb59b
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,24 @@ std::pair<fs::path, fs::path> GetBackupPath(const CWallet& wallet)
return {pathCustom, pathWithFile};
}

typedef std::multimap<std::time_t, fs::path> folder_set_t;
static folder_set_t buildBackupsMapSortedByLastWrite(const std::string& strWalletFile, const fs::path& backupsDir)
{
folder_set_t folder_set;
fs::directory_iterator end_iter;
// Build map of backup files for current(!) wallet sorted by last write time
for (fs::directory_iterator dir_iter(backupsDir); dir_iter != end_iter; ++dir_iter) {
// Only check regular files
if (fs::is_regular_file(dir_iter->status())) {
// Only add the backups for the current wallet, e.g. wallet.dat.*
if(dir_iter->path().stem().string() == strWalletFile) {
folder_set.insert(folder_set_t::value_type(fs::last_write_time(dir_iter->path()), *dir_iter));
}
}
}
return folder_set;
}

bool AutoBackupWallet(const std::string& strWalletFile, std::string& strBackupWarning, std::string& strBackupError)
{
strBackupWarning = strBackupError = "";
Expand Down Expand Up @@ -908,19 +926,8 @@ bool AutoBackupWallet(const std::string& strWalletFile, std::string& strBackupWa
}

// Keep only the last 10 backups, including the new one of course
typedef std::multimap<std::time_t, fs::path> folder_set_t;
folder_set_t folder_set;
fs::directory_iterator end_iter;
// Build map of backup files for current(!) wallet sorted by last write time
for (fs::directory_iterator dir_iter(backupsDir); dir_iter != end_iter; ++dir_iter) {
// Only check regular files
if (fs::is_regular_file(dir_iter->status())) {
// Only add the backups for the current wallet, e.g. wallet.dat.*
if(dir_iter->path().stem().string() == strWalletFile) {
folder_set.insert(folder_set_t::value_type(fs::last_write_time(dir_iter->path()), *dir_iter));
}
}
}
folder_set_t folder_set = buildBackupsMapSortedByLastWrite(strWalletFile, backupsDir);

// Loop backward through backup files and keep the N newest ones (1 <= N <= 10)
int counter = 0;
for (const std::pair<const std::time_t, fs::path>& file : reverse_iterate(folder_set)) {
Expand All @@ -944,25 +951,8 @@ void MultiBackup(const CWallet& wallet, fs::path pathCustom, fs::path pathWithFi
{
int nThreshold = gArgs.GetArg("-custombackupthreshold", DEFAULT_CUSTOMBACKUPTHRESHOLD);
if (nThreshold > 0) {

typedef std::multimap<std::time_t, fs::path> folder_set_t;
folder_set_t folderSet;
fs::directory_iterator end_iter;

pathCustom.make_preferred();
// Build map of backup files for current(!) wallet sorted by last write time

fs::path currentFile;
for (fs::directory_iterator dir_iter(pathCustom); dir_iter != end_iter; ++dir_iter) {
// Only check regular files
if (fs::is_regular_file(dir_iter->status())) {
currentFile = dir_iter->path().filename();
// Only add the backups for the current wallet, e.g. wallet.dat.*
if (dir_iter->path().stem().string() == wallet.GetDBHandle().GetName()) {
folderSet.insert(folder_set_t::value_type(fs::last_write_time(dir_iter->path()), *dir_iter));
}
}
}
folder_set_t folderSet = buildBackupsMapSortedByLastWrite(wallet.GetDBHandle().GetName(), pathCustom);

int counter = 0; //TODO: add seconds to avoid naming conflicts
for (auto entry : folderSet) {
Expand Down

0 comments on commit 7dbb59b

Please sign in to comment.