diff options
Diffstat (limited to 'updater/main.cpp')
-rw-r--r-- | updater/main.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/updater/main.cpp b/updater/main.cpp new file mode 100644 index 0000000..e39a0b2 --- /dev/null +++ b/updater/main.cpp @@ -0,0 +1,73 @@ + +#include <stdio.h> +#ifdef WINDOWS + #include <direct.h> + #define GetCurrentDir _getcwd + #define ClientName "client.exe" +#else + #include <unistd.h> + #define GetCurrentDir getcwd + #define ClientName "client" +#endif +#include <errno.h> +#include <QFileInfo> +#include <QProcess> +#include <QString> +#include "Logger.h" + + +int main() +{ + Logger::Info("Starting updater application\n"); + char currentPath[FILENAME_MAX]; + if (! GetCurrentDir(currentPath, sizeof(currentPath))) + { + return errno; + } + // check if whether client.bin.latest exists + // it shoultd be renamed to client.exe (on Windows) ot just client (on Linux) + QString cwd(currentPath); + QFileInfo binInfo(cwd + "/client.bin.latest"); + if (! binInfo.exists()) + { + Logger::Fatal("New client version not present.\n"); + Logger::Fatal("Terminating.\n"); + return -1; + } + + // remove old client binary + QFile client(cwd + "/" + ClientName); + if (client.exists()) + { + Logger::Trace("Removing old client executable\n"); + if (! client.remove()) + { + Logger::Fatal("Can't remove %s\n", client.fileName().toStdString().c_str()); + return -1; + } + } + else + { + Logger::Warn("Client executable is absent\n"); + } + + if (QFile::rename(cwd + "/client.bin.latest", cwd + "/" + ClientName)) + { + Logger::Info("Client successfully updated\n"); + } + else + { + Logger::Fatal("Can't rename 'client.bin.latest'\n"); + return -1; + } + + // starting client process + QString program(cwd + "/" + ClientName); + if (! QProcess::startDetached(program)) + { + Logger::Fatal("Failed to start client application\n"); + return -1; + } + + return 0; +} |