From 16011de197ef5573090236e2834d9fdabc63a3ea Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Tue, 5 Nov 2024 11:20:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(index):=E6=B7=BB=E5=8A=A0/media=E5=90=8E?= =?UTF-8?q?=E6=8F=92=E5=85=A5u=E7=9B=98=EF=BC=8Cu=E7=9B=98=E5=86=85?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=B2=A1=E8=A2=AB=E7=B4=A2=E5=BC=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libsearch/dirwatcher/dir-watcher.cpp | 1 + libsearch/dirwatcher/dir-watcher.h | 1 + .../file-system-watcher-private.h | 1 + .../filesystemwatcher/file-system-watcher.cpp | 36 ++++++++++++++++--- .../filesystemwatcher/file-system-watcher.h | 2 +- .../dirwatcher/dir-watcher.cpp | 2 ++ .../dirwatcher/dir-watcher.h | 1 + .../dirwatcher/volume-manager.cpp | 15 +++++++- .../dirwatcher/volume-manager.h | 1 + 9 files changed, 54 insertions(+), 6 deletions(-) diff --git a/libsearch/dirwatcher/dir-watcher.cpp b/libsearch/dirwatcher/dir-watcher.cpp index 4f0442b..9a56347 100644 --- a/libsearch/dirwatcher/dir-watcher.cpp +++ b/libsearch/dirwatcher/dir-watcher.cpp @@ -38,6 +38,7 @@ DirWatcher::DirWatcher(QObject *parent) } else { connect(m_dbusInterface, SIGNAL(appendIndexItem(QString, QStringList)), this, SLOT(sendAppendSignal(QString, QStringList))); connect(m_dbusInterface, SIGNAL(removeIndexItem(QString)), this, SLOT(sendRemoveSignal(QString))); + connect(m_dbusInterface, SIGNAL(mountAdded(QString)), this, SIGNAL(mountAdded(QString))); } } diff --git a/libsearch/dirwatcher/dir-watcher.h b/libsearch/dirwatcher/dir-watcher.h index 7a5b3b1..be45dda 100644 --- a/libsearch/dirwatcher/dir-watcher.h +++ b/libsearch/dirwatcher/dir-watcher.h @@ -50,6 +50,7 @@ public Q_SLOTS: Q_SIGNALS: void appendIndexItem(const QString&, const QStringList&); void removeIndexItem(const QString&); + void mountAdded(const QString&); private: DirWatcher(QObject *parent = nullptr); ~DirWatcher(); diff --git a/libsearch/filesystemwatcher/file-system-watcher-private.h b/libsearch/filesystemwatcher/file-system-watcher-private.h index dc5a169..1d8b0b9 100644 --- a/libsearch/filesystemwatcher/file-system-watcher-private.h +++ b/libsearch/filesystemwatcher/file-system-watcher-private.h @@ -56,6 +56,7 @@ private: QStringList m_watchedRootPaths; FileSystemWatcher *q = nullptr; bool m_recursive = true; + QStringList m_failedWatchPaths; }; diff --git a/libsearch/filesystemwatcher/file-system-watcher.cpp b/libsearch/filesystemwatcher/file-system-watcher.cpp index 9853bb4..fbb3abe 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.cpp +++ b/libsearch/filesystemwatcher/file-system-watcher.cpp @@ -30,6 +30,7 @@ #include "ukui-search-qdbus.h" #include "file-utils.h" +#include "dir-watcher.h" using namespace UkuiSearch; FileSystemWatcherPrivate::FileSystemWatcherPrivate(FileSystemWatcher *parent) : q(parent) { @@ -183,6 +184,21 @@ FileSystemWatcher::FileSystemWatcher(bool recursive, WatchEvents events, WatchFl d->m_watchEvents = events; d->m_watchFlags = flags; d->m_recursive = recursive; + connect(DirWatcher::getDirWatcher(), &DirWatcher::mountAdded, this, [ & ] (const QString& mountPoint) { + for (const auto &path: d->m_failedWatchPaths) { + if (FileUtils::isOrUnder(path, mountPoint)) { + qDebug() << "the path that fail to watch has been mounted: " << path; + d->m_failedWatchPaths.removeAll(path); + for(const QString &changedPath : traverse(path)) { + QFileInfo info(changedPath); + if (info.isReadable() && info.isExecutable()) { + Q_EMIT accessibleChanged(changedPath, info.isDir(), true); + } + } + break; + } + } + }); } FileSystemWatcher::~FileSystemWatcher() @@ -400,10 +416,17 @@ void FileSystemWatcher::eventProcess(int socket) free(buf); } -QStringList FileSystemWatcher::traverse(QString &path) +QStringList FileSystemWatcher::traverse(const QString &path) { QStringList paths; - d->addWatch(path); + QFileInfo info(path); + if (info.isReadable() && info.isExecutable()) { + d->addWatch(path); + } else { + qWarning() << "Fail to watch" << path << ".Permission Dined"; + d->m_failedWatchPaths.append(path); + } + if(!d->m_recursive || QFileInfo(path).isSymLink()) { return paths; } @@ -419,8 +442,13 @@ QStringList FileSystemWatcher::traverse(QString &path) list = dir.entryInfoList(); for(auto i : list) { if(i.isDir() && !(i.isSymLink())) { - queue.enqueue(i.absoluteFilePath()); - d->addWatch(i.absoluteFilePath()); + if (i.isReadable() && i.isExecutable()) { + queue.enqueue(i.absoluteFilePath()); + d->addWatch(i.absoluteFilePath()); + } else { + qWarning() << "Fail to watch" << i.absoluteFilePath(); + d->m_failedWatchPaths.append(i.absoluteFilePath()); + } } paths.append(i.absoluteFilePath()); } diff --git a/libsearch/filesystemwatcher/file-system-watcher.h b/libsearch/filesystemwatcher/file-system-watcher.h index 6acb3b3..31f4d2c 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.h +++ b/libsearch/filesystemwatcher/file-system-watcher.h @@ -164,7 +164,7 @@ Q_SIGNALS: void accessibleChanged(const QString& path, bool isDir, bool accessable); private Q_SLOTS: void eventProcess(int socket); - QStringList traverse(QString &path); + QStringList traverse(const QString &path); private: diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp index 49fb989..9591ea8 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.cpp @@ -28,6 +28,7 @@ #include #include "config.h" #include "fileindexserviceadaptor.h" +#include "volume-manager.h" #define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf" #define INDEXABLE_DIR_VALUE "IndexableDir" @@ -41,6 +42,7 @@ DirWatcher::DirWatcher(QObject *parent) : QObject(parent) //兼容旧版配置 Config::self()->processCompatibleCache(); + connect(VolumeManager::self(), &VolumeManager::MountAdded, this, &DirWatcher::mountAdded); new FileindexAdaptor(this); } diff --git a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h index 9364808..515b079 100644 --- a/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h +++ b/ukui-search-service-dir-manager/dirwatcher/dir-watcher.h @@ -83,6 +83,7 @@ private: Q_SIGNALS: void appendIndexItem(const QString&, const QStringList&); void removeIndexItem(const QString&); + void mountAdded(const QString&); //abondoned void udiskRemoved(); void indexDirsChanged(); diff --git a/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp b/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp index dc1d6ff..d733405 100644 --- a/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp +++ b/ukui-search-service-dir-manager/dirwatcher/volume-manager.cpp @@ -163,7 +163,20 @@ void VolumeManager::refresh() void VolumeManager::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, VolumeManager *pThis) { Q_UNUSED(monitor) - Q_UNUSED(gmount) + QString mountPoint; + auto rootFile = g_mount_get_root(gmount); + if (rootFile) { + auto gMountPoint = g_file_get_uri(rootFile); + mountPoint = gMountPoint; + g_free(gMountPoint); + if(!(mountPoint.startsWith("smb://"))){ + gMountPoint = g_filename_from_uri(mountPoint.toUtf8().constData(), nullptr, nullptr); + mountPoint = gMountPoint; + g_free(gMountPoint); + } + g_object_unref(rootFile); + } + Q_EMIT pThis->MountAdded(mountPoint); //TODO 识别U盘等移动设备 pThis->refresh(); } diff --git a/ukui-search-service-dir-manager/dirwatcher/volume-manager.h b/ukui-search-service-dir-manager/dirwatcher/volume-manager.h index 7f75e1f..532ffbe 100644 --- a/ukui-search-service-dir-manager/dirwatcher/volume-manager.h +++ b/ukui-search-service-dir-manager/dirwatcher/volume-manager.h @@ -61,6 +61,7 @@ public: void refresh(); Q_SIGNALS: void VolumeDataUpdated(); + void MountAdded(const QString&); private: explicit VolumeManager(QObject *parent = nullptr);