From d800f6cf4f11962f0db05178d716b4eda494e1fa Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 13 Sep 2017 18:26:28 +0200 Subject: [PATCH] try to fix broken databases (by rotating the old file and starting a new one) --- server/logging/logengine.cpp | 36 +++++++++++++++++++++++++++++++----- server/logging/logengine.h | 4 +++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/server/logging/logengine.cpp b/server/logging/logengine.cpp index 61757f43..ac86b922 100644 --- a/server/logging/logengine.cpp +++ b/server/logging/logengine.cpp @@ -123,6 +123,7 @@ #include #include #include +#include #define DB_SCHEMA_VERSION 2 @@ -145,14 +146,20 @@ LogEngine::LogEngine(QObject *parent): if (!m_db.isValid()) { qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText(); - return; + rotate(m_db.databaseName()); } if (!m_db.open()) { qCWarning(dcLogEngine) << "Error opening log database:" << m_db.lastError().driverText() << m_db.lastError().databaseText(); - return; + rotate(m_db.databaseName()); } - initDB(); + if (!initDB()) { + qCWarning(dcLogEngine()) << "Error initializing database. Trying to correct it."; + rotate(m_db.databaseName()); + if (!initDB()) { + qCWarning(dcLogEngine()) << "Error fixing log database. Giving up. Logs can't be stored."; + } + } } /*! Destructs the \l{LogEngine}. */ @@ -376,7 +383,22 @@ void LogEngine::checkDBSize() } } -void LogEngine::initDB() +void LogEngine::rotate(const QString &dbName) +{ + int index = 1; + while (QFileInfo(QString("%1.%2").arg(dbName).arg(index)).exists()) { + index++; + } + qCDebug(dcLogEngine()) << "Backing up old database file to" << QString("%1.%2").arg(dbName).arg(index); + QFile f(dbName); + if (!f.rename(QString("%1.%2").arg(dbName).arg(index))) { + qCWarning(dcLogEngine()) << "Error backing up old database."; + } else { + qCDebug(dcLogEngine()) << "Successfully moved old databse"; + } +} + +bool LogEngine::initDB() { m_db.close(); m_db.open(); @@ -397,6 +419,7 @@ void LogEngine::initDB() } } else { qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table."; + return false; } if (!m_db.tables().contains("sourceTypes")) { @@ -435,13 +458,16 @@ void LogEngine::initDB() "FOREIGN KEY(loggingEventType) REFERENCES loggingEventTypes(id)" ");"); - if (m_db.lastError().isValid()) + if (m_db.lastError().isValid()) { qCWarning(dcLogEngine) << "Error creating log table in database. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText(); + return false; + } } qCDebug(dcLogEngine) << "Initialized logging DB successfully. (maximum DB size:" << m_dbMaxSize << ")"; + return true; } } diff --git a/server/logging/logengine.h b/server/logging/logengine.h index 8dcd9aa0..a9184f0b 100644 --- a/server/logging/logengine.h +++ b/server/logging/logengine.h @@ -52,10 +52,12 @@ private: QSqlDatabase m_db; int m_dbMaxSize; - void initDB(); + bool initDB(); void appendLogEntry(const LogEntry &entry); void checkDBSize(); + void rotate(const QString &dbName); + private: // Only GuhCore and ruleEngine are allowed to log events. friend class GuhCore;