feat(file-watcher):文件可读权限发生改变后更新索引

This commit is contained in:
iaom 2024-05-06 18:03:16 +08:00
parent 7d59fd81fd
commit 497eec01c8
5 changed files with 53 additions and 15 deletions

View File

@ -23,7 +23,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <cerrno>
#include <QDir> #include <QDir>
#include <QQueue> #include <QQueue>
#include <QtConcurrentRun> #include <QtConcurrentRun>
@ -360,6 +360,22 @@ void FileSystemWatcher::eventProcess(int socket)
if (event->mask & EventAttributeChange) { if (event->mask & EventAttributeChange) {
// qDebug() << path << "--EventAttributeChange"; // qDebug() << path << "--EventAttributeChange";
Q_EMIT attributeChanged(path); Q_EMIT attributeChanged(path);
if (event->mask & IN_ISDIR) {
QFileInfo info(path);
if(!info.isReadable() || !info.isExecutable()) {
for(const QString &removedPath : d->removeWatch(path)) {
Q_EMIT accessibleChanged(removedPath, true, false);
}
} else {
if (!d->m_watchPathHash.values().contains(path)) {
for(const QString &createdPath : traverse(path)) {
Q_EMIT accessibleChanged(createdPath, QFileInfo(createdPath).isDir(), true);
}
}
}
} else {
Q_EMIT accessibleChanged(path, false, QFileInfo(path).isReadable());
}
} }
if (event->mask & EventAccess) { if (event->mask & EventAccess) {
// qDebug() << path << "--EventAccess"; // qDebug() << path << "--EventAccess";

View File

@ -158,7 +158,10 @@ Q_SIGNALS:
* Note:if the new file moved here overwrited a file already existed, there will not be a 'deleted' signal. * Note:if the new file moved here overwrited a file already existed, there will not be a 'deleted' signal.
*/ */
void moveTo(const QString& path, bool isDir); void moveTo(const QString& path, bool isDir);
/**
* Emitted when the current user's file permissions changed(FileSystemWatcher::EventAttributeChange)
*/
void accessibleChanged(const QString& path, bool isDir, bool accessable);
private Q_SLOTS: private Q_SLOTS:
void eventProcess(int socket); void eventProcess(int socket);
QStringList traverse(QString &path); QStringList traverse(QString &path);

View File

@ -22,17 +22,23 @@ using namespace UkuiSearch;
FileWatcher::FileWatcher(QObject *parent) : QObject(parent), m_config(FileIndexerConfig::getInstance()) FileWatcher::FileWatcher(QObject *parent) : QObject(parent), m_config(FileIndexerConfig::getInstance())
{ {
qRegisterMetaType<QVector<PendingFile>>("QVector<PendingFile>"); qRegisterMetaType<QVector<PendingFile>>("QVector<PendingFile>");
m_watcher = new FileSystemWatcher(); m_watcher = new FileSystemWatcher(true,
m_pendingFileQUeue = PendingFileQueue::getInstance(); FileSystemWatcher::WatchEvents(FileSystemWatcher::EventMove | FileSystemWatcher::EventMoveSelf |
FileSystemWatcher::EventCreate | FileSystemWatcher::EventDelete |
FileSystemWatcher::EventDeleteSelf | FileSystemWatcher::EventUnmount |
FileSystemWatcher::EventModify | FileSystemWatcher::EventAttributeChange),
FileSystemWatcher::WatchFlags(), nullptr);
m_pendingFileQueue = PendingFileQueue::getInstance();
connect(m_watcher, &FileSystemWatcher::created, this, &FileWatcher::onFileCreated); connect(m_watcher, &FileSystemWatcher::created, this, &FileWatcher::onFileCreated);
connect(m_watcher, &FileSystemWatcher::moveTo, this, &FileWatcher::onFileMoveTo); connect(m_watcher, &FileSystemWatcher::moveTo, this, &FileWatcher::onFileMoveTo);
connect(m_watcher, &FileSystemWatcher::modified, this, &FileWatcher::onFileModefied); connect(m_watcher, &FileSystemWatcher::modified, this, &FileWatcher::onFileModified);
connect(m_watcher, &FileSystemWatcher::deleted, this, &FileWatcher::onFileDeletedOrMoved); connect(m_watcher, &FileSystemWatcher::deleted, this, &FileWatcher::onFileDeletedOrMoved);
connect(m_watcher, &FileSystemWatcher::moved, this, &FileWatcher::onFileDeletedOrMoved); connect(m_watcher, &FileSystemWatcher::moved, this, &FileWatcher::onFileDeletedOrMoved);
connect(m_watcher, &FileSystemWatcher::unmounted, this, &FileWatcher::onFileDeletedOrMoved); connect(m_watcher, &FileSystemWatcher::unmounted, this, &FileWatcher::onFileDeletedOrMoved);
connect(m_watcher, &FileSystemWatcher::accessibleChanged, this, &FileWatcher::onFileAccessibleChanged);
connect(m_pendingFileQUeue, &PendingFileQueue::filesUpdate, this, &FileWatcher::filesUpdate); connect(m_pendingFileQueue, &PendingFileQueue::filesUpdate, this, &FileWatcher::filesUpdate);
} }
FileWatcher::~FileWatcher() FileWatcher::~FileWatcher()
@ -56,7 +62,7 @@ void FileWatcher::removeWatch(const QString &path, bool updateIndex)
PendingFile file(pathToDelete); PendingFile file(pathToDelete);
file.setIsDir(); file.setIsDir();
file.setDeleted(); file.setDeleted();
m_pendingFileQUeue->enqueue(file); m_pendingFileQueue->enqueue(file);
} }
} }
} }
@ -77,7 +83,7 @@ void FileWatcher::onFileDeletedOrMoved(const QString &path, bool isDir)
PendingFile file(path); PendingFile file(path);
file.setIsDir(isDir); file.setIsDir(isDir);
file.setDeleted(); file.setDeleted();
m_pendingFileQUeue->enqueue(file); m_pendingFileQueue->enqueue(file);
} }
void FileWatcher::onFileCreated(const QString &path, bool isDir) void FileWatcher::onFileCreated(const QString &path, bool isDir)
@ -85,7 +91,7 @@ void FileWatcher::onFileCreated(const QString &path, bool isDir)
PendingFile file(path); PendingFile file(path);
file.setIsDir(isDir); file.setIsDir(isDir);
file.setCreated(); file.setCreated();
m_pendingFileQUeue->enqueue(file); m_pendingFileQueue->enqueue(file);
} }
void FileWatcher::onFileMoveTo(const QString &path, bool isDir) void FileWatcher::onFileMoveTo(const QString &path, bool isDir)
@ -93,12 +99,24 @@ void FileWatcher::onFileMoveTo(const QString &path, bool isDir)
PendingFile file(path); PendingFile file(path);
file.setIsDir(isDir); file.setIsDir(isDir);
file.setMoveTo(); file.setMoveTo();
m_pendingFileQUeue->enqueue(file); m_pendingFileQueue->enqueue(file);
} }
void FileWatcher::onFileModefied(const QString &path) void FileWatcher::onFileModified(const QString &path)
{ {
PendingFile file(path); PendingFile file(path);
file.setModified(); file.setModified();
m_pendingFileQUeue->enqueue(file); m_pendingFileQueue->enqueue(file);
}
void FileWatcher::onFileAccessibleChanged(const QString &path, bool isDir, bool accessible)
{
PendingFile file(path);
file.setIsDir(isDir);
if(accessible) {
file.setModified();
} else {
file.setDeleted();
}
m_pendingFileQueue->enqueue(file);
} }

View File

@ -63,11 +63,12 @@ Q_SIGNALS:
private: private:
void onFileCreated(const QString& path, bool isDir); void onFileCreated(const QString& path, bool isDir);
void onFileMoveTo(const QString& path, bool isDir); void onFileMoveTo(const QString& path, bool isDir);
void onFileModefied(const QString& path); void onFileModified(const QString& path);
void onFileDeletedOrMoved(const QString& path, bool isDir); void onFileDeletedOrMoved(const QString& path, bool isDir);
void onFileAccessibleChanged(const QString& path, bool isDir, bool accessible);
FileSystemWatcher *m_watcher = nullptr; FileSystemWatcher *m_watcher = nullptr;
FileIndexerConfig *m_config = nullptr; FileIndexerConfig *m_config = nullptr;
PendingFileQueue *m_pendingFileQUeue = nullptr; PendingFileQueue *m_pendingFileQueue = nullptr;
}; };
} }

View File

@ -80,7 +80,7 @@ void PendingFile::merge(const PendingFile& file)
void PendingFile::printFlags() const void PendingFile::printFlags() const
{ {
// qDebug() << "Created:" << m_created; qDebug() << "Created:" << m_created;
qDebug() << "Deleted:" << m_deleted; qDebug() << "Deleted:" << m_deleted;
qDebug() << "Modified:" << m_modified; qDebug() << "Modified:" << m_modified;
qDebug() << "Is dir:" << m_isDir; qDebug() << "Is dir:" << m_isDir;