From 2e60491ceb0548b0bea93207c13b974d6a6cf5cc Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 29 Feb 2024 17:25:29 +0100 Subject: Re-download updates when their checksum no longer matches The Mana World currently likes to just update its "TMW.zip" file, whereas updates were always given unique names in the past. With this change, the client checks the Adler32 checksum to know when it should re-download an update file. This matches the behavior of ManaPlus commit 96150f1aeacf55d311c41ffe12d9e754b1cda001. --- src/net/download.cpp | 45 ++++++++++++++++++++------------------------- src/net/download.h | 11 +++++++---- 2 files changed, 27 insertions(+), 29 deletions(-) (limited to 'src/net') diff --git a/src/net/download.cpp b/src/net/download.cpp index dc928d56..8a41ebfa 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -33,32 +33,33 @@ const char *DOWNLOAD_ERROR_MESSAGE_THREAD = "Could not create download thread!"; +namespace Net { + /** * Calculates the Alder-32 checksum for the given file. */ -static unsigned long fadler32(FILE *file) +unsigned long Download::fadler32(FILE *file) { - // Obtain file size - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); + if (!file || fseek(file, 0, SEEK_END) != 0) + return 0; + + const long fileSize = ftell(file); + if (fileSize < 0) + return 0; + rewind(file); // Calculate Adler-32 checksum - char *buffer = (char*) malloc(fileSize); + void *buffer = malloc(fileSize); const size_t read = fread(buffer, 1, fileSize, file); - unsigned long adler = adler32(0L, Z_NULL, 0); - adler = adler32(adler, (Bytef*) buffer, read); + unsigned long adler = adler32_z(0L, Z_NULL, 0); + adler = adler32_z(adler, (Bytef*) buffer, read); free(buffer); return adler; } -enum { - OPTIONS_NONE = 0, - OPTIONS_MEMORY = 1 -}; -namespace Net{ Download::Download(void *ptr, const std::string &url, DownloadUpdate updateFunction): mPtr(ptr), @@ -92,18 +93,12 @@ void Download::noCache() addHeader("Cache-Control: no-cache"); } -void Download::setFile(const std::string &filename, int64_t adler32) +void Download::setFile(const std::string &filename, + std::optional adler32) { mOptions.memoryWrite = false; mFileName = filename; - - if (adler32 > -1) - { - mAdler = (unsigned long) adler32; - mOptions.checkAdler = true; - } - else - mOptions.checkAdler = false; + mAdler = adler32; } void Download::setWriteFunction(WriteFunction write) @@ -142,7 +137,7 @@ void Download::cancel() } } -char *Download::getError() +const char *Download::getError() const { return mError; } @@ -223,7 +218,7 @@ int Download::downloadThread(void *ptr) curl_easy_setopt(d->mCurl, CURLOPT_URL, d->mUrl.c_str()); curl_easy_setopt(d->mCurl, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(d->mCurl, CURLOPT_XFERINFOFUNCTION, downloadProgress); - curl_easy_setopt(d->mCurl, CURLOPT_PROGRESSDATA, ptr); + curl_easy_setopt(d->mCurl, CURLOPT_XFERINFODATA, ptr); curl_easy_setopt(d->mCurl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(d->mCurl, CURLOPT_CONNECTTIMEOUT, 15); @@ -262,7 +257,7 @@ int Download::downloadThread(void *ptr) if (!d->mOptions.memoryWrite) { // Don't check resources.xml checksum - if (d->mOptions.checkAdler) + if (d->mAdler) { unsigned long adler = fadler32(file); @@ -274,7 +269,7 @@ int Download::downloadThread(void *ptr) ::remove(d->mFileName.c_str()); logger->log("Checksum for file %s failed: (%lx/%lx)", d->mFileName.c_str(), - adler, d->mAdler); + adler, *d->mAdler); attempts++; continue; // Bail out here to avoid the renaming } diff --git a/src/net/download.h b/src/net/download.h index 500de0b6..0ce8cc8a 100644 --- a/src/net/download.h +++ b/src/net/download.h @@ -21,6 +21,7 @@ #include // pulls in int64_t #include #include +#include #include @@ -61,7 +62,8 @@ class Download */ void noCache(); - void setFile(const std::string &filename, int64_t adler32 = -1); + void setFile(const std::string &filename, + std::optional adler32 = {}); void setWriteFunction(WriteFunction write); @@ -79,7 +81,9 @@ class Download */ void cancel(); - char *getError(); + const char *getError() const; + + static unsigned long fadler32(FILE *file); private: static int downloadThread(void *ptr); @@ -91,11 +95,10 @@ class Download struct { unsigned cancel : 1; unsigned memoryWrite: 1; - unsigned checkAdler: 1; } mOptions; std::string mFileName; WriteFunction mWriteFunction = nullptr; - unsigned long mAdler; + std::optional mAdler; DownloadUpdate mUpdateFunction; SDL_Thread *mThread = nullptr; CURL *mCurl = nullptr; -- cgit v1.2.3-70-g09d2