check if config folder exists if not create it

right appdata folder for windows hosts
seperate config path and config name in cmake header
This commit is contained in:
lukas 2020-05-21 11:26:15 +02:00
parent a356bca964
commit ca47949a38
2 changed files with 33 additions and 6 deletions

View File

@ -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 <exe>, <deb> or <rpm>" 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})\";
}"
)

View File

@ -6,6 +6,7 @@
#include <cstring>
#include <fstream>
#include <libconfig.h++>
#include <sys/stat.h>
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();