Improve error detection when saving files

This commit is contained in:
Chocobo1 2021-09-03 22:56:19 +08:00
parent 6a6268c068
commit 81139c0098
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
5 changed files with 21 additions and 23 deletions

View file

@ -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;
}

View file

@ -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());
}
}

View file

@ -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());
}
}

View file

@ -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
{

View file

@ -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()));
}
}