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 14 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
23 changes: 14 additions & 9 deletions src/utils/configfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const OptionId kConfigFileId{
"Path to a configuration file. The format of the file is one command line "
"parameter per line, e.g.:\n--weights=/path/to/weights",
'c'};
const char* kDefaultConfigFile = "lc0.config";
const char* kDefaultConfigFile = "<default>";
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for nitpicking, but I suggest moving both constants here:

const char* kDefaultConfigFile = "lc0.config";
const char* kDefaultConfigFileParam = "<default>";

(or if it's one, better it be the filename and not the option value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a problem. Do these changes match what you were thinking?

Copy link
Member

Choose a reason for hiding this comment

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

Looks good!

} // namespace

std::vector<std::string> ConfigFile::arguments_;
Expand Down Expand Up @@ -81,28 +81,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(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() + "/lc0.config";
}

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