From 672e6f820788f8e8efcb8181faf895466f36f979 Mon Sep 17 00:00:00 2001 From: Sude Date: Tue, 23 May 2017 16:53:24 +0300 Subject: [PATCH] Galaxy: Check for orphaned files Check and list orphaned files after download/installation with --galaxy-install is finished --- include/downloader.h | 2 ++ src/downloader.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/include/downloader.h b/include/downloader.h index aae9acc..f760bec 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -122,6 +122,8 @@ class Downloader static size_t writeData(void *ptr, size_t size, size_t nmemb, FILE *stream); static size_t readData(void *ptr, size_t size, size_t nmemb, FILE *stream); + std::vector galaxyGetOrphanedFiles(const std::vector& items, const std::string& install_path); + Website *gogWebsite; API *gogAPI; galaxyAPI *gogGalaxy; diff --git a/src/downloader.cpp b/src/downloader.cpp index 735ac58..cc2624f 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -3846,6 +3846,12 @@ void Downloader::galaxyInstallGame(const std::string& product_id, int build_inde free(chunk.memory); } } + + std::cout << "Checking for orphaned files" << std::endl; + std::vector orphans = this->galaxyGetOrphanedFiles(items, install_path); + std::cout << "\t" << orphans.size() << " orphaned files" << std::endl; + for (unsigned int i = 0; i < orphans.size(); ++i) + std::cout << "\t" << orphans[i] << std::endl; } void Downloader::galaxyShowBuilds(const std::string& product_id, int build_index) @@ -3890,3 +3896,77 @@ void Downloader::galaxyShowBuilds(const std::string& product_id, int build_index std::cout << json << std::endl; } + +std::vector Downloader::galaxyGetOrphanedFiles(const std::vector& items, const std::string& install_path) +{ + std::vector orphans; + std::vector item_paths; + for (unsigned int i = 0; i < items.size(); ++i) + item_paths.push_back(install_path + "/" + items[i].path); + + std::vector filepath_vector; + try + { + std::size_t pathlen = Globals::globalConfig.dirConf.sDirectory.length(); + if (boost::filesystem::exists(install_path)) + { + if (boost::filesystem::is_directory(install_path)) + { + // Recursively iterate over files in directory + boost::filesystem::recursive_directory_iterator end_iter; + boost::filesystem::recursive_directory_iterator dir_iter(install_path); + while (dir_iter != end_iter) + { + if (boost::filesystem::is_regular_file(dir_iter->status())) + { + std::string filepath = dir_iter->path().string(); + if (Globals::globalConfig.ignorelist.isBlacklisted(filepath.substr(pathlen))) + { + if (Globals::globalConfig.bVerbose) + std::cerr << "skipped ignorelisted file " << filepath << std::endl; + } + else + { + filepath_vector.push_back(dir_iter->path()); + } + } + dir_iter++; + } + } + } + else + std::cerr << install_path << " does not exist" << std::endl; + } + catch (const boost::filesystem::filesystem_error& ex) + { + std::cout << ex.what() << std::endl; + } + + std::sort(item_paths.begin(), item_paths.end()); + std::sort(filepath_vector.begin(), filepath_vector.end()); + + if (!filepath_vector.empty()) + { + for (unsigned int i = 0; i < filepath_vector.size(); ++i) + { + bool bFileIsOrphaned = true; + for (std::vector::iterator it = item_paths.begin(); it != item_paths.end(); it++) + { + boost::filesystem::path item_path = *it; + boost::filesystem::path file_path = filepath_vector[i].native(); + + if (item_path == file_path) + { + bFileIsOrphaned = false; + item_paths.erase(it); + break; + } + } + + if (bFileIsOrphaned) + orphans.push_back(filepath_vector[i].string()); + } + } + + return orphans; +}