From 37ab7933677a388c4e6b7dc531dade13cd6fec8a Mon Sep 17 00:00:00 2001 From: iaom Date: Thu, 8 Dec 2022 10:34:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B7=BB=E5=8A=A0=E5=92=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=B4=A2=E5=BC=95=E7=9B=AE=E5=BD=95=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filesystemwatcher/file-system-watcher.cpp | 16 +++++++++------- libsearch/index/index-scheduler.cpp | 1 + 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libsearch/filesystemwatcher/file-system-watcher.cpp b/libsearch/filesystemwatcher/file-system-watcher.cpp index 39bf4f8..c47beea 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.cpp +++ b/libsearch/filesystemwatcher/file-system-watcher.cpp @@ -139,15 +139,17 @@ void FileSystemWatcherPrivate::addWatchWithBlackList(const QStringList &pathList QStringList FileSystemWatcherPrivate::removeWatch(const QString &path) { QStringList paths; - for(int wd : m_watchPathHash.keys()) { - QString tmpPath = m_watchPathHash.value(wd); - if(tmpPath.length() >= path.length()) { - if(FileUtils::isOrUnder(tmpPath, path)) { - //fix me:This function can be slow (O(n)) - paths.append(removeWatch(m_watchPathHash.key(path))); - } + QHash::iterator iter = m_watchPathHash.begin(); + while(iter != m_watchPathHash.end()) { + if(iter.value().length() >= path.length() && FileUtils::isOrUnder(iter.value(), path)) { + paths.append(iter.value()); + inotify_rm_watch(m_inotifyFd, iter.key()); + iter = m_watchPathHash.erase(iter); + } else { + ++iter; } } + m_watchPathHash.squeeze(); return paths; } diff --git a/libsearch/index/index-scheduler.cpp b/libsearch/index/index-scheduler.cpp index 6fd2b51..9a7dada 100644 --- a/libsearch/index/index-scheduler.cpp +++ b/libsearch/index/index-scheduler.cpp @@ -61,6 +61,7 @@ void IndexScheduler::addNewPath(const QString &folders, const QStringList &black FirstRunIndexer *indexer = new FirstRunIndexer(QStringList(folders), blackList, m_stop, FirstRunIndexer::WorkMode::Add, target); connect(indexer, &FirstRunIndexer::done, this, &IndexScheduler::addNewPathFinished, Qt::QueuedConnection); m_threadPool.start(indexer); + m_fileWatcher.addWatch(folders, blackList); } }