From 017dca232872e5ef20f4c8113b3a274860db6760 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 4 Jul 2023 23:34:14 +0800 Subject: [PATCH] Move findPathInList from discoveryphase to common syncjournaldb Signed-off-by: Claudio Cambra --- src/common/syncjournaldb.cpp | 27 ++++++++++++++++++++++++++ src/common/syncjournaldb.h | 3 +++ src/libsync/discoveryphase.cpp | 35 ++++------------------------------ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/common/syncjournaldb.cpp b/src/common/syncjournaldb.cpp index 23e33c169..7cbc9b540 100644 --- a/src/common/syncjournaldb.cpp +++ b/src/common/syncjournaldb.cpp @@ -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); diff --git a/src/common/syncjournaldb.h b/src/common/syncjournaldb.h index d9a3e36b8..eb0ab0e21 100644 --- a/src/common/syncjournaldb.h +++ b/src/common/syncjournaldb.h @@ -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); diff --git a/src/libsync/discoveryphase.cpp b/src/libsync/discoveryphase.cpp index 14ff89794..e1d95c28a 100644 --- a/src/libsync/discoveryphase.cpp +++ b/src/libsync/discoveryphase.cpp @@ -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; } -- 2.30.2