Drop StripRedundantLastElementsOfPath() function #24265

pull hebasto wants to merge 5 commits into bitcoin:master from hebasto:220204-norm changing 7 files +121 −26
  1. hebasto commented at 9:11 PM on February 4, 2022: member

    Switching to std::filesystems makes possible to leverage std::filesystem::path::lexically_normal and get rid of ugly StripRedundantLastElementsOfPath() crutch.

    To make its usage simple and error-proof, a new ArgsManager::GetPathArg() member function introduced which guarantees to return a normalized with no trailing slashes paths provided via -datadir, -blocksdir or -walletdir command-line arguments or configure options.

  2. ryanofsky commented at 9:25 PM on February 4, 2022: contributor

    Also it became possible to re-enable Windows tests which were disabled in #20744. So this PR, apparently, is an alternative to the #24251.

    This all sounds good. IMO, it would be good to merge #24251 before this PR. #24251 has two very simple fixes for the regressions caused by #20744 yesterday. This PR should be trivial to rebase if #24251 is merged first, and should become a little simpler, too.

  3. hebasto force-pushed on Feb 4, 2022
  4. DrahtBot added the label Utils/log/libs on Feb 4, 2022
  5. DrahtBot added the label Wallet on Feb 4, 2022
  6. in src/util/system.h:275 in 034f12a115 outdated
     270 | +     * It is guaranteed that the returned path has no trailing slashes.
     271 | +     *
     272 | +     * @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
     273 | +     * @return Normalized path which is get from a specified pathlike argument
     274 | +     */
     275 | +    fs::path GetNormalPath(std::string pathlike_arg) const;
    


    ryanofsky commented at 9:39 PM on February 4, 2022:

    In commit "util: Add ArgsManager::GetNormalPath() function" (034f12a115e21b4205b532a3f946b129e83505d9)

    Minor: Would vote for calling this GetPathArg to be consistent with GetIntArg GetBoolArg, instead of GetNormalPath


    hebasto commented at 10:36 PM on February 4, 2022:

    Thanks! Done.

  7. in src/util/system.cpp:408 in 034f12a115 outdated
     403 | +{
     404 | +    const std::string path_str{GetArg(pathlike_arg, "")};
     405 | +    if (path_str.empty()) return {};
     406 | +    // Adding a slash followed by parent_path() call
     407 | +    // guaranties no trailing slashes in the resulted path.
     408 | +    return fs::PathFromString(path_str + "/").lexically_normal().parent_path();
    


    ryanofsky commented at 9:46 PM on February 4, 2022:

    In commit "util: Add ArgsManager::GetNormalPath() function" (034f12a115e21b4205b532a3f946b129e83505d9)

    I think previous approach which avoids direct string manipulation and avoids assuming "/" is a path separator is a little cleaner. Would suggest something like:

    fs::path result = fs::PathFromString(path_str).lexically_normal(); // Collapse slashes and dots
    return result.has_filename() ? result : result.parent_path(); // Remove trailing slash, if present
    

    Also it would be great to write a simple unit test for this new function.


    hebasto commented at 1:22 PM on February 5, 2022:

    Thanks! Done.

  8. hebasto force-pushed on Feb 4, 2022
  9. hebasto commented at 10:35 PM on February 4, 2022: member

    @ryanofsky

    Thanks for your suggestions! Most of them are implemented.

    Also it would be great to write a simple unit test for this new function.

    Will add in the morning :)

  10. fanquake commented at 3:01 AM on February 5, 2022: member

    This all sounds good. IMO, it would be good to merge #24251 before this PR.

    I agree, we'll merge #24251 first. Maybe rebase on top?

  11. DrahtBot cross-referenced this on Feb 5, 2022 from issue util: if `DataDir` is a symbolic link, manually follow it by nymkappa
  12. DrahtBot cross-referenced this on Feb 5, 2022 from issue util: Avoid buggy std::filesystem:::create_directories() call by hebasto
  13. DrahtBot commented at 9:02 AM on February 5, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #24266 (util: Avoid buggy std::filesystem:::create_directories() call by hebasto)
    • #23732 (refactor: Remove gArgs from bdb.h and sqlite.h by kiminuo)
    • #17783 (util: Fix -norpcwhitelist, -norpcallowip, and similar corner case behavior by ryanofsky)
    • #17581 (refactor: Remove settings merge reverse precedence code by ryanofsky)
    • #17580 (refactor: Add ALLOW_LIST flags and enforce usage in CheckArgFlags by ryanofsky)
    • #17493 (util: Forbid ambiguous multiple assignments in config file by ryanofsky)
    • #16545 (refactor: Implement missing error checking for ArgsManager flags by ryanofsky)

    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.

  14. DrahtBot cross-referenced this on Feb 5, 2022 from issue Re-enable windows path tests disabled by #20744 by ryanofsky
  15. DrahtBot added the label Needs rebase on Feb 5, 2022
  16. DrahtBot cross-referenced this on Feb 5, 2022 from issue refactor: Remove `gArgs` from `bdb.h` and `sqlite.h` by kiminuo
  17. hebasto force-pushed on Feb 5, 2022
  18. hebasto commented at 1:21 PM on February 5, 2022: member

    Updated e69ab379283af554d60c484d5715112b887fa0c3 -> 05c8d8cd075c37f43ad195bceac3eaa1f2907e07 (pr24265.02 -> pr24265.03):

    • rebased on top of the #24251
    • unit test added
  19. DrahtBot removed the label Needs rebase on Feb 5, 2022
  20. prusnak commented at 4:43 PM on February 5, 2022: contributor

    There are couple of more places where we can do similar optimization, but for arguments which expect filename, not path (= directory).

    src/init.cpp:138:    return AbsPathForConfigVal(fs::PathFromString(args.GetArg("-pid", BITCOIN_PID_FILENAME)));
    src/init.cpp:1249:            fs::path asmap_path = fs::PathFromString(args.GetArg("-asmap", ""));
    src/init/common.cpp:84:    LogInstance().m_file_path = AbsPathForConfigVal(fs::PathFromString(args.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)));
    src/bench/bench_bitcoin.cpp:112:    args.output_csv = fs::PathFromString(argsman.GetArg("-output_csv", ""));
    src/bench/bench_bitcoin.cpp:113:    args.output_json = fs::PathFromString(argsman.GetArg("-output_json", ""));
    

    It would be possible to introduce a GetFileArg function using the following refactor and use that to simplify the lines above:

    diff --git a/src/util/system.cpp b/src/util/system.cpp
    index 496c82584..ef7c1cfff 100644
    --- a/src/util/system.cpp
    +++ b/src/util/system.cpp
    @@ -386,9 +386,14 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
         return std::nullopt;
     }
     
    -fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
    +fs::path ArgsManager::GetFileArg(std::string pathlike_arg, const std::string& strDefault = "") const
     {
    -    auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal();
    +    return fs::PathFromString(GetArg(pathlike_arg, strDefault)).lexically_normal();
    +}
    +
    +fs::path ArgsManager::GetPathArg(std::string pathlike_arg, const std::string& strDefault = "") const
    +{
    +    auto result = GetFileArg(pathlike_arg, strDefault);
         // Remove trailing slash, if present.
         return result.has_filename() ? result : result.parent_path();
     }
    

    What do you think?

  21. in src/test/getarg_tests.cpp:162 in d32f6c0dc8 outdated
     158 | @@ -159,6 +159,98 @@ BOOST_AUTO_TEST_CASE(intarg)
     159 |      BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0);
     160 |  }
     161 |  
     162 | +BOOST_AUTO_TEST_CASE(patharg)
    


    ryanofsky commented at 5:00 PM on February 5, 2022:

    In commit "util: Add ArgsManager::GetPathArg() function" (d32f6c0dc89f1a609d59a222b09eabb1eb0da542)

    Nice tests!

  22. ryanofsky commented at 5:42 PM on February 5, 2022: contributor

    There are couple of more places where we can do similar optimization, but for arguments which expect filename, not path (= directory).

    It would be possible to introduce a GetFileArg function using the following refactor and use that to simplify the lines above:

    I'd push back on these suggestions a little and save these things for followups. They would be natural extensions of this PR, but would risk unintentionally changing behavior and don't need to be part of it. Also a std::string default argument would pass more paths as strings, when I think paths should be represented by fs::path not std::string wherever possible.

  23. ryanofsky approved
  24. ryanofsky commented at 5:50 PM on February 5, 2022: contributor

    Code review ACK 05c8d8cd075c37f43ad195bceac3eaa1f2907e07. This looks great, and I really like the way you broke it up into isolated commits. Make this very easy to review.

    re: #24265#issue-1124619834

    Also it became possible to re-enable Windows tests which were disabled in #20744. So this PR, apparently, is an alternative to the #24251.

    This is a little out of date and could be removed from the description

  25. hebasto commented at 6:01 PM on February 5, 2022: member

    There are couple of more places where we can do similar optimization, but for arguments which expect filename, not path (= directory). It would be possible to introduce a GetFileArg function using the following refactor and use that to simplify the lines above:

    I'd push back on these suggestions a little and save these things for followups. They would be natural extensions of this PR, but would risk unintentionally changing behavior and don't need to be part of it. Also a std::string default argument would pass more paths as strings, when I think paths should be represented by fs::path not std::string wherever possible.

    Agree.

  26. hebasto commented at 6:06 PM on February 5, 2022: member

    Also it became possible to re-enable Windows tests which were disabled in #20744. So this PR, apparently, is an alternative to the #24251.

    This is a little out of date and could be removed from the description

    PR description has been updated.

  27. DrahtBot cross-referenced this on Feb 5, 2022 from issue util: Fix -norpcwhitelist, -norpcallowip, and similar corner case behavior by ryanofsky
  28. DrahtBot cross-referenced this on Feb 5, 2022 from issue refactor: Remove settings merge reverse precedence code by ryanofsky
  29. DrahtBot cross-referenced this on Feb 5, 2022 from issue refactor: Add ALLOW_LIST flags and enforce usage in CheckArgFlags by ryanofsky
  30. DrahtBot cross-referenced this on Feb 5, 2022 from issue util: Forbid ambiguous multiple assignments in config file by ryanofsky
  31. DrahtBot cross-referenced this on Feb 5, 2022 from issue refactor: Implement missing error checking for ArgsManager flags by ryanofsky
  32. prusnak cross-referenced this on Feb 5, 2022 from issue Introduce GetFileArg and use it where possible by prusnak
  33. prusnak commented at 6:40 PM on February 5, 2022: contributor

    I'd push back on these suggestions a little and save these things for followups.

    Makes sense. Created a draft PR #24274 which builds on top of this PR.

  34. hebasto renamed this:
    Drop StripRedundantLastElementsOfPath() function and re-enable Windows tests
    Drop StripRedundantLastElementsOfPath() function
    on Feb 5, 2022
  35. hebasto commented at 8:32 AM on February 6, 2022: member

    @prusnak

    I'd push back on these suggestions a little and save these things for followups.

    Makes sense. Created a draft PR #24274 which builds on top of this PR.

    Mind leaving (N)ACK review comment?

  36. in src/util/system.h:275 in 05c8d8cd07 outdated
     270 | +     * It is guaranteed that the returned path has no trailing slashes.
     271 | +     *
     272 | +     * @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
     273 | +     * @return Normalized path which is get from a specified pathlike argument
     274 | +     */
     275 | +    fs::path GetPathArg(std::string pathlike_arg) const;
    


    prusnak commented at 9:21 AM on February 6, 2022:

    Other Get...Arg methods use the camelCase notation for arguments. Let's change to pathlikeArg or similar.


    hebasto commented at 9:39 AM on February 6, 2022:

    For new code we strive to follow our symbol naming convention:

    Variable (including function arguments) and namespace names are all lowercase and may use _ to separate words (snake_case).

  37. in src/util/system.cpp:389 in 05c8d8cd07 outdated
     385 | @@ -399,6 +386,13 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
     386 |      return std::nullopt;
     387 |  }
     388 |  
     389 | +fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
    


    prusnak commented at 9:22 AM on February 6, 2022:

    Other Get...Arg methods use the camelCase notation for arguments. Let's change to pathlikeArg or similar.


    hebasto commented at 9:39 AM on February 6, 2022:
  38. prusnak approved
  39. prusnak commented at 9:52 AM on February 6, 2022: contributor

    ACK 05c8d8c

  40. MarcoFalke added this to the milestone 23.0 on Feb 6, 2022
  41. MarcoFalke removed this from the milestone 23.0 on Feb 6, 2022
  42. MarcoFalke commented at 9:57 AM on February 6, 2022: member

    This is "refactoring"?

  43. hebasto commented at 10:03 AM on February 6, 2022: member

    @MarcoFalke

    This is "refactoring"?

    There are some changes, I assume they are nice, in behavior. For example, the following works:

    $ ./src/bitcoind -datadir=/root/../home/hebasto/.bitcoin
    
  44. MarcoFalke removed the label Wallet on Feb 7, 2022
  45. DrahtBot added the label Needs rebase on Feb 8, 2022
  46. laanwj commented at 3:49 PM on February 9, 2022: member

    Concept ACK.

  47. util: Add ArgsManager::GetPathArg() function
    Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
    540ca5111f
  48. Use ArgsManager::GetPathArg() for "-datadir" option 15b632bf16
  49. Use ArgsManager::GetPathArg() for "-blocksdir" option 06fed4c21e
  50. Use ArgsManager::GetPathArg() for "-walletdir" option ecd094e2b1
  51. util: Drop no longer needed StripRedundantLastElementsOfPath() function ebda2b8c81
  52. hebasto force-pushed on Feb 9, 2022
  53. hebasto commented at 5:43 PM on February 9, 2022: member

    Rebased 05c8d8cd075c37f43ad195bceac3eaa1f2907e07 -> ebda2b8c819d989327c6b3e29237dfb43628e647 (pr24265.03 -> pr24265.04) due to the conflict with #24266.

  54. ryanofsky approved
  55. ryanofsky commented at 5:54 PM on February 9, 2022: contributor

    Code review ACK ebda2b8c819d989327c6b3e29237dfb43628e647. Only change since last review is rebase which simplified the last commit

  56. DrahtBot removed the label Needs rebase on Feb 9, 2022
  57. fanquake merged this on Feb 9, 2022
  58. fanquake closed this on Feb 9, 2022

  59. prusnak commented at 9:58 PM on February 9, 2022: contributor

    Since this PR has been merged to master, I rebased #24274 and marked it as ready for review.

  60. ryanofsky cross-referenced this on Feb 9, 2022 from issue util: Make ArgsManager::GetPathArg more widely usable by ryanofsky
  61. sidhujag referenced this in commit 5081a72c21 on Feb 10, 2022
  62. hebasto deleted the branch on Feb 10, 2022
  63. MarcoFalke referenced this in commit 6687bb24ae on Mar 7, 2022
  64. hebasto cross-referenced this on Mar 25, 2022 from issue util: Use ArgsManager::GetPathArg more widely by hebasto
  65. fanquake referenced this in commit e09ad284c7 on Aug 4, 2022
  66. bitcoin locked this on Feb 10, 2023

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: 2026-05-20 06:53 UTC