From f671834068965e9b4d4d19cbc4d401b63e29b341 Mon Sep 17 00:00:00 2001 From: Sude Date: Wed, 26 Feb 2014 14:59:23 +0200 Subject: [PATCH] Retry failed/partially downloaded files Added --retries option to set maximum number of retries --- include/config.h | 1 + include/downloader.h | 1 + main.cpp | 1 + src/downloader.cpp | 14 +++++++++++++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/config.h b/include/config.h index 60ddbaf..784d8cb 100644 --- a/include/config.h +++ b/include/config.h @@ -50,6 +50,7 @@ class Config std::string sOrphanRegex; unsigned int iInstallerType; unsigned int iInstallerLanguage; + int iRetries; size_t iChunkSize; curl_off_t iDownloadRate; long int iTimeout; diff --git a/include/downloader.h b/include/downloader.h index fbeee33..aee850f 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -78,6 +78,7 @@ class Downloader std::string coverXML; size_t resume_position; + int retries; std::ofstream report_ofs; }; diff --git a/main.cpp b/main.cpp index 7f36985..bf5f26a 100644 --- a/main.cpp +++ b/main.cpp @@ -151,6 +151,7 @@ int main(int argc, char *argv[]) ("verbose", bpo::value(&config.bVerbose)->zero_tokens()->default_value(false), "Print lots of information") ("insecure", bpo::value(&bInsecure)->zero_tokens()->default_value(false), "Don't verify authenticity of SSL certificates") ("timeout", bpo::value(&config.iTimeout)->default_value(10), "Set timeout for connection\nMaximum time in seconds that connection phase is allowed to take") + ("retries", bpo::value(&config.iRetries)->default_value(3), "Set maximum number of retries on failed download") ; // Options read from config file options_cfg_only.add_options() diff --git a/src/downloader.cpp b/src/downloader.cpp index 310538f..ff49025 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -49,6 +49,7 @@ Downloader::~Downloader() int Downloader::init() { this->resume_position = 0; + this->retries = 0; // Initialize curl and set curl options curl_global_init(CURL_GLOBAL_ALL); @@ -765,7 +766,7 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil fclose(outfile); // Download failed and was not a resume attempt so delete the file - if (res != CURLE_OK && !bResume) + if ( (res != CURLE_OK || res != CURLE_PARTIAL_FILE) && !bResume ) { boost::filesystem::path path = filepath; if (boost::filesystem::exists(path)) @@ -782,6 +783,17 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil this->report_ofs << report_line << std::endl; } + // Retry partially downloaded file + if (res == CURLE_PARTIAL_FILE && (this->retries < config.iRetries) ) + { + this->retries++; + res = this->downloadFile(url, filepath, xml_data); + } + else + { + this->retries = 0; // Reset retries counter + } + return res; }