diff --git a/libsearch/filesystemwatcher/file-system-watcher-private.h b/libsearch/filesystemwatcher/file-system-watcher-private.h index cbb4561..277ed2d 100644 --- a/libsearch/filesystemwatcher/file-system-watcher-private.h +++ b/libsearch/filesystemwatcher/file-system-watcher-private.h @@ -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; + }; diff --git a/libsearch/filesystemwatcher/file-system-watcher.cpp b/libsearch/filesystemwatcher/file-system-watcher.cpp index 14a3f22..85b25fe 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.cpp +++ b/libsearch/filesystemwatcher/file-system-watcher.cpp @@ -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) { diff --git a/libsearch/filesystemwatcher/file-system-watcher.h b/libsearch/filesystemwatcher/file-system-watcher.h index 12a8472..647d17e 100644 --- a/libsearch/filesystemwatcher/file-system-watcher.h +++ b/libsearch/filesystemwatcher/file-system-watcher.h @@ -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);