diff --git a/src/engine/config.cpp b/src/engine/config.cpp index 6c942cae8..1a4c9766e 100644 --- a/src/engine/config.cpp +++ b/src/engine/config.cpp @@ -23,7 +23,10 @@ #include "../fileutils.h" #include -bool DivConfig::save(const char* path) { +#define REDUNDANCY_NUM_ATTEMPTS 5 +#define CHECK_BUF_SIZE 8192 + +bool DivConfig::save(const char* path, bool redundancy) { logD("opening config for write: %s",path); FILE* f=ps_fopen(path,"wb"); if (f==NULL) { @@ -84,21 +87,94 @@ void DivConfig::parseLine(const char* line) { } } -bool DivConfig::loadFromFile(const char* path, bool createOnFail) { +bool DivConfig::loadFromFile(const char* path, bool createOnFail, bool redundancy) { char line[4096]; logD("opening config for read: %s",path); - FILE* f=ps_fopen(path,"rb"); - if (f==NULL) { - logD("config does not exist"); - if (createOnFail) { - logI("creating default config."); - reportError(fmt::sprintf("Creating default config: %s",strerror(errno))); - return save(path); - } else { - reportError(fmt::sprintf("COULD NOT LOAD CONFIG %s",strerror(errno))); - return false; + + FILE* f=NULL; + + if (redundancy) { + unsigned char* readBuf=new unsigned char[CHECK_BUF_SIZE]; + size_t readBufLen=0; + for (int i=0; i0) { + snprintf(line,4095,"%s.%d",path,i); + } else { + strncpy(line,path,4095); + } + logV("trying: %s",line); + + // try to open config + f=ps_fopen(line,"rb"); + // check whether we could open it + if (f==NULL) { + logV("fopen(): %s",strerror(errno)); + continue; + } + + // check whether there's something + while (!feof(f)) { + readBufLen=fread(readBuf,1,CHECK_BUF_SIZE,f); + if (ferror(f)) { + logV("fread(): %s",strerror(errno)); + break; + } + + for (size_t j=0; j& configMap(); diff --git a/src/engine/configEngine.cpp b/src/engine/configEngine.cpp index eccf3986e..f793d35c2 100644 --- a/src/engine/configEngine.cpp +++ b/src/engine/configEngine.cpp @@ -110,12 +110,12 @@ void DivEngine::initConfDir() { bool DivEngine::saveConf() { configFile=configPath+String(CONFIG_FILE); - return conf.save(configFile.c_str()); + return conf.save(configFile.c_str(),true); } bool DivEngine::loadConf() { configFile=configPath+String(CONFIG_FILE); - return conf.loadFromFile(configFile.c_str()); + return conf.loadFromFile(configFile.c_str(),true,true); } bool DivEngine::getConfBool(String key, bool fallback) {