check config file keys for validity (#296)

* check config file keys for validity, use setValue instead of direct access
* Update config.d
  Add 'drive_id' to be initialised, set to an empty string
* exit application if there is a configuration file error
* Issue #293 was caused by a spelling error in the configuration file. 
  If the configuration file has errors, we should not load it or run
  using the application defaults as this may have undesirable consequences
  for users data
* missed returning false on key issue
* Missed this edit of the file
This commit is contained in:
Norbert Preining 2018-12-20 08:51:21 +09:00 committed by GitHub
parent bbe672bff1
commit 025a3b2878
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View file

@ -24,7 +24,7 @@ final class Config
syncListFilePath = configDirName ~ "/sync_list";
}
void init()
bool init()
{
// Default configuration directory
setValue("sync_dir", "~/OneDrive");
@ -39,10 +39,20 @@ final class Config
setValue("monitor_interval", "45");
// Configure the default logging directory to be /var/log/onedrive/
setValue("log_dir", "/var/log/onedrive/");
// Configure a default empty value for drive_id
setValue("drive_id", "");
if (!load(userConfigFilePath)) {
log.vlog("No config file found, using defaults");
// What was the reason for failure?
if (!exists(userConfigFilePath)) {
log.vlog("No config file found, using application defaults");
return true;
} else {
log.log("Configuration file has errors - please check your configuration");
return false;
}
}
return true;
}
string getValue(string key)
@ -82,10 +92,17 @@ final class Config
if (!c.empty) {
c.popFront(); // skip the whole match
string key = c.front.dup;
c.popFront();
values[key] = c.front.dup;
auto p = key in values;
if (p) {
c.popFront();
setValue(key, c.front.dup);
} else {
log.log("Unknown key in config file: ", key);
return false;
}
} else {
log.log("Malformed config line: ", line);
return false;
}
}
return true;

View file

@ -155,7 +155,11 @@ int main(string[] args)
log.vlog("Using Config Dir: ", configDirName);
if (!exists(configDirName)) mkdirRecurse(configDirName);
auto cfg = new config.Config(configDirName);
cfg.init();
if(!cfg.init()){
// There was an error loading the configuration
// Error message already printed
return EXIT_FAILURE;
}
// Set the local path OneDrive root
string syncDir;