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 11 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
22 changes: 13 additions & 9 deletions src/utils/configfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,22 @@ 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;
// Check to see if we are using the default config file or not.
const bool using_default_config =
filename == std::string(kDefaultConfigFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have a separate value for default behaviour, like "<default>" or "<auto>" (which still will read $LC0_DIRECTORY/lc0.config), similarly to what we have for weights file (there we have "<autodiscover>").

Otherwise when run like ./lc0 -c lc0.cfg it will search in the current directory, while for ./lc0 -c lc0.config it will search in the directory of the binary, which is a confusing inconsistency.

Also in future we'll try several locations by default (possibly with different file names, not only directories, e.g. /etc/lc0.cfg vs ~/.lc0rc), and it's more logical if the parameter doesn't have file name in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made changes along these lines. Let me know if this is what you were thinking 🙂


// 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() + "/" + filename;
}

// Parses the file into the arguments_ vector.
if (!ParseFile(filename, options)) return false;
Expand All @@ -100,14 +108,10 @@ bool ConfigFile::ParseFile(const 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);

if (!input.is_open()) {
// It is okay if we cannot open the default file since it is normal
// for it to not exist.
if (using_default_config) return true;
// for it to not exist. Check it is default by comparing to <default>.
if (filename == std::string(kDefaultConfigFile)) return true;

CERR << "Could not open configuration file: " << filename;
return false;
Expand Down
1 change: 1 addition & 0 deletions src/utils/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ConfigFile {
// Parses the config file into the arguments_ vector.
static bool ParseFile(const 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