From 2e45a5a7776b79732cd500c7462574f8fbe7ca23 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sat, 27 Jul 2013 16:40:52 +0200 Subject: fix inotify race condition --- distfoldd/watcher.cc | 20 +++++++++++++------- distfoldd/watcher.h | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'distfoldd') diff --git a/distfoldd/watcher.cc b/distfoldd/watcher.cc index 664ea58..39b68f3 100644 --- a/distfoldd/watcher.cc +++ b/distfoldd/watcher.cc @@ -19,10 +19,8 @@ Watcher::Watcher(const QString& path, QObject *parent) : _notifier = new QSocketNotifier(_fd, QSocketNotifier::Read, this); connect(_notifier, SIGNAL(activated(int)), SLOT(readInotify())); - QStringList l = scanDirs(QDir(path)); - foreach (const QString& s, l) { - addWatch(s); - } + // Scan directories and add watches + addWatches(scanDirs(QDir(path))); } void Watcher::readInotify() @@ -58,8 +56,9 @@ void Watcher::readInotify() if (event->mask & (IN_CREATE|IN_MOVED_TO)) { if (event->mask & IN_ISDIR) { - // TODO There might already be folders in here. - addWatch(filePath); + // Perform a recursive scan in case there are directories + // there already. + addWatches(scanDirs(QDir(filePath))); } emit pathAdded(filePath); } @@ -81,7 +80,7 @@ void Watcher::readInotify() QStringList Watcher::scanDirs(const QDir &dir) { Q_ASSERT(dir.isReadable()); - QStringList l(dir.absolutePath()); + QStringList l(dir.absolutePath()); // Add dir to the list QStringList sub_dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); foreach (const QString& s, sub_dirs) { @@ -92,6 +91,13 @@ QStringList Watcher::scanDirs(const QDir &dir) return l; } +void Watcher::addWatches(const QStringList &paths) +{ + foreach (const QString& s, paths) { + addWatch(s); + } +} + void Watcher::addWatch(const QString& path) { int wd = inotify_add_watch(_fd, path.toLocal8Bit().constData(), _mask); diff --git a/distfoldd/watcher.h b/distfoldd/watcher.h index 861248e..74a9162 100644 --- a/distfoldd/watcher.h +++ b/distfoldd/watcher.h @@ -22,6 +22,7 @@ private slots: private: QStringList scanDirs(const QDir& dir); + void addWatches(const QStringList &paths); void addWatch(const QString& path); void removeWatch(const QString& path); void removeWatch(int wd); -- cgit v1.2.3