From 81139c0098e858486b9b73fc132bcd623997dbfc Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 3 Sep 2021 22:56:19 +0800 Subject: [PATCH] Improve error detection when saving files --- src/app/upgrade.cpp | 2 +- src/base/asyncfilestorage.cpp | 13 +++++-------- src/base/net/downloadhandlerimpl.cpp | 8 ++------ src/base/net/geoipmanager.cpp | 10 ++++++++-- src/gui/rss/automatedrssdownloader.cpp | 11 +++++------ 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/app/upgrade.cpp b/src/app/upgrade.cpp index 592a7fafb..aaf762a7c 100644 --- a/src/app/upgrade.cpp +++ b/src/app/upgrade.cpp @@ -63,7 +63,7 @@ namespace { file.close(); Utils::Fs::forceRemove(savePath); - LogMsg(errorMsgFormat.arg(savePath, QLatin1String("Write incomplete.")) , Log::WARNING); + LogMsg(errorMsgFormat.arg(savePath, QObject::tr("Write incomplete")) , Log::WARNING); return; } diff --git a/src/base/asyncfilestorage.cpp b/src/base/asyncfilestorage.cpp index c2c86d794..2380928d1 100644 --- a/src/base/asyncfilestorage.cpp +++ b/src/base/asyncfilestorage.cpp @@ -67,15 +67,12 @@ QDir AsyncFileStorage::storageDir() const void AsyncFileStorage::store_impl(const QString &fileName, const QByteArray &data) { const QString filePath = m_storageDir.absoluteFilePath(fileName); - QSaveFile file(filePath); qDebug() << "AsyncFileStorage: Saving data to" << filePath; - if (file.open(QIODevice::WriteOnly)) + + QSaveFile file(filePath); + if (!file.open(QIODevice::WriteOnly) || (file.write(data) != data.length()) || !file.commit()) { - file.write(data); - if (!file.commit()) - { - qDebug() << "AsyncFileStorage: Failed to save data"; - emit failed(filePath, file.errorString()); - } + qDebug() << "AsyncFileStorage: Failed to save data"; + emit failed(filePath, file.errorString()); } } diff --git a/src/base/net/downloadhandlerimpl.cpp b/src/base/net/downloadhandlerimpl.cpp index 1a3781282..108f81314 100644 --- a/src/base/net/downloadhandlerimpl.cpp +++ b/src/base/net/downloadhandlerimpl.cpp @@ -47,9 +47,7 @@ namespace QFile file {filePath}; if (!file.open(QIODevice::WriteOnly)) return false; - - file.write(replyData); - return true; + return (file.write(replyData) == replyData.length()); } QTemporaryFile tmpfile {Utils::Fs::tempPath() + "XXXXXX"}; @@ -59,9 +57,7 @@ namespace return false; filePath = tmpfile.fileName(); - - tmpfile.write(replyData); - return true; + return (tmpfile.write(replyData) == replyData.length()); } } diff --git a/src/base/net/geoipmanager.cpp b/src/base/net/geoipmanager.cpp index a7c9e1473..82ca0f05c 100644 --- a/src/base/net/geoipmanager.cpp +++ b/src/base/net/geoipmanager.cpp @@ -451,11 +451,17 @@ void GeoIPManager::downloadFinished(const DownloadResult &result) specialFolderLocation(SpecialFolder::Data) + GEODB_FOLDER); if (!QDir(targetPath).exists()) QDir().mkpath(targetPath); + QFile targetFile(QString::fromLatin1("%1/%2").arg(targetPath, GEODB_FILENAME)); - if (!targetFile.open(QFile::WriteOnly) || (targetFile.write(data) == -1)) - LogMsg(tr("Couldn't save downloaded IP geolocation database file."), Log::WARNING); + if (!targetFile.open(QFile::WriteOnly) || (targetFile.write(data) != data.length())) + { + LogMsg(tr("Couldn't save downloaded IP geolocation database file. Reason: %1") + .arg(targetFile.errorString()), Log::WARNING); + } else + { LogMsg(tr("Successfully updated IP geolocation database."), Log::INFO); + } } else { diff --git a/src/gui/rss/automatedrssdownloader.cpp b/src/gui/rss/automatedrssdownloader.cpp index 98e3f63cd..17bc478dd 100644 --- a/src/gui/rss/automatedrssdownloader.cpp +++ b/src/gui/rss/automatedrssdownloader.cpp @@ -452,13 +452,12 @@ void AutomatedRssDownloader::on_exportBtn_clicked() path += EXT_LEGACY; } + const QByteArray rules {RSS::AutoDownloader::instance()->exportRules(format)}; QFile file {path}; - if (!file.open(QFile::WriteOnly) - || (file.write(RSS::AutoDownloader::instance()->exportRules(format)) == -1)) - { - QMessageBox::critical( - this, tr("I/O Error") - , tr("Failed to create the destination file. Reason: %1").arg(file.errorString())); + if (!file.open(QFile::WriteOnly) || (file.write(rules) != rules.length())) + { + QMessageBox::critical(this, tr("I/O Error") + , tr("Failed to create the destination file. Reason: %1").arg(file.errorString())); } }