Fix datadir handling #15864

pull hebasto wants to merge 7 commits into bitcoin:master from hebasto:20190421-datadir-handling changing 7 files +26 −18
  1. hebasto commented at 8:44 pm on April 21, 2019: member

    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:

  2. fanquake added the label Utils/log/libs on Apr 21, 2019
  3. in src/util/system.cpp:793 in e7bf2899ce outdated
    788@@ -784,7 +789,8 @@ void ClearDatadirCache()
    789 
    790 fs::path GetConfigFile(const std::string& confPath)
    791 {
    792-    return AbsPathForConfigVal(fs::path(confPath), false);
    793+    fs::path path = fs::path(confPath);
    794+    return path.is_absolute() ? path : AbsPathForConfigVal(path, false);
    


    promag commented at 10:45 pm on April 21, 2019:
    Is this necessary? Looks like fs::absolute already does this in AbsPathForConfigVal.

    hebasto commented at 4:12 am on April 22, 2019:
    Yes, it is needed to not touch default datadir unnecessarily, before all configs are read.

    ryanofsky commented at 3:06 pm on April 29, 2019:

    It seems like a better fix (simpler and more general) would be to add:

    0if (path.is_absolute()) return path;
    

    as the first line of AbsPathForConfigVal.

  4. in src/util/system.cpp:778 in e7bf2899ce outdated
    772@@ -773,6 +773,11 @@ const fs::path &GetDataDir(bool fNetSpecific)
    773     return path;
    774 }
    775 
    776+bool CheckDataDirOption()
    777+{
    778+    return gArgs.IsArgSet("-datadir") ? fs::is_directory(fs::system_complete(gArgs.GetArg("-datadir", ""))) : true;
    


    promag commented at 10:49 pm on April 21, 2019:
    This doesn’t check the default data dir.

    hebasto commented at 4:14 am on April 22, 2019:
    You are right. This behavior is intentional.

    ryanofsky commented at 6:50 pm on May 7, 2019:

    You are right. This behavior is intentional.

    Would be helpful to say what the function is supposed to do in the header. Maybe

    0//! Return true if -datadir value points to a valid directory or was not specified.
    

    It might be also be less confusing to write this as:

    0return !gArgs.IsArgSet("-datadir") || fs::is_directory(fs::system_complete(gArgs.GetArg("-datadir", "")))
    

    ryanofsky commented at 7:01 pm on May 7, 2019:

    In commit “Add CheckDataDirOption() function” (a45f692366d7bb4814af83e3b645c9e5a5a734e6)

    I don’t think IsArgSet is the right function to call here. If somebody has a datadir option specified in the config file, but wants to unset it on the command line by passing -nodatadir or -datadir="", it seems like this should be allowed. Would suggest:

    0std::string datadir = gArgs.GetArg("-datadir", "");
    1return datadir.empty() || fs::is_directory(fs::system_complete(datadir));
    

    And a corresponding change in GetDataDir()


    hebasto commented at 4:14 pm on May 9, 2019:

    I don’t think IsArgSet is the right function to call here. If somebody has a datadir option specified in the config file, but wants to unset it on the command line by passing -nodatadir or -datadir="", it seems like this should be allowed.

    Good catch! Agree.

  5. hebasto commented at 4:19 am on April 22, 2019: member
    @promag Thank you for your review. To be clear, the premature address to the default datadir, until possible -datadir option is read from the config file(s), is the root of all issues fixed by this PR.
  6. promag commented at 10:01 am on April 22, 2019: member

    On my system:

     0# create a file in the default data dir
     1touch "/Users/joao/Library/Application Support/Bitcoin"
     2
     3# launch with default data dir
     4src/bitcoind
     5
     6
     7************************
     8EXCEPTION: N5boost10filesystem16filesystem_errorE
     9boost::filesystem::create_directory: File exists: "/Users/joao/Library/Application Support/Bitcoin"
    10bitcoin in AppInit()
    11
    12Assertion failed: (globalChainBaseParams), function BaseParams, file chainparamsbase.cpp, line 30.
    13[1]    53221 abort      src/bitcoind
    
  7. hebasto commented at 5:38 pm on April 22, 2019: member

    @promag

    On my system:

     0# create a file in the default data dir
     1touch "/Users/joao/Library/Application Support/Bitcoin"
     2
     3# launch with default data dir
     4src/bitcoind
     5
     6
     7************************
     8EXCEPTION: N5boost10filesystem16filesystem_errorE
     9boost::filesystem::create_directory: File exists: "/Users/joao/Library/Application Support/Bitcoin"
    10bitcoin in AppInit()
    11
    12Assertion failed: (globalChainBaseParams), function BaseParams, file chainparamsbase.cpp, line 30.
    13[1]    53221 abort      src/bitcoind
    

    The same behavior is observed on master as well. This PR does not change it.

  8. DrahtBot commented at 11:47 am on April 28, 2019: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #16224 (gui: Bilingual GUI error messages by hebasto)

    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.

  9. ryanofsky commented at 3:09 pm on April 29, 2019: member

    This comment doesn’t seem to be true: #15864#issue-272240173

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

    Since GetConfigFile is called all three places. It would also seem worrying if this were true, since it would mean the different binaries wouldn’t behave consistently.

  10. hebasto force-pushed on Apr 29, 2019
  11. hebasto commented at 8:58 pm on April 29, 2019: member

    @ryanofsky Thank you for your review. Your comment has been addressed. Also commits for bitcoin-qt and tools (bitcoin-cli, bitcoin-wallet) have been added.

    Would you mind re-reviewing?

  12. in src/util/system.cpp:1212 in f46eec4f80 outdated
    1225@@ -1226,6 +1226,9 @@ int64_t GetStartupTime()
    1226 
    1227 fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
    1228 {
    1229+    if (path.is_absolute()) {
    


    ryanofsky commented at 6:43 pm on May 7, 2019:

    In commit “Return absolute path early in AbsPathForConfigVal” (f46eec4f80117ce242ed8c86b6216ece3eb20fba)

    It would be helpful if the commit message said why this change was being made. Is this an optimization or is it preventing an error or does it have some other effect on behavior?


    hebasto commented at 3:48 pm on May 9, 2019:
    Fixed.
  13. in src/bitcoind.cpp:95 in 160470f76f outdated
     94@@ -95,8 +95,7 @@ static bool AppInit(int argc, char* argv[])
     95 
     96     try
     97     {
     98-        if (!fs::is_directory(GetDataDir(false)))
     99-        {
    100+        if (!CheckDataDirOption()) {
    


    ryanofsky commented at 7:11 pm on May 7, 2019:

    In commit “Fix datadir handling in bitcoind” (160470f76f22edd1f9ef6448584381fc5626b9c4)

    I can’t figure out what this commit is doing. Is it fixing #15745 or #15864? Is the idea to prevent the datadir from being cached here? Can you write in the commit description what problem this is fixing and how it works?


    hebasto commented at 6:51 pm on July 5, 2019:
    Please see #15864 (comment)
  14. ryanofsky approved
  15. ryanofsky commented at 7:46 pm on May 7, 2019: member

    utACK c4ab7420ef5dc4e6925519f8571a936f38720862. It’s not really clear to me which bugs are being fixed by these changes, and how exactly the fixes work, but the basic idea seems to be to avoid calling GetDataDir() various places early on startup when it isn’t necessary, to avoid caching bad values.

    The changes seem worth making as optimizations or for clarity regardless of the bugs. But it’d also make sense as followup to look into:

    • Whether caching datadir is a useful optimization
    • Whether it makes sense to interpret a non-absolute “-conf” argument relative to the datadir

    Both of these seem questionable to begin with.

  16. hebasto commented at 3:30 pm on May 9, 2019: member

    @ryanofsky

    It’s not really clear to me which bugs are being fixed by these changes, and how exactly the fixes work, but the basic idea seems to be to avoid calling GetDataDir() various places early on startup when it isn’t necessary, to avoid caching bad values.

    The root of the reported bugs (#15240, #15745) is GetDataDir() tries to create data directory. This step is premature when config file has not been read yet.

  17. hebasto force-pushed on May 9, 2019
  18. promag commented at 4:35 pm on May 9, 2019: member
    I think we could replace the directory creation with an assertion (that datadir already exists) and create the directory right before the first call to GetDataDir() - probably on init.
  19. hebasto force-pushed on May 9, 2019
  20. hebasto commented at 6:23 pm on May 9, 2019: member

    @promag

    I think we could replace the directory creation with an assertion (that datadir already exists) and create the directory right before the first call to GetDataDir() - probably on init.

    Is it a bit out of the scope of this PR?

  21. hebasto commented at 6:24 pm on May 9, 2019: member
    @ryanofsky All your comments have been addressed.
  22. in src/util/system.cpp:744 in 5df1581b9e outdated
    752@@ -753,8 +753,9 @@ const fs::path &GetDataDir(bool fNetSpecific)
    753     if (!path.empty())
    754         return path;
    755 
    756-    if (gArgs.IsArgSet("-datadir")) {
    757-        path = fs::system_complete(gArgs.GetArg("-datadir", ""));
    758+    std::string datadir = gArgs.GetArg("-datadir", "");
    


    ryanofsky commented at 7:37 pm on May 9, 2019:

    In commit “Add CheckDataDirOption() function” (5df1581b9ee6f13593b33fa9629ea9fbadcad732)

    Ideally commit message might also mention that it changes the behavior of the GetDataDir function. The effect I think is to now allow -nodatadir or -datadir='' options on the command line to override any datadir specified in the config file and reset it to default. In theory this could even be mentioned in release notes, but it’s probably too niche.


    hebasto commented at 9:31 pm on May 26, 2019:

    The effect I think is to now allow -nodatadir or -datadir='' options on the command line to override any datadir specified in the config file and reset it to default.

    Unfortunately, neither -nodatadir nor -datadir='' on the command line cannot unset datadir specified in the config file or reset it to default. See: #16097.


    MarcoFalke commented at 10:56 pm on July 2, 2019:
    Then, why is this changed?

    hebasto commented at 7:57 pm on July 5, 2019:

    The initial idea was:

    I don’t think IsArgSet is the right function to call here.

    If we recall: https://github.com/bitcoin/bitcoin/blob/8c69fae94410f54bad13be0f34d54370fddbf4b3/src/util/system.cpp#L470-L474

    using IsArgNegated is (logically) inappropriate for -datadir option, IMO.

    Also (but minor), the suggested code uses only one function call (GetArg) instead of two calls (IsArgSet and GetArg).


    hebasto commented at 4:30 pm on July 24, 2019:

    @ryanofsky @MarcoFalke

    The effect I think is to now allow -nodatadir or -datadir='' options on the command line to override any datadir specified in the config file and reset it to default.

    Actually, with this PR -datadir='' (with empty string) or -datadir will reset it to default. So, going to close #16416 in favor of this PR.

  23. ryanofsky approved
  24. ryanofsky commented at 7:51 pm on May 9, 2019: member

    utACK 9bddf12763e1bcc41c176f8b7f0b883efed49823. Changes since last review are adding commit descriptions, documenting new CheckDataDirOption() method, and handling -nodatadir and -datadir="" better.

    This PR should be tagged as needing release notes if release notes aren’t added before it is merged in a doc/release-notes-15864.md file. I think you could take the description from #15864 (comment) and basically turn it into a release note, describing the class of bugs that are fixed and not mentioning specific function names.

  25. hebasto commented at 10:57 pm on June 16, 2019: member
    @MarcoFalke would you mind reviewing this PR?
  26. hebasto force-pushed on Jun 17, 2019
  27. hebasto commented at 3:56 pm on June 17, 2019: member
    Rebased after #16205 merged.
  28. MarcoFalke commented at 5:18 pm on June 17, 2019: member
    Concept ACK
  29. ryanofsky approved
  30. ryanofsky commented at 6:44 pm on June 27, 2019: member
    utACK 4f0de6214ae591bfb37ef745140f45fcc97aec33. No changes since last review except rebase and replacing fprintf with tfm::format.
  31. DrahtBot added the label Needs rebase on Jun 28, 2019
  32. hebasto force-pushed on Jun 28, 2019
  33. hebasto commented at 7:36 pm on June 28, 2019: member
    Rebased on top of #16300.
  34. in src/util/system.cpp:1214 in c21150fd6f outdated
    1198@@ -1199,6 +1199,9 @@ int64_t GetStartupTime()
    1199 
    1200 fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
    1201 {
    1202+    if (path.is_absolute()) {
    1203+        return path;
    1204+    }
    


    MarcoFalke commented at 7:55 pm on June 28, 2019:
    Would be nice if each commit had a test that failed before compilation and passes after. That might simplify review, no?

    hebasto commented at 3:33 am on June 29, 2019:
    Do tests enabled in 5124b1cef24a9b30876ddedf922dfdb555bd4ace do this job?

    MarcoFalke commented at 6:38 pm on July 8, 2019:
    Can they be enabled in this commit (the first commit)?

    hebasto commented at 7:16 pm on July 8, 2019:

    Do you mean to combine the “Enable all tests in feature_config_args.py " commit (5124b1cef24a9b30876ddedf922dfdb555bd4ace) into the “Return absolute path early in AbsPathForConfigVal” commit (c21150fd6f598196fae93cf4bddc6e7625a43660)?

    Master branch plus this combined commit will fail the test. And the test will be passed on top of the 584621f9a81b6cc547c8f1365f1b358866e9a335 and b921f6ee951f0ad6d87eeeb372136a7314af0b0e commits.

    Did I understand your intention correctly?

  35. DrahtBot removed the label Needs rebase on Jun 28, 2019
  36. ryanofsky approved
  37. ryanofsky commented at 6:35 pm on July 8, 2019: member
    utACK 5f55360ea36487131a14dbc0a2518ec3babc1580. No changes since last review other than rebase and comment tweak.
  38. DrahtBot added the label Needs rebase on Jul 8, 2019
  39. hebasto force-pushed on Jul 10, 2019
  40. hebasto commented at 5:03 pm on July 10, 2019: member
    Rebased.
  41. DrahtBot removed the label Needs rebase on Jul 10, 2019
  42. hebasto commented at 12:52 pm on July 13, 2019: member
    @laanwj Would you mind reviewing this PR?
  43. ryanofsky approved
  44. ryanofsky commented at 5:30 pm on July 15, 2019: member
    utACK fd7310f24422901e0d4cf82c93f04b2b1b458865. No changes since last review other than rebase after #16291
  45. MarcoFalke commented at 10:50 pm on July 23, 2019: member
    The 09a307183aca7b2f6e1a14caf4430a1fc546e704 commit would have to be in the test commit or before it, otherwise the test running with the gui might fail on the test commit?
  46. hebasto force-pushed on Jul 23, 2019
  47. hebasto force-pushed on Jul 24, 2019
  48. hebasto force-pushed on Jul 24, 2019
  49. hebasto force-pushed on Jul 24, 2019
  50. hebasto force-pushed on Jul 24, 2019
  51. hebasto force-pushed on Jul 24, 2019
  52. DrahtBot added the label Needs rebase on Jul 24, 2019
  53. hebasto renamed this:
    Fix datadir handling
    WIP: Fix datadir handling
    on Jul 24, 2019
  54. Return absolute path early in AbsPathForConfigVal
    This prevents premature GetDataDir() calls, e.g., when config file is
    not read yet.
    c1f325126c
  55. Add CheckDataDirOption() function 740d41ce9f
  56. Fix datadir handling in bitcoind
    This prevents premature tries to access or create the default datadir.
    This is useful when the -datadir option is specified and the default
    datadir is unreachable.
    50824093bb
  57. hebasto force-pushed on Jul 24, 2019
  58. hebasto renamed this:
    WIP: Fix datadir handling
    Fix datadir handling
    on Jul 24, 2019
  59. Fix datadir handling in bitcoin-qt
    This prevents premature tries to access or create the default datadir.
    This is useful when the -datadir option is specified and the default
    datadir is unreachable.
    b28dada374
  60. Fix datadir handling in bitcoin-cli
    This prevents premature tries to access or create the default datadir.
    This is useful when the -datadir option is specified and the default
    datadir is unreachable.
    7e33a18a34
  61. Use CheckDataDirOption() for code uniformity
    All other clients and tools use CheckDataDirOption() rather
    fs::is_directory(GetDataDir(false)) for the first datadir check.
    66f5c17f8a
  62. hebasto commented at 4:11 pm on July 24, 2019: member

    Rebased. @MarcoFalke

    The 09a3071 commit would have to be in the test commit or before it, otherwise the test running with the gui might fail on the test commit?

    Commits have been rearranged. The test commit is the last one now.

  63. Enable all tests in feature_config_args.py ffea41f530
  64. hebasto force-pushed on Jul 24, 2019
  65. DrahtBot removed the label Needs rebase on Jul 24, 2019
  66. MarcoFalke added this to the milestone 0.19.0 on Aug 16, 2019
  67. MarcoFalke referenced this in commit 83112db129 on Aug 19, 2019
  68. MarcoFalke merged this on Aug 19, 2019
  69. MarcoFalke closed this on Aug 19, 2019

  70. sidhujag referenced this in commit 894dd2f309 on Aug 19, 2019
  71. hebasto deleted the branch on Aug 19, 2019
  72. hebasto commented at 1:52 pm on October 17, 2019: member

    @MarcoFalke #15240 (comment)

    Label “Needs backport 0.18” ?

  73. fanquake added the label Needs backport (0.18) on Oct 17, 2019
  74. fanquake removed the label Needs backport (0.18) on Mar 13, 2020
  75. deadalnix referenced this in commit c5a632bec6 on Apr 22, 2020
  76. deadalnix referenced this in commit 68cb14deaa on Apr 23, 2020
  77. furszy referenced this in commit 7b265f22e8 on Jan 20, 2022
  78. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-21 15:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me