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:
Hodlinator 2024-12-02 10:31:19 +01:00
parent e4b6b1822c
commit e85abe92c7
No known key found for this signature in database
2 changed files with 13 additions and 1 deletions

View file

@ -16,6 +16,7 @@
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <list>
@ -130,6 +131,10 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
const auto conf_path{GetConfigFilePath()};
std::ifstream stream;
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};
// If the file is explicitly specified, it must be readable
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({});
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 (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
return false;

View file

@ -125,6 +125,8 @@ bool StartLogging(const ArgsManager& args)
fs::path config_file_path = args.GetConfigFilePath();
if (args.IsArgNegated("-conf")) {
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)) {
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
} else if (args.IsArgSet("-conf")) {