Skip to content

Commit a0e133e

Browse files
committed
refactor: avoid convoluted casting by not using std::time_t
std::clock_cast is a part of C++20, hence the original TODO comment but it's still not implemented in Clang, as of this writing. We can sidestep the cast by not using time_t to begin with.
1 parent 690a719 commit a0e133e

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

src/wallet/wallet.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <policy/settings.h>
2323
#include <primitives/block.h>
2424
#include <primitives/transaction.h>
25+
#include <reverse_iterator.h>
2526
#include <script/descriptor.h>
2627
#include <script/script.h>
2728
#include <script/sign.h>
@@ -3851,44 +3852,29 @@ bool CWallet::AutoBackupWallet(const fs::path& wallet_path, bilingual_str& error
38513852
}
38523853

38533854
// 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;
38573856
// Build map of backup files for current(!) wallet sorted by last write time
38583857
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)) {
38613859
// 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();
38653862
// 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));
38773865
}
38783866
}
38793867
}
38803868

38813869
// 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)) {
38853872
counter++;
3886-
if (counter > nWalletBackups)
3887-
{
3873+
if (counter > nWalletBackups) {
38883874
// More than nWalletBackups backups: delete oldest one(s)
38893875
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));
38923878
} catch(fs::filesystem_error &error) {
38933879
warnings.push_back(strprintf(_("Failed to delete backup, error: %s"), fsbridge::get_filesystem_error_message(error)));
38943880
WalletLogPrintf("%s\n", Join(warnings, Untranslated("\n")).original);

0 commit comments

Comments
 (0)