Skip to content
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

Add check for absolute path of config file #1276

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/utils/configfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const OptionId kConfigFileId{
"parameter per line, e.g.:\n--weights=/path/to/weights",
'c'};
const char* kDefaultConfigFile = "lc0.config";
const char* kDefaultConfigFileParam = "<default>";
} // namespace

std::vector<std::string> ConfigFile::arguments_;
Expand All @@ -55,7 +56,7 @@ void ConfigFile::PopulateOptions(OptionsParser* options) {
// ProcessAllFlags() that should be called only once, and needs the config file.
std::string ConfigFile::ProcessConfigFlag(
const std::vector<std::string>& args) {
std::string filename = kDefaultConfigFile;
std::string filename = kDefaultConfigFileParam;
for (auto iter = args.begin(), end = args.end(); iter != end; ++iter) {
std::string param = *iter;

Expand All @@ -81,28 +82,33 @@ std::string ConfigFile::ProcessConfigFlag(
bool ConfigFile::Init(OptionsParser* options) {
arguments_.clear();

// Get the relative path from the config file parameter.
// Get the path from the config file parameter.
std::string filename = ProcessConfigFlag(CommandLine::Arguments());

// If filename is an empty string then return true. This is to override
// If filename is an empty string then return true. This is to override
// loading the default configuration file.
if (filename == "") return true;

filename = CommandLine::BinaryDirectory() + "/" + filename;

// Parses the file into the arguments_ vector.
if (!ParseFile(filename, options)) return false;

return true;
}

bool ConfigFile::ParseFile(const std::string& filename,
bool ConfigFile::ParseFile(std::string& filename,
OptionsParser* options) {
std::ifstream input(filename);

// Check to see if we are using the default config file or not.
OptionsDict dict = options->GetOptionsDict();
const bool using_default_config = dict.IsDefault<std::string>(kConfigFileId);
const bool using_default_config =
filename == std::string(kDefaultConfigFileParam);

// If no logfile was set on the command line, then the default is
// to check in the binary directory.
if (using_default_config) {
filename = CommandLine::BinaryDirectory() + '/' + kDefaultConfigFile;
}

std::ifstream input(filename);

if (!input.is_open()) {
// It is okay if we cannot open the default file since it is normal
Expand Down
3 changes: 2 additions & 1 deletion src/utils/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class ConfigFile {

private:
// Parses the config file into the arguments_ vector.
static bool ParseFile(const std::string& filename, OptionsParser* options);
static bool ParseFile(std::string& filename, OptionsParser* options);

// Returns the absolute path to the config file argument given.
static std::string ProcessConfigFlag(const std::vector<std::string>& args);

static std::vector<std::string> arguments_;
Expand Down