mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-23 07:15:29 +01:00
args: Catch directories in place of config files
Previously passing a directory path as -conf would lead to an ifstream being opened for it, and would not trigger any errors.
This commit is contained in:
parent
e4b6b1822c
commit
e85abe92c7
2 changed files with 13 additions and 1 deletions
|
@ -16,6 +16,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
@ -130,6 +131,10 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
||||||
const auto conf_path{GetConfigFilePath()};
|
const auto conf_path{GetConfigFilePath()};
|
||||||
std::ifstream stream;
|
std::ifstream stream;
|
||||||
if (!conf_path.empty()) { // path is empty when -noconf is specified
|
if (!conf_path.empty()) { // path is empty when -noconf is specified
|
||||||
|
if (fs::is_directory(conf_path)) {
|
||||||
|
error = strprintf("Config file \"%s\" is a directory.", fs::PathToString(conf_path));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
stream = std::ifstream{conf_path};
|
stream = std::ifstream{conf_path};
|
||||||
// If the file is explicitly specified, it must be readable
|
// If the file is explicitly specified, it must be readable
|
||||||
if (IsArgSet("-conf") && !stream.good()) {
|
if (IsArgSet("-conf") && !stream.good()) {
|
||||||
|
@ -177,7 +182,12 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
||||||
const size_t default_includes = add_includes({});
|
const size_t default_includes = add_includes({});
|
||||||
|
|
||||||
for (const std::string& conf_file_name : conf_file_names) {
|
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)};
|
const auto include_conf_path{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};
|
||||||
|
if (fs::is_directory(include_conf_path)) {
|
||||||
|
error = strprintf("Included config file \"%s\" is a directory.", fs::PathToString(include_conf_path));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::ifstream conf_file_stream{include_conf_path};
|
||||||
if (conf_file_stream.good()) {
|
if (conf_file_stream.good()) {
|
||||||
if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
|
if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -125,6 +125,8 @@ bool StartLogging(const ArgsManager& args)
|
||||||
fs::path config_file_path = args.GetConfigFilePath();
|
fs::path config_file_path = args.GetConfigFilePath();
|
||||||
if (args.IsArgNegated("-conf")) {
|
if (args.IsArgNegated("-conf")) {
|
||||||
LogInfo("Config file: <disabled>");
|
LogInfo("Config file: <disabled>");
|
||||||
|
} else if (fs::is_directory(config_file_path)) {
|
||||||
|
LogWarning("Config file: %s (is directory, not file)", fs::PathToString(config_file_path));
|
||||||
} else if (fs::exists(config_file_path)) {
|
} else if (fs::exists(config_file_path)) {
|
||||||
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
|
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
|
||||||
} else if (args.IsArgSet("-conf")) {
|
} else if (args.IsArgSet("-conf")) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue