Ref T321 Improve CAtomicFile error message and advise the user to try restarting or manually deleting.

This commit is contained in:
Mat Sutcliffe
2019-02-23 19:01:10 +00:00
parent dbdc65793e
commit 69afe771d2
2 changed files with 32 additions and 11 deletions

View File

@@ -146,12 +146,9 @@ namespace BlackMisc
: MoveFileEx(encode(fileName()).c_str(), encode(m_originalFilename).c_str(), MOVEFILE_WRITE_THROUGH);
if (! result)
{
m_renameError = true;
wchar_t *s = nullptr;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), 0, reinterpret_cast<LPWSTR>(&s), 0, nullptr);
// MS 2018-09 Testing T321 ("failed to write session file")
//setErrorString((replace ? "ReplaceFile: " : "MoveFileEx: ") + QString::fromWCharArray(s).simplified());
CLogMessage(this).error((replace ? "ReplaceFile: " : "MoveFileEx: ") + QString::fromWCharArray(s).simplified() + "\n" + getStackTraceAlways().join("\n"));
const QString windowsError = (replace ? u"ReplaceFile: " : u"MoveFileEx: ") % QString::fromWCharArray(s).simplified();
LocalFree(reinterpret_cast<HLOCAL>(s));
// fall back to non-atomic remove-and-rename
@@ -160,11 +157,18 @@ namespace BlackMisc
QFile old(m_originalFilename);
if (!old.remove())
{
setErrorString(old.errorString());
// fall back failed, so report the reasons for the original failure AND the fall back failure
m_renameError = true;
setErrorString(windowsError % u" QFile::remove: " % old.errorString());
return;
}
}
rename(m_originalFilename);
if (rename(m_originalFilename))
{
// fall back succeeded, so just log the reason for the original failure
CLogMessage(this).debug(u"CAtomicFile replacing %1: %2") << m_originalFilename << windowsError;
}
// else if rename() failed, then the reason is already reported by QFile
}
}
#else

View File

@@ -491,7 +491,8 @@ namespace BlackMisc
if (! revisionFile.checkedClose())
{
CLogMessage(this).error(u"Failed to write to %1: %2 (%3 %4)") << revisionFile.fileName() << revisionFile.errorString() << QThread::currentThread()->objectName() << Q_FUNC_INFO;
static const QString advice = QStringLiteral("If this error persists, try restarting your computer or delete the file manually.");
CLogMessage(this).error(u"Failed to replace %1: %2 (%3)") << revisionFile.fileName() << revisionFile.errorString() << advice;
}
}
@@ -651,9 +652,17 @@ namespace BlackMisc
timestamps.insert(key, timestamp);
json.insert("timestamps", timestamps);
if (!(revisionFile.seek(0) && revisionFile.resize(0) && revisionFile.write(QJsonDocument(json).toJson()) && revisionFile.checkedClose()))
if (revisionFile.seek(0) && revisionFile.resize(0) && revisionFile.write(QJsonDocument(json).toJson()))
{
CLogMessage(this).error(u"Failed to write to %1: %2 (%3 %4)") << revisionFile.fileName() << revisionFile.errorString() << QThread::currentThread()->objectName() << Q_FUNC_INFO;
if (!revisionFile.checkedClose())
{
static const QString advice = QStringLiteral("If this error persists, try restarting your computer or delete the file manually.");
CLogMessage(this).error(u"Failed to replace %1: %2 (%3)") << revisionFile.fileName() << revisionFile.errorString() << advice;
}
}
else
{
CLogMessage(this).error(u"Failed to write to %1: %2") << revisionFile.fileName() << revisionFile.errorString();
}
}
m_lockFile.unlock();
@@ -804,9 +813,17 @@ namespace BlackMisc
apps.replaceOrAdd(currentProcess);
json.insert("apps", apps.toJson());
json.insert("uuid", uuid.toString());
if (!(file.seek(0) && file.resize(0) && file.write(QJsonDocument(json).toJson()) && file.checkedClose()))
if (file.seek(0) && file.resize(0) && file.write(QJsonDocument(json).toJson()))
{
CLogMessage(this).error(u"Failed to write to session file %1: %2 (%3 %4)") << m_filename << file.errorString() << QThread::currentThread()->objectName() << Q_FUNC_INFO;
if (!file.checkedClose())
{
static const QString advice = QStringLiteral("If this error persists, try restarting your computer or delete the file manually.");
CLogMessage(this).error(u"Failed to replace %1: %2 (%3)") << file.fileName() << file.errorString() << advice;
}
}
else
{
CLogMessage(this).error(u"Failed to write to %1: %2") << file.fileName() << file.errorString();
}
}