-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Drop StripRedundantLastElementsOfPath() function #24265
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
Conversation
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. |
Thanks for your suggestions! Most of them are implemented.
Will add in the morning :) |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, 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. |
Updated e69ab37 -> 05c8d8c (pr24265.02 -> pr24265.03):
|
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 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? |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 05c8d8c. This looks great, and I really like the way you broke it up into isolated commits. Make this very easy to review.
re: #24265 (comment)
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
@@ -159,6 +159,98 @@ BOOST_AUTO_TEST_CASE(intarg) | |||
BOOST_CHECK_EQUAL(m_local_args.GetIntArg("-bar", 11), 0); | |||
} | |||
|
|||
BOOST_AUTO_TEST_CASE(patharg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "util: Add ArgsManager::GetPathArg() function" (d32f6c0)
Nice tests!
Agree. |
Makes sense. Created a draft PR #24274 which builds on top of this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 05c8d8c
This is "refactoring"? |
There are some changes, I assume they are nice, in behavior. For example, the following works:
|
Concept ACK. |
Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
Rebased 05c8d8c -> ebda2b8 (pr24265.03 -> pr24265.04) due to the conflict with #24266. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK ebda2b8. Only change since last review is rebase which simplified the last commit
Since this PR has been merged to master, I rebased #24274 and marked it as ready for review. |
ebda2b8 util: Drop no longer needed StripRedundantLastElementsOfPath() function (Hennadii Stepanov) ecd094e Use ArgsManager::GetPathArg() for "-walletdir" option (Hennadii Stepanov) 06fed4c Use ArgsManager::GetPathArg() for "-blocksdir" option (Hennadii Stepanov) 15b632b Use ArgsManager::GetPathArg() for "-datadir" option (Hennadii Stepanov) 540ca51 util: Add ArgsManager::GetPathArg() function (Hennadii Stepanov) Pull request description: [Switching](bitcoin#20744) to `std::filesystems` makes possible to leverage [`std::filesystem::path::lexically_normal`](https://en.cppreference.com/w/cpp/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. ACKs for top commit: ryanofsky: Code review ACK ebda2b8. Only change since last review is rebase which simplified the last commit Tree-SHA512: ed86959b6038b7152b5a1d473478667b72caab1716cc9149e1a75833d50511f22157e4e5e55a9465d1fa76b90bce5e5286f4e4f0d1ae65ebd9c012fae19d835f
60aa179 Use GetPathArg where possible (Pavol Rusnak) 5b946ed util, refactor: Use GetPathArg to read "-settings" value (Ryan Ofsky) 687e655 util: Add GetPathArg default path argument (Ryan Ofsky) Pull request description: Improve `ArgsManager::GetPathArg` method added in recent PR #24265, so it is usable more places. This PR starts to use it for the `-settings` option. This can also be helpful for #24274 which is parsing more path options. - Add `GetPathArg` default argument so it is less awkward to use to parse options that have default values. - Fix `GetPathArg` negated argument handling. Return path{} not path{"0"} when path argument is negated. - Add unit tests for default and negated cases - Move `GetPathArg` method declaration next to `GetArg` declaration. The two methods are close substitutes for each, so this should help keep them consistent and make them more discoverable. ACKs for top commit: w0xlt: Tested ACK 60aa179 on Ubuntu 21.10 hebasto: re-ACK 60aa179 Tree-SHA512: 3d24b885d8bbeef39ea5d0556e2f09b9e5f4a21179cef11cbbbc1b84da29c8fb66ba698889054ce28d80bc25926687654c8532ed46054bf5b2dd1837866bd1cd
…idely b01f336 util, refactor: Drop explicit conversion to fs::path (Hennadii Stepanov) 138c668 util, refactor: Use GetPathArg to read "-rpccookiefile" value (Hennadii Stepanov) 1276090 util, refactor: Use GetPathArg to read "-conf" value (Hennadii Stepanov) Pull request description: This PR is a continuation of bitcoin/bitcoin#24265 and bitcoin/bitcoin#24306. Now the following command-line arguments / configure options been read with the `GetPathArg` method: - `-conf`, also `includeconf` values been normalized - `-rpccookiefile` ACKs for top commit: jarolrod: Code Review ACK b01f336 ryanofsky: Code review ACK b01f336. Changes since last review: just dropping first commit (NormalizedPathFromString) as suggested Tree-SHA512: 2d26d50b73542acdbcc63a32068977b2a49a017d31ca337471a0446f964eb0a6e3e4e3bb1ebe6771566a260f2cae3bc2ebe93b4b523183cea0d51768daab85c9
Switching to
std::filesystems
makes possible to leveragestd::filesystem::path::lexically_normal
and get rid of uglyStripRedundantLastElementsOfPath()
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.