From 025a3b28786ea15084a66b887bf6af63e46e027d Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 20 Dec 2018 08:51:21 +0900 Subject: [PATCH] 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 --- src/config.d | 25 +++++++++++++++++++++---- src/main.d | 6 +++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/config.d b/src/config.d index 6d8b3e92..ea9d3186 100644 --- a/src/config.d +++ b/src/config.d @@ -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; diff --git a/src/main.d b/src/main.d index 33781b0e..eaa70cc3 100644 --- a/src/main.d +++ b/src/main.d @@ -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;