Move findPathInList from discoveryphase to common syncjournaldb
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 4 Jul 2023 15:34:14 +0000 (23:34 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 4 Aug 2023 09:40:39 +0000 (17:40 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/common/syncjournaldb.cpp
src/common/syncjournaldb.h
src/libsync/discoveryphase.cpp

index 23e33c16999c395dce317a36e9b0119b0055e16e..7cbc9b540778a8ff0d9373f6c41a7eabd82716cd 100644 (file)
@@ -205,6 +205,33 @@ bool SyncJournalDb::maybeMigrateDb(const QString &localPath, const QString &abso
     return true;
 }
 
+bool SyncJournalDb::findPathInSelectiveSyncList(const QStringList &list, const QString &path)
+{
+    Q_ASSERT(std::is_sorted(list.begin(), list.end()));
+
+    if (list.size() == 1 && list.first() == QLatin1String("/")) {
+        // Special case for the case "/" is there, it matches everything
+        return true;
+    }
+
+    QString pathSlash = path + QLatin1Char('/');
+
+    // Since the list is sorted, we can do a binary search.
+    // If the path is a prefix of another item or right after in the lexical order.
+    auto it = std::lower_bound(list.begin(), list.end(), pathSlash);
+
+    if (it != list.end() && *it == pathSlash) {
+        return true;
+    }
+
+    if (it == list.begin()) {
+        return false;
+    }
+    --it;
+    Q_ASSERT(it->endsWith(QLatin1Char('/'))); // Folder::setSelectiveSyncBlackList makes sure of that
+    return pathSlash.startsWith(*it);
+}
+
 bool SyncJournalDb::exists()
 {
     QMutexLocker locker(&_mutex);
index d9a3e36b84b3a6df6b0205479f427253b3787527..eb0ab0e21e0ac7149219cc9263cf610a27a322ba 100644 (file)
@@ -58,6 +58,9 @@ public:
     /// Migrate a csync_journal to the new path, if necessary. Returns false on error
     static bool maybeMigrateDb(const QString &localPath, const QString &absoluteJournalPath);
 
+    /// Given a sorted list of paths ending with '/', return whether or not the given path is within one of the paths of the list
+    static bool findPathInSelectiveSyncList(const QStringList &list, const QString &path);
+
     // To verify that the record could be found check with SyncJournalFileRecord::isValid()
     [[nodiscard]] bool getFileRecord(const QString &filename, SyncJournalFileRecord *rec) { return getFileRecord(filename.toUtf8(), rec); }
     [[nodiscard]] bool getFileRecord(const QByteArray &filename, SyncJournalFileRecord *rec);
index 14ff897947f8c65fe1ae72327ab5999fe56639ec..e1d95c28a183ec3ba9b8166775cf1e966672ff97 100644 (file)
@@ -40,34 +40,6 @@ namespace OCC {
 
 Q_LOGGING_CATEGORY(lcDiscovery, "nextcloud.sync.discovery", QtInfoMsg)
 
-/* Given a sorted list of paths ending with '/', return whether or not the given path is within one of the paths of the list*/
-static bool findPathInList(const QStringList &list, const QString &path)
-{
-    Q_ASSERT(std::is_sorted(list.begin(), list.end()));
-
-    if (list.size() == 1 && list.first() == QLatin1String("/")) {
-        // Special case for the case "/" is there, it matches everything
-        return true;
-    }
-
-    QString pathSlash = path + QLatin1Char('/');
-
-    // Since the list is sorted, we can do a binary search.
-    // If the path is a prefix of another item or right after in the lexical order.
-    auto it = std::lower_bound(list.begin(), list.end(), pathSlash);
-
-    if (it != list.end() && *it == pathSlash) {
-        return true;
-    }
-
-    if (it == list.begin()) {
-        return false;
-    }
-    --it;
-    Q_ASSERT(it->endsWith(QLatin1Char('/'))); // Folder::setSelectiveSyncBlackList makes sure of that
-    return pathSlash.startsWith(*it);
-}
-
 bool DiscoveryPhase::isInSelectiveSyncBlackList(const QString &path) const
 {
     if (_selectiveSyncBlackList.isEmpty()) {
@@ -76,7 +48,7 @@ bool DiscoveryPhase::isInSelectiveSyncBlackList(const QString &path) const
     }
 
     // Block if it is in the black list
-    if (findPathInList(_selectiveSyncBlackList, path)) {
+    if (SyncJournalDb::findPathInSelectiveSyncList(_selectiveSyncBlackList, path)) {
         return true;
     }
 
@@ -138,7 +110,7 @@ void DiscoveryPhase::checkSelectiveSyncNewFolder(const QString &path,
     }
 
     // If this path or the parent is in the white list, then we do not block this file
-    if (findPathInList(_selectiveSyncWhiteList, path)) {
+    if (SyncJournalDb::findPathInSelectiveSyncList(_selectiveSyncWhiteList, path)) {
         return callback(false);
     }
 
@@ -159,7 +131,8 @@ void DiscoveryPhase::checkSelectiveSyncNewFolder(const QString &path,
 void DiscoveryPhase::checkSelectiveSyncExistingFolder(const QString &path)
 {
     // If no size limit is enforced, or if is in whitelist (explicitly allowed) or in blacklist (explicitly disallowed), do nothing.
-    if (!notifyExistingFolderOverLimit() || findPathInList(_selectiveSyncWhiteList, path) || findPathInList(_selectiveSyncBlackList, path)) {
+    if (!notifyExistingFolderOverLimit() || SyncJournalDb::findPathInSelectiveSyncList(_selectiveSyncWhiteList, path)
+        || SyncJournalDb::findPathInSelectiveSyncList(_selectiveSyncBlackList, path)) {
         return;
     }