From 8f84fc7902b737aafc3523b3c6baa27b623b5a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Sun, 30 Mar 2025 22:24:17 +0200 Subject: [PATCH] Log libraries validation when YACReaderLibrary and YACReaderLibraryServer start --- YACReaderLibrary/db/data_base_management.cpp | 46 ++++++++++++++++++++ YACReaderLibrary/db/data_base_management.h | 23 ++++++++++ YACReaderLibrary/main.cpp | 9 +++- YACReaderLibraryServer/main.cpp | 10 ++++- 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/YACReaderLibrary/db/data_base_management.cpp b/YACReaderLibrary/db/data_base_management.cpp index 54169ced..f64f4c50 100644 --- a/YACReaderLibrary/db/data_base_management.cpp +++ b/YACReaderLibrary/db/data_base_management.cpp @@ -1097,6 +1097,52 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath) return returnValue; } +DatabaseAccess DataBaseManagement::getDatabaseAccess(const QString &libraryPath) +{ + DatabaseAccess access = { false, false, false, false }; + + auto libraryDataPath = LibraryPaths::libraryDataPath(libraryPath); + auto libraryDatabasePath = LibraryPaths::libraryDatabasePath(libraryPath); + + QFile libraryDatabase(libraryDatabasePath); + if (!libraryDatabase.exists()) { + return access; + } + + access.libraryExists = true; + + QDir libraryData(libraryDataPath); + QFile testFile(libraryData.filePath("test")); + if (testFile.open(QIODevice::WriteOnly)) { + access.canWriteToFolder = true; + testFile.close(); + testFile.remove(); + } + + QString connectionName = "test"; + { + QSqlDatabase db = DataBaseManagement::loadDatabaseFromFile(libraryDatabasePath); + + QSqlQuery versionQuery(db); + bool read = versionQuery.exec("SELECT version FROM db_info"); + + read = read && versionQuery.next(); + read = read && !versionQuery.record().value(0).toString().isEmpty(); + + access.canRead = read; + + QSqlQuery writeQuery(db); + bool write = db.transaction(); + write = write && writeQuery.exec("CREATE TABLE test_write (id INTEGER);"); + write = write && db.rollback(); + + access.canWrite = write; + } + QSqlDatabase::removeDatabase(connectionName); + + return access; +} + // COMICS_INFO_EXPORTER ComicsInfoExporter::ComicsInfoExporter() : QThread() diff --git a/YACReaderLibrary/db/data_base_management.h b/YACReaderLibrary/db/data_base_management.h index 3a96cf74..e9c67a71 100644 --- a/YACReaderLibrary/db/data_base_management.h +++ b/YACReaderLibrary/db/data_base_management.h @@ -29,6 +29,27 @@ private: void run() override; }; +struct DatabaseAccess { + bool libraryExists; + bool canRead; // db read + bool canWrite; // db write + bool canWriteToFolder; // disk write + + operator QString() const + { + if (libraryExists && canRead && canWrite && canWriteToFolder) { + return "OK"; + } else if (!libraryExists) { + return "WARNING! Library does not exist on disk"; + } else { + return QString("WARNING! DB read access: %1, DB write access: %2, can write to disk: %3") + .arg(canRead ? "YES" : "NO") + .arg(canWrite ? "YES" : "NO") + .arg(canWriteToFolder ? "YES" : "NO"); + } + } +}; + class DataBaseManagement : public QObject { Q_OBJECT @@ -58,6 +79,8 @@ public: static QString checkValidDB(const QString &fullPath); // retorna "" si la DB es inválida ó la versión si es válida. static int compareVersions(const QString &v1, const QString v2); // retorna <0 si v1 < v2, 0 si v1 = v2 y >0 si v1 > v2 static bool updateToCurrentVersion(const QString &libraryPath); + + static DatabaseAccess getDatabaseAccess(const QString &libraryPath); }; #endif diff --git a/YACReaderLibrary/main.cpp b/YACReaderLibrary/main.cpp index b5c14281..0af94d5f 100644 --- a/YACReaderLibrary/main.cpp +++ b/YACReaderLibrary/main.cpp @@ -17,6 +17,7 @@ #include "yacreader_http_server.h" #include "yacreader_local_server.h" #include "comic_db.h" +#include "data_base_management.h" #include "db_helper.h" #include "yacreader_libraries.h" #include "exit_check.h" @@ -85,7 +86,13 @@ void logSystemAndConfig() OpenGLChecker checker; QLOG_INFO() << "OpenGL version : " << checker.textVersionDescription(); - QLOG_INFO() << "Libraries: " << DBHelper::getLibraries().getLibraries(); + auto libraries = DBHelper::getLibraries().getLibraries(); + QLOG_INFO() << "Libraries: "; + for (auto library : libraries) { + QLOG_INFO() << " " << library; + auto access = DataBaseManagement::getDatabaseAccess(library.getPath()); + QLOG_INFO() << " > STATUS: " << access; + } QLOG_INFO() << "--------------------------------------------"; } diff --git a/YACReaderLibraryServer/main.cpp b/YACReaderLibraryServer/main.cpp index b6597143..0d0f0bc3 100644 --- a/YACReaderLibraryServer/main.cpp +++ b/YACReaderLibraryServer/main.cpp @@ -5,6 +5,7 @@ #include #include "comic_db.h" +#include "data_base_management.h" #include "db_helper.h" #include "yacreader_http_server.h" #include "yacreader_global.h" @@ -461,7 +462,14 @@ void logSystemAndConfig() for (const auto &line : globalInfo.split("\n")) { QLOG_INFO() << line; } - QLOG_INFO() << "Libraries: " << DBHelper::getLibraries().getLibraries(); + + auto libraries = DBHelper::getLibraries().getLibraries(); + QLOG_INFO() << "Libraries: "; + for (auto library : libraries) { + QLOG_INFO() << " " << library; + auto access = DataBaseManagement::getDatabaseAccess(library.getPath()); + QLOG_INFO() << " > STATUS: " << access; + } QLOG_INFO() << "--------------------------------------------"; }