summaryrefslogtreecommitdiff
path: root/src/net/download.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-29 17:25:29 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-29 17:25:29 +0100
commit2e60491ceb0548b0bea93207c13b974d6a6cf5cc (patch)
tree43ea8aefa4aa1646df82a5163b9cdae0a39df48e /src/net/download.cpp
parentdd1db3753ecf8a4d782aee1f518759763d8caf87 (diff)
downloadmana-2e60491ceb0548b0bea93207c13b974d6a6cf5cc.tar.gz
mana-2e60491ceb0548b0bea93207c13b974d6a6cf5cc.tar.bz2
mana-2e60491ceb0548b0bea93207c13b974d6a6cf5cc.tar.xz
mana-2e60491ceb0548b0bea93207c13b974d6a6cf5cc.zip
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.
Diffstat (limited to 'src/net/download.cpp')
-rw-r--r--src/net/download.cpp45
1 files changed, 20 insertions, 25 deletions
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<unsigned long> 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
}