From 43a39a3bfbffa2997cf8ca5126ee3ac5157f62c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 11 Jan 2014 02:06:22 +0100 Subject: [PATCH] Harden CacheDownload. It's now super hard. SRSLY. --- CMakeLists.txt | 2 ++ logic/net/CacheDownload.cpp | 62 ++++++++++++++++++++++--------------- logic/net/CacheDownload.h | 2 ++ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d3779a74a..cab0b51c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ IF(UNIX) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) ENDIF() +set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${PROJECT_BINARY_DIR}/jars) + ######## Set compiler flags ######## IF(APPLE) message(STATUS "Using APPLE CMAKE_CXX_FLAGS") diff --git a/logic/net/CacheDownload.cpp b/logic/net/CacheDownload.cpp index 17fd7f2aa..8a8d00f00 100644 --- a/logic/net/CacheDownload.cpp +++ b/logic/net/CacheDownload.cpp @@ -33,8 +33,10 @@ CacheDownload::CacheDownload(QUrl url, MetaEntryPtr entry) void CacheDownload::start() { + m_status = Job_InProgress; if (!m_entry->stale) { + m_status = Job_Finished; emit succeeded(m_index_within_job); return; } @@ -43,12 +45,14 @@ void CacheDownload::start() if (!ensureFilePathExists(m_target_path)) { QLOG_ERROR() << "Could not create folder for " + m_target_path; + m_status = Job_Failed; emit failed(m_index_within_job); return; } - if(!m_output_file.open(QIODevice::WriteOnly)) + if (!m_output_file.open(QIODevice::WriteOnly)) { QLOG_ERROR() << "Could not open " + m_target_path + " for writing"; + m_status = Job_Failed; emit failed(m_index_within_job); return; } @@ -90,12 +94,21 @@ void CacheDownload::downloadError(QNetworkReply::NetworkError error) void CacheDownload::downloadFinished() { // if the download succeeded - if (m_status != Job_Failed) + if (m_status == Job_Failed) + { + m_output_file.cancelWriting(); + m_reply.reset(); + m_status = Job_Failed; + emit failed(m_index_within_job); + return; + } + + if (wroteAnyData) { // nothing went wrong... - m_status = Job_Finished; if (m_output_file.commit()) { + m_status = Job_Finished; m_entry->md5sum = md5sum.result().toHex().constData(); } else @@ -103,44 +116,43 @@ void CacheDownload::downloadFinished() QLOG_ERROR() << "Failed to commit changes to " << m_target_path; m_output_file.cancelWriting(); m_reply.reset(); + m_status = Job_Failed; emit failed(m_index_within_job); return; } - - QFileInfo output_file_info(m_target_path); - - m_entry->etag = m_reply->rawHeader("ETag").constData(); - if (m_reply->hasRawHeader("Last-Modified")) - { - m_entry->remote_changed_timestamp = m_reply->rawHeader("Last-Modified").constData(); - } - m_entry->local_changed_timestamp = - output_file_info.lastModified().toUTC().toMSecsSinceEpoch(); - m_entry->stale = false; - MMC->metacache()->updateEntry(m_entry); - - m_reply.reset(); - emit succeeded(m_index_within_job); - return; } - // else the download failed else { - m_output_file.cancelWriting(); - m_reply.reset(); - emit failed(m_index_within_job); - return; + m_status = Job_Finished; } + + QFileInfo output_file_info(m_target_path); + + m_entry->etag = m_reply->rawHeader("ETag").constData(); + if (m_reply->hasRawHeader("Last-Modified")) + { + m_entry->remote_changed_timestamp = m_reply->rawHeader("Last-Modified").constData(); + } + m_entry->local_changed_timestamp = + output_file_info.lastModified().toUTC().toMSecsSinceEpoch(); + m_entry->stale = false; + MMC->metacache()->updateEntry(m_entry); + + m_reply.reset(); + emit succeeded(m_index_within_job); + return; } void CacheDownload::downloadReadyRead() { QByteArray ba = m_reply->readAll(); md5sum.addData(ba); - if(m_output_file.write(ba) != ba.size()) + if (m_output_file.write(ba) != ba.size()) { QLOG_ERROR() << "Failed writing into " + m_target_path; + m_status = Job_Failed; m_reply->abort(); emit failed(m_index_within_job); } + wroteAnyData = true; } diff --git a/logic/net/CacheDownload.h b/logic/net/CacheDownload.h index 921e231b3..48be1dae2 100644 --- a/logic/net/CacheDownload.h +++ b/logic/net/CacheDownload.h @@ -33,6 +33,8 @@ public: /// the hash-as-you-download QCryptographicHash md5sum; + bool wroteAnyData = false; + public: explicit CacheDownload(QUrl url, MetaEntryPtr entry); static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry)