File 0011-Open-baloo-lmdb-database-read-only-beside-in-baloo_f.patch of Package baloo5

From 02047b524a176da447d8c96e15c7e2abae8339ae Mon Sep 17 00:00:00 2001
From: Christoph Cullmann <cullmann@kde.org>
Date: Sun, 11 Sep 2016 23:36:27 +0200
Subject: [PATCH 11/13] Open baloo lmdb database read-only beside in
 baloo_file/baloo_file_extractor + balooctl (for some commands) + unit tests

At the moment, any application that uses baloo can corrupt the db.
Now, only the things that need to write to it open it with read-write.
This only works as long as the library exposes only read-only things like Query/...

REVIEW: 128892
---
 src/engine/database.cpp              |  6 +++---
 src/engine/database.h                | 14 +++++++++++++-
 src/file/extractor/app.cpp           |  2 +-
 src/lib/file.cpp                     |  2 +-
 src/lib/searchstore.cpp              |  2 +-
 src/lib/taglistjob.cpp               |  2 +-
 src/qml/experimental/monitor.cpp     |  2 +-
 src/tools/balooctl/main.cpp          |  8 ++++----
 src/tools/balooctl/statuscommand.cpp |  2 +-
 src/tools/balooshow/main.cpp         |  2 +-
 10 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/engine/database.cpp b/src/engine/database.cpp
index c83f540..0c91d0e 100644
--- a/src/engine/database.cpp
+++ b/src/engine/database.cpp
@@ -79,7 +79,7 @@ bool Database::open(OpenMode mode)
     }
     QFileInfo indexInfo(dir, QStringLiteral("index"));
 
-    if (mode == OpenDatabase && !indexInfo.exists()) {
+    if ((mode != CreateDatabase) && !indexInfo.exists()) {
         return false;
     }
 
@@ -117,7 +117,7 @@ bool Database::open(OpenMode mode)
 
     // The directory needs to be created before opening the environment
     QByteArray arr = QFile::encodeName(indexInfo.absoluteFilePath());
-    rc = mdb_env_open(m_env, arr.constData(), MDB_NOSUBDIR | MDB_NOMEMINIT, 0664);
+    rc = mdb_env_open(m_env, arr.constData(), MDB_NOSUBDIR | MDB_NOMEMINIT | ((mode == ReadOnlyDatabase) ? MDB_RDONLY : 0), 0664);
     if (rc) {
         mdb_env_close(m_env);
         m_env = nullptr;
@@ -136,7 +136,7 @@ bool Database::open(OpenMode mode)
     // Individual Databases
     //
     MDB_txn* txn;
-    if (mode == OpenDatabase) {
+    if (mode != CreateDatabase) {
         int rc = mdb_txn_begin(m_env, NULL, MDB_RDONLY, &txn);
         Q_ASSERT_X(rc == 0, "Database::transaction ro begin", mdb_strerror(rc));
         if (rc) {
diff --git a/src/engine/database.h b/src/engine/database.h
index 6ccb2a5..735ecd5 100644
--- a/src/engine/database.h
+++ b/src/engine/database.h
@@ -49,8 +49,20 @@ public:
      * Database open mode
      */
     enum OpenMode {
+        /**
+         * Create + open read-write dabase.
+         */
         CreateDatabase,
-        OpenDatabase
+
+        /**
+         * Read-Write Database, only works if database exists.
+         */
+        ReadWriteDatabase,
+
+        /**
+         * Read-Only Database, only works if database exists.
+         */
+        ReadOnlyDatabase
     };
 
     /**
diff --git a/src/file/extractor/app.cpp b/src/file/extractor/app.cpp
index 0ca7276..6843165 100644
--- a/src/file/extractor/app.cpp
+++ b/src/file/extractor/app.cpp
@@ -55,7 +55,7 @@ App::App(QObject* parent)
 void App::slotNewInput()
 {
     Database *db = globalDatabaseInstance();
-    if (!db->open(Database::OpenDatabase)) {
+    if (!db->open(Database::ReadWriteDatabase)) {
         qCritical() << "Failed to open the database";
         exit(1);
     }
diff --git a/src/lib/file.cpp b/src/lib/file.cpp
index cbbc912..1176f35 100644
--- a/src/lib/file.cpp
+++ b/src/lib/file.cpp
@@ -96,7 +96,7 @@ bool File::load()
     }
 
     Database *db = globalDatabaseInstance();
-    if (!db->open(Database::OpenDatabase)) {
+    if (!db->open(Database::ReadOnlyDatabase)) {
         return false;
     }
 
diff --git a/src/lib/searchstore.cpp b/src/lib/searchstore.cpp
index 060a4fd..265016b 100644
--- a/src/lib/searchstore.cpp
+++ b/src/lib/searchstore.cpp
@@ -48,7 +48,7 @@ SearchStore::SearchStore()
     : m_db(0)
 {
     m_db = globalDatabaseInstance();
-    if (!m_db->open(Database::OpenDatabase)) {
+    if (!m_db->open(Database::ReadOnlyDatabase)) {
         m_db = 0;
     }
 
diff --git a/src/lib/taglistjob.cpp b/src/lib/taglistjob.cpp
index 76ac8ff..3d1b0c8 100644
--- a/src/lib/taglistjob.cpp
+++ b/src/lib/taglistjob.cpp
@@ -46,7 +46,7 @@ TagListJob::~TagListJob()
 void TagListJob::start()
 {
     Database *db = globalDatabaseInstance();
-    if (!db->open(Database::OpenDatabase)) {
+    if (!db->open(Database::ReadOnlyDatabase)) {
         setError(UserDefinedError);
         setErrorText(QStringLiteral("Failed to open the database"));
         emitResult();
diff --git a/src/qml/experimental/monitor.cpp b/src/qml/experimental/monitor.cpp
index 11c06ae..679b914 100644
--- a/src/qml/experimental/monitor.cpp
+++ b/src/qml/experimental/monitor.cpp
@@ -126,7 +126,7 @@ void Monitor::balooStarted(const QString& service)
 void Monitor::fetchTotalFiles()
 {
     Baloo::Database *db = Baloo::globalDatabaseInstance();
-    if (db->open(Baloo::Database::OpenDatabase)) {
+    if (db->open(Baloo::Database::ReadOnlyDatabase)) {
         Baloo::Transaction tr(db, Baloo::Transaction::ReadOnly);
         m_totalFiles = tr.size();
         m_filesIndexed = tr.size() - tr.phaseOneSize();
diff --git a/src/tools/balooctl/main.cpp b/src/tools/balooctl/main.cpp
index 2a6b175..b12b39d 100644
--- a/src/tools/balooctl/main.cpp
+++ b/src/tools/balooctl/main.cpp
@@ -191,7 +191,7 @@ int main(int argc, char* argv[])
         }
 
         Database *db = globalDatabaseInstance();
-        if (!db->open(Database::OpenDatabase)) {
+        if (!db->open(Database::ReadWriteDatabase)) {
             out << "Baloo Index could not be opened\n";
             return 1;
         }
@@ -230,7 +230,7 @@ int main(int argc, char* argv[])
         }
 
         Database *db = globalDatabaseInstance();
-        if (!db->open(Database::OpenDatabase)) {
+        if (!db->open(Database::ReadWriteDatabase)) {
             out << "Baloo Index could not be opened\n";
             return 1;
         }
@@ -260,7 +260,7 @@ int main(int argc, char* argv[])
 
     if (command == QStringLiteral("indexSize")) {
         Database *db = globalDatabaseInstance();
-        if (!db->open(Database::OpenDatabase)) {
+        if (!db->open(Database::ReadOnlyDatabase)) {
             out << "Baloo Index could not be opened\n";
             return 1;
         }
@@ -311,7 +311,7 @@ int main(int argc, char* argv[])
 
     if (command == QStringLiteral("checkDb")) {
         Database *db = globalDatabaseInstance();
-        if (!db->open(Database::OpenDatabase)) {
+        if (!db->open(Database::ReadOnlyDatabase)) {
             out << "Baloo Index could not be opened\n";
             return 1;
         }
diff --git a/src/tools/balooctl/statuscommand.cpp b/src/tools/balooctl/statuscommand.cpp
index 1a56c64..fede101 100644
--- a/src/tools/balooctl/statuscommand.cpp
+++ b/src/tools/balooctl/statuscommand.cpp
@@ -56,7 +56,7 @@ int StatusCommand::exec(const QCommandLineParser& parser)
     }
 
     Database *db = globalDatabaseInstance();
-    if (!db->open(Database::OpenDatabase)) {
+    if (!db->open(Database::ReadOnlyDatabase)) {
         out << i18n("Baloo Index could not be opened") << endl;
         return 1;
     }
diff --git a/src/tools/balooshow/main.cpp b/src/tools/balooshow/main.cpp
index f45f2e0..54c64df 100644
--- a/src/tools/balooshow/main.cpp
+++ b/src/tools/balooshow/main.cpp
@@ -101,7 +101,7 @@ int main(int argc, char* argv[])
     QString text;
 
     Baloo::Database *db = Baloo::globalDatabaseInstance();
-    if (!db->open(Baloo::Database::OpenDatabase)) {
+    if (!db->open(Baloo::Database::ReadOnlyDatabase)) {
         stream << i18n("The Baloo index could not be opened. Please run \"balooctl status\" to see if Baloo is enabled and working.")
                << endl;
         return 1;
-- 
2.10.0