From bd1d34d7781d35f17666adb3df6abb7c9b7fcc32 Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Fri, 2 Dec 2011 03:54:00 +0200 Subject: MD5 hash check for existing & downloaded files --- client/DownloadClient.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'client/DownloadClient.cpp') diff --git a/client/DownloadClient.cpp b/client/DownloadClient.cpp index 3fe7b59..723dbcb 100644 --- a/client/DownloadClient.cpp +++ b/client/DownloadClient.cpp @@ -1,6 +1,7 @@ #include #include "DownloadClient.h" +#include "Utility.h" DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId(-1) @@ -9,7 +10,6 @@ DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId DownloadClient::DownloadClient(QString addr): SslClient(addr), isDownloading(false), current(NULL), currentId(-1) { - DownloadClient(); } void DownloadClient::Download(const string &filename) @@ -73,7 +73,6 @@ void DownloadClient::DoDownload() void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &data) { Logger::Trace("Recieved file data: %u bytes\n", data.size()); - if (type != RegularFile) { Logger::Error("Invalid reply recieved: %x\n", type); @@ -85,8 +84,6 @@ void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray & return; } - pair file = files[currentId]; - string filename = file.first; if (data.size() != 0) { current->write(data); @@ -94,15 +91,27 @@ void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray & } else { - Logger::Trace("Last (empty) packet recieved\n"); + Logger::Trace("Last (empty) packet recieved\n"); current->close(); + + // check file's md5 hash value + pair file = files[currentId]; + QString md5string = QString::fromLocal8Bit(file.second.c_str()); + QString filename = QString::fromLocal8Bit(file.first.c_str()); + QByteArray md5 = md5_sum(filename); + if (md5 == md5string) + { + Logger::Trace("File %s was downloaded correctly!\n", filename.toStdString().c_str()); + } + else + { + Logger::Error("File %s was downloaded incorrectly! Removing and trying again!\n", filename.toStdString().c_str()); + current->remove(); + currentId--; + } + // free pointer to the current file delete current; current = NULL; - - /** - * @todo md5 hash check - */ - // proceed to the next file DoDownload(); } -- cgit v1.2.3