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@541
|
85 if (!alreadyWatched.contains(path)) {
|
Chris@541
|
86 m_watcher.addPath(path);
|
Chris@541
|
87 } else {
|
Chris@541
|
88 alreadyWatched.remove(path);
|
Chris@541
|
89 }
|
Chris@539
|
90 }
|
Chris@541
|
91
|
Chris@541
|
92 // Remove the remaining paths, those that were being watched
|
Chris@541
|
93 // before but that are not in the list we were given
|
Chris@541
|
94 foreach (QString path, alreadyWatched) {
|
Chris@541
|
95 m_watcher.removePath(path);
|
Chris@541
|
96 }
|
Chris@541
|
97
|
Chris@540
|
98 debugPrint();
|
Chris@539
|
99 }
|
Chris@539
|
100
|
Chris@539
|
101 void
|
Chris@538
|
102 FsWatcher::addWorkDirectory(QString path)
|
Chris@538
|
103 {
|
Chris@538
|
104 // QFileSystemWatcher will refuse to add a file or directory to
|
Chris@538
|
105 // its watch list that it is already watching -- fine -- but it
|
Chris@538
|
106 // prints a warning when this happens, which we wouldn't want. So
|
Chris@538
|
107 // we'll check for duplicates ourselves.
|
Chris@538
|
108 QSet<QString> alreadyWatched =
|
Chris@538
|
109 QSet<QString>::fromList(m_watcher.directories());
|
Chris@538
|
110
|
Chris@538
|
111 std::deque<QString> pending;
|
Chris@538
|
112 pending.push_back(path);
|
Chris@538
|
113
|
Chris@538
|
114 while (!pending.empty()) {
|
Chris@538
|
115
|
Chris@538
|
116 QString path = pending.front();
|
Chris@538
|
117 pending.pop_front();
|
Chris@538
|
118 if (!alreadyWatched.contains(path)) {
|
Chris@538
|
119 m_watcher.addPath(path);
|
Chris@540
|
120 m_dirContents[path] = scanDirectory(path);
|
Chris@538
|
121 }
|
Chris@538
|
122
|
Chris@538
|
123 QDir d(path);
|
Chris@538
|
124 if (d.exists()) {
|
Chris@538
|
125 d.setFilter(QDir::Dirs | QDir::NoDotAndDotDot |
|
Chris@538
|
126 QDir::Readable | QDir::NoSymLinks);
|
Chris@538
|
127 foreach (QString entry, d.entryList()) {
|
Chris@538
|
128 if (entry.startsWith('.')) continue;
|
Chris@538
|
129 QString entryPath = d.absoluteFilePath(entry);
|
Chris@538
|
130 pending.push_back(entryPath);
|
Chris@538
|
131 }
|
Chris@538
|
132 }
|
Chris@538
|
133 }
|
Chris@538
|
134 }
|
Chris@538
|
135
|
Chris@538
|
136 void
|
Chris@538
|
137 FsWatcher::setIgnoredFilePrefixes(QStringList prefixes)
|
Chris@538
|
138 {
|
Chris@538
|
139 QMutexLocker locker(&m_mutex);
|
Chris@538
|
140 m_ignoredPrefixes = prefixes;
|
Chris@538
|
141 }
|
Chris@538
|
142
|
Chris@538
|
143 void
|
Chris@538
|
144 FsWatcher::setIgnoredFileSuffixes(QStringList suffixes)
|
Chris@538
|
145 {
|
Chris@538
|
146 QMutexLocker locker(&m_mutex);
|
Chris@538
|
147 m_ignoredSuffixes = suffixes;
|
Chris@538
|
148 }
|
Chris@538
|
149
|
Chris@538
|
150 int
|
Chris@538
|
151 FsWatcher::getNewToken()
|
Chris@538
|
152 {
|
Chris@538
|
153 QMutexLocker locker(&m_mutex);
|
Chris@538
|
154 int token = ++m_lastToken;
|
Chris@538
|
155 m_tokenMap[token] = m_lastCounter;
|
Chris@538
|
156 return token;
|
Chris@538
|
157 }
|
Chris@538
|
158
|
Chris@538
|
159 QSet<QString>
|
Chris@538
|
160 FsWatcher::getChangedPaths(int token)
|
Chris@538
|
161 {
|
Chris@538
|
162 QMutexLocker locker(&m_mutex);
|
Chris@538
|
163 size_t lastUpdatedAt = m_tokenMap[token];
|
Chris@538
|
164 QSet<QString> changed;
|
Chris@538
|
165 for (QHash<QString, size_t>::const_iterator i = m_changes.begin();
|
Chris@538
|
166 i != m_changes.end(); ++i) {
|
Chris@538
|
167 if (i.value() > lastUpdatedAt) {
|
Chris@538
|
168 changed.insert(i.key());
|
Chris@538
|
169 }
|
Chris@538
|
170 }
|
Chris@538
|
171 m_tokenMap[token] = m_lastCounter;
|
Chris@538
|
172 return changed;
|
Chris@538
|
173 }
|
Chris@538
|
174
|
Chris@538
|
175 void
|
Chris@538
|
176 FsWatcher::fsDirectoryChanged(QString path)
|
Chris@538
|
177 {
|
Chris@538
|
178 {
|
Chris@538
|
179 QMutexLocker locker(&m_mutex);
|
Chris@540
|
180
|
Chris@538
|
181 if (shouldIgnore(path)) return;
|
Chris@540
|
182
|
Chris@540
|
183 QSet<QString> files = scanDirectory(path);
|
Chris@540
|
184 if (files == m_dirContents[path]) {
|
Chris@540
|
185 #ifdef DEBUG_FSWATCHER
|
Chris@540
|
186 std::cerr << "FsWatcher: Directory " << path << " has changed, but not in a way that we are monitoring" << std::endl;
|
Chris@540
|
187 #endif
|
Chris@540
|
188 return;
|
Chris@541
|
189 } else {
|
Chris@541
|
190 #ifdef DEBUG_FSWATCHER
|
Chris@541
|
191 std::cerr << "FsWatcher: Directory " << path << " has changed" << std::endl;
|
Chris@541
|
192 #endif
|
Chris@541
|
193 m_dirContents[path] = files;
|
Chris@540
|
194 }
|
Chris@540
|
195
|
Chris@540
|
196 size_t counter = ++m_lastCounter;
|
Chris@540
|
197 m_changes[path] = counter;
|
Chris@538
|
198 }
|
Chris@540
|
199
|
Chris@538
|
200 emit changed();
|
Chris@538
|
201 }
|
Chris@538
|
202
|
Chris@538
|
203 void
|
Chris@538
|
204 FsWatcher::fsFileChanged(QString path)
|
Chris@538
|
205 {
|
Chris@540
|
206 {
|
Chris@540
|
207 QMutexLocker locker(&m_mutex);
|
Chris@540
|
208
|
Chris@540
|
209 // We don't check whether the file matches an ignore pattern,
|
Chris@540
|
210 // because we are only notified for file changes if we are
|
Chris@540
|
211 // watching the file explicitly, i.e. the file is in the
|
Chris@540
|
212 // tracked file paths list. So we never want to ignore them
|
Chris@540
|
213
|
Chris@541
|
214 std::cerr << "FsWatcher: Tracked file " << path << " has changed" << std::endl;
|
Chris@541
|
215
|
Chris@540
|
216 size_t counter = ++m_lastCounter;
|
Chris@540
|
217 m_changes[path] = counter;
|
Chris@540
|
218 }
|
Chris@540
|
219
|
Chris@540
|
220 emit changed();
|
Chris@538
|
221 }
|
Chris@538
|
222
|
Chris@538
|
223 bool
|
Chris@538
|
224 FsWatcher::shouldIgnore(QString path)
|
Chris@538
|
225 {
|
Chris@540
|
226 QFileInfo fi(path);
|
Chris@540
|
227 QString fn(fi.fileName());
|
Chris@540
|
228 foreach (QString pfx, m_ignoredPrefixes) {
|
Chris@541
|
229 if (fn.startsWith(pfx)) {
|
Chris@541
|
230 std::cerr << "(ignoring: " << path << ")" << std::endl;
|
Chris@541
|
231 return true;
|
Chris@541
|
232 }
|
Chris@540
|
233 }
|
Chris@540
|
234 foreach (QString sfx, m_ignoredSuffixes) {
|
Chris@541
|
235 if (fn.endsWith(sfx)) {
|
Chris@541
|
236 std::cerr << "(ignoring: " << path << ")" << std::endl;
|
Chris@541
|
237 return true;
|
Chris@541
|
238 }
|
Chris@540
|
239 }
|
Chris@540
|
240 return false;
|
Chris@538
|
241 }
|
Chris@538
|
242
|
Chris@540
|
243 QSet<QString>
|
Chris@540
|
244 FsWatcher::scanDirectory(QString path)
|
Chris@540
|
245 {
|
Chris@540
|
246 QSet<QString> files;
|
Chris@540
|
247 QDir d(path);
|
Chris@540
|
248 if (d.exists()) {
|
Chris@540
|
249 d.setFilter(QDir::Files | QDir::NoDotAndDotDot |
|
Chris@540
|
250 QDir::Readable | QDir::NoSymLinks);
|
Chris@540
|
251 foreach (QString entry, d.entryList()) {
|
Chris@540
|
252 if (entry.startsWith('.')) continue;
|
Chris@540
|
253 if (shouldIgnore(entry)) continue;
|
Chris@540
|
254 files.insert(entry);
|
Chris@540
|
255 }
|
Chris@540
|
256 }
|
Chris@541
|
257 // std::cerr << "scanDirectory:" << std::endl;
|
Chris@541
|
258 // foreach (QString f, files) std::cerr << f << std::endl;
|
Chris@540
|
259 return files;
|
Chris@540
|
260 }
|
Chris@540
|
261
|
Chris@540
|
262 void
|
Chris@540
|
263 FsWatcher::debugPrint()
|
Chris@540
|
264 {
|
Chris@540
|
265 #ifdef DEBUG_FSWATCHER
|
Chris@540
|
266 std::cerr << "FsWatcher: Now watching " << m_watcher.directories().size()
|
Chris@540
|
267 << " directories and " << m_watcher.files().size()
|
Chris@540
|
268 << " files" << std::endl;
|
Chris@540
|
269 #endif
|
Chris@540
|
270 }
|