summaryrefslogtreecommitdiff
path: root/src/gui/updatewindow.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-11 19:59:55 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-11 19:59:55 +0000
commitfc63e88f869919a8b035a5c41db8d05626526bde (patch)
tree8f1928d082ebbffc549c712a3f406c2141f43e01 /src/gui/updatewindow.cpp
parent7b0f0ec315e66dcbdda58ada8b09a59bc5dcf6c9 (diff)
downloadmana-fc63e88f869919a8b035a5c41db8d05626526bde.tar.gz
mana-fc63e88f869919a8b035a5c41db8d05626526bde.tar.bz2
mana-fc63e88f869919a8b035a5c41db8d05626526bde.tar.xz
mana-fc63e88f869919a8b035a5c41db8d05626526bde.zip
Merged 0.0 changes from revision 3065 to 3177 to trunk.
Diffstat (limited to 'src/gui/updatewindow.cpp')
-rw-r--r--src/gui/updatewindow.cpp131
1 files changed, 81 insertions, 50 deletions
diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp
index fe78a27b..d8130cd3 100644
--- a/src/gui/updatewindow.cpp
+++ b/src/gui/updatewindow.cpp
@@ -46,13 +46,41 @@
#include "../resources/resourcemanager.h"
+/**
+ * Calculates the Alder-32 checksum for the given file.
+ */
+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 = (char*) malloc(fileSize);
+ fread(buffer, 1, fileSize, file);
+ unsigned long adler = adler32(0L, Z_NULL, 0);
+ adler = adler32(adler, (Bytef*) buffer, fileSize);
+ free(buffer);
+
+ return adler;
+}
+
UpdaterWindow::UpdaterWindow():
Window("Updating..."),
- mThread(NULL), mMutex(NULL), mDownloadStatus(UPDATE_NEWS),
- mUpdateHost(""), mCurrentFile("news.txt"), mBasePath(""),
- mStoreInMemory(true), mDownloadComplete(true), mUserCancel(false),
- mDownloadedBytes(0), mMemoryBuffer(NULL),
- mCurlError(new char[CURL_ERROR_SIZE]), mLineIndex(0)
+ mThread(NULL),
+ mDownloadStatus(UPDATE_NEWS),
+ mUpdateHost(""),
+ mCurrentFile("news.txt"),
+ mCurrentChecksum(0),
+ mBasePath(""),
+ mStoreInMemory(true),
+ mDownloadComplete(true),
+ mUserCancel(false),
+ mDownloadedBytes(0),
+ mMemoryBuffer(NULL),
+ mCurlError(new char[CURL_ERROR_SIZE]),
+ mLineIndex(0)
{
mCurlError[0] = 0;
@@ -151,7 +179,7 @@ void UpdaterWindow::action(const gcn::ActionEvent &event)
}
else if (event.getId() == "play")
{
- state = STATE_LOGIN;
+ state = STATE_LOADDATA;
}
}
@@ -206,22 +234,17 @@ int UpdaterWindow::updateProgress(void *ptr,
return 0;
}
-size_t UpdaterWindow::memoryWrite(void *ptr,
- size_t size, size_t nmemb, FILE *stream)
+size_t
+UpdaterWindow::memoryWrite(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
UpdaterWindow *uw = reinterpret_cast<UpdaterWindow *>(stream);
size_t totalMem = size * nmemb;
- uw->mMemoryBuffer = (char*)realloc(uw->mMemoryBuffer,
- uw->mDownloadedBytes + totalMem + 1);
+ uw->mMemoryBuffer = (char*) realloc(uw->mMemoryBuffer,
+ uw->mDownloadedBytes + totalMem);
if (uw->mMemoryBuffer)
{
memcpy(&(uw->mMemoryBuffer[uw->mDownloadedBytes]), ptr, totalMem);
uw->mDownloadedBytes += totalMem;
-
- // Make sure the memory buffer is NULL terminated, because this
- // function is used to download text files that are later parsed as a
- // string.
- uw->mMemoryBuffer[uw->mDownloadedBytes] = 0;
}
return totalMem;
@@ -236,8 +259,10 @@ int UpdaterWindow::downloadThread(void *ptr)
std::string outFilename;
std::string url(uw->mUpdateHost + "/" + uw->mCurrentFile);
- while (attempts < 3 && !uw->mDownloadComplete) {
+ while (attempts < 3 && !uw->mDownloadComplete)
+ {
FILE *outfile = NULL;
+ FILE *newfile = NULL;
uw->setLabel(uw->mCurrentFile + " (0%)");
curl = curl_easy_init();
@@ -282,61 +307,67 @@ int UpdaterWindow::downloadThread(void *ptr)
uw->mDownloadStatus = UPDATE_ERROR;
switch (res)
{
- case CURLE_COULDNT_CONNECT: // give more debug info on that error
- std::cerr << "curl error " << res << " : " << uw->mCurlError << " " << url.c_str()
- << std::endl;
+ case CURLE_COULDNT_CONNECT:
+ // give more debug info on that error
+ std::cerr << "curl error " << res << ": "
+ << uw->mCurlError << " " << url.c_str()
+ << std::endl;
break;
default:
- std::cerr << "curl error " << res << " : " << uw->mCurlError << " host: " << url.c_str()
- << std::endl;
+ std::cerr << "curl error " << res << ": "
+ << uw->mCurlError << " host: " << url.c_str()
+ << std::endl;
}
}
curl_easy_cleanup(curl);
- uw->mDownloadComplete = true;
-
if (!uw->mStoreInMemory)
{
- long fileSize;
- char *buffer;
- // Obtain file size.
- fseek(outfile, 0, SEEK_END);
- fileSize = ftell(outfile);
- rewind(outfile);
- buffer = (char*)malloc(fileSize);
- fread(buffer, 1, fileSize, outfile);
- fclose(outfile);
-
- // Give the file the proper name
- std::string newName(uw->mBasePath + "/updates/" +
- uw->mCurrentFile.c_str());
-
- // Any existing file with this name is deleted first, otherwise the
- // rename will fail on Windows.
- ::remove(newName.c_str());
- ::rename(outFilename.c_str(), newName.c_str());
-
// Don't check resources2.txt checksum
if (uw->mDownloadStatus == UPDATE_RESOURCES)
{
- // Calculate Adler-32 checksum
- unsigned long adler = adler32(0L, Z_NULL, 0);
- adler = adler32(adler, (Bytef *)buffer, fileSize);
- free(buffer);
+ unsigned long adler = fadler32(outfile);
+
+ if (uw->mCurrentChecksum != adler)
+ {
+ fclose(outfile);
- if (uw->mCurrentChecksum != adler) {
- uw->mDownloadComplete = false;
// Remove the corrupted file
- ::remove(newName.c_str());
+ ::remove(outFilename.c_str());
logger->log(
"Checksum for file %s failed: (%lx/%lx)",
uw->mCurrentFile.c_str(),
adler, uw->mCurrentChecksum);
+ attempts++;
+ continue; // Bail out here to avoid the renaming
}
}
+ fclose(outfile);
+
+ // Give the file the proper name
+ std::string newName(uw->mBasePath + "/updates/" +
+ uw->mCurrentFile.c_str());
+ // Any existing file with this name is deleted first, otherwise
+ // the rename will fail on Windows.
+ ::remove(newName.c_str());
+ ::rename(outFilename.c_str(), newName.c_str());
+
+ // Check if we can open it and no errors were encountered
+ // during renaming
+ newfile = fopen(newName.c_str(), "rb");
+ if (newfile)
+ {
+ fclose(newfile);
+ uw->mDownloadComplete = true;
+ }
+ }
+ else
+ {
+ // It's stored in memory, we're done
+ uw->mDownloadComplete = true;
}
}
attempts++;
@@ -398,8 +429,8 @@ void UpdaterWindow::logic()
mCurrentFile = "resources2.txt";
mStoreInMemory = false;
- download();
mDownloadStatus = UPDATE_LIST;
+ download(); // download() changes mDownloadComplete to false
}
break;
case UPDATE_LIST: