Skip to content

Commit

Permalink
args: Properly support -noconf and catch directories
Browse files Browse the repository at this point in the history
peek() in order to catch directories being specified instead of configuration files. Previously passing a directory path as -conf would lead to an ifstream being opened for it, and would not trigger any errors.

-noconf would previously lead to an ifstream "successfully" being opened to the ".bitcoin"-directory (not a file)). With the change to AbsPathForConfigVal we will now try to open the marginally more correct absolute path "".

Other users of AbsPathForConfigVal() have been updated to handle negation in 2 prior commits.
  • Loading branch information
hodlinator committed Nov 14, 2024
1 parent 2faa3cb commit 20e950b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)

const auto conf_path{GetConfigFilePath()};
std::ifstream stream{conf_path};
// Calling peek() on ifstreams opened on directories flags them as !good().
(void)stream.peek();

// not ok to have a config file specified that cannot be opened
if (IsArgSet("-conf") && !stream.good()) {
if ((IsArgSet("-conf") && !IsArgNegated("-conf")) && !stream.good()) {
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
return false;
}
Expand Down Expand Up @@ -176,6 +178,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)

for (const std::string& conf_file_name : conf_file_names) {
std::ifstream conf_file_stream{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};
(void)conf_file_stream.peek();
if (conf_file_stream.good()) {
if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
return false;
Expand Down Expand Up @@ -213,7 +216,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)

fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
{
if (path.is_absolute()) {
if (path.is_absolute() || path.empty()) {
return path;
}
return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
Expand Down

0 comments on commit 20e950b

Please sign in to comment.