If settings.json exists, but is unreadable, we should error instead of overwriting.
Fixes #22571
If settings.json exists, but is unreadable, we should error instead of overwriting.
Fixes #22571
66+
67 fsbridge::ifstream file;
68 file.open(path);
69- if (!file.is_open()) return true; // Ok for file not to exist.
70+ if (!file.is_open()) {
71+ errors.emplace_back(strprintf("Unable to read settings file %s. Are the permissions correct?", path.string()));
Concept ACK.
nit: Maybe error message can be changed to
0-Are the permissions correct?
1+Please check permissions.
59@@ -60,9 +60,16 @@ bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& va
60 values.clear();
61 errors.clear();
62
63+ if (!fs::exists(path)) {
64+ return true; // Ok for file to not exist
65+ }
This should be a single line as per https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#coding-style-c (bullet point 7)
Also, the comment should be above the line(even though there are lines that stray away from this):
0// Ok for file to not exist
1if (!fs::exists(path)) return true;
Also consider adding a comment to the change on line 69 to make it more clear (although the error message is kind of self explanatory)
tACK 2b071265c37da22f15769945fd159b50a14792a3
Tested on macOS v11.4
Can confirm patch works as advertised in the op, running bitcoin-qt
reports a file permission error if settings.json
can’t be read instead of overwriting the file (as is currently done on master
).
On master
Running bitcoin-qt
after changing the file ownership & permissions of settings.json
to root
& -rwx------
(via chmod 700 settings.json
) respectively, opens without an error and overwrites settings.json
.
After patch
However, running bitcoin-qt
after changing file ownership to root
and file permissions to -rwx------
, the permissions error is reported, and the file is no longer overwritten:
tACK 2b071265c37da22f15769945fd159b50a14792a3 Compiled and Tested on Ubuntu 20.04
This PR adds the functionality to display an error message when the settings.json file exists but could not be read instead of simply going forward and overwriting it. This is done by returning False result (instead of True, as done by the master) when the file is there but could not be opened. Tested the PR by navigating to the .bitcoin folder followed by sudo chmod root:root settings.json
and sudo chmod a-rwx settings.json
as suggested here. The PR is successful in doing what is said in the description.
Here is the screenshot of the successful working of the PR.
ON MASTER (after running src/qt/bitcoin-qt)
ON PR (after running src/qt/bitcoin-qt)
I agree with the changes suggested by op. Because it might happen that the user might have added some custom changes to the settings.json file, the user could lose those changes if, for some reason, the file was not able to be read at the time of starting of bitcoin-qt. Also, the user should be aware of when the Client opened the settings file correctly and when it was overwritten because the settings file was not read. This PR can tackle both of these issues.
tACK https://github.com/bitcoin/bitcoin/pull/22591/commits/2b071265c37da22f15769945fd159b50a14792a3
Tried on Windows because normally such things behave differently in Windows resulting in some issues. Changed the permissions for settings.json file and tried opening bitcoin-qt
.
Master branch:
This created a file: settings.json.tmp
:
PR branch:
nit: Couldn’t find anything in debug.log. Maybe we can print this error in logs as well. Or do we require -debug=something
for it?
Same error is printed if I try to run bitcoind
:
0Error: Failed loading settings file:
1- E:\regtest\settings.json. Please check permissions.
Also tried running bitcoind
and bitcoin-qt
with file open in other programs (editors, defender etc.) but didn’t see any errors or unexpected behavior.
I am not sure if there can be more reasons for is_open()
returning false. ‘Please check permissions’ looks good enough for now as a hint in the error or most likely reason for the error.
65+
66 fsbridge::ifstream file;
67 file.open(path);
68- if (!file.is_open()) return true; // Ok for file not to exist.
69+ if (!file.is_open()) {
70+ errors.emplace_back(strprintf("%s. Please check permissions.", path.string()));
In commit “error if settings.json exists, but is unreadable” (2b071265c37da22f15769945fd159b50a14792a3)
Indent should be 4 instead of 2 spaces
Also this text is pretty inconsistent with the other errors below. The other errors mention what problem happened at the path, while this just prints the path with no description of the failure, only a request to check permissions. I didn’t really follow the previous review discussion, but for clarity and consistency with other errors below I’d consider changing to something like:
0errors.emplace_back(strprintf("Failed to open settings file %s for reading (may be inaccessible due to permissions)", path.string()));
Following the comment above, I was curious to see how the two PRs would look once both are merged. So I decided to rebase this PR atop https://github.com/bitcoin-core/gui/pull/379 and test.
I also tested with @ryanofsky’s suggestion above (result added below). After seeing the errors side-by-side, @tylerchambers I’ll also recommend considering including the suggestion.
Tested on macOS v11.4
As I did in my previous test, I ran bitcoin-qt
after changing file ownership to root
and file permissions to -rwx------
, then got the following cli error and GUI prompt dialog:
0$ src/qt/bitcoin-qt
1Error: Settings file could not be read:
2- /Users/abubakar/Library/Application Support/Bitcoin/settings.json. Please check permissions.
Note: Now the settings.json
is only overwritten if the user explicitly clicks reset
, else it remains untouched. IMHO, the new GUI prompt is a lot sleeker and less noisy.
After opening the details:
0$ src/qt/bitcoin-qt
1Error: Settings file could not be read:
2- Failed to open settings file /Users/abubakar/Library/Application Support/Bitcoin/settings.json for reading (may be inaccessible due to permissions)
I am really glad to see how these two PRs work well to tackle this issue! 🍾
ACK 2b071265c37da22f15769945fd159b50a14792a3 📁
Tested by first removing permissions via chmod 000 ~/.bitcoin/settings.json
and then starting bitcoind
. In master, the file is overwritten, in the PR branch, the introduced error message appears and bitcoind exits with error code 1.