Added --check-orphans to check for orphaned files

Checks for orphaned files (files found on local filesystem that are not found on GOG servers) and prints their path
This commit is contained in:
Sude 2013-10-14 22:31:12 +03:00
parent 34236e3774
commit f2794ededf
4 changed files with 121 additions and 1 deletions

View File

@ -32,6 +32,7 @@ class Config
bool bNoUnicode; // don't use Unicode in console output bool bNoUnicode; // don't use Unicode in console output
bool bNoColor; // don't use colors bool bNoColor; // don't use colors
bool bVerifyPeer; bool bVerifyPeer;
bool bCheckOrphans;
std::string sGameRegex; std::string sGameRegex;
std::string sDirectory; std::string sDirectory;
std::string sXMLFile; std::string sXMLFile;

View File

@ -48,6 +48,7 @@ class Downloader
void updateCheck(); void updateCheck();
void repair(); void repair();
void download(); void download();
void checkOrphans();
CURL* curlhandle; CURL* curlhandle;
Timer timer; Timer timer;
Config config; Config config;

View File

@ -103,6 +103,7 @@ int main(int argc, char *argv[])
("verbose", bpo::value<bool>(&config.bVerbose)->zero_tokens()->default_value(false), "Print lots of information") ("verbose", bpo::value<bool>(&config.bVerbose)->zero_tokens()->default_value(false), "Print lots of information")
("insecure", bpo::value<bool>(&bInsecure)->zero_tokens()->default_value(false), "Don't verify authenticity of SSL certificates") ("insecure", bpo::value<bool>(&bInsecure)->zero_tokens()->default_value(false), "Don't verify authenticity of SSL certificates")
("timeout", bpo::value<long int>(&config.iTimeout)->default_value(10), "Set timeout for connection\nMaximum time in seconds that connection phase is allowed to take") ("timeout", bpo::value<long int>(&config.iTimeout)->default_value(10), "Set timeout for connection\nMaximum time in seconds that connection phase is allowed to take")
("check-orphans", bpo::value<bool>(&config.bCheckOrphans)->zero_tokens()->default_value(false), "Check for orphaned files (files found on local filesystem that are not found on GOG servers)")
; ;
bpo::store(bpo::parse_command_line(argc, argv, desc), vm); bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
@ -200,6 +201,8 @@ int main(int argc, char *argv[])
downloader.download(); downloader.download();
else if (config.bListDetails || config.bList) // Detailed list of games/extras else if (config.bListDetails || config.bList) // Detailed list of games/extras
downloader.listGames(); downloader.listGames();
else if (config.bCheckOrphans)
downloader.checkOrphans();
else else
{ // Show help message { // Show help message
std::cout << config.sVersionString << std::endl std::cout << config.sVersionString << std::endl

View File

@ -77,6 +77,14 @@ int Downloader::init()
if (!config.bNoCover && config.bDownload && !config.bUpdateCheck) if (!config.bNoCover && config.bDownload && !config.bUpdateCheck)
coverXML = this->getResponse("https://sites.google.com/site/gogdownloader/GOG_covers_v2.xml"); coverXML = this->getResponse("https://sites.google.com/site/gogdownloader/GOG_covers_v2.xml");
if (config.bCheckOrphans) // Always check everything when checking for orphaned files
{
config.bNoInstallers = false;
config.bNoExtras = false;
config.bNoPatches = false;
config.bNoLanguagePacks = false;
}
if (!config.bUpdateCheck) // updateCheck() calls getGameList() if needed if (!config.bUpdateCheck) // updateCheck() calls getGameList() if needed
this->getGameList(); this->getGameList();
@ -200,7 +208,7 @@ void Downloader::getGameList()
} }
} }
if (config.bListDetails || config.bDownload || config.bRepair) if (config.bListDetails || config.bDownload || config.bRepair || config.bCheckOrphans)
this->getGameDetails(); this->getGameDetails();
} }
@ -1401,3 +1409,110 @@ std::vector<gameFile> Downloader::getExtras(const std::string& gamename, const s
return extras; return extras;
} }
void Downloader::checkOrphans()
{
std::vector<std::string> orphans;
for (unsigned int i = 0; i < games.size(); ++i)
{
std::cout << "Checking for orphaned files " << i+1 << " / " << games.size() << "\r" << std::flush;
boost::filesystem::path path (config.sDirectory + games[i].gamename);
std::vector<boost::filesystem::path> filepath_vector;
try
{
if (boost::filesystem::exists(path))
{
if (boost::filesystem::is_directory(path))
{
boost::filesystem::recursive_directory_iterator end_iter;
boost::filesystem::recursive_directory_iterator dir_iter(path);
while (dir_iter != end_iter)
{
if (boost::filesystem::is_regular_file(dir_iter->status()))
{
std::string filename = dir_iter->path().filename().string();
boost::regex expression(".*\\.(zip|exe|bin|dmg|old)$");
boost::match_results<std::string::const_iterator> what;
if (boost::regex_search(filename, what, expression))
filepath_vector.push_back(dir_iter->path());
}
dir_iter++;
}
}
}
else
std::cout << path << " does not exist" << std::endl;
}
catch (const boost::filesystem::filesystem_error& ex)
{
std::cout << ex.what() << std::endl;
}
if (!filepath_vector.empty())
{
for (unsigned int j = 0; j < filepath_vector.size(); ++j)
{
bool bFoundFile = false;
for (unsigned int k = 0; k < games[i].installers.size(); ++k)
{
if (games[i].installers[k].path.find(filepath_vector[j].filename().string()) != std::string::npos)
{
bFoundFile = true;
break;
}
}
if (!bFoundFile)
{
for (unsigned int k = 0; k < games[i].extras.size(); ++k)
{
if (games[i].extras[k].path.find(filepath_vector[j].filename().string()) != std::string::npos)
{
bFoundFile = true;
break;
}
}
}
if (!bFoundFile)
{
for (unsigned int k = 0; k < games[i].patches.size(); ++k)
{
if (games[i].patches[k].path.find(filepath_vector[j].filename().string()) != std::string::npos)
{
bFoundFile = true;
break;
}
}
}
if (!bFoundFile)
{
for (unsigned int k = 0; k < games[i].languagepacks.size(); ++k)
{
if (games[i].languagepacks[k].path.find(filepath_vector[j].filename().string()) != std::string::npos)
{
bFoundFile = true;
break;
}
}
}
if (!bFoundFile)
orphans.push_back(filepath_vector[j].string());
}
}
}
std::cout << std::endl;
if (!orphans.empty())
{
for (unsigned int i = 0; i < orphans.size(); ++i)
{
std::cout << orphans[i] << std::endl;
}
}
else
{
std::cout << "No orphaned files" << std::endl;
}
return;
}