annotate src/fswatcher.cpp @ 548:dca5bd5b2a06

Merge from branch "fswatcher"
author Chris Cannam
date Tue, 14 Feb 2012 17:55:39 +0000
parents 7829da6abe97
children 533519ebc0cb
rev   line source
Chris@538 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@538 2
Chris@538 3 /*
Chris@538 4 EasyMercurial
Chris@538 5
Chris@538 6 Based on hgExplorer by Jari Korhonen
Chris@538 7 Copyright (c) 2010 Jari Korhonen
Chris@538 8 Copyright (c) 2011 Chris Cannam
Chris@538 9 Copyright (c) 2011 Queen Mary, University of London
Chris@538 10
Chris@538 11 This program is free software; you can redistribute it and/or
Chris@538 12 modify it under the terms of the GNU General Public License as
Chris@538 13 published by the Free Software Foundation; either version 2 of the
Chris@538 14 License, or (at your option) any later version. See the file
Chris@538 15 COPYING included with this distribution for more information.
Chris@538 16 */
Chris@538 17
Chris@538 18 #include "fswatcher.h"
Chris@540 19 #include "debug.h"
Chris@538 20
Chris@538 21 #include <QMutexLocker>
Chris@538 22 #include <QDir>
Chris@538 23
Chris@538 24 #include <deque>
Chris@538 25
Chris@540 26 #define DEBUG_FSWATCHER 1
Chris@540 27
Chris@539 28 /*
Chris@539 29 * Watching the filesystem is trickier than it seems at first glance.
Chris@539 30 *
Chris@539 31 * We ideally should watch every directory, and every file that is
Chris@539 32 * tracked by Hg. If a new file is created in a directory, then we
Chris@539 33 * need to respond in order to show it as a potential candidate to be
Chris@539 34 * added.
Chris@539 35 *
Chris@539 36 * Complicating matters though is that Hg itself might modify the
Chris@539 37 * filesystem. This can happen even in "read-only" operations: for
Chris@539 38 * example, hg stat creates files called hg-checklink and hg-checkexec
Chris@539 39 * to test properties of the filesystem. So we need to know to ignore
Chris@539 40 * those files; unfortunately, when watching a directory (which is how
Chris@539 41 * we find out about the creation of new files) we are notified only
Chris@540 42 * that the directory has changed -- we aren't told what changed.
Chris@540 43 *
Chris@540 44 * This means that, when a directory changes, we need to rescan the
Chris@540 45 * directory to learn whether the set of files in it _excluding_ files
Chris@540 46 * matching our ignore patterns differs from the previous scan, and
Chris@540 47 * ignore the change if it doesn't.
Chris@539 48 */
Chris@539 49
Chris@538 50 FsWatcher::FsWatcher() :
Chris@538 51 m_lastToken(0),
Chris@538 52 m_lastCounter(0)
Chris@538 53 {
Chris@538 54 connect(&m_watcher, SIGNAL(directoryChanged(QString)),
Chris@538 55 this, SLOT(fsDirectoryChanged(QString)));
Chris@538 56 connect(&m_watcher, SIGNAL(fileChanged(QString)),
Chris@538 57 this, SLOT(fsFileChanged(QString)));
Chris@538 58 }
Chris@538 59
Chris@538 60 FsWatcher::~FsWatcher()
Chris@538 61 {
Chris@538 62 }
Chris@538 63
Chris@538 64 void
Chris@538 65 FsWatcher::setWorkDirPath(QString path)
Chris@538 66 {
Chris@538 67 QMutexLocker locker(&m_mutex);
Chris@541 68 if (m_workDirPath == path) return;
Chris@538 69 m_watcher.removePaths(m_watcher.directories());
Chris@538 70 m_watcher.removePaths(m_watcher.files());
Chris@538 71 m_workDirPath = path;
Chris@538 72 addWorkDirectory(path);
Chris@540 73 debugPrint();
Chris@538 74 }
Chris@538 75
Chris@538 76 void
Chris@539 77 FsWatcher::setTrackedFilePaths(QStringList paths)
Chris@539 78 {
Chris@539 79 QMutexLocker locker(&m_mutex);
Chris@541 80
Chris@541 81 QSet<QString> alreadyWatched =
Chris@541 82 QSet<QString>::fromList(m_watcher.files());
Chris@541 83
Chris@539 84 foreach (QString path, paths) {
Chris@542 85 path = m_workDirPath + QDir::separator() + path;
Chris@541 86 if (!alreadyWatched.contains(path)) {
Chris@541 87 m_watcher.addPath(path);
Chris@541 88 } else {
Chris@541 89 alreadyWatched.remove(path);
Chris@541 90 }
Chris@539 91 }
Chris@541 92
Chris@541 93 // Remove the remaining paths, those that were being watched
Chris@541 94 // before but that are not in the list we were given
Chris@541 95 foreach (QString path, alreadyWatched) {
Chris@541 96 m_watcher.removePath(path);
Chris@541 97 }
Chris@541 98
Chris@540 99 debugPrint();
Chris@539 100 }
Chris@539 101
Chris@539 102 void
Chris@538 103 FsWatcher::addWorkDirectory(QString path)
Chris@538 104 {
Chris@538 105 // QFileSystemWatcher will refuse to add a file or directory to
Chris@538 106 // its watch list that it is already watching -- fine -- but it
Chris@538 107 // prints a warning when this happens, which we wouldn't want. So
Chris@538 108 // we'll check for duplicates ourselves.
Chris@538 109 QSet<QString> alreadyWatched =
Chris@538 110 QSet<QString>::fromList(m_watcher.directories());
Chris@538 111
Chris@538 112 std::deque<QString> pending;
Chris@538 113 pending.push_back(path);
Chris@538 114
Chris@538 115 while (!pending.empty()) {
Chris@538 116
Chris@538 117 QString path = pending.front();
Chris@538 118 pending.pop_front();
Chris@538 119 if (!alreadyWatched.contains(path)) {
Chris@538 120 m_watcher.addPath(path);
Chris@540 121 m_dirContents[path] = scanDirectory(path);
Chris@538 122 }
Chris@538 123
Chris@538 124 QDir d(path);
Chris@538 125 if (d.exists()) {
Chris@538 126 d.setFilter(QDir::Dirs | QDir::NoDotAndDotDot |
Chris@538 127 QDir::Readable | QDir::NoSymLinks);
Chris@538 128 foreach (QString entry, d.entryList()) {
Chris@538 129 if (entry.startsWith('.')) continue;
Chris@538 130 QString entryPath = d.absoluteFilePath(entry);
Chris@538 131 pending.push_back(entryPath);
Chris@538 132 }
Chris@538 133 }
Chris@538 134 }
Chris@538 135 }
Chris@538 136
Chris@538 137 void
Chris@538 138 FsWatcher::setIgnoredFilePrefixes(QStringList prefixes)
Chris@538 139 {
Chris@538 140 QMutexLocker locker(&m_mutex);
Chris@538 141 m_ignoredPrefixes = prefixes;
Chris@538 142 }
Chris@538 143
Chris@538 144 void
Chris@538 145 FsWatcher::setIgnoredFileSuffixes(QStringList suffixes)
Chris@538 146 {
Chris@538 147 QMutexLocker locker(&m_mutex);
Chris@538 148 m_ignoredSuffixes = suffixes;
Chris@538 149 }
Chris@538 150
Chris@538 151 int
Chris@538 152 FsWatcher::getNewToken()
Chris@538 153 {
Chris@538 154 QMutexLocker locker(&m_mutex);
Chris@538 155 int token = ++m_lastToken;
Chris@538 156 m_tokenMap[token] = m_lastCounter;
Chris@538 157 return token;
Chris@538 158 }
Chris@538 159
Chris@538 160 QSet<QString>
Chris@538 161 FsWatcher::getChangedPaths(int token)
Chris@538 162 {
Chris@538 163 QMutexLocker locker(&m_mutex);
Chris@538 164 size_t lastUpdatedAt = m_tokenMap[token];
Chris@538 165 QSet<QString> changed;
Chris@538 166 for (QHash<QString, size_t>::const_iterator i = m_changes.begin();
Chris@538 167 i != m_changes.end(); ++i) {
Chris@538 168 if (i.value() > lastUpdatedAt) {
Chris@538 169 changed.insert(i.key());
Chris@538 170 }
Chris@538 171 }
Chris@538 172 m_tokenMap[token] = m_lastCounter;
Chris@538 173 return changed;
Chris@538 174 }
Chris@538 175
Chris@538 176 void
Chris@538 177 FsWatcher::fsDirectoryChanged(QString path)
Chris@538 178 {
Chris@538 179 {
Chris@538 180 QMutexLocker locker(&m_mutex);
Chris@540 181
Chris@538 182 if (shouldIgnore(path)) return;
Chris@540 183
Chris@540 184 QSet<QString> files = scanDirectory(path);
Chris@540 185 if (files == m_dirContents[path]) {
Chris@540 186 #ifdef DEBUG_FSWATCHER
Chris@540 187 std::cerr << "FsWatcher: Directory " << path << " has changed, but not in a way that we are monitoring" << std::endl;
Chris@540 188 #endif
Chris@540 189 return;
Chris@541 190 } else {
Chris@541 191 #ifdef DEBUG_FSWATCHER
Chris@541 192 std::cerr << "FsWatcher: Directory " << path << " has changed" << std::endl;
Chris@541 193 #endif
Chris@541 194 m_dirContents[path] = files;
Chris@540 195 }
Chris@540 196
Chris@540 197 size_t counter = ++m_lastCounter;
Chris@540 198 m_changes[path] = counter;
Chris@538 199 }
Chris@540 200
Chris@538 201 emit changed();
Chris@538 202 }
Chris@538 203
Chris@538 204 void
Chris@538 205 FsWatcher::fsFileChanged(QString path)
Chris@538 206 {
Chris@540 207 {
Chris@540 208 QMutexLocker locker(&m_mutex);
Chris@540 209
Chris@540 210 // We don't check whether the file matches an ignore pattern,
Chris@540 211 // because we are only notified for file changes if we are
Chris@540 212 // watching the file explicitly, i.e. the file is in the
Chris@540 213 // tracked file paths list. So we never want to ignore them
Chris@540 214
Chris@541 215 std::cerr << "FsWatcher: Tracked file " << path << " has changed" << std::endl;
Chris@541 216
Chris@540 217 size_t counter = ++m_lastCounter;
Chris@540 218 m_changes[path] = counter;
Chris@540 219 }
Chris@540 220
Chris@540 221 emit changed();
Chris@538 222 }
Chris@538 223
Chris@538 224 bool
Chris@538 225 FsWatcher::shouldIgnore(QString path)
Chris@538 226 {
Chris@540 227 QFileInfo fi(path);
Chris@540 228 QString fn(fi.fileName());
Chris@540 229 foreach (QString pfx, m_ignoredPrefixes) {
Chris@541 230 if (fn.startsWith(pfx)) {
Chris@541 231 std::cerr << "(ignoring: " << path << ")" << std::endl;
Chris@541 232 return true;
Chris@541 233 }
Chris@540 234 }
Chris@540 235 foreach (QString sfx, m_ignoredSuffixes) {
Chris@541 236 if (fn.endsWith(sfx)) {
Chris@541 237 std::cerr << "(ignoring: " << path << ")" << std::endl;
Chris@541 238 return true;
Chris@541 239 }
Chris@540 240 }
Chris@540 241 return false;
Chris@538 242 }
Chris@538 243
Chris@540 244 QSet<QString>
Chris@540 245 FsWatcher::scanDirectory(QString path)
Chris@540 246 {
Chris@540 247 QSet<QString> files;
Chris@540 248 QDir d(path);
Chris@540 249 if (d.exists()) {
Chris@540 250 d.setFilter(QDir::Files | QDir::NoDotAndDotDot |
Chris@540 251 QDir::Readable | QDir::NoSymLinks);
Chris@540 252 foreach (QString entry, d.entryList()) {
Chris@540 253 if (entry.startsWith('.')) continue;
Chris@540 254 if (shouldIgnore(entry)) continue;
Chris@540 255 files.insert(entry);
Chris@540 256 }
Chris@540 257 }
Chris@541 258 // std::cerr << "scanDirectory:" << std::endl;
Chris@541 259 // foreach (QString f, files) std::cerr << f << std::endl;
Chris@540 260 return files;
Chris@540 261 }
Chris@540 262
Chris@540 263 void
Chris@540 264 FsWatcher::debugPrint()
Chris@540 265 {
Chris@540 266 #ifdef DEBUG_FSWATCHER
Chris@540 267 std::cerr << "FsWatcher: Now watching " << m_watcher.directories().size()
Chris@540 268 << " directories and " << m_watcher.files().size()
Chris@540 269 << " files" << std::endl;
Chris@540 270 #endif
Chris@540 271 }