Skip to content

Commit 34d1590

Browse files
committed
add utilities for deleting on-disk leveldb data
Used in later commits to remove leveldb directories for - invalid snapshot chainstates, and - background-vaildation chainstates that have finished serving their purpose.
1 parent 252abd1 commit 34d1590

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/validation.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4767,6 +4767,46 @@ const AssumeutxoData* ExpectedAssumeutxo(
47674767
return nullptr;
47684768
}
47694769

4770+
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
4771+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
4772+
{
4773+
AssertLockHeld(::cs_main);
4774+
4775+
if (is_snapshot) {
4776+
fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
4777+
4778+
if (fs::exists(base_blockhash_path)) {
4779+
bool removed = fs::remove(base_blockhash_path);
4780+
if (!removed) {
4781+
LogPrintf("[snapshot] failed to remove file %s\n",
4782+
fs::PathToString(base_blockhash_path));
4783+
}
4784+
} else {
4785+
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
4786+
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
4787+
}
4788+
}
4789+
4790+
std::string path_str = fs::PathToString(db_path);
4791+
LogPrintf("Removing leveldb dir at %s\n", path_str);
4792+
4793+
// We have to destruct before this call leveldb::DB in order to release the db
4794+
// lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
4795+
const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
4796+
4797+
if (!destroyed) {
4798+
LogPrintf("error: leveldb DestroyDB call failed on %s\n", path_str);
4799+
}
4800+
4801+
// Datadir should be removed from filesystem; otherwise initialization may detect
4802+
// it on subsequent statups and get confused.
4803+
//
4804+
// If the base_blockhash_path removal above fails in the case of snapshot
4805+
// chainstates, this will return false since leveldb won't remove a non-empty
4806+
// directory.
4807+
return destroyed && !fs::exists(db_path);
4808+
}
4809+
47704810
bool ChainstateManager::ActivateSnapshot(
47714811
AutoFile& coins_file,
47724812
const SnapshotMetadata& metadata,

0 commit comments

Comments
 (0)