From b59011f52c95d2f6ac19152641c418d9f0221b43 Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Sat, 3 Dec 2011 22:50:51 +0200 Subject: Client updater app --- updater/main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 updater/main.cpp (limited to 'updater/main.cpp') 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 +#ifdef WINDOWS + #include + #define GetCurrentDir _getcwd + #define ClientName "client.exe" +#else + #include + #define GetCurrentDir getcwd + #define ClientName "client" +#endif +#include +#include +#include +#include +#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; +} -- cgit v1.2.3