Close files on error

This commit is contained in:
Floris Bos 2020-07-03 21:08:51 +02:00
parent 9a263f0211
commit 19bfc8eacb
3 changed files with 34 additions and 26 deletions

View file

@ -530,34 +530,27 @@ void DownloadThread::_onDownloadError(const QString &msg)
emit error(msg); emit error(msg);
} }
void DownloadThread::_closeFiles()
{
_file.close();
#ifdef Q_OS_WIN
_volumeFile.close();
#endif
if (_cachefile.isOpen())
_cachefile.close();
}
void DownloadThread::_writeComplete() 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(); QByteArray computedHash = _writehash.result().toHex();
qDebug() << "Hash of uncompressed image:" << computedHash; qDebug() << "Hash of uncompressed image:" << computedHash;
if (!_expectedHash.isEmpty() && _expectedHash != computedHash) if (!_expectedHash.isEmpty() && _expectedHash != computedHash)
{ {
qDebug() << "Mismatch with expected hash:" << _expectedHash; qDebug() << "Mismatch with expected hash:" << _expectedHash;
_file.close();
if (_cachefile.isOpen()) if (_cachefile.isOpen())
_cachefile.remove(); _cachefile.remove();
#ifdef Q_OS_WIN
_volumeFile.close();
#endif
DownloadThread::_onDownloadError(tr("Download corrupt. Hash does not match")); DownloadThread::_onDownloadError(tr("Download corrupt. Hash does not match"));
_closeFiles();
return; return;
} }
if (_cacheEnabled && _expectedHash == computedHash) if (_cacheEnabled && _expectedHash == computedHash)
@ -566,13 +559,27 @@ void DownloadThread::_writeComplete()
emit cacheFileUpdated(computedHash); 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 */ /* Verify */
if (_verifyEnabled && !_verify()) if (_verifyEnabled && !_verify())
{ {
_file.close(); _closeFiles();
#ifdef Q_OS_WIN
_volumeFile.close();
#endif
return; return;
} }
@ -595,10 +602,7 @@ void DownloadThread::_writeComplete()
_firstBlock = nullptr; _firstBlock = nullptr;
} }
_file.close(); _closeFiles();
#ifdef Q_OS_WIN
_volumeFile.close();
#endif
#ifdef Q_OS_DARWIN #ifdef Q_OS_DARWIN
QThread::sleep(1); QThread::sleep(1);

View file

@ -139,6 +139,7 @@ protected:
bool _openAndPrepareDevice(); bool _openAndPrepareDevice();
void _writeCache(const char *buf, size_t len); void _writeCache(const char *buf, size_t len);
qint64 _sectorsWritten(); qint64 _sectorsWritten();
void _closeFiles();
/* /*
* libcurl callbacks * libcurl callbacks

View file

@ -36,6 +36,7 @@ void LocalFileExtractThread::run()
if (!_inputfile.open(_inputfile.ReadOnly)) if (!_inputfile.open(_inputfile.ReadOnly))
{ {
_onDownloadError(tr("Error opening image file")); _onDownloadError(tr("Error opening image file"));
_closeFiles();
return; return;
} }
_lastDlTotal = _inputfile.size(); _lastDlTotal = _inputfile.size();
@ -45,6 +46,8 @@ void LocalFileExtractThread::run()
else else
extractMultiFileRun(); extractMultiFileRun();
if (_cancelled)
_closeFiles();
} }
ssize_t LocalFileExtractThread::_on_read(struct archive *, const void **buff) ssize_t LocalFileExtractThread::_on_read(struct archive *, const void **buff)