MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / Salvage

Method Salvage

src/wallet/db.cpp:376–434  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

374static const char *DATA_END = "DATA=END";
375
376bool BerkeleyEnvironment::Salvage(const std::string& strFile, bool fAggressive, std::vector<BerkeleyEnvironment::KeyValPair>& vResult)
377{
378 LOCK(cs_db);
379 assert(mapFileUseCount.count(strFile) == 0);
380
381 u_int32_t flags = DB_SALVAGE;
382 if (fAggressive)
383 flags |= DB_AGGRESSIVE;
384
385 std::stringstream strDump;
386
387 Db db(dbenv.get(), 0);
388 int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
389 if (result == DB_VERIFY_BAD) {
390 LogPrintf("BerkeleyEnvironment::Salvage: Database salvage found errors, all data may not be recoverable.\n");
391 if (!fAggressive) {
392 LogPrintf("BerkeleyEnvironment::Salvage: Rerun with aggressive mode to ignore errors and continue.\n");
393 return false;
394 }
395 }
396 if (result != 0 && result != DB_VERIFY_BAD) {
397 LogPrintf("BerkeleyEnvironment::Salvage: Database salvage failed with result %d.\n", result);
398 return false;
399 }
400
401 // Format of bdb dump is ascii lines:
402 // header lines...
403 // HEADER=END
404 // hexadecimal key
405 // hexadecimal value
406 // ... repeated
407 // DATA=END
408
409 std::string strLine;
410 while (!strDump.eof() && strLine != HEADER_END)
411 getline(strDump, strLine); // Skip past header
412
413 std::string keyHex, valueHex;
414 while (!strDump.eof() && keyHex != DATA_END) {
415 getline(strDump, keyHex);
416 if (keyHex != DATA_END) {
417 if (strDump.eof())
418 break;
419 getline(strDump, valueHex);
420 if (valueHex == DATA_END) {
421 LogPrintf("BerkeleyEnvironment::Salvage: WARNING: Number of keys in data does not match number of values.\n");
422 break;
423 }
424 vResult.push_back(make_pair(ParseHex(keyHex), ParseHex(valueHex)));
425 }
426 }
427
428 if (keyHex != DATA_END) {
429 LogPrintf("BerkeleyEnvironment::Salvage: WARNING: Unexpected end of file while reading salvage output.\n");
430 return false;
431 }
432
433 return (result == 0);

Callers 1

RecoverMethod · 0.80

Calls 6

ParseHexFunction · 0.85
getMethod · 0.80
countMethod · 0.45
verifyMethod · 0.45
eofMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected