Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet: Add seconds to backup filename so it doesn't conflict with ba…
Browse files Browse the repository at this point in the history
…ckups created closing-opening the wallet under the same minute.

Plus remove `FormatISO8601DateTimeForBackup` function that was a copy of `FormatISO8601DateTime` without the seconds.
furszy committed May 19, 2021
1 parent d5d3990 commit 7a3e9e9
Showing 5 changed files with 3 additions and 29 deletions.
5 changes: 0 additions & 5 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
@@ -90,11 +90,6 @@ BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
}

BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTimeForBackup)
{
BOOST_CHECK_EQUAL(FormatISO8601DateTimeForBackup(1586310600), ".20200408T0150Z");
}

BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
{
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
13 changes: 0 additions & 13 deletions src/utiltime.cpp
Original file line number Diff line number Diff line change
@@ -109,19 +109,6 @@ std::string FormatISO8601DateTime(int64_t nTime) {
return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
}

std::string FormatISO8601DateTimeForBackup(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
#ifdef HAVE_GMTIME_R
if (gmtime_r(&time_val, &ts) == nullptr) {
#else
if (gmtime_s(&ts, &time_val) != 0) {
#endif
return {};
}
return strprintf(".%04i%02i%02iT%02i%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min);
}

std::string FormatISO8601Date(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
1 change: 0 additions & 1 deletion src/utiltime.h
Original file line number Diff line number Diff line change
@@ -40,7 +40,6 @@ T GetTime();
* helper functions if possible.
*/
std::string FormatISO8601DateTime(int64_t nTime);
std::string FormatISO8601DateTimeForBackup(int64_t nTime);
std::string FormatISO8601Date(int64_t nTime);
std::string FormatISO8601Time(int64_t nTime);

2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
@@ -4491,7 +4491,7 @@ bool CWalletTx::AcceptToMemoryPool(CValidationState& state, bool fLimitFree, boo

std::string CWallet::GetUniqueWalletBackupName() const
{
return strprintf("%s%s", (dbw ? dbw->GetName() : "null"), FormatISO8601DateTimeForBackup(GetTime()));
return strprintf("%s%s", (dbw ? dbw->GetName() : "null"), FormatISO8601DateTime(GetTime()));
}

CWallet::CWallet() : dbw(new CWalletDBWrapper())
11 changes: 2 additions & 9 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
@@ -909,11 +909,11 @@ bool AutoBackupWallet(const std::string& strWalletFile, std::string& strBackupWa

// Copy wallet file
fs::path sourceFile = GetDataDir() / strWalletFile;
fs::path backupFile = backupsDir / (strWalletFile + FormatISO8601DateTimeForBackup(GetTime()));
fs::path backupFile = backupsDir / (strWalletFile + FormatISO8601DateTime(GetTime()));
sourceFile.make_preferred();
backupFile.make_preferred();
if (fs::exists(backupFile)) {
strBackupWarning = _("Failed to create backup, file already exists! This could happen if you restarted wallet in less than 60 seconds. You can continue if you are ok with this.");
strBackupWarning = _("Failed to create backup, filename already exists!");
LogPrintf("%s\n", strBackupWarning);
return false;
}
@@ -944,13 +944,6 @@ void MultiBackup(const CWallet& wallet, fs::path pathCustom, fs::path pathWithFi
if (!cleanWalletBackups(folderSet, nThreshold, strBackupWarning)) {
NotifyBacked(wallet, false, strBackupWarning);
}

// TODO: add seconds to avoid naming conflicts
for (const auto& entry : folderSet) {
if(entry.second == pathWithFile) {
pathWithFile += "(1)";
}
}
}
AttemptBackupWallet(&wallet, pathSrc.string(), pathWithFile.string());
}

0 comments on commit 7a3e9e9

Please sign in to comment.