Skip to content

Commit 9f536d4

Browse files
committed
wallettool: Have RecoverDatabaseFile return errors and warnings
Instead of logging or printing these errors and warnings, return them to the caller.
1 parent 06e263a commit 9f536d4

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/wallet/salvage.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ static const char *HEADER_END = "HEADER=END";
1616
static const char *DATA_END = "DATA=END";
1717
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
1818

19-
bool RecoverDatabaseFile(const fs::path& file_path)
19+
bool RecoverDatabaseFile(const fs::path& file_path, bilingual_str& error, std::vector<bilingual_str>& warnings)
2020
{
2121
std::string filename;
2222
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
2323

24-
bilingual_str open_err;
25-
if (!env->Open(open_err)) {
26-
tfm::format(std::cerr, "%s\n", open_err.original);
24+
if (!env->Open(error)) {
2725
return false;
2826
}
2927

@@ -39,11 +37,9 @@ bool RecoverDatabaseFile(const fs::path& file_path)
3937

4038
int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr,
4139
newFilename.c_str(), DB_AUTO_COMMIT);
42-
if (result == 0)
43-
LogPrintf("Renamed %s to %s\n", filename, newFilename);
44-
else
40+
if (result != 0)
4541
{
46-
LogPrintf("Failed to rename %s to %s\n", filename, newFilename);
42+
error = strprintf(Untranslated("Failed to rename %s to %s"), filename, newFilename);
4743
return false;
4844
}
4945

@@ -60,10 +56,10 @@ bool RecoverDatabaseFile(const fs::path& file_path)
6056
Db db(env->dbenv.get(), 0);
6157
result = db.verify(newFilename.c_str(), nullptr, &strDump, DB_SALVAGE | DB_AGGRESSIVE);
6258
if (result == DB_VERIFY_BAD) {
63-
LogPrintf("Salvage: Database salvage found errors, all data may not be recoverable.\n");
59+
warnings.push_back(Untranslated("Salvage: Database salvage found errors, all data may not be recoverable."));
6460
}
6561
if (result != 0 && result != DB_VERIFY_BAD) {
66-
LogPrintf("Salvage: Database salvage failed with result %d.\n", result);
62+
error = strprintf(Untranslated("Salvage: Database salvage failed with result %d."), result);
6763
return false;
6864
}
6965

@@ -87,7 +83,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
8783
break;
8884
getline(strDump, valueHex);
8985
if (valueHex == DATA_END) {
90-
LogPrintf("Salvage: WARNING: Number of keys in data does not match number of values.\n");
86+
warnings.push_back(Untranslated("Salvage: WARNING: Number of keys in data does not match number of values."));
9187
break;
9288
}
9389
salvagedData.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
@@ -96,18 +92,17 @@ bool RecoverDatabaseFile(const fs::path& file_path)
9692

9793
bool fSuccess;
9894
if (keyHex != DATA_END) {
99-
LogPrintf("Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
95+
warnings.push_back(Untranslated("Salvage: WARNING: Unexpected end of file while reading salvage output."));
10096
fSuccess = false;
10197
} else {
10298
fSuccess = (result == 0);
10399
}
104100

105101
if (salvagedData.empty())
106102
{
107-
LogPrintf("Salvage(aggressive) found no records in %s.\n", newFilename);
103+
error = strprintf(Untranslated("Salvage(aggressive) found no records in %s."), newFilename);
108104
return false;
109105
}
110-
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
111106

112107
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
113108
int ret = pdbCopy->open(nullptr, // Txn pointer
@@ -117,7 +112,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
117112
DB_CREATE, // Flags
118113
0);
119114
if (ret > 0) {
120-
LogPrintf("Cannot create database file %s\n", filename);
115+
error = strprintf(Untranslated("Cannot create database file %s"), filename);
121116
pdbCopy->close(0);
122117
return false;
123118
}
@@ -141,7 +136,7 @@ bool RecoverDatabaseFile(const fs::path& file_path)
141136
}
142137
if (!fReadOK)
143138
{
144-
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
139+
warnings.push_back(strprintf(Untranslated("WARNING: WalletBatch::Recover skipping %s: %s"), strType, strErr));
145140
continue;
146141
}
147142
Dbt datKey(&row.first[0], row.first.size());

src/wallet/salvage.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <fs.h>
1010
#include <streams.h>
1111

12-
bool RecoverDatabaseFile(const fs::path& file_path);
12+
struct bilingual_str;
13+
14+
bool RecoverDatabaseFile(const fs::path& file_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
1315

1416
#endif // BITCOIN_WALLET_SALVAGE_H

src/wallet/wallettool.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,18 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
126126
WalletShowInfo(wallet_instance.get());
127127
wallet_instance->Flush(true);
128128
} else if (command == "salvage") {
129-
return RecoverDatabaseFile(path);
129+
bilingual_str error;
130+
std::vector<bilingual_str> warnings;
131+
bool ret = RecoverDatabaseFile(path, error, warnings);
132+
if (!ret) {
133+
for (const auto warning : warnings) {
134+
tfm::format(std::cerr, "%s\n", warning.original);
135+
}
136+
if (!error.empty()) {
137+
tfm::format(std::cerr, "%s\n", error.original);
138+
}
139+
}
140+
return ret;
130141
}
131142
} else {
132143
tfm::format(std::cerr, "Invalid command: %s\n", command);

0 commit comments

Comments
 (0)