From 173bcc8d7e2f1ee73cf2c8464ffc22528263ecd4 Mon Sep 17 00:00:00 2001 From: Markus Date: Sat, 26 Oct 2019 16:18:47 +0200 Subject: [PATCH] add new subfunction and function for removing dir / removing settings and cachedir --- parameterManager.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++-- parameterManager.h | 23 +++++++----- programManager.cpp | 43 +++++++++++++++++++++- programManager.h | 3 +- 4 files changed, 139 insertions(+), 17 deletions(-) diff --git a/parameterManager.cpp b/parameterManager.cpp index fec9c7d..e86a3c7 100644 --- a/parameterManager.cpp +++ b/parameterManager.cpp @@ -56,14 +56,13 @@ int manageParameter(Settings &settings, int argc, char **argv) if(settings.debugMode) std::cerr << ">>> Debug In " << __FUNCTION__ << ": setPaths f() failed." << std::endl; return -1; - } - if(loadDefaulOptions(settings) != 0) { + + } else if(loadDefaulOptions(settings) != 0) { if(settings.debugMode) std::cerr << ">>> Debug In " << __FUNCTION__ << ": loadDefaulOptions f() failed." << std::endl; return 28; - } - if(argc < 2) { + } else if(argc < 2) { std::cout << " => Error: Keine Unteroption angegeben." << std::endl; std::cout << "Aufruf: " << settings.programName << " [Unteroption] [PARAMETER]" << std::endl; std::cout << "\"" << settings.programName << " --help\" liefert weitere Informationen." << std::endl; @@ -1253,3 +1252,83 @@ void unterOption_update_help(std::string programName) << " Mit dieser Option wird dieses Helpmenue ausgegeben." << std::endl; } + +bool removeDirIsOk(std::string path, Settings *settings) +{ + if(path == "") { + std::cout << " => Error: Ungültiger Pfad: '" << path << "'." << std::endl; + return false; + } else if(path.back() != settings->pathSymbol ) + path.push_back(settings->pathSymbol); + + if(settings->askForEveryDir) { + std::cout << "Zur Bestätigung des Löschens des Ordners: '" << path << "'," << std::endl + << "geben sie 'OK' ein: " << std::flush; + std::string input; + std::getline(std::cin, input); + if(input != "OK") { + std::cout << "Abbruch..." << std::endl; + return false; + } + + } + + + DIR* dirp = nullptr; + if( (dirp = opendir( path.c_str() )) == nullptr ) { + perror(std::string(" => Error: Konnte Verzeichnis nicht öffnen: '" + path + "'").c_str()); + return false; + } + + struct dirent * dp; + while ((dp = readdir(dirp)) != nullptr) { + if(strcmp( dp->d_name, "." ) == 0 || strcmp ( dp->d_name, ".." ) == 0) + continue; + else if(dirExists((path + dp->d_name + "/"))) { //if(dp->d_type == DT_DIR) { + if( ! removeDirIsOk(path + dp->d_name + "/" , settings)) { + if(settings->debugMode) + std::cout << " => Error im Unterordner: '" << path + dp->d_name << "'" << std::endl; + return false; + } + } else if(fileExists( path + dp->d_name )) { + if(remove((path + dp->d_name).c_str()) != 0) { + perror((" => Error: Das Löschen von '" + path + dp->d_name + "' ist fehlgeschlagen").c_str()); + return false; + } else { + if(settings->debugMode) + std::cout << " => DEBUG: Erfolgreich '" << path + dp->d_name << "' gelöscht." << std::endl; + } + } else { + std::cout << " => Error: '" << path << dp->d_name << "'ist keine Datei oder Ordner." << std::endl; + return false; + } + + } + closedir(dirp); + + if(rmdir(path.c_str()) != 0) { + perror((" => Das löschen des Ordners '" + path + "' ist fehlgeschlagen.").c_str()); + return false; + + } else if (settings->debugMode) + std::cout << " => DEBUG: Ordner '" << path << "' erfolgreich gelöscht." << std::endl; + + return true; +} + + + + + + + + + + + + + + + + + diff --git a/parameterManager.h b/parameterManager.h index 13906db..02aa45f 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -17,6 +17,7 @@ #include // PATH_MAX #include // readlink() +#include #ifdef _WIN32 @@ -49,17 +50,17 @@ struct Settings { const std::string ProgrammFileUrl = "https://cloud.obermui.de/s/H47Xoqy2czfJzYp/download?path=%2F&files=S_New4-WINx86.exe"; #endif 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/", + accountFilePath = "", + accountNumberPath= "", + cookieFilePath = "", + serienListPath = "", + lastUpdateDateFilePath = "", + configDir="", + cacheDir="", - defaultsFilePath = "/tmp/defaults", + defaultsFilePath = "", proxy_ip = "127.0.0.1", - languages = "GerDub,GerSub,Eng", + languages = "GerDub,GerSub,Eng,", genaueHoster = "", version = "3.3.2", defaultFileVersion="1.6", @@ -71,7 +72,8 @@ struct Settings { bool colorless = false, debugMode = false, search_IgnoreUpperLower = true, - search_wantUpdate = false; + search_wantUpdate = false, + askForEveryDir = true; int startEpisode = 1, stopEpisode = 0, startSeason = 1, @@ -97,6 +99,7 @@ bool nothingExists(std::string path); bool createDirIsOk(std::string path); bool makePathIsOk(std::string path); +bool removeDirIsOk(std::string path, Settings *settings); int unterOption_help(Settings &settings); void unterOption_printVersion(Settings &settings); diff --git a/programManager.cpp b/programManager.cpp index a79f4c4..d140176 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -1101,6 +1101,32 @@ int ProgramManager::updateModus(Settings *settings) return 0; } +int ProgramManager::cleanUpSettingsAndCache(Settings *settings) +{ + std::string input; + std::cout << "Bitte Bestätigen sie, dass sie alle Einstellungen und" << std::endl + << "Zwischengespeicherten Dateien löschen wollen:\nGeben Sie 'CONFIRM' ein: " << std::flush; + std::getline(std::cin, input); + if( input != "CONFIRM" ) { + std::cout << "Abbruch..." << std::endl; + return 0; + } + + //Cache Dir + if( ! removeDirIsOk(settings->cacheDir, settings)) { + std::cout << " => Error: Das löschen des Cache-Ordners ist fehlgeschlagen." << std::endl; + return 1; + } + //setting dir + if( ! removeDirIsOk(settings->configDir, settings)) { + std::cout << " => Error: Das löschen des Settings-Ordners ist fehlgeschlagen." << std::endl; + return 1; + } + + + return 0; +} + int ProgramManager::searchModus_update(Settings *settings) { @@ -1238,8 +1264,21 @@ int ProgramManager::listDir(std::string &list,std::string path, int maxDepth) //verkleiner varibable um 1, um zu verhindern, das endlose schleife entsteht maxDepth--; - if(path[path.length()-1] != '/') - path.push_back('/'); + if(path[path.length()-1] != '/') { + if(path[path.length()-1] != '\\') { // Wenn kein \ oder / am ende ist + if(path.find("/") == std::string::npos) { // wenn im namen ein / ist füge am ende / hinzu + if(path.find("\\") == std::string::npos) { // wenn im name ein \ ist add \ to en or error + std::cout << " => Error: Pfad zu dem Ordner enthält kein Pfadsymbol." << std::endl; + return -2; + } else { + path.push_back('\\'); + } + } else { + path.push_back('/'); + } + } + } + DIR* dirp = opendir(path.c_str()); if(!dirp) { diff --git a/programManager.h b/programManager.h index 89c160d..d7c2258 100644 --- a/programManager.h +++ b/programManager.h @@ -6,7 +6,6 @@ #include "accountManager.h" #include -#include #include @@ -48,6 +47,8 @@ private: int infoModus(Settings * settings); int newsModus(Settings *settings); int updateModus(Settings *settings); + int cleanUpSettingsAndCache(Settings *settings); + PageManager pageManager; std::vector threadList;