Skip to content

Commit 2c2150a

Browse files
committed
Merge bitcoin/bitcoin#26828: assumeutxo: catch and log fs::remove error instead of two exist checks
0e21b56 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 0e21b56 jamesob: ACK bitcoin/bitcoin@0e21b56 achow101: ACK 0e21b56 Tree-SHA512: 137d0be5266cfd947e5e50ec93b895ac659adadf9413bef3468744bfdacee8dbe7d9bdfaf91784c45708610325d2241a114f4be4e622a108a639b3672b618fd2
2 parents 6a47337 + 0e21b56 commit 2c2150a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/validation.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4999,15 +4999,15 @@ static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
49994999
if (is_snapshot) {
50005000
fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
50015001

5002-
if (fs::exists(base_blockhash_path)) {
5003-
bool removed = fs::remove(base_blockhash_path);
5004-
if (!removed) {
5005-
LogPrintf("[snapshot] failed to remove file %s\n",
5006-
fs::PathToString(base_blockhash_path));
5002+
try {
5003+
bool existed = fs::remove(base_blockhash_path);
5004+
if (!existed) {
5005+
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
5006+
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
50075007
}
5008-
} else {
5009-
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
5010-
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
5008+
} catch (const fs::filesystem_error& e) {
5009+
LogPrintf("[snapshot] failed to remove file %s: %s\n",
5010+
fs::PathToString(base_blockhash_path), fsbridge::get_filesystem_error_message(e));
50115011
}
50125012
}
50135013

0 commit comments

Comments
 (0)