From 27b33bfad4700376c25b587f556ce6e98579a076 Mon Sep 17 00:00:00 2001 From: Markus Date: Sun, 13 Oct 2019 14:28:53 +0200 Subject: [PATCH] v3.2.0: add updater tests --- parameterManager.cpp | 3 ++ parameterManager.h | 9 +++- programManager.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ programManager.h | 1 + 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/parameterManager.cpp b/parameterManager.cpp index 9c81e4e..49213a0 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -43,6 +43,9 @@ int setPaths(Settings &settings) settings.serienListPath = SettingsDir + "SerienListe"; settings.defaultsFilePath = SettingsDir + "Defaults"; + settings.cacheDir = CacheDir; + settings.configDir = SettingsDir; + return 0; } diff --git a/parameterManager.h b/parameterManager.h index a63c66c..86ffae8 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -29,7 +29,8 @@ enum Modus { DIRECT_LINK_MODUS = 1, SEARCH_MODUS = 2, INFO_MODUS = 3, - NEWS_MODUS = 4 + NEWS_MODUS = 4, + UPDATE_MODUS = 5 }; @@ -39,18 +40,22 @@ struct Settings { Settings(std::string name) : name(name) {} const std::string programName = "S_New4"; + const std::string VersionFileUrl = "https://cloud.obermui.de/s/cLRYJQAMqWDFt5g/download"; + std::string name, accountFilePath = "/tmp/a", accountNumberPath= "/tmp/a_n", cookieFilePath = "/tmp/S_New4_cookies", serienListPath = "/tmp/SerienListe", lastUpdateDateFilePath = "/tmp/lastUpdateDateFile", + configDir="/tmp/", + cacheDir="/tmp/", defaultsFilePath = "/tmp/defaults", proxy_ip = "127.0.0.1", languages = "GerDub,GerSub,Eng", genaueHoster = "", - version = "3.1.1", + version = "3.2.0", defaultFileVersion="1.6", outputFilePath = "", default_checkPath = "", diff --git a/programManager.cpp b/programManager.cpp index 5d3639e..d7e074c 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -27,6 +27,9 @@ int ProgramManager::start(Settings *settings) return 174; } + settings->modus = UPDATE_MODUS; + + switch (settings->modus) { case Modus::DEFAULT_MODUS: return defaultModus(settings); @@ -38,6 +41,8 @@ int ProgramManager::start(Settings *settings) return infoModus(settings); case Modus::NEWS_MODUS: return newsModus(settings); + case Modus::UPDATE_MODUS: + return updateModus(settings); default: return -1; } @@ -960,6 +965,103 @@ int ProgramManager::newsModus(Settings *settings) << ((settings->colorless) ? "" : "\033[92m") << id << ((settings->colorless) ? "" : "\033[0m") << std::endl; } + return 0; +} + +#include + +int ProgramManager::updateModus(Settings *settings) +{ + std::cout << "Check newst Version..." << std::endl; + std::string html; + + if( (html = pageManager.getServerRequest(settings->VersionFileUrl).html) == "-1") { + if(settings->debugMode) + std::cerr << "Download newst Version file failed" << std::endl; + return 1; + } else { + html = pageManager.replace(html, "\r", ""); + } + + // Get newst Version + std::stringstream strstream(html+ "\n"); + std::string neusteVersion, dieseVersion; + std::getline(strstream, neusteVersion); + if( !isNumber( (neusteVersion = pageManager.replace(neusteVersion, ".", "")) ) ) { + std::cout << " => Error: Gedownloadete neuste Version ist ungültig: '" << neusteVersion << "'" << std::endl; + return 2; + } else { + dieseVersion = pageManager.replace(settings->version, ".", ""); + } + + std::cout << "Neueste Version: " << neusteVersion << " diese Version " << dieseVersion << std::endl; + + if(neusteVersion <= dieseVersion) { + std::cout << " => INFO: Du benützt bereits die neuste Release-Version." << std::endl; + return 0; + } + + std::cout << "Lade Dateien herunter..." << std::endl; + + std::string downloadUrl; + std::getline(strstream, downloadUrl); + if(downloadUrl.find("/download") == std::string::npos) { + std::cout << " => Error: Gedownloadete Datei hat ungültigen DownloadLink: '" << downloadUrl << "'" << std::endl; + return 3; + } + + std::string downloadedFilePath = settings->cacheDir + "NeusteVersion" + ((settings->pathSymbol == '\\') ? ".exe" : ""); + if( pageManager.downLoadToFile(downloadedFilePath, downloadUrl) != 0) { + if(settings->debugMode) + std::cerr << "Download new Version failed" << std::endl; + return 3; + } + + + std::cout << "Mache das Programm ausführbar..." << std::endl; +#ifdef __linux__ + chmod(downloadedFilePath.c_str(), S_IRWXU); +#endif + + std::cout << "Überprüfe funktionalität der neuen Version..." << std::endl; + + if( system((downloadedFilePath + " --version" ).c_str()) != 0) { + perror("Ausführen der neues Version fehlgeschlagen"); + return 4; + } + + + std::string exePath; +#ifdef __linux__ + char result[PATH_MAX + 1]; + if(readlink("/proc/self/exe", result, PATH_MAX) == -1) { + perror("Readlink failed"); + return 5; + } + exePath = result; +#endif +#ifdef _WIN32 + wchar_t buffer[MAX_PATH + 1]; + GetModuleFileName( nullptr, buffer, MAX_PATH ); + std::wstring wstr(buffer); + exePath = std::string(wstr.begin(), wstr.end()); +#endif + + std::cout << "Ersetzte jetzige Version mit neuer Version des Programms..." << std::endl; + + if(rename(downloadedFilePath.c_str(), exePath.c_str()) != 0) { + perror("Mv failed"); + return 6; + } else { + std::cout << "Erfolgreich verschoben: '" << downloadedFilePath << "' -> '" << exePath << "'" << std::endl; + } + + + std::cout << "Update Erfolgreich ausgeführt." << std::endl; + + + + return 0; } diff --git a/programManager.h b/programManager.h index e4ddcfb..89c160d 100644 --- a/programManager.h +++ b/programManager.h @@ -47,6 +47,7 @@ private: int searchModus(Settings * settings, std::string *saveTo = nullptr, bool noPrint = false); int infoModus(Settings * settings); int newsModus(Settings *settings); + int updateModus(Settings *settings); PageManager pageManager; std::vector threadList;