Harden CacheDownload.

It's now super hard. SRSLY.
This commit is contained in:
Petr Mrázek 2014-01-11 02:06:22 +01:00
parent 8e286c2b5c
commit 43a39a3bfb
3 changed files with 41 additions and 25 deletions

View File

@ -25,6 +25,8 @@ IF(UNIX)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
ENDIF() ENDIF()
set(CMAKE_JAVA_TARGET_OUTPUT_DIR ${PROJECT_BINARY_DIR}/jars)
######## Set compiler flags ######## ######## Set compiler flags ########
IF(APPLE) IF(APPLE)
message(STATUS "Using APPLE CMAKE_CXX_FLAGS") message(STATUS "Using APPLE CMAKE_CXX_FLAGS")

View File

@ -33,8 +33,10 @@ CacheDownload::CacheDownload(QUrl url, MetaEntryPtr entry)
void CacheDownload::start() void CacheDownload::start()
{ {
m_status = Job_InProgress;
if (!m_entry->stale) if (!m_entry->stale)
{ {
m_status = Job_Finished;
emit succeeded(m_index_within_job); emit succeeded(m_index_within_job);
return; return;
} }
@ -43,12 +45,14 @@ void CacheDownload::start()
if (!ensureFilePathExists(m_target_path)) if (!ensureFilePathExists(m_target_path))
{ {
QLOG_ERROR() << "Could not create folder for " + m_target_path; QLOG_ERROR() << "Could not create folder for " + m_target_path;
m_status = Job_Failed;
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; 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"; QLOG_ERROR() << "Could not open " + m_target_path + " for writing";
m_status = Job_Failed;
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
@ -90,12 +94,21 @@ void CacheDownload::downloadError(QNetworkReply::NetworkError error)
void CacheDownload::downloadFinished() void CacheDownload::downloadFinished()
{ {
// if the download succeeded // 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... // nothing went wrong...
m_status = Job_Finished;
if (m_output_file.commit()) if (m_output_file.commit())
{ {
m_status = Job_Finished;
m_entry->md5sum = md5sum.result().toHex().constData(); m_entry->md5sum = md5sum.result().toHex().constData();
} }
else else
@ -103,9 +116,15 @@ void CacheDownload::downloadFinished()
QLOG_ERROR() << "Failed to commit changes to " << m_target_path; QLOG_ERROR() << "Failed to commit changes to " << m_target_path;
m_output_file.cancelWriting(); m_output_file.cancelWriting();
m_reply.reset(); m_reply.reset();
m_status = Job_Failed;
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
}
else
{
m_status = Job_Finished;
}
QFileInfo output_file_info(m_target_path); QFileInfo output_file_info(m_target_path);
@ -123,15 +142,6 @@ void CacheDownload::downloadFinished()
emit succeeded(m_index_within_job); emit succeeded(m_index_within_job);
return; return;
} }
// else the download failed
else
{
m_output_file.cancelWriting();
m_reply.reset();
emit failed(m_index_within_job);
return;
}
}
void CacheDownload::downloadReadyRead() void CacheDownload::downloadReadyRead()
{ {
@ -140,7 +150,9 @@ void CacheDownload::downloadReadyRead()
if (m_output_file.write(ba) != ba.size()) if (m_output_file.write(ba) != ba.size())
{ {
QLOG_ERROR() << "Failed writing into " + m_target_path; QLOG_ERROR() << "Failed writing into " + m_target_path;
m_status = Job_Failed;
m_reply->abort(); m_reply->abort();
emit failed(m_index_within_job); emit failed(m_index_within_job);
} }
wroteAnyData = true;
} }

View File

@ -33,6 +33,8 @@ public:
/// the hash-as-you-download /// the hash-as-you-download
QCryptographicHash md5sum; QCryptographicHash md5sum;
bool wroteAnyData = false;
public: public:
explicit CacheDownload(QUrl url, MetaEntryPtr entry); explicit CacheDownload(QUrl url, MetaEntryPtr entry);
static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry) static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry)