Merge bitcoin/bitcoin#26828: assumeutxo: catch and log fs::remove error instead of two exist checks

0e21b56a44 assumeutxo: catch and log fs::remove error instead of two exist checks (Andrew Toth)

Pull request description:

  Fixes a block of code which seems to be incorrectly performing two existence checks instead of catching and logging errors. `fs::remove` returns `false` only if the file being removed does not exist, so it is redundant with the `fs::exists` check. If an error does occur when trying to remove an existing file, `fs::remove` will throw. See https://en.cppreference.com/w/cpp/filesystem/remove.

  Also see https://github.com/bitcoin/bitcoin/blob/master/src/init.cpp#L326-L332 for a similar pattern.

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 0e21b56a44
  jamesob:
    ACK 0e21b56a44
  achow101:
    ACK 0e21b56a44

Tree-SHA512: 137d0be5266cfd947e5e50ec93b895ac659adadf9413bef3468744bfdacee8dbe7d9bdfaf91784c45708610325d2241a114f4be4e622a108a639b3672b618fd2
This commit is contained in:
Andrew Chow 2023-06-23 16:15:50 -04:00
commit 2c2150aa04
No known key found for this signature in database
GPG key ID: 17565732E08E5E41

View file

@ -4999,15 +4999,15 @@ static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
if (is_snapshot) {
fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
if (fs::exists(base_blockhash_path)) {
bool removed = fs::remove(base_blockhash_path);
if (!removed) {
LogPrintf("[snapshot] failed to remove file %s\n",
fs::PathToString(base_blockhash_path));
try {
bool existed = fs::remove(base_blockhash_path);
if (!existed) {
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
}
} else {
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
} catch (const fs::filesystem_error& e) {
LogPrintf("[snapshot] failed to remove file %s: %s\n",
fs::PathToString(base_blockhash_path), fsbridge::get_filesystem_error_message(e));
}
}