Story#8966:the '/data' directory can be searched by direct search.

This commit is contained in:
baijunjie 2022-04-08 11:15:18 +08:00
parent 777b5c06ef
commit 5e818464c8
3 changed files with 57 additions and 29 deletions

View File

@ -6,6 +6,8 @@
#include <QDBusArgument> #include <QDBusArgument>
#include <QThread> #include <QThread>
#include <fstab.h> #include <fstab.h>
#include <QMutexLocker>
static std::once_flag flag; static std::once_flag flag;
static DirWatcher *global_intance = nullptr; static DirWatcher *global_intance = nullptr;
@ -36,17 +38,15 @@ DirWatcher *DirWatcher::getDirWatcher()
QStringList DirWatcher::currentindexableDir() QStringList DirWatcher::currentindexableDir()
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
QStringList indexableDirList = m_indexableDirList; QStringList indexableDirList = m_indexableDirList;
s_mutex.unlock();
return indexableDirList; return indexableDirList;
} }
QStringList DirWatcher::currentBlackListOfIndex() QStringList DirWatcher::currentBlackListOfIndex()
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
QStringList blackListOfIndex = m_blackListOfIndex; QStringList blackListOfIndex = m_blackListOfIndex;
s_mutex.unlock();
return blackListOfIndex; return blackListOfIndex;
} }
@ -59,7 +59,7 @@ QStringList DirWatcher::blackListOfDir(const QString &dirPath)
//new TODO: Optimize the search algorithm. //new TODO: Optimize the search algorithm.
//There is no processing for the subvolumes.May be a bug. //There is no processing for the subvolumes.May be a bug.
QStringList blackListOfDir; QStringList blackListOfDir;
s_mutex.lock(); QMutexLocker locker(&s_mutex);
for (auto t = m_repeatedlyMountedDeviceInfo.constBegin(); t != m_repeatedlyMountedDeviceInfo.constEnd(); t++) { for (auto t = m_repeatedlyMountedDeviceInfo.constBegin(); t != m_repeatedlyMountedDeviceInfo.constEnd(); t++) {
QString topRepeatedMountPoint; QString topRepeatedMountPoint;
for (QString mountPoint: t.value()) { for (QString mountPoint: t.value()) {
@ -76,40 +76,59 @@ QStringList DirWatcher::blackListOfDir(const QString &dirPath)
} }
} }
} }
s_mutex.unlock(); for (auto i = m_infoOfSubvolume.constBegin(); i != m_infoOfSubvolume.constEnd(); i++) {
QString mountPoint = i.value();
QString spec = i.key();
//排除搜索列表指定多个目录时子卷会重复包含的情况,比如同时指定/home和/data/home
QString tmp = dirPath;
if (dirPath.startsWith(mountPoint)) {
blackListOfDir.append(tmp.replace(0, mountPoint.length(), spec));
}
if (dirPath.startsWith(spec)) {
blackListOfDir.append(tmp.replace(0, spec.length(), mountPoint));
}
}
return blackListOfDir; return blackListOfDir;
} }
void DirWatcher::appendBlackListItemOfIndex(const QString &path) void DirWatcher::appendBlackListItemOfIndex(const QString &path)
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_blackListOfIndex.append(path); m_blackListOfIndex.append(path);
m_blackListOfIndex = m_blackListOfIndex.toSet().toList(); m_blackListOfIndex = m_blackListOfIndex.toSet().toList();
s_mutex.unlock();
} }
void DirWatcher::appendBlackListItemOfIndex(const QStringList &pathList) void DirWatcher::appendBlackListItemOfIndex(const QStringList &pathList)
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_blackListOfIndex.append(pathList); m_blackListOfIndex.append(pathList);
m_blackListOfIndex = m_blackListOfIndex.toSet().toList(); m_blackListOfIndex = m_blackListOfIndex.toSet().toList();
s_mutex.unlock();
} }
void DirWatcher::removeBlackListItemOfIndex(const QString &path) void DirWatcher::removeBlackListItemOfIndex(const QString &path)
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_blackListOfIndex.removeAll(path); m_blackListOfIndex.removeAll(path);
s_mutex.unlock();
} }
void DirWatcher::removeBlackListItemOfIndex(const QStringList &pathList) void DirWatcher::removeBlackListItemOfIndex(const QStringList &pathList)
{ {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
for (QString path: pathList) { for (QString path: pathList) {
m_blackListOfIndex.removeAll(path); m_blackListOfIndex.removeAll(path);
} }
s_mutex.unlock(); }
QStringList DirWatcher::currentSearchableDir()
{
QMutexLocker locker(&s_mutex);
return m_searchableDirList;
}
QStringList DirWatcher::searchableDirForSearchApplication()
{
QMutexLocker locker(&s_mutex);
return m_searchableListForApplication;
} }
void DirWatcher::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis) void DirWatcher::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis)
@ -121,7 +140,7 @@ void DirWatcher::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWa
void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis) void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis)
{ {
qDebug() << "Mount Removed"; qDebug() << "Mount Removed";
s_mutex.lock(); QMutexLocker locker(&s_mutex);
//处理u盘设备 //处理u盘设备
if (pThis->m_removedUDiskDevice != NULL) { if (pThis->m_removedUDiskDevice != NULL) {
pThis->m_currentUDiskDeviceInfo.remove(pThis->m_removedUDiskDevice); pThis->m_currentUDiskDeviceInfo.remove(pThis->m_removedUDiskDevice);
@ -130,7 +149,6 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di
qDebug() << "m_currentUDiskDeviceInfo:" << pThis->m_currentUDiskDeviceInfo; qDebug() << "m_currentUDiskDeviceInfo:" << pThis->m_currentUDiskDeviceInfo;
qDebug() << "m_blackListOfIndex:" << pThis->m_blackListOfIndex; qDebug() << "m_blackListOfIndex:" << pThis->m_blackListOfIndex;
pThis->m_removedUDiskDevice = ""; pThis->m_removedUDiskDevice = "";
s_mutex.unlock();
return; return;
} }
@ -138,12 +156,10 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di
GFile* rootFile; GFile* rootFile;
rootFile = g_mount_get_root(mount); rootFile = g_mount_get_root(mount);
if (!rootFile) { if (!rootFile) {
s_mutex.unlock();
return; return;
} }
QString mountPoint = g_file_get_uri(rootFile); QString mountPoint = g_file_get_uri(rootFile);
if (mountPoint.isEmpty()) { if (mountPoint.isEmpty()) {
s_mutex.unlock();
return; return;
} }
//处理uri转码,处理子卷情况 //处理uri转码,处理子卷情况
@ -158,13 +174,11 @@ void DirWatcher::mountRemoveCallback(GVolumeMonitor *monitor, GMount *gmount, Di
pThis->m_blackListOfIndex.removeAll(removedMountPoint.replace(t.key(), t.value())); pThis->m_blackListOfIndex.removeAll(removedMountPoint.replace(t.key(), t.value()));
} }
} }
s_mutex.unlock();
qDebug() << "m_currentMountedDeviceInfo:" << pThis->m_currentMountedDeviceInfo; qDebug() << "m_currentMountedDeviceInfo:" << pThis->m_currentMountedDeviceInfo;
qDebug() << "m_repeatedlyMountedDeviceInfo:" << pThis->m_repeatedlyMountedDeviceInfo; qDebug() << "m_repeatedlyMountedDeviceInfo:" << pThis->m_repeatedlyMountedDeviceInfo;
qDebug() << "m_currentUDiskDeviceInfo:" << pThis->m_currentUDiskDeviceInfo; qDebug() << "m_currentUDiskDeviceInfo:" << pThis->m_currentUDiskDeviceInfo;
qDebug() << "m_blackListOfIndex:" << pThis->m_blackListOfIndex; qDebug() << "m_blackListOfIndex:" << pThis->m_blackListOfIndex;
} else { } else {
s_mutex.unlock();
qWarning() << "There's someting wrong with the MountPoint!"; qWarning() << "There's someting wrong with the MountPoint!";
} }
g_object_unref(rootFile); g_object_unref(rootFile);
@ -176,6 +190,10 @@ void DirWatcher::initData()
m_indexableDirList << "/data" << QDir::homePath(); m_indexableDirList << "/data" << QDir::homePath();
m_blackListOfIndex << "/data/home" << "/data/root"; m_blackListOfIndex << "/data/home" << "/data/root";
//目前方案:可搜索目录(服务)默认根目录,可搜索目录(应用)默认家目录和/data目录
m_searchableListForApplication << "/data" << QDir::homePath();
m_searchableDirList << "/";
//init auto mounted device list //init auto mounted device list
setfsent(); setfsent();
while (1) { while (1) {
@ -225,7 +243,7 @@ void DirWatcher::initDiskWatcher()
void DirWatcher::handleDisk() void DirWatcher::handleDisk()
{ {
//init current mounted device info //init current mounted device info
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_currentMountedDeviceInfo.clear(); m_currentMountedDeviceInfo.clear();
for (QStorageInfo &storage: QStorageInfo::mountedVolumes()) { for (QStorageInfo &storage: QStorageInfo::mountedVolumes()) {
if (storage.isValid() && storage.isReady() && QString(storage.device()).contains(QRegExp("/sd[a-z][1-9]"))) { if (storage.isValid() && storage.isReady() && QString(storage.device()).contains(QRegExp("/sd[a-z][1-9]"))) {
@ -270,17 +288,15 @@ void DirWatcher::handleDisk()
qDebug() << "m_currentUDiskDeviceInfo:" << m_currentUDiskDeviceInfo; qDebug() << "m_currentUDiskDeviceInfo:" << m_currentUDiskDeviceInfo;
qDebug() << "m_blackListOfIndex:" << m_blackListOfIndex; qDebug() << "m_blackListOfIndex:" << m_blackListOfIndex;
s_mutex.unlock();
} }
void DirWatcher::handleAddedUDiskDevice(QDBusMessage msg) void DirWatcher::handleAddedUDiskDevice(QDBusMessage msg)
{ {
QDBusObjectPath objPath = msg.arguments().at(0).value<QDBusObjectPath>(); QDBusObjectPath objPath = msg.arguments().at(0).value<QDBusObjectPath>();
if (objPath.path().contains(QRegExp("/sd[a-z][1-9]"))) { if (objPath.path().contains(QRegExp("/sd[a-z][1-9]"))) {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_addedUDiskDeviceList.append(objPath.path().section("/",-1)); m_addedUDiskDeviceList.append(objPath.path().section("/",-1));
qDebug() << "Add Udisk:" << m_addedUDiskDeviceList; qDebug() << "Add Udisk:" << m_addedUDiskDeviceList;
s_mutex.unlock();
} }
} }
@ -289,9 +305,8 @@ void DirWatcher::handleRemovedUDiskDevice(QDBusMessage msg)
Q_EMIT this->udiskRemoved(); Q_EMIT this->udiskRemoved();
QDBusObjectPath objPath = msg.arguments().at(0).value<QDBusObjectPath>(); QDBusObjectPath objPath = msg.arguments().at(0).value<QDBusObjectPath>();
if (objPath.path().contains(QRegExp("/sd[a-z][1-9]"))) { if (objPath.path().contains(QRegExp("/sd[a-z][1-9]"))) {
s_mutex.lock(); QMutexLocker locker(&s_mutex);
m_removedUDiskDevice = objPath.path().section("/",-1); m_removedUDiskDevice = objPath.path().section("/",-1);
m_addedUDiskDeviceList.removeAll(m_removedUDiskDevice); m_addedUDiskDeviceList.removeAll(m_removedUDiskDevice);
s_mutex.unlock();
} }
} }

View File

@ -27,6 +27,8 @@ public:
void removeBlackListItemOfIndex(const QString &path); void removeBlackListItemOfIndex(const QString &path);
void removeBlackListItemOfIndex(const QStringList &pathList); void removeBlackListItemOfIndex(const QStringList &pathList);
QStringList currentSearchableDir();
QStringList searchableDirForSearchApplication();
QStringList blackListOfDir(const QString &dirPath); QStringList blackListOfDir(const QString &dirPath);
static void mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis); static void mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, DirWatcher *pThis);
@ -49,6 +51,8 @@ private:
QStringList m_blackListOfIndex; QStringList m_blackListOfIndex;
QStringList m_indexableDirList; QStringList m_indexableDirList;
QStringList m_searchableDirList;
QStringList m_searchableListForApplication;
QStringList m_autoMountList; QStringList m_autoMountList;
QMultiMap<QString, QString> m_infoOfSubvolume; QMultiMap<QString, QString> m_infoOfSubvolume;
QMap<QString, QStringList> m_currentMountedDeviceInfo; QMap<QString, QStringList> m_currentMountedDeviceInfo;

View File

@ -18,6 +18,7 @@
* *
*/ */
#include "search-manager.h" #include "search-manager.h"
#include "dir-watcher.h"
using namespace UkuiSearch; using namespace UkuiSearch;
size_t SearchManager::uniqueSymbolFile = 0; size_t SearchManager::uniqueSymbolFile = 0;
@ -523,11 +524,19 @@ DirectSearch::DirectSearch(QString keyword, DataQueue<SearchPluginIface::ResultI
void DirectSearch::run() { void DirectSearch::run() {
QStringList blockList = GlobalSettings::getInstance()->getBlockDirs(); QStringList blockList = GlobalSettings::getInstance()->getBlockDirs();
if(blockList.contains(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).remove(0,1), Qt::CaseSensitive)) { QStringList searchPath = DirWatcher::getDirWatcher()->searchableDirForSearchApplication();
QQueue<QString> bfs;
for (const QString &path : searchPath) {
if (blockList.contains(path)) {
continue;
}
blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path));
bfs.enqueue(path);
}
if (bfs.isEmpty()) {
return; return;
} }
QQueue<QString> bfs;
bfs.enqueue(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
QFileInfoList list; QFileInfoList list;
QDir dir; QDir dir;
// QDir::Hidden // QDir::Hidden