Browse Source

Improve error detection when saving files

adaptive-webui-19844
Chocobo1 3 years ago
parent
commit
81139c0098
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 2
      src/app/upgrade.cpp
  2. 13
      src/base/asyncfilestorage.cpp
  3. 8
      src/base/net/downloadhandlerimpl.cpp
  4. 10
      src/base/net/geoipmanager.cpp
  5. 11
      src/gui/rss/automatedrssdownloader.cpp

2
src/app/upgrade.cpp

@ -63,7 +63,7 @@ namespace @@ -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;
}

13
src/base/asyncfilestorage.cpp

@ -67,15 +67,12 @@ QDir AsyncFileStorage::storageDir() const @@ -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());
}
}

8
src/base/net/downloadhandlerimpl.cpp

@ -47,9 +47,7 @@ namespace @@ -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 @@ -59,9 +57,7 @@ namespace
return false;
filePath = tmpfile.fileName();
tmpfile.write(replyData);
return true;
return (tmpfile.write(replyData) == replyData.length());
}
}

10
src/base/net/geoipmanager.cpp

@ -451,11 +451,17 @@ void GeoIPManager::downloadFinished(const DownloadResult &result) @@ -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
{

11
src/gui/rss/automatedrssdownloader.cpp

@ -452,13 +452,12 @@ void AutomatedRssDownloader::on_exportBtn_clicked() @@ -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()));
}
}

Loading…
Cancel
Save