vfs: Allow retrieving of pin state paths and flags
authorChristian Kamm <mail@ckamm.de>
Tue, 8 Jan 2019 14:15:17 +0000 (15:15 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:33 +0000 (10:58 +0100)
src/common/syncjournaldb.cpp
src/common/syncjournaldb.h
test/testsyncjournaldb.cpp

index eb2e3f32ae54ed376a25ccad68e273814561a13e..04ecff58de16c343866f0f2f8109d4ad61f9426a 100644 (file)
@@ -2167,6 +2167,22 @@ void SyncJournalDb::wipePinStateForPathAndBelow(const QByteArray &path)
     query.exec();
 }
 
+Optional<QVector<QPair<QByteArray, PinState>>> SyncJournalDb::rawPinStates()
+{
+    QMutexLocker lock(&_mutex);
+    if (!checkConnect())
+        return {};
+
+    SqlQuery query("SELECT path, pinState FROM flags;", _db);
+    query.exec();
+
+    QVector<QPair<QByteArray, PinState>> result;
+    while (query.next()) {
+        result.append({ query.baValue(0), static_cast<PinState>(query.intValue(1)) });
+    }
+    return result;
+}
+
 void SyncJournalDb::commit(const QString &context, bool startTrans)
 {
     QMutexLocker lock(&_mutex);
index dd6b4ff3dcd682f259a528aa503ecebc9cc799f0..5cb785a30c095faa726f341461efdc9fa366fcb3 100644 (file)
@@ -304,6 +304,13 @@ public:
      */
     void wipePinStateForPathAndBelow(const QByteArray &path);
 
+    /**
+     * Returns list of all paths with their pin state as in the db.
+     *
+     * Returns nothing on db error.
+     */
+    Optional<QVector<QPair<QByteArray, PinState>>> rawPinStates();
+
     /**
      * Only used for auto-test:
      * when positive, will decrease the counter for every database operation.
index 4a0d377482d247e2cf47cb23d950a4cb31c1f9a8..b8f63d9fc0307f2c52f271ae10c3b400a5a9f7eb 100644 (file)
@@ -345,7 +345,12 @@ private slots:
             return *state;
         };
 
+        _db.wipePinStateForPathAndBelow("");
+        auto list = _db.rawPinStates();
+        QCOMPARE(list->size(), 0);
+
         // Make a thrice-nested setup
+        make("", PinState::AlwaysLocal);
         make("local", PinState::AlwaysLocal);
         make("online", PinState::OnlineOnly);
         make("inherit", PinState::Inherited);
@@ -355,12 +360,15 @@ private slots:
             make(QByteArray(base) + "online", PinState::OnlineOnly);
 
             for (auto base2 : {"local/", "online/", "inherit/"}) {
-                make(QByteArray(base) + base2 + "/inherit", PinState::Inherited);
-                make(QByteArray(base) + base2 + "/local", PinState::AlwaysLocal);
-                make(QByteArray(base) + base2 + "/online", PinState::OnlineOnly);
+                make(QByteArray(base) + base2 + "inherit", PinState::Inherited);
+                make(QByteArray(base) + base2 + "local", PinState::AlwaysLocal);
+                make(QByteArray(base) + base2 + "online", PinState::OnlineOnly);
             }
         }
 
+        list = _db.rawPinStates();
+        QCOMPARE(list->size(), 4 + 9 + 27);
+
         // Baseline direct checks (the fallback for unset root pinstate is AlwaysLocal)
         QCOMPARE(get("local"), PinState::AlwaysLocal);
         QCOMPARE(get("online"), PinState::OnlineOnly);
@@ -410,12 +418,16 @@ private slots:
         QCOMPARE(getRaw("local/local"), PinState::Inherited);
         QCOMPARE(getRaw("local/local/local"), PinState::Inherited);
         QCOMPARE(getRaw("local/local/online"), PinState::Inherited);
+        list = _db.rawPinStates();
+        QCOMPARE(list->size(), 4 + 9 + 27 - 4);
 
         // Wiping everything
         _db.wipePinStateForPathAndBelow("");
         QCOMPARE(getRaw(""), PinState::Inherited);
         QCOMPARE(getRaw("local"), PinState::Inherited);
         QCOMPARE(getRaw("online"), PinState::Inherited);
+        list = _db.rawPinStates();
+        QCOMPARE(list->size(), 0);
     }
 
 private: