Chris@538: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@538: Chris@538: /* Chris@538: EasyMercurial Chris@538: Chris@538: Based on hgExplorer by Jari Korhonen Chris@538: Copyright (c) 2010 Jari Korhonen Chris@560: Copyright (c) 2012 Chris Cannam Chris@560: Copyright (c) 2012 Queen Mary, University of London Chris@538: Chris@538: This program is free software; you can redistribute it and/or Chris@538: modify it under the terms of the GNU General Public License as Chris@538: published by the Free Software Foundation; either version 2 of the Chris@538: License, or (at your option) any later version. See the file Chris@538: COPYING included with this distribution for more information. Chris@538: */ Chris@538: Chris@538: #include "fswatcher.h" Chris@540: #include "debug.h" Chris@538: Chris@538: #include Chris@538: #include Chris@538: Chris@538: #include Chris@538: Chris@562: //#define DEBUG_FSWATCHER 1 Chris@540: Chris@539: /* Chris@539: * Watching the filesystem is trickier than it seems at first glance. Chris@539: * Chris@539: * We ideally should watch every directory, and every file that is Chris@539: * tracked by Hg. If a new file is created in a directory, then we Chris@539: * need to respond in order to show it as a potential candidate to be Chris@539: * added. Chris@539: * Chris@539: * Complicating matters though is that Hg itself might modify the Chris@539: * filesystem. This can happen even in "read-only" operations: for Chris@539: * example, hg stat creates files called hg-checklink and hg-checkexec Chris@539: * to test properties of the filesystem. So we need to know to ignore Chris@539: * those files; unfortunately, when watching a directory (which is how Chris@539: * we find out about the creation of new files) we are notified only Chris@540: * that the directory has changed -- we aren't told what changed. Chris@540: * Chris@540: * This means that, when a directory changes, we need to rescan the Chris@540: * directory to learn whether the set of files in it _excluding_ files Chris@540: * matching our ignore patterns differs from the previous scan, and Chris@540: * ignore the change if it doesn't. Chris@539: */ Chris@539: Chris@538: FsWatcher::FsWatcher() : Chris@538: m_lastToken(0), Chris@538: m_lastCounter(0) Chris@538: { Chris@538: connect(&m_watcher, SIGNAL(directoryChanged(QString)), Chris@538: this, SLOT(fsDirectoryChanged(QString))); Chris@538: connect(&m_watcher, SIGNAL(fileChanged(QString)), Chris@538: this, SLOT(fsFileChanged(QString))); Chris@538: } Chris@538: Chris@538: FsWatcher::~FsWatcher() Chris@538: { Chris@538: } Chris@538: Chris@538: void Chris@538: FsWatcher::setWorkDirPath(QString path) Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@541: if (m_workDirPath == path) return; Chris@562: // annoyingly, removePaths prints a warning if given an empty list Chris@562: if (!m_watcher.directories().empty()) { Chris@562: m_watcher.removePaths(m_watcher.directories()); Chris@562: } Chris@562: if (!m_watcher.files().empty()) { Chris@562: m_watcher.removePaths(m_watcher.files()); Chris@562: } Chris@538: m_workDirPath = path; Chris@538: addWorkDirectory(path); Chris@540: debugPrint(); Chris@538: } Chris@538: Chris@538: void Chris@539: FsWatcher::setTrackedFilePaths(QStringList paths) Chris@539: { Chris@539: QMutexLocker locker(&m_mutex); Chris@541: Chris@541: QSet alreadyWatched = Chris@541: QSet::fromList(m_watcher.files()); Chris@541: Chris@539: foreach (QString path, paths) { Chris@542: path = m_workDirPath + QDir::separator() + path; Chris@541: if (!alreadyWatched.contains(path)) { Chris@541: m_watcher.addPath(path); Chris@541: } else { Chris@541: alreadyWatched.remove(path); Chris@541: } Chris@539: } Chris@541: Chris@541: // Remove the remaining paths, those that were being watched Chris@541: // before but that are not in the list we were given Chris@541: foreach (QString path, alreadyWatched) { Chris@541: m_watcher.removePath(path); Chris@541: } Chris@541: Chris@540: debugPrint(); Chris@539: } Chris@539: Chris@539: void Chris@538: FsWatcher::addWorkDirectory(QString path) Chris@538: { Chris@538: // QFileSystemWatcher will refuse to add a file or directory to Chris@538: // its watch list that it is already watching -- fine -- but it Chris@538: // prints a warning when this happens, which we wouldn't want. So Chris@538: // we'll check for duplicates ourselves. Chris@538: QSet alreadyWatched = Chris@538: QSet::fromList(m_watcher.directories()); Chris@538: Chris@538: std::deque pending; Chris@538: pending.push_back(path); Chris@538: Chris@538: while (!pending.empty()) { Chris@538: Chris@538: QString path = pending.front(); Chris@538: pending.pop_front(); Chris@538: if (!alreadyWatched.contains(path)) { Chris@538: m_watcher.addPath(path); Chris@540: m_dirContents[path] = scanDirectory(path); Chris@538: } Chris@538: Chris@538: QDir d(path); Chris@538: if (d.exists()) { Chris@538: d.setFilter(QDir::Dirs | QDir::NoDotAndDotDot | Chris@538: QDir::Readable | QDir::NoSymLinks); Chris@538: foreach (QString entry, d.entryList()) { Chris@538: if (entry.startsWith('.')) continue; Chris@538: QString entryPath = d.absoluteFilePath(entry); Chris@538: pending.push_back(entryPath); Chris@538: } Chris@538: } Chris@538: } Chris@538: } Chris@538: Chris@538: void Chris@538: FsWatcher::setIgnoredFilePrefixes(QStringList prefixes) Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@538: m_ignoredPrefixes = prefixes; Chris@538: } Chris@538: Chris@538: void Chris@538: FsWatcher::setIgnoredFileSuffixes(QStringList suffixes) Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@538: m_ignoredSuffixes = suffixes; Chris@538: } Chris@538: Chris@538: int Chris@538: FsWatcher::getNewToken() Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@538: int token = ++m_lastToken; Chris@538: m_tokenMap[token] = m_lastCounter; Chris@538: return token; Chris@538: } Chris@538: Chris@538: QSet Chris@538: FsWatcher::getChangedPaths(int token) Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@538: size_t lastUpdatedAt = m_tokenMap[token]; Chris@538: QSet changed; Chris@538: for (QHash::const_iterator i = m_changes.begin(); Chris@538: i != m_changes.end(); ++i) { Chris@538: if (i.value() > lastUpdatedAt) { Chris@538: changed.insert(i.key()); Chris@538: } Chris@538: } Chris@538: m_tokenMap[token] = m_lastCounter; Chris@538: return changed; Chris@538: } Chris@538: Chris@538: void Chris@538: FsWatcher::fsDirectoryChanged(QString path) Chris@538: { Chris@538: { Chris@538: QMutexLocker locker(&m_mutex); Chris@540: Chris@538: if (shouldIgnore(path)) return; Chris@540: Chris@540: QSet files = scanDirectory(path); Chris@540: if (files == m_dirContents[path]) { Chris@540: #ifdef DEBUG_FSWATCHER Chris@540: std::cerr << "FsWatcher: Directory " << path << " has changed, but not in a way that we are monitoring" << std::endl; Chris@540: #endif Chris@540: return; Chris@541: } else { Chris@541: #ifdef DEBUG_FSWATCHER Chris@541: std::cerr << "FsWatcher: Directory " << path << " has changed" << std::endl; Chris@541: #endif Chris@541: m_dirContents[path] = files; Chris@540: } Chris@540: Chris@540: size_t counter = ++m_lastCounter; Chris@540: m_changes[path] = counter; Chris@538: } Chris@540: Chris@538: emit changed(); Chris@538: } Chris@538: Chris@538: void Chris@538: FsWatcher::fsFileChanged(QString path) Chris@538: { Chris@540: { Chris@540: QMutexLocker locker(&m_mutex); Chris@540: Chris@540: // We don't check whether the file matches an ignore pattern, Chris@540: // because we are only notified for file changes if we are Chris@540: // watching the file explicitly, i.e. the file is in the Chris@540: // tracked file paths list. So we never want to ignore them Chris@540: Chris@563: #ifdef DEBUG_FSWATCHER Chris@541: std::cerr << "FsWatcher: Tracked file " << path << " has changed" << std::endl; Chris@563: #endif Chris@541: Chris@540: size_t counter = ++m_lastCounter; Chris@540: m_changes[path] = counter; Chris@540: } Chris@540: Chris@540: emit changed(); Chris@538: } Chris@538: Chris@538: bool Chris@538: FsWatcher::shouldIgnore(QString path) Chris@538: { Chris@540: QFileInfo fi(path); Chris@540: QString fn(fi.fileName()); Chris@540: foreach (QString pfx, m_ignoredPrefixes) { Chris@541: if (fn.startsWith(pfx)) { Chris@563: #ifdef DEBUG_FSWATCHER Chris@541: std::cerr << "(ignoring: " << path << ")" << std::endl; Chris@563: #endif Chris@541: return true; Chris@541: } Chris@540: } Chris@540: foreach (QString sfx, m_ignoredSuffixes) { Chris@541: if (fn.endsWith(sfx)) { Chris@563: #ifdef DEBUG_FSWATCHER Chris@541: std::cerr << "(ignoring: " << path << ")" << std::endl; Chris@563: #endif Chris@541: return true; Chris@541: } Chris@540: } Chris@540: return false; Chris@538: } Chris@538: Chris@540: QSet Chris@540: FsWatcher::scanDirectory(QString path) Chris@540: { Chris@540: QSet files; Chris@540: QDir d(path); Chris@540: if (d.exists()) { Chris@540: d.setFilter(QDir::Files | QDir::NoDotAndDotDot | Chris@540: QDir::Readable | QDir::NoSymLinks); Chris@540: foreach (QString entry, d.entryList()) { Chris@540: if (entry.startsWith('.')) continue; Chris@540: if (shouldIgnore(entry)) continue; Chris@540: files.insert(entry); Chris@540: } Chris@540: } Chris@541: // std::cerr << "scanDirectory:" << std::endl; Chris@541: // foreach (QString f, files) std::cerr << f << std::endl; Chris@540: return files; Chris@540: } Chris@540: Chris@540: void Chris@540: FsWatcher::debugPrint() Chris@540: { Chris@540: #ifdef DEBUG_FSWATCHER Chris@540: std::cerr << "FsWatcher: Now watching " << m_watcher.directories().size() Chris@540: << " directories and " << m_watcher.files().size() Chris@540: << " files" << std::endl; Chris@540: #endif Chris@540: }