diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-02-26 21:48:31 +0200 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-02-26 21:48:31 +0200 |
commit | 96150f1aeacf55d311c41ffe12d9e754b1cda001 (patch) | |
tree | 94252db740b9c2b6ad8122523baa732c0b913fbc | |
parent | 4c04665b29cd7304717cefe49d70d881d8b38ca3 (diff) | |
download | manaplus-96150f1aeacf55d311c41ffe12d9e754b1cda001.tar.gz manaplus-96150f1aeacf55d311c41ffe12d9e754b1cda001.tar.bz2 manaplus-96150f1aeacf55d311c41ffe12d9e754b1cda001.tar.xz manaplus-96150f1aeacf55d311c41ffe12d9e754b1cda001.zip |
Add ability to redownload update files if file checksum incorrect.
-rw-r--r-- | src/gui/updatewindow.cpp | 14 | ||||
-rw-r--r-- | src/gui/updatewindow.h | 2 | ||||
-rw-r--r-- | src/net/download.cpp | 39 | ||||
-rw-r--r-- | src/net/download.h | 2 |
4 files changed, 37 insertions, 20 deletions
diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 132f8bcfc..f4281cff1 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -635,7 +635,8 @@ void UpdaterWindow::logic() std::ifstream temp( (mUpdatesDir + "/" + mCurrentFile).c_str()); - if (!temp.is_open()) + if (!temp.is_open() || !validateFile(mUpdatesDir + "/" + + mCurrentFile, mCurrentChecksum)) { temp.close(); download(); @@ -671,3 +672,14 @@ void UpdaterWindow::logic() break; } } + +bool UpdaterWindow::validateFile(std::string filePath, long hash) +{ + FILE *file = fopen(filePath.c_str(), "rb"); + if (!file) + return false; + + unsigned long adler = Net::Download::fadler32(file); + fclose(file); + return adler == hash; +} diff --git a/src/gui/updatewindow.h b/src/gui/updatewindow.h index 1246c9b35..13b72f3a8 100644 --- a/src/gui/updatewindow.h +++ b/src/gui/updatewindow.h @@ -136,6 +136,8 @@ private: static size_t memoryWrite(void *ptr, size_t size, size_t nmemb, void *stream); + bool validateFile(std::string filePath, long hash); + enum UpdateDownloadStatus { UPDATE_ERROR = 0, diff --git a/src/net/download.cpp b/src/net/download.cpp index 95dfcd298..40d7c872a 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -37,25 +37,6 @@ const char *DOWNLOAD_ERROR_MESSAGE_THREAD = "Could not create download thread!"; -/** - * Calculates the Alder-32 checksum for the given file. - */ -static unsigned long fadler32(FILE *file) -{ - // Obtain file size - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); - rewind(file); - - // Calculate Adler-32 checksum - char *buffer = static_cast<char*>(malloc(fileSize)); - const size_t read = fread(buffer, 1, fileSize, file); - unsigned long adler = adler32(0L, Z_NULL, 0); - adler = adler32(static_cast<uInt>(adler), (Bytef*)buffer, read); - free(buffer); - - return adler; -} enum { @@ -97,6 +78,26 @@ Download::~Download() free(mError); } +/** + * Calculates the Alder-32 checksum for the given file. + */ +unsigned long Download::fadler32(FILE *file) +{ + // Obtain file size + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + rewind(file); + + // Calculate Adler-32 checksum + char *buffer = static_cast<char*>(malloc(fileSize)); + const size_t read = fread(buffer, 1, fileSize, file); + unsigned long adler = adler32(0L, Z_NULL, 0); + adler = adler32(static_cast<uInt>(adler), (Bytef*)buffer, read); + free(buffer); + + return adler; +} + void Download::addHeader(const std::string &header) { mHeaders = curl_slist_append(mHeaders, header.c_str()); diff --git a/src/net/download.h b/src/net/download.h index bb931bb15..71bb41cee 100644 --- a/src/net/download.h +++ b/src/net/download.h @@ -95,6 +95,8 @@ class Download void setIgnoreError(bool n) { mIgnoreError = n; } + static unsigned long fadler32(FILE *file); + private: static int downloadThread(void *ptr); static int downloadProgress(void *clientp, double dltotal, |