Fix --repair on 32 bit platforms for files > 2 GB.

This commit is contained in:
Erik Fleischer 2015-07-05 19:23:11 +02:00
parent 7e8f707846
commit fb23318d5a

View File

@ -1225,6 +1225,7 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil
{ {
bResume = true; bResume = true;
fseek(outfile, 0, SEEK_END); fseek(outfile, 0, SEEK_END);
// use ftello to support large files on 32 bit platforms
offset = ftello(outfile); offset = ftello(outfile);
curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, offset); curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, offset);
this->resume_position = offset; this->resume_position = offset;
@ -1366,10 +1367,10 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
{ {
int res = 0; int res = 0;
FILE *outfile; FILE *outfile;
uintmax_t offset=0, from_offset, to_offset, filesize; off_t offset=0, from_offset, to_offset, filesize;
std::string filehash; std::string filehash;
int chunks; int chunks;
std::vector<uintmax_t> chunk_from, chunk_to; std::vector<off_t> chunk_from, chunk_to;
std::vector<std::string> chunk_hash; std::vector<std::string> chunk_hash;
bool bParsingFailed = false; bool bParsingFailed = false;
@ -1475,7 +1476,8 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
if ((outfile = fopen(filepath.c_str(), "r+"))!=NULL ) if ((outfile = fopen(filepath.c_str(), "r+"))!=NULL )
{ {
fseek(outfile, 0, SEEK_END); fseek(outfile, 0, SEEK_END);
offset = ftell(outfile); // use ftello to support large files on 32 bit platforms
offset = ftello(outfile);
} }
else else
{ {
@ -1504,6 +1506,7 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
return res; return res;
} }
// check if file sizes match
if (offset != filesize) if (offset != filesize)
{ {
std::cout << "Filesizes don't match" << std::endl std::cout << "Filesizes don't match" << std::endl
@ -1561,13 +1564,14 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
int iChunksRepaired = 0; int iChunksRepaired = 0;
for (int i=0; i<chunks; i++) for (int i=0; i<chunks; i++)
{ {
uintmax_t chunk_begin = chunk_from.at(i); off_t chunk_begin = chunk_from.at(i);
uintmax_t chunk_end = chunk_to.at(i); off_t chunk_end = chunk_to.at(i);
uintmax_t size=0, chunk_size = chunk_end - chunk_begin + 1; off_t size=0, chunk_size = chunk_end - chunk_begin + 1;
std::string range = std::to_string(chunk_begin) + "-" + std::to_string(chunk_end); // Download range string for curl std::string range = std::to_string(chunk_begin) + "-" + std::to_string(chunk_end); // Download range string for curl
std::cout << "\033[0K\rChunk " << i << " (" << chunk_size << " bytes): "; std::cout << "\033[0K\rChunk " << i << " (" << chunk_size << " bytes): ";
fseek(outfile, chunk_begin, SEEK_SET); // use fseeko to support large files on 32 bit platforms
fseeko(outfile, chunk_begin, SEEK_SET);
unsigned char *chunk = (unsigned char *) malloc(chunk_size * sizeof(unsigned char *)); unsigned char *chunk = (unsigned char *) malloc(chunk_size * sizeof(unsigned char *));
if (chunk == NULL) if (chunk == NULL)
{ {
@ -1587,7 +1591,8 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
if (hash != chunk_hash.at(i)) if (hash != chunk_hash.at(i))
{ {
std::cout << "Failed - downloading chunk" << std::endl; std::cout << "Failed - downloading chunk" << std::endl;
fseek(outfile, chunk_begin, SEEK_SET); // use fseeko to support large files on 32 bit platforms
fseeko(outfile, chunk_begin, SEEK_SET);
curl_easy_setopt(curlhandle, CURLOPT_URL, url.c_str()); curl_easy_setopt(curlhandle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, outfile); curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, outfile);
curl_easy_setopt(curlhandle, CURLOPT_RANGE, range.c_str()); //download range curl_easy_setopt(curlhandle, CURLOPT_RANGE, range.c_str()); //download range