Skip to content

assumeutxo: catch and log fs::remove error instead of two exist checks - #26828

Merged
achow101 merged 1 commit into
bitcoin:masterfrom
andrewtoth:assumeutxo-remove-fix
Jun 23, 2023
Merged

achow101 merged 1 commit into
bitcoin:masterfrom
andrewtoth:assumeutxo-remove-fix

Conversation

@andrewtoth

Copy link
Copy Markdown
Contributor

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.

@DrahtBot

DrahtBot commented Jan 5, 2023 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK MarcoFalke, jamesob, achow101
Concept ACK brunoerg, luke-jr

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@andrewtoth
andrewtoth force-pushed the assumeutxo-remove-fix branch from 4fe7d35 to cff2f3c Compare January 5, 2023 22:33
@andrewtoth
andrewtoth force-pushed the assumeutxo-remove-fix branch from cff2f3c to 0e21b56 Compare January 5, 2023 22:35
@fanquake
fanquake requested a review from jamesob January 6, 2023 06:17
@jamesob

jamesob commented Jan 7, 2023

Copy link
Copy Markdown
Contributor

Approach ACK, thanks for fixing this. Will give a thorough look in the next day or two.

Comment thread src/validation.cpp
LogPrintf("[snapshot] failed to remove file %s\n",
fs::PathToString(base_blockhash_path));
try {
bool existed = fs::remove(base_blockhash_path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
bool existed = fs::remove(base_blockhash_path);
const bool existed{fs::remove(base_blockhash_path)};

@brunoerg brunoerg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@luke-jr luke-jr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK (also fine with @brunoerg's nit being addressed)

@maflcko

maflcko commented Jun 22, 2023

Copy link
Copy Markdown
Member

lgtm ACK 0e21b56

No idea what the link in OP should point to, but I presume it is

bitcoin/src/init.cpp

Lines 326 to 332 in fff80cd

try {
if (!fs::remove(GetPidFile(*node.args))) {
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
}
} catch (const fs::filesystem_error& e) {
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
}

@jamesob

jamesob commented Jun 22, 2023

Copy link
Copy Markdown
Contributor

ACK 0e21b56

Built locally. Makes sense, thank you for the cleanup!

@DrahtBot
DrahtBot removed the request for review from jamesob June 22, 2023 17:08
@achow101

Copy link
Copy Markdown
Member

ACK 0e21b56

@achow101
achow101 merged commit 2c2150a into bitcoin:master Jun 23, 2023
@andrewtoth
andrewtoth deleted the assumeutxo-remove-fix branch August 17, 2023 20:41
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 18, 2023
Summary:
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.

This is a backport of [[bitcoin/bitcoin#26828 | core#26828]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D14995
@bitcoin bitcoin locked and limited conversation to collaborators Aug 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants