From c28e2e8e0cfa0ae4358f798c3bd34c8678de9541 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 1 Jan 2014 13:17:34 +0300 Subject: add ability for auto backup main configuration file on startup. --- src/client.cpp | 18 ++++++++++++++++++ src/client.h | 2 ++ src/utils/files.cpp | 33 +++++++++++++++++++++++++++++++++ src/utils/files.h | 3 +++ 4 files changed, 56 insertions(+) (limited to 'src') diff --git a/src/client.cpp b/src/client.cpp index 483b260ee..d27896a14 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -110,6 +110,7 @@ #include "utils/files.h" #include "utils/fuzzer.h" #include "utils/gettext.h" +#include "utils/files.h" #include "utils/mkdir.h" #include "utils/paths.h" #include "utils/physfstools.h" @@ -297,6 +298,7 @@ void Client::gameInit() #ifdef USE_FUZZER Fuzzer::init(); #endif + backupConfig(); initConfiguration(); paths.setDefaultValues(getPathsDefaults()); initFeatures(); @@ -2142,6 +2144,22 @@ void Client::initConfiguration() const } } +void Client::backupConfig() const +{ + const std::string confName = mConfigDir + "/config.xml.bak"; + const int maxFileIndex = 5; + ::remove((confName + toString(maxFileIndex)).c_str()); + for (int f = maxFileIndex; f > 1; f --) + { + const std::string fileName1 = confName + toString(f - 1); + const std::string fileName2 = confName + toString(f); + Files::renameFile(fileName1, fileName2); + } + const std::string fileName3 = mConfigDir + "/config.xml"; + const std::string fileName4 = confName + toString(1); + Files::copyFile(fileName3, fileName4); +} + /** * Parse the update host and determine the updates directory * Then verify that the directory exists (creating if needed). diff --git a/src/client.h b/src/client.h index dcf761847..dce388439 100644 --- a/src/client.h +++ b/src/client.h @@ -366,6 +366,8 @@ private: void storeSafeParameters() const; + void backupConfig() const; + void gameClear(); void testsClear(); diff --git a/src/utils/files.cpp b/src/utils/files.cpp index 216a83991..40a4da757 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -163,3 +163,36 @@ int Files::renameFile(const std::string &restrict srcName, return ::rename(srcName.c_str(), dstName.c_str()); #endif } + +int Files::copyFile(const std::string &restrict srcName, + const std::string &restrict dstName) +{ + FILE *srcFile = fopen(srcName.c_str(), "rb"); + if (srcFile == nullptr) + return -1; + FILE *dstFile = fopen(dstName.c_str(), "w+b"); + if (dstFile == nullptr) + { + fclose(srcFile); + return -1; + } + + const int chunkSize = 500000; + char *buf = new char[chunkSize]; + size_t sz = 0; + while ((sz = fread(buf, 1, chunkSize, srcFile))) + { + if (fwrite(buf, 1, sz, dstFile) != sz) + { + delete [] buf; + fclose(srcFile); + fclose(dstFile); + return -1; + } + } + + delete [] buf; + fclose(srcFile); + fclose(dstFile); + return 0; +} diff --git a/src/utils/files.h b/src/utils/files.h index 5eb29031e..e7106c111 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -49,6 +49,9 @@ namespace Files int renameFile(const std::string &restrict pFrom, const std::string &restrict pTo); + + int copyFile(const std::string &restrict pFrom, + const std::string &restrict pTo); } // namespace Files #endif // UTILS_FILES_H -- cgit v1.2.3-70-g09d2