diff options
author | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-12-04 19:20:03 +0200 |
---|---|---|
committer | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-12-04 19:20:03 +0200 |
commit | 9fa1566a04b2706476b493e0442035adfc952e53 (patch) | |
tree | fb580df39df8dbc490ad2b29b1ad509aadced059 | |
parent | 7817832a2ccf7b665491cd5cbfb4a4e84251490d (diff) |
FIX client&updater directory path check
-rw-r--r-- | client/main.cpp | 30 | ||||
-rw-r--r-- | updater/main.cpp | 47 |
2 files changed, 37 insertions, 40 deletions
diff --git a/client/main.cpp b/client/main.cpp index a1904ae..4b6d734 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,39 +1,37 @@ -#include <stdio.h> #ifdef WINDOWS - #include <direct.h> - #define GetCurrentDir _getcwd #define UpdaterName "updater.exe" + #define PathSlash "\\" #else - #include <unistd.h> - #define GetCurrentDir getcwd #define UpdaterName "updater" + #define PathSlash "/" #endif -#include <errno.h> #include <QFileInfo> #include <QProcess> #include <QString> +#include <string> #include "ProxyClientApp.h" #include "client.h" +using std::string; + ProxyClientApp *this_app; int main(int argc, char *argv[]) { Logger::Info("Starting client application\n"); - char currentPath[FILENAME_MAX]; - if (! GetCurrentDir(currentPath, sizeof(currentPath))) - { - return errno; - } - QString cwd = currentPath; - // check if client should be updated - QFileInfo newClient(cwd + "/client.bin.latest"); + string path = argv[0]; + size_t p = path.find_last_of(PathSlash); + QString dir = QString::fromStdString(path.substr(0, p)); + Logger::Trace("Application path: %s\n", dir.toStdString().c_str()); + + // check if client should be update + QFileInfo newClient(dir + PathSlash + "client.bin.latest"); if (newClient.exists()) { Logger::Info("New client version found!\n"); - QString updater = cwd + "/" UpdaterName; + QString updater = dir + PathSlash + UpdaterName; if (QProcess::startDetached(updater)) { Logger::Info("Updater launched\n"); @@ -47,7 +45,7 @@ int main(int argc, char *argv[]) // check if initial config exists (config.cfg) // without it application is useless - QFileInfo configInfo(cwd + "/config.cfg"); + QFileInfo configInfo(dir + PathSlash + "config.cfg"); if (! configInfo.exists()) { Logger::Fatal("Initial configuration file (config.cfg) do not exist!\n"); diff --git a/updater/main.cpp b/updater/main.cpp index e39a0b2..b0198f6 100644 --- a/updater/main.cpp +++ b/updater/main.cpp @@ -1,42 +1,41 @@ -#include <stdio.h> #ifdef WINDOWS - #include <direct.h> - #define GetCurrentDir _getcwd #define ClientName "client.exe" + #define PathSlash "\\" #else - #include <unistd.h> - #define GetCurrentDir getcwd #define ClientName "client" + #define PathSlash "/" #endif -#include <errno.h> #include <QFileInfo> #include <QProcess> #include <QString> +#include <string> #include "Logger.h" +using std::string; -int main() + +int main(int argc, char* argv[]) { - Logger::Info("Starting updater application\n"); - char currentPath[FILENAME_MAX]; - if (! GetCurrentDir(currentPath, sizeof(currentPath))) + Logger::Info("Starting updater application\n"); + + string path = argv[0]; + size_t p = path.find_last_of(PathSlash); + QString dir = QString::fromStdString(path.substr(0, p)); + Logger::Trace("Application path: %s\n", dir.toStdString().c_str()); + + // check if whether client.bin.latest exists + // it shoultd be renamed to client.exe (on Windows) ot just client (on Linux) + QFileInfo binInfo(dir + PathSlash +"client.bin.latest"); + if (! binInfo.exists()) { - return errno; + Logger::Fatal("New client version not present.\n"); + Logger::Fatal("Terminating.\n"); + return -1; } - // 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); + QFile client(dir + PathSlash + ClientName); if (client.exists()) { Logger::Trace("Removing old client executable\n"); @@ -51,7 +50,7 @@ int main() Logger::Warn("Client executable is absent\n"); } - if (QFile::rename(cwd + "/client.bin.latest", cwd + "/" + ClientName)) + if (QFile::rename(dir + PathSlash + "client.bin.latest", dir + PathSlash + ClientName)) { Logger::Info("Client successfully updated\n"); } @@ -62,7 +61,7 @@ int main() } // starting client process - QString program(cwd + "/" + ClientName); + QString program(dir + PathSlash + ClientName); if (! QProcess::startDetached(program)) { Logger::Fatal("Failed to start client application\n"); |