|
22 | 22 | #include <policy/settings.h> |
23 | 23 | #include <primitives/block.h> |
24 | 24 | #include <primitives/transaction.h> |
| 25 | +#include <reverse_iterator.h> |
25 | 26 | #include <script/descriptor.h> |
26 | 27 | #include <script/script.h> |
27 | 28 | #include <script/sign.h> |
@@ -3851,44 +3852,29 @@ bool CWallet::AutoBackupWallet(const fs::path& wallet_path, bilingual_str& error |
3851 | 3852 | } |
3852 | 3853 |
|
3853 | 3854 | // Keep only the last 10 backups, including the new one of course |
3854 | | - typedef std::multimap<std::time_t, fs::path> folder_set_t; |
3855 | | - folder_set_t folder_set; |
3856 | | - fs::directory_iterator end_iter; |
| 3855 | + std::multimap<fs::file_time_type, fs::path> folder_set; |
3857 | 3856 | // Build map of backup files for current(!) wallet sorted by last write time |
3858 | 3857 | fs::path currentFile; |
3859 | | - for (fs::directory_iterator dir_iter(backupsDir); dir_iter != end_iter; ++dir_iter) |
3860 | | - { |
| 3858 | + for (const auto& entry : fs::directory_iterator(backupsDir)) { |
3861 | 3859 | // Only check regular files |
3862 | | - if ( fs::is_regular_file(dir_iter->status())) |
3863 | | - { |
3864 | | - currentFile = dir_iter->path().filename(); |
| 3860 | + if (entry.is_regular_file()) { |
| 3861 | + currentFile = entry.path().filename(); |
3865 | 3862 | // Only add the backups for the current wallet, e.g. wallet.dat.* |
3866 | | - if (fs::PathToString(dir_iter->path().stem()) == strWalletName) { |
3867 | | - folder_set.insert(folder_set_t::value_type( |
3868 | | - // TODO: C++17 compliant time conversion code is abominable, switch to C++20 |
3869 | | - // compliant code when C++17 support is dropped |
3870 | | - std::chrono::system_clock::to_time_t( |
3871 | | - std::chrono::time_point_cast<std::chrono::system_clock::duration>( |
3872 | | - fs::last_write_time(dir_iter->path()) - fs::file_time_type::clock::now() + std::chrono::system_clock::now() |
3873 | | - ) |
3874 | | - ), |
3875 | | - *dir_iter |
3876 | | - )); |
| 3863 | + if (fs::PathToString(entry.path().stem()) == strWalletName) { |
| 3864 | + folder_set.insert(decltype(folder_set)::value_type(fs::last_write_time(entry.path()), entry)); |
3877 | 3865 | } |
3878 | 3866 | } |
3879 | 3867 | } |
3880 | 3868 |
|
3881 | 3869 | // Loop backward through backup files and keep the N newest ones (1 <= N <= 10) |
3882 | | - int counter = 0; |
3883 | | - for(auto it = folder_set.rbegin(); it != folder_set.rend(); ++it) { |
3884 | | - std::pair<const std::time_t, fs::path> file = *it; |
| 3870 | + int counter{0}; |
| 3871 | + for (const auto& [entry_time, entry] : reverse_iterate(folder_set)) { |
3885 | 3872 | counter++; |
3886 | | - if (counter > nWalletBackups) |
3887 | | - { |
| 3873 | + if (counter > nWalletBackups) { |
3888 | 3874 | // More than nWalletBackups backups: delete oldest one(s) |
3889 | 3875 | try { |
3890 | | - fs::remove(file.second); |
3891 | | - WalletLogPrintf("Old backup deleted: %s\n", fs::PathToString(file.second)); |
| 3876 | + fs::remove(entry); |
| 3877 | + WalletLogPrintf("Old backup deleted: %s\n", fs::PathToString(entry)); |
3892 | 3878 | } catch(fs::filesystem_error &error) { |
3893 | 3879 | warnings.push_back(strprintf(_("Failed to delete backup, error: %s"), fsbridge::get_filesystem_error_message(error))); |
3894 | 3880 | WalletLogPrintf("%s\n", Join(warnings, Untranslated("\n")).original); |
|
0 commit comments