author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <qplatformdefs.h> |
|
43 |
||
44 |
#include "qfilesystemwatcher.h" |
|
45 |
#include "qfilesystemwatcher_kqueue_p.h" |
|
46 |
#include "private/qcore_unix_p.h" |
|
47 |
||
48 |
#include <qdebug.h> |
|
49 |
#include <qfile.h> |
|
50 |
#include <qsocketnotifier.h> |
|
51 |
#include <qvarlengtharray.h> |
|
52 |
||
53 |
#include <sys/types.h> |
|
54 |
#include <sys/event.h> |
|
55 |
#include <sys/stat.h> |
|
56 |
#include <sys/time.h> |
|
57 |
#include <fcntl.h> |
|
58 |
||
59 |
QT_BEGIN_NAMESPACE |
|
60 |
||
61 |
// #define KEVENT_DEBUG |
|
62 |
#ifdef KEVENT_DEBUG |
|
63 |
# define DEBUG qDebug |
|
64 |
#else |
|
65 |
# define DEBUG if(false)qDebug |
|
66 |
#endif |
|
67 |
||
68 |
QKqueueFileSystemWatcherEngine *QKqueueFileSystemWatcherEngine::create() |
|
69 |
{ |
|
70 |
int kqfd = kqueue(); |
|
71 |
if (kqfd == -1) |
|
72 |
return 0; |
|
73 |
return new QKqueueFileSystemWatcherEngine(kqfd); |
|
74 |
} |
|
75 |
||
76 |
QKqueueFileSystemWatcherEngine::QKqueueFileSystemWatcherEngine(int kqfd) |
|
77 |
: kqfd(kqfd) |
|
78 |
{ |
|
79 |
fcntl(kqfd, F_SETFD, FD_CLOEXEC); |
|
80 |
||
81 |
if (pipe(kqpipe) == -1) { |
|
82 |
perror("QKqueueFileSystemWatcherEngine: cannot create pipe"); |
|
83 |
kqpipe[0] = kqpipe[1] = -1; |
|
84 |
return; |
|
85 |
} |
|
86 |
fcntl(kqpipe[0], F_SETFD, FD_CLOEXEC); |
|
87 |
fcntl(kqpipe[1], F_SETFD, FD_CLOEXEC); |
|
88 |
||
89 |
struct kevent kev; |
|
90 |
EV_SET(&kev, |
|
91 |
kqpipe[0], |
|
92 |
EVFILT_READ, |
|
93 |
EV_ADD | EV_ENABLE, |
|
94 |
0, |
|
95 |
0, |
|
96 |
0); |
|
97 |
if (kevent(kqfd, &kev, 1, 0, 0, 0) == -1) { |
|
98 |
perror("QKqueueFileSystemWatcherEngine: cannot watch pipe, kevent returned"); |
|
99 |
return; |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
QKqueueFileSystemWatcherEngine::~QKqueueFileSystemWatcherEngine() |
|
104 |
{ |
|
105 |
stop(); |
|
106 |
wait(); |
|
107 |
||
108 |
close(kqfd); |
|
109 |
close(kqpipe[0]); |
|
110 |
close(kqpipe[1]); |
|
111 |
||
112 |
foreach (int id, pathToID.values()) |
|
113 |
::close(id < 0 ? -id : id); |
|
114 |
} |
|
115 |
||
116 |
QStringList QKqueueFileSystemWatcherEngine::addPaths(const QStringList &paths, |
|
117 |
QStringList *files, |
|
118 |
QStringList *directories) |
|
119 |
{ |
|
120 |
QMutexLocker locker(&mutex); |
|
121 |
||
122 |
QStringList p = paths; |
|
123 |
QMutableListIterator<QString> it(p); |
|
124 |
while (it.hasNext()) { |
|
125 |
QString path = it.next(); |
|
126 |
int fd; |
|
127 |
#if defined(O_EVTONLY) |
|
128 |
fd = qt_safe_open(QFile::encodeName(path), O_EVTONLY); |
|
129 |
#else |
|
130 |
fd = qt_safe_open(QFile::encodeName(path), O_RDONLY); |
|
131 |
#endif |
|
132 |
if (fd == -1) { |
|
133 |
perror("QKqueueFileSystemWatcherEngine::addPaths: open"); |
|
134 |
continue; |
|
135 |
} |
|
136 |
||
137 |
QT_STATBUF st; |
|
138 |
if (QT_FSTAT(fd, &st) == -1) { |
|
139 |
perror("QKqueueFileSystemWatcherEngine::addPaths: fstat"); |
|
140 |
::close(fd); |
|
141 |
continue; |
|
142 |
} |
|
143 |
int id = (S_ISDIR(st.st_mode)) ? -fd : fd; |
|
144 |
if (id < 0) { |
|
145 |
if (directories->contains(path)) { |
|
146 |
::close(fd); |
|
147 |
continue; |
|
148 |
} |
|
149 |
} else { |
|
150 |
if (files->contains(path)) { |
|
151 |
::close(fd); |
|
152 |
continue; |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
struct kevent kev; |
|
157 |
EV_SET(&kev, |
|
158 |
fd, |
|
159 |
EVFILT_VNODE, |
|
160 |
EV_ADD | EV_ENABLE | EV_ONESHOT, |
|
161 |
NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE, |
|
162 |
0, |
|
163 |
0); |
|
164 |
if (kevent(kqfd, &kev, 1, 0, 0, 0) == -1) { |
|
165 |
perror("QKqueueFileSystemWatcherEngine::addPaths: kevent"); |
|
166 |
::close(fd); |
|
167 |
continue; |
|
168 |
} |
|
169 |
||
170 |
it.remove(); |
|
171 |
if (id < 0) { |
|
172 |
DEBUG() << "QKqueueFileSystemWatcherEngine: added directory path" << path; |
|
173 |
directories->append(path); |
|
174 |
} else { |
|
175 |
DEBUG() << "QKqueueFileSystemWatcherEngine: added file path" << path; |
|
176 |
files->append(path); |
|
177 |
} |
|
178 |
||
179 |
pathToID.insert(path, id); |
|
180 |
idToPath.insert(id, path); |
|
181 |
} |
|
182 |
||
183 |
if (!isRunning()) |
|
184 |
start(); |
|
185 |
else |
|
186 |
write(kqpipe[1], "@", 1); |
|
187 |
||
188 |
return p; |
|
189 |
} |
|
190 |
||
191 |
QStringList QKqueueFileSystemWatcherEngine::removePaths(const QStringList &paths, |
|
192 |
QStringList *files, |
|
193 |
QStringList *directories) |
|
194 |
{ |
|
195 |
QMutexLocker locker(&mutex); |
|
196 |
||
197 |
QStringList p = paths; |
|
198 |
QMutableListIterator<QString> it(p); |
|
199 |
while (it.hasNext()) { |
|
200 |
QString path = it.next(); |
|
201 |
int id = pathToID.take(path); |
|
202 |
QString x = idToPath.take(id); |
|
203 |
if (x.isEmpty() || x != path) |
|
204 |
continue; |
|
205 |
||
206 |
int fd = id < 0 ? -id : id; |
|
207 |
struct kevent kev; |
|
208 |
EV_SET(&kev, |
|
209 |
fd, |
|
210 |
EVFILT_VNODE, |
|
211 |
EV_DELETE, |
|
212 |
NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE, |
|
213 |
0, |
|
214 |
0); |
|
215 |
if (kevent(kqfd, &kev, 1, 0, 0, 0) == -1) { |
|
216 |
perror("QKqueueFileSystemWatcherEngine::removeWatch: kevent"); |
|
217 |
} |
|
218 |
::close(fd); |
|
219 |
||
220 |
it.remove(); |
|
221 |
if (id < 0) |
|
222 |
directories->removeAll(path); |
|
223 |
else |
|
224 |
files->removeAll(path); |
|
225 |
} |
|
226 |
||
227 |
if (pathToID.isEmpty()) { |
|
228 |
stop(); |
|
229 |
locker.unlock(); |
|
230 |
wait(); |
|
231 |
locker.relock(); |
|
232 |
} else { |
|
233 |
write(kqpipe[1], "@", 1); |
|
234 |
} |
|
235 |
||
236 |
return p; |
|
237 |
} |
|
238 |
||
239 |
void QKqueueFileSystemWatcherEngine::stop() |
|
240 |
{ |
|
241 |
write(kqpipe[1], "q", 1); |
|
242 |
} |
|
243 |
||
244 |
void QKqueueFileSystemWatcherEngine::run() |
|
245 |
{ |
|
246 |
static const struct timespec ZeroTimeout = { 0, 0 }; |
|
247 |
||
248 |
forever { |
|
249 |
struct kevent kev; |
|
250 |
DEBUG() << "QKqueueFileSystemWatcherEngine: waiting for kevents..."; |
|
251 |
int r = kevent(kqfd, 0, 0, &kev, 1, 0); |
|
252 |
if (r < 0) { |
|
253 |
perror("QKqueueFileSystemWatcherEngine: error during kevent wait"); |
|
254 |
return; |
|
255 |
} |
|
256 |
||
257 |
QMutexLocker locker(&mutex); |
|
258 |
do { |
|
259 |
int fd = kev.ident; |
|
260 |
||
261 |
DEBUG() << "QKqueueFileSystemWatcherEngine: processing kevent" << kev.ident << kev.filter; |
|
262 |
if (fd == kqpipe[0]) { |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
263 |
// read all pending data from the pipe |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
264 |
QByteArray ba; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
265 |
ba.resize(kev.data); |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
266 |
if (read(kqpipe[0], ba.data(), ba.size()) != ba.size()) { |
0 | 267 |
perror("QKqueueFileSystemWatcherEngine: error reading from pipe"); |
268 |
return; |
|
269 |
} |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
270 |
// read the command from the buffer (but break and return on 'q') |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
271 |
char cmd = 0; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
272 |
for (int i = 0; i < ba.size(); ++i) { |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
273 |
cmd = ba.constData()[i]; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
274 |
if (cmd == 'q') |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
275 |
break; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
276 |
} |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
277 |
// handle the command |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
278 |
switch (cmd) { |
0 | 279 |
case 'q': |
280 |
DEBUG() << "QKqueueFileSystemWatcherEngine: thread received 'q', exiting..."; |
|
281 |
return; |
|
282 |
case '@': |
|
283 |
DEBUG() << "QKqueueFileSystemWatcherEngine: thread received '@', continuing..."; |
|
284 |
break; |
|
285 |
default: |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
286 |
DEBUG() << "QKqueueFileSystemWatcherEngine: thread received unknow message" << cmd; |
0 | 287 |
break; |
288 |
} |
|
289 |
} else { |
|
290 |
int id = fd; |
|
291 |
QString path = idToPath.value(id); |
|
292 |
if (path.isEmpty()) { |
|
293 |
// perhaps a directory? |
|
294 |
id = -id; |
|
295 |
path = idToPath.value(id); |
|
296 |
if (path.isEmpty()) { |
|
297 |
DEBUG() << "QKqueueFileSystemWatcherEngine: received a kevent for a file we're not watching"; |
|
298 |
continue; |
|
299 |
} |
|
300 |
} |
|
301 |
if (kev.filter != EVFILT_VNODE) { |
|
302 |
DEBUG() << "QKqueueFileSystemWatcherEngine: received a kevent with the wrong filter"; |
|
303 |
continue; |
|
304 |
} |
|
305 |
||
306 |
if ((kev.fflags & (NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME)) != 0) { |
|
307 |
DEBUG() << path << "removed, removing watch also"; |
|
308 |
||
309 |
pathToID.remove(path); |
|
310 |
idToPath.remove(id); |
|
311 |
::close(fd); |
|
312 |
||
313 |
if (id < 0) |
|
314 |
emit directoryChanged(path, true); |
|
315 |
else |
|
316 |
emit fileChanged(path, true); |
|
317 |
} else { |
|
318 |
DEBUG() << path << "changed, re-enabling watch"; |
|
319 |
||
320 |
if (id < 0) |
|
321 |
emit directoryChanged(path, false); |
|
322 |
else |
|
323 |
emit fileChanged(path, false); |
|
324 |
||
325 |
// renable the watch |
|
326 |
EV_SET(&kev, |
|
327 |
fd, |
|
328 |
EVFILT_VNODE, |
|
329 |
EV_ADD | EV_ENABLE | EV_ONESHOT, |
|
330 |
NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE, |
|
331 |
0, |
|
332 |
0); |
|
333 |
if (kevent(kqfd, &kev, 1, 0, 0, 0) == -1) { |
|
334 |
perror("QKqueueFileSystemWatcherEngine::processKqueueEvents: kevent EV_ADD"); |
|
335 |
} |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
// are there any more? |
|
340 |
r = kevent(kqfd, 0, 0, &kev, 1, &ZeroTimeout); |
|
341 |
} while (r > 0); |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
QT_END_NAMESPACE |