From 37fa6214520ac34353b49027446a406756c301ae Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Tue, 6 Jul 2021 16:53:32 +0800 Subject: [PATCH] [Fix] Path inclusive relation judgment incorrectly. --- libsearch/file-utils.cpp | 19 +++++++++++++++++++ libsearch/file-utils.h | 2 ++ libsearch/global-settings.cpp | 8 ++++++-- libsearch/global-settings.h | 1 + libsearch/index/inotify-watch.cpp | 5 +++-- libsearch/index/pending-file-queue.cpp | 3 ++- libsearch/index/search-manager.cpp | 4 ++-- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index f6f8e50..31f1f69 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -178,6 +178,25 @@ QString FileUtils::getSettingName(const QString& setting) { return setting.right(setting.length() - setting.lastIndexOf("/") - 1); } +bool FileUtils::isOrUnder(QString pathA, QString pathB) +{ + if(!pathA.startsWith("/")) + pathA.prepend("/"); + if(!pathB.startsWith("/")) + pathB.prepend("/"); + + if(pathA == pathB) + return true; + + if(pathA.length() > pathB.length()) + return false; + + if(pathA.startsWith(pathB + "/")) + return true; + + return false; +} + void FileUtils::loadHanziTable(const QString &fileName) { QFile file(fileName); diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index a352d63..f0a9085 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -67,6 +67,8 @@ public: static QString getFileName(const QString &); static QString getAppName(const QString &); static QString getSettingName(const QString &); + //A is or under B + static bool isOrUnder(QString pathA, QString pathB); //chinese character to pinyin static QMap map_chinese2pinyin; diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index cba42e5..3aebc89 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -155,15 +155,19 @@ bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool rem //why QSetting's key can't start with "/"?? QString pathKey = path.right(path.length() - 1); + if (pathKey.endsWith(QLatin1Char('/'))) { + pathKey = pathKey.mid(0, pathKey.length() - 1); + } + QStringList blockDirs = m_block_dirs_settings->allKeys(); for(QString i : blockDirs) { - if(pathKey.startsWith(i)) { + if(FileUtils::isOrUnder(pathKey, i)) { // returnCode = QString(tr("My parent folder has been blocked!")); returnCode = PATH_PARENT_BLOCKED; return false; } - if(i.startsWith(pathKey)) + if(FileUtils::isOrUnder(i, pathKey)) m_block_dirs_settings->remove(i); } m_block_dirs_settings->setValue(pathKey, "0"); diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index 4a61cc2..de520ab 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -36,6 +36,7 @@ #include #include #include "libsearch_global.h" +#include "file-utils.h" #define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise" #define TRANSPARENCY_KEY "transparency" diff --git a/libsearch/index/inotify-watch.cpp b/libsearch/index/inotify-watch.cpp index ea59b19..eb845a8 100644 --- a/libsearch/index/inotify-watch.cpp +++ b/libsearch/index/inotify-watch.cpp @@ -49,7 +49,7 @@ bool InotifyWatch::removeWatch(const QString &path, bool removeFromDatabase) for(QMap::Iterator i = currentPath.begin(); i != currentPath.end();) { // qDebug() << i.value(); // if(i.value().length() > path.length()) { - if(i.value().startsWith(path)) { + if(FileUtils::isOrUnder(i.value(), path)) { qDebug() << "remove path: " << i.value(); inotify_rm_watch(m_inotifyFd, currentPath.key(path)); PendingFile f(i.value()); @@ -65,7 +65,8 @@ bool InotifyWatch::removeWatch(const QString &path, bool removeFromDatabase) for(QMap::Iterator i = currentPath.begin(); i != currentPath.end();) { // qDebug() << i.value(); if(i.value().length() > path.length()) { - if(i.value().startsWith(path)) { + if(FileUtils::isOrUnder(i.value(), path)) { +// if(i.value().startsWith(path + "/")) { // qDebug() << "remove path: " << i.value(); inotify_rm_watch(m_inotifyFd, currentPath.key(path)); currentPath.erase(i++); diff --git a/libsearch/index/pending-file-queue.cpp b/libsearch/index/pending-file-queue.cpp index ab45da3..47a93c9 100644 --- a/libsearch/index/pending-file-queue.cpp +++ b/libsearch/index/pending-file-queue.cpp @@ -18,6 +18,7 @@ * */ #include "pending-file-queue.h" +#include "file-utils.h" #include using namespace Zeeker; static PendingFileQueue *global_instance_pending_file_queue = nullptr; @@ -88,7 +89,7 @@ void PendingFileQueue::enqueue(const PendingFile &file) // Because our datebase need to delete those indexs one by one. if(file.shouldRemoveIndex() && file.isDir()) { const auto keepFile = [&file](const PendingFile& pending) { - return (!pending.path().startsWith(file.path()) || pending.shouldRemoveIndex()); + return (!FileUtils::isOrUnder(pending.path(), file.path()) || pending.shouldRemoveIndex()); }; const auto end = m_cache.end(); const auto droppedFilesBegin = std::stable_partition(m_cache.begin(), end, keepFile); diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index dc5585b..c501594 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -81,7 +81,7 @@ void SearchManager::onKeywordSearch(QString keyword, QQueue *searchResu bool SearchManager::isBlocked(QString &path) { QStringList blockList = GlobalSettings::getInstance()->getBlockDirs(); for(QString i : blockList) { - if(path.startsWith(i.prepend("/"))) + if(FileUtils::isOrUnder(path, i)) return true; } return false; @@ -414,7 +414,7 @@ void DirectSearch::run() { bool findIndex = false; for (QString j : blockList) { - if (i.absoluteFilePath().startsWith(j.prepend("/"))) { + if (FileUtils::isOrUnder(i.absoluteFilePath(), j)) { findIndex = true; break; }