From ca47949a38bb53dad50e3a62ccf2ba7c946dbbfe Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 11:26:15 +0200 Subject: [PATCH] check if config folder exists if not create it right appdata folder for windows hosts seperate config path and config name in cmake header --- CMakeLists.txt | 9 +++++---- src/Config.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae57b65..71afba9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ SET(LIB_METHOD STATIC) #SHARED / STATIC option(BUILD_DOC "Build documentation" ON) # additional dependency for Doxygen option(PACKAGING "Allow Packaging to , or " ON) # additional dependencies for RPMbuild,dpkg or NSIS option(TESTS "Build Tests" false) # additional dependencies for GTEST - to build tests -set(WinBuild true) +set(WinBuild false) # helper variables SET(CMAKE_CXX_STANDARD 17) @@ -65,13 +65,13 @@ if (${WinBuild}) # or add_definitions(-DCURL_STATICLIB) # windows config path is same as executable - set(CONFIG_PATH "../Common Files/iprefresher/iprefresher.cfg") + set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") else () set(LIBSUFFIX .so) set(SUFFIX "") # set /etc/ config path - set(CONFIG_PATH "/etc/iprefresher.cfg") + set(CONFIG_PATH "\"/etc/\"") endif () @@ -149,7 +149,8 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h namespace Version { const std::string VERSION = \"${PROJECT_VERSION}\"; - const std::string ConfigDir = \"${CONFIG_PATH}\"; + const std::string ConfigDir = ${CONFIG_PATH}; + const std::string ConfName = \"iprefresher.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; }" ) diff --git a/src/Config.cpp b/src/Config.cpp index 32aba2a..217bdc7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -6,6 +6,7 @@ #include #include #include +#include std::string Config::dynuapikey; std::string Config::domainid; //id of the dynu domain @@ -19,13 +20,38 @@ bool Config::telegramSupport; bool Config::readConfig() { libconfig::Config cfg; try { - cfg.readFile(Version::ConfigDir.c_str()); + cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); } catch (const libconfig::FileIOException &fioex) { std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; + // check if config folder exists + struct stat info{}; + + if (stat(Version::ConfigDir.c_str(), &info) != 0) { + Logger::warning("The config folder doesn't exist. Trying to create it."); + +#ifdef __unix + int check = mkdir(Version::ConfigDir.c_str(), 777); +#else + int check = mkdir(Version::ConfigDir.c_str()); +#endif + + // check if directory is created or not + if (!check) + Logger::message("config directory successfully created. "); + else + Logger::error("unable to create config directory."); + + } else if (info.st_mode & S_IFDIR) { + Logger::debug("config directory exists already"); + } else { + Logger::error("A file exists with the same name as the config dir should be"); + } + + std::ofstream myfile; - myfile.open(Version::ConfigDir); + myfile.open(Version::ConfigDir + Version::ConfName); if (myfile.is_open()) { myfile << Version::SAMPLECONFIG; myfile.close();