增加非递归监听模式。

This commit is contained in:
iaom 2022-08-05 14:07:24 +08:00
parent ee5d5ae413
commit 8462a8cbcf
3 changed files with 18 additions and 6 deletions

View File

@ -42,6 +42,8 @@ public:
QStringList removeWatch(const QString &path);
QString removeWatch(int wd);
private:
void init();
void traverse(QStringList pathList);
@ -57,6 +59,8 @@ private:
QThreadPool *m_pool = nullptr;
FileSystemWatcher *q = nullptr;
bool m_recursive = true;
};

View File

@ -61,6 +61,9 @@ void FileSystemWatcherPrivate::traverse(QStringList pathList)
addWatch(path);
queue.enqueue(path);
}
if(!m_recursive) {
return;
}
QFileInfoList list;
QDir dir;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
@ -146,12 +149,13 @@ void FileSystemWatcherPrivate::init()
}
}
FileSystemWatcher::FileSystemWatcher(WatchEvents events, WatchFlags flags, QObject *parent)
FileSystemWatcher::FileSystemWatcher(bool recursive, WatchEvents events, WatchFlags flags, QObject *parent)
: QObject(parent)
, d(new FileSystemWatcherPrivate(this))
{
d->m_watchEvents = events;
d->m_watchFlags = flags;
d->m_recursive = recursive;
}
FileSystemWatcher::~FileSystemWatcher()
@ -220,7 +224,7 @@ void FileSystemWatcher::eventProcess(int socket)
if(event->mask & EventCreate) {
qDebug() << path << "--EventCreate";
Q_EMIT created(path, event->mask & IN_ISDIR);
if(event->mask & IN_ISDIR) {
if(event->mask & IN_ISDIR && d->m_recursive) {
if(!QFileInfo(path).isSymLink()){
addWatch(QStringList(path));
}
@ -256,8 +260,10 @@ void FileSystemWatcher::eventProcess(int socket)
if (event->mask & EventMoveTo) {
qDebug() << path << "--EventMoveTo";
Q_EMIT created(path, event->mask & IN_ISDIR);
if (event->mask & IN_ISDIR) {
addWatch(QStringList(path));
if (event->mask & IN_ISDIR && d->m_recursive) {
if(!QFileInfo(path).isSymLink()){
addWatch(QStringList(path));
}
}
}
if (event->mask & EventOpen) {

View File

@ -75,6 +75,7 @@ public:
/**
* @brief FileSystemWatcher
* @param recursive if the watcher should watch the paths recursively. .
* @param events Events that shoude be concerned,default: FileSystemWatcher::EventMove | FileSystemWatcher::EventMoveSelf |
FileSystemWatcher::EventCreate | FileSystemWatcher::EventDelete |
FileSystemWatcher::EventDeleteSelf | FileSystemWatcher::EventUnmount |
@ -82,7 +83,8 @@ public:
* @param flags Watch flags.
* @param parent
*/
explicit FileSystemWatcher(WatchEvents events = WatchEvents(FileSystemWatcher::EventMove | FileSystemWatcher::EventMoveSelf |
explicit FileSystemWatcher(bool recursive = true,
WatchEvents events = WatchEvents(FileSystemWatcher::EventMove | FileSystemWatcher::EventMoveSelf |
FileSystemWatcher::EventCreate | FileSystemWatcher::EventDelete |
FileSystemWatcher::EventDeleteSelf | FileSystemWatcher::EventUnmount |
FileSystemWatcher::EventModify),
@ -94,7 +96,7 @@ public:
FileSystemWatcher(FileSystemWatcher &) = delete;
FileSystemWatcher &operator =(const FileSystemWatcher &) = delete;
bool isWatchingPath(const QString& path) const;
// bool isWatchingPath(const QString& path) const;
public Q_SLOTS:
void addWatch(const QStringList &pathList);