Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for datadir after the config files were read #13621

Closed

Conversation

Flowdalic
Copy link
Contributor

@Flowdalic Flowdalic commented Jul 10, 2018

If we first check for datadir existence and then read the config file
we bail out in situations where the default datadir is invalid,
e.g. because of an non-existing HOME, but the datadir specified in
config files is valid. We should not do that, as the user may specified a
valid datadir in the config file and expects the setting to take effect.

See for example #12255 (comment) where users hit this.

@fanquake fanquake changed the title Check for datadir after the config files where read Check for datadir after the config files were read Jul 10, 2018
@Flowdalic Flowdalic force-pushed the init-swap-datadir-readconf branch 2 times, most recently from 6bcdbd3 to 94a038b Compare July 10, 2018 07:30
@maflcko
Copy link
Member

maflcko commented Jul 11, 2018

Doesn't the location of the default config file depend on the datadir?

@Flowdalic
Copy link
Contributor Author

Flowdalic commented Jul 11, 2018

Doesn't the location of the default config file depend on the datadir?

Yes, BITCOIN_CONF_FILENAME, which is set to bitcoin.conf is feed into AbsPathForConfigVal() which uses the datadir setting. However, if no datadir is explicitly set it defaults to "". I think that this means that bitcoin.conf is opened relative to the processe's working directory.

It appears that you have a specific problem in mind, which I don't see. Care to elaborate?

@maflcko
Copy link
Member

maflcko commented Jul 11, 2018

@AtsukiTak
Copy link
Contributor

Now the second if clause seems meaningless.

https://github.com/Flowdalic/bitcoin/blob/94a038b8694229c0153ede48a53701424ca51e38/src/bitcoind.cpp#L98-L102

Because it already checked in ReadConfigFiles.

bitcoin/src/util.cpp

Lines 909 to 912 in 4a7e64f

if (!fs::is_directory(GetDataDir(false))) {
error = strprintf("specified data directory \"%s\" does not exist.", gArgs.GetArg("-datadir", "").c_str());
return false;
}

@Flowdalic
Copy link
Contributor Author

Right, the check can get probably removed.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 17, 2018

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Flowdalic Flowdalic force-pushed the init-swap-datadir-readconf branch 6 times, most recently from be2ba85 to 9b1987f Compare July 19, 2018 15:47
As the real datadir may be set in the configuration file, this would
case the default datadir to get created.
If datadir is not a directory, do not fallback to empty path as this would shadow the original path set in the config file. Consider

    bitcoin.conf:
    datadir=/does/not/exist

and bitcoind started with "-conf=bitcoin.conf", which would previusly yield

    Error: Specified data directory "" does not exist.

when it should state

    Error: Specified data directory "/does/not/exist" does not exist.
If we first check for datadir existence and then read the config file
we bail out in situations where the default datadir is invalid,
e.g. because of an non-existing HOME, but the datadir specified in
config files is valid. We should not do that, as the user specified a
valid datadir and expects the setting to take effect.

Also remove datadir check in ReadConfigFiles() which now became
superfluous: We check for the datadir in AppInit() already, there is
no need to check also in ReadConfigFiles().

Furthermore re-enable the disabled tests in feature_config_args.py,
which where disabled in fabe28a ("qa: Temporarily disable test
that reads the default datadir location"), as those tests do not touch
"$HOME/.bitcoin" any more.
@Flowdalic
Copy link
Contributor Author

Rebased. Also fixed the cause and re-enabled the tests which were disabled in #13687 with fabe28a.

I had a hard time working on the argument and configuration file parsing code. It is a little bit spagettish and some parts are messy.

I used default argument values in order to optionally change GetDataDir() to not create the data directory, which, I assumed, made the change less intrusive. But I am happy to change it towards a GetDataDir() / GetDataDirAndCreateIfNecessary(). I am undecied which of those two approaches would be better.

if (!gArgs.ReadConfigFiles(error, true)) {
fprintf(stderr, "Error reading configuration file: %s\n", error.c_str());
return EXIT_FAILURE;
}
if (!fs::is_directory(GetDataDir(false))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems never fail unless user specify unpermitted path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It fails if the specified path is not a directory. I think that is sensible and was already previous behavior. Or do have a specific change request?

Copy link
Contributor

Choose a reason for hiding this comment

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

If I specify a file, boost::filesystem::create_directory throw an error.
If I specify a non-exist path, a new directory is created and no error happens.

I think you just missed second argument; GetDataDir(false, false) might be what you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for my poor English. Please feel free to ask me if my English is not clear.

@AtsukiTak
Copy link
Contributor

Nice work :)
But this PR seems to contains lots of changes for me such as

  • Check for datadir after the config files were read
  • Create directory if user specify non exist directory
  • Do not fallback to empy path in GetDataDir()

I think you should leave only first one, and move rests to another PR, and then discuss about them.

@AtsukiTak
Copy link
Contributor

I don't feel like that this patch series is worth splitting up, as it is pretty small

I see. So please don't care about that. That was just newbie's nonsense comment.

@leishman
Copy link
Contributor

I don't think our PRs conflict, but just wanted to make you aware of a minor change I'm putting in the logging behavior: #14057

I'm also working on logic to create a bitcoin.conf.example file: #13761

@Flowdalic
Copy link
Contributor Author

Flowdalic commented Sep 7, 2018

Pinging @MarcoFalke since this PR re-enables tests he disabled in fabe28a (#13687). Would be great to hear your thoughts on this.

@maflcko
Copy link
Member

maflcko commented Sep 7, 2018

I haven't looked at the most recent code, but I still think that the location of the default config file depends on the datadir? See https://dev.visucore.com/bitcoin/doxygen/class_args_manager.html#aec848fd54ace0df3df48dac446feb7d2 which calls GetDataDir.

So you are changing how and what config files are read. Not sure if we want this behavior change without discussion first.

@maflcko
Copy link
Member

maflcko commented Sep 7, 2018

Also, you modified how bitcoind and bitcoin-cli work, but not the gui. Wouldn't that lead to issues when the gui is used as rpc server via the bitcoin-cli?

So if you really want to go ahead with these changes, they need tests that fail on master but pass with your fixes for bitcoind as well as the bitcoin-qt and bitcoin-cli.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 8, 2018

Needs rebase

@fanquake
Copy link
Member

fanquake commented Jan 5, 2019

Closing "up for grabs".

@fanquake fanquake closed this Jan 5, 2019
@hebasto hebasto mentioned this pull request Apr 21, 2019
maflcko pushed a commit that referenced this pull request Aug 19, 2019
ffea41f Enable all tests in feature_config_args.py (Hennadii Stepanov)
66f5c17 Use CheckDataDirOption() for code uniformity (Hennadii Stepanov)
7e33a18 Fix datadir handling in bitcoin-cli (Hennadii Stepanov)
b28dada Fix datadir handling in bitcoin-qt (Hennadii Stepanov)
5082409 Fix datadir handling in bitcoind (Hennadii Stepanov)
740d41c Add CheckDataDirOption() function (Hennadii Stepanov)
c1f3251 Return absolute path early in AbsPathForConfigVal (Hennadii Stepanov)

Pull request description:

  Fix #15240, see: #15240 (comment)
  Fix #15745
  Fix broken `feature_config_args.py` tests (disabled by MarcoFalke@fabe28a0cdcfa13e0e595a0905e3642a960d3077). All test are enabled now.
  This PR is alternative to #13621.

  User's `$HOME` directory is not touched unnecessarily now.

  ~To make reviewing easier only `bitcoind` code is modified (neither `bitcoin-cli` nor `bitcoin-qt`).~

  Refs:
  - #15745 (comment) by **laanwj**
  - #16220

Top commit has no ACKs.

Tree-SHA512: 4a4cda10e0b67c8f374da0c9567003d2b566d948e7f8550fe246868b5794c15010e88ea206009480b9cd2f737f310a15e984f920730448f99a895893bed351df
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 19, 2019
ffea41f Enable all tests in feature_config_args.py (Hennadii Stepanov)
66f5c17 Use CheckDataDirOption() for code uniformity (Hennadii Stepanov)
7e33a18 Fix datadir handling in bitcoin-cli (Hennadii Stepanov)
b28dada Fix datadir handling in bitcoin-qt (Hennadii Stepanov)
5082409 Fix datadir handling in bitcoind (Hennadii Stepanov)
740d41c Add CheckDataDirOption() function (Hennadii Stepanov)
c1f3251 Return absolute path early in AbsPathForConfigVal (Hennadii Stepanov)

Pull request description:

  Fix bitcoin#15240, see: bitcoin#15240 (comment)
  Fix bitcoin#15745
  Fix broken `feature_config_args.py` tests (disabled by MarcoFalke@fabe28a0cdcfa13e0e595a0905e3642a960d3077). All test are enabled now.
  This PR is alternative to bitcoin#13621.

  User's `$HOME` directory is not touched unnecessarily now.

  ~To make reviewing easier only `bitcoind` code is modified (neither `bitcoin-cli` nor `bitcoin-qt`).~

  Refs:
  - bitcoin#15745 (comment) by **laanwj**
  - bitcoin#16220

Top commit has no ACKs.

Tree-SHA512: 4a4cda10e0b67c8f374da0c9567003d2b566d948e7f8550fe246868b5794c15010e88ea206009480b9cd2f737f310a15e984f920730448f99a895893bed351df
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants