From 19bfc8eacbc0a9bedac51a7e36a1113d049082f1 Mon Sep 17 00:00:00 2001 From: Floris Bos Date: Fri, 3 Jul 2020 21:08:51 +0200 Subject: [PATCH] Close files on error --- downloadthread.cpp | 56 ++++++++++++++++++++------------------ downloadthread.h | 1 + localfileextractthread.cpp | 3 ++ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/downloadthread.cpp b/downloadthread.cpp index 7972a90..903657e 100644 --- a/downloadthread.cpp +++ b/downloadthread.cpp @@ -530,34 +530,27 @@ void DownloadThread::_onDownloadError(const QString &msg) emit error(msg); } +void DownloadThread::_closeFiles() +{ + _file.close(); +#ifdef Q_OS_WIN + _volumeFile.close(); +#endif + if (_cachefile.isOpen()) + _cachefile.close(); +} + void DownloadThread::_writeComplete() { - if (!_file.flush()) - { - DownloadThread::_onDownloadError(tr("Error writing to storage (while flushing)")); - return; - } - -#ifndef Q_OS_WIN - if (::fsync(_file.handle()) != 0) { - DownloadThread::_onDownloadError(tr("Error writing to storage (while fsync)")); - return; - } -#endif - - qDebug() << "Write done in" << _timer.elapsed() / 1000 << "seconds"; QByteArray computedHash = _writehash.result().toHex(); qDebug() << "Hash of uncompressed image:" << computedHash; if (!_expectedHash.isEmpty() && _expectedHash != computedHash) { qDebug() << "Mismatch with expected hash:" << _expectedHash; - _file.close(); if (_cachefile.isOpen()) _cachefile.remove(); -#ifdef Q_OS_WIN - _volumeFile.close(); -#endif DownloadThread::_onDownloadError(tr("Download corrupt. Hash does not match")); + _closeFiles(); return; } if (_cacheEnabled && _expectedHash == computedHash) @@ -566,13 +559,27 @@ void DownloadThread::_writeComplete() emit cacheFileUpdated(computedHash); } + if (!_file.flush()) + { + DownloadThread::_onDownloadError(tr("Error writing to storage (while flushing)")); + _closeFiles(); + return; + } + +#ifndef Q_OS_WIN + if (::fsync(_file.handle()) != 0) { + DownloadThread::_onDownloadError(tr("Error writing to storage (while fsync)")); + _closeFiles(); + return; + } +#endif + + qDebug() << "Write done in" << _timer.elapsed() / 1000 << "seconds"; + /* Verify */ if (_verifyEnabled && !_verify()) { - _file.close(); -#ifdef Q_OS_WIN - _volumeFile.close(); -#endif + _closeFiles(); return; } @@ -595,10 +602,7 @@ void DownloadThread::_writeComplete() _firstBlock = nullptr; } - _file.close(); -#ifdef Q_OS_WIN - _volumeFile.close(); -#endif + _closeFiles(); #ifdef Q_OS_DARWIN QThread::sleep(1); diff --git a/downloadthread.h b/downloadthread.h index ba87521..2f46b95 100644 --- a/downloadthread.h +++ b/downloadthread.h @@ -139,6 +139,7 @@ protected: bool _openAndPrepareDevice(); void _writeCache(const char *buf, size_t len); qint64 _sectorsWritten(); + void _closeFiles(); /* * libcurl callbacks diff --git a/localfileextractthread.cpp b/localfileextractthread.cpp index 839def6..cc5081e 100644 --- a/localfileextractthread.cpp +++ b/localfileextractthread.cpp @@ -36,6 +36,7 @@ void LocalFileExtractThread::run() if (!_inputfile.open(_inputfile.ReadOnly)) { _onDownloadError(tr("Error opening image file")); + _closeFiles(); return; } _lastDlTotal = _inputfile.size(); @@ -45,6 +46,8 @@ void LocalFileExtractThread::run() else extractMultiFileRun(); + if (_cancelled) + _closeFiles(); } ssize_t LocalFileExtractThread::_on_read(struct archive *, const void **buff)