Skip to content

Commit

Permalink
check config file keys for validity (#296)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
norbusan authored Dec 19, 2018
1 parent bbe672b commit 025a3b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Config
syncListFilePath = configDirName ~ "/sync_list";
}

void init()
bool init()
{
// Default configuration directory
setValue("sync_dir", "~/OneDrive");
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 025a3b2

Please sign in to comment.