Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qfilesystemwatcher_inotify.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qfilesystemwatcher.h" | - | ||||||
41 | #include "qfilesystemwatcher_inotify_p.h" | - | ||||||
42 | - | |||||||
43 | #ifndef QT_NO_FILESYSTEMWATCHER | - | ||||||
44 | - | |||||||
45 | #include "private/qcore_unix_p.h" | - | ||||||
46 | #include "private/qsystemerror_p.h" | - | ||||||
47 | - | |||||||
48 | #include <qdebug.h> | - | ||||||
49 | #include <qfile.h> | - | ||||||
50 | #include <qfileinfo.h> | - | ||||||
51 | #include <qsocketnotifier.h> | - | ||||||
52 | #include <qvarlengtharray.h> | - | ||||||
53 | - | |||||||
54 | #if defined(Q_OS_LINUX) | - | ||||||
55 | #include <sys/syscall.h> | - | ||||||
56 | #include <sys/ioctl.h> | - | ||||||
57 | #include <unistd.h> | - | ||||||
58 | #include <fcntl.h> | - | ||||||
59 | #endif | - | ||||||
60 | - | |||||||
61 | #if defined(QT_NO_INOTIFY) | - | ||||||
62 | - | |||||||
63 | #if defined(Q_OS_QNX) | - | ||||||
64 | // These files should only be compiled on QNX if the inotify headers are found | - | ||||||
65 | #error "Should not get here." | - | ||||||
66 | #endif | - | ||||||
67 | - | |||||||
68 | #include <linux/types.h> | - | ||||||
69 | - | |||||||
70 | #if defined(__i386__) | - | ||||||
71 | # define __NR_inotify_init 291 | - | ||||||
72 | # define __NR_inotify_add_watch 292 | - | ||||||
73 | # define __NR_inotify_rm_watch 293 | - | ||||||
74 | # define __NR_inotify_init1 332 | - | ||||||
75 | #elif defined(__x86_64__) | - | ||||||
76 | # define __NR_inotify_init 253 | - | ||||||
77 | # define __NR_inotify_add_watch 254 | - | ||||||
78 | # define __NR_inotify_rm_watch 255 | - | ||||||
79 | # define __NR_inotify_init1 294 | - | ||||||
80 | #elif defined(__powerpc__) || defined(__powerpc64__) | - | ||||||
81 | # define __NR_inotify_init 275 | - | ||||||
82 | # define __NR_inotify_add_watch 276 | - | ||||||
83 | # define __NR_inotify_rm_watch 277 | - | ||||||
84 | # define __NR_inotify_init1 318 | - | ||||||
85 | #elif defined (__ia64__) | - | ||||||
86 | # define __NR_inotify_init 1277 | - | ||||||
87 | # define __NR_inotify_add_watch 1278 | - | ||||||
88 | # define __NR_inotify_rm_watch 1279 | - | ||||||
89 | # define __NR_inotify_init1 1318 | - | ||||||
90 | #elif defined (__s390__) || defined (__s390x__) | - | ||||||
91 | # define __NR_inotify_init 284 | - | ||||||
92 | # define __NR_inotify_add_watch 285 | - | ||||||
93 | # define __NR_inotify_rm_watch 286 | - | ||||||
94 | # define __NR_inotify_init1 324 | - | ||||||
95 | #elif defined (__alpha__) | - | ||||||
96 | # define __NR_inotify_init 444 | - | ||||||
97 | # define __NR_inotify_add_watch 445 | - | ||||||
98 | # define __NR_inotify_rm_watch 446 | - | ||||||
99 | // no inotify_init1 for the Alpha | - | ||||||
100 | #elif defined (__sparc__) || defined (__sparc64__) | - | ||||||
101 | # define __NR_inotify_init 151 | - | ||||||
102 | # define __NR_inotify_add_watch 152 | - | ||||||
103 | # define __NR_inotify_rm_watch 156 | - | ||||||
104 | # define __NR_inotify_init1 322 | - | ||||||
105 | #elif defined (__arm__) | - | ||||||
106 | # define __NR_inotify_init 316 | - | ||||||
107 | # define __NR_inotify_add_watch 317 | - | ||||||
108 | # define __NR_inotify_rm_watch 318 | - | ||||||
109 | # define __NR_inotify_init1 360 | - | ||||||
110 | #elif defined (__sh__) | - | ||||||
111 | # define __NR_inotify_init 290 | - | ||||||
112 | # define __NR_inotify_add_watch 291 | - | ||||||
113 | # define __NR_inotify_rm_watch 292 | - | ||||||
114 | # define __NR_inotify_init1 332 | - | ||||||
115 | #elif defined (__sh64__) | - | ||||||
116 | # define __NR_inotify_init 318 | - | ||||||
117 | # define __NR_inotify_add_watch 319 | - | ||||||
118 | # define __NR_inotify_rm_watch 320 | - | ||||||
119 | # define __NR_inotify_init1 360 | - | ||||||
120 | #elif defined (__mips__) | - | ||||||
121 | # define __NR_inotify_init 284 | - | ||||||
122 | # define __NR_inotify_add_watch 285 | - | ||||||
123 | # define __NR_inotify_rm_watch 286 | - | ||||||
124 | # define __NR_inotify_init1 329 | - | ||||||
125 | #elif defined (__hppa__) | - | ||||||
126 | # define __NR_inotify_init 269 | - | ||||||
127 | # define __NR_inotify_add_watch 270 | - | ||||||
128 | # define __NR_inotify_rm_watch 271 | - | ||||||
129 | # define __NR_inotify_init1 314 | - | ||||||
130 | #elif defined (__avr32__) | - | ||||||
131 | # define __NR_inotify_init 240 | - | ||||||
132 | # define __NR_inotify_add_watch 241 | - | ||||||
133 | # define __NR_inotify_rm_watch 242 | - | ||||||
134 | // no inotify_init1 for AVR32 | - | ||||||
135 | #elif defined (__mc68000__) | - | ||||||
136 | # define __NR_inotify_init 284 | - | ||||||
137 | # define __NR_inotify_add_watch 285 | - | ||||||
138 | # define __NR_inotify_rm_watch 286 | - | ||||||
139 | # define __NR_inotify_init1 328 | - | ||||||
140 | #elif defined (__aarch64__) | - | ||||||
141 | # define __NR_inotify_init1 26 | - | ||||||
142 | # define __NR_inotify_add_watch 27 | - | ||||||
143 | # define __NR_inotify_rm_watch 28 | - | ||||||
144 | // no inotify_init for aarch64 | - | ||||||
145 | #else | - | ||||||
146 | # error "This architecture is not supported. Please see http://www.qt-project.org/" | - | ||||||
147 | #endif | - | ||||||
148 | - | |||||||
149 | #if !defined(IN_CLOEXEC) && defined(O_CLOEXEC) && defined(__NR_inotify_init1) | - | ||||||
150 | # define IN_CLOEXEC O_CLOEXEC | - | ||||||
151 | #endif | - | ||||||
152 | - | |||||||
153 | QT_BEGIN_NAMESPACE | - | ||||||
154 | - | |||||||
155 | #ifdef QT_LINUXBASE | - | ||||||
156 | // ### the LSB doesn't standardize syscall, need to wait until glib2.4 is standardized | - | ||||||
157 | static inline int syscall(...) { return -1; } | - | ||||||
158 | #endif | - | ||||||
159 | - | |||||||
160 | static inline int inotify_init() | - | ||||||
161 | { | - | ||||||
162 | #ifdef __NR_inotify_init | - | ||||||
163 | return syscall(__NR_inotify_init); | - | ||||||
164 | #else | - | ||||||
165 | return syscall(__NR_inotify_init1, 0); | - | ||||||
166 | #endif | - | ||||||
167 | } | - | ||||||
168 | - | |||||||
169 | static inline int inotify_add_watch(int fd, const char *name, __u32 mask) | - | ||||||
170 | { | - | ||||||
171 | return syscall(__NR_inotify_add_watch, fd, name, mask); | - | ||||||
172 | } | - | ||||||
173 | - | |||||||
174 | static inline int inotify_rm_watch(int fd, __u32 wd) | - | ||||||
175 | { | - | ||||||
176 | return syscall(__NR_inotify_rm_watch, fd, wd); | - | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | #ifdef IN_CLOEXEC | - | ||||||
180 | static inline int inotify_init1(int flags) | - | ||||||
181 | { | - | ||||||
182 | return syscall(__NR_inotify_init1, flags); | - | ||||||
183 | } | - | ||||||
184 | #endif | - | ||||||
185 | - | |||||||
186 | // the following struct and values are documented in linux/inotify.h | - | ||||||
187 | extern "C" { | - | ||||||
188 | - | |||||||
189 | struct inotify_event { | - | ||||||
190 | __s32 wd; | - | ||||||
191 | __u32 mask; | - | ||||||
192 | __u32 cookie; | - | ||||||
193 | __u32 len; | - | ||||||
194 | char name[0]; | - | ||||||
195 | }; | - | ||||||
196 | - | |||||||
197 | #define IN_ACCESS 0x00000001 | - | ||||||
198 | #define IN_MODIFY 0x00000002 | - | ||||||
199 | #define IN_ATTRIB 0x00000004 | - | ||||||
200 | #define IN_CLOSE_WRITE 0x00000008 | - | ||||||
201 | #define IN_CLOSE_NOWRITE 0x00000010 | - | ||||||
202 | #define IN_OPEN 0x00000020 | - | ||||||
203 | #define IN_MOVED_FROM 0x00000040 | - | ||||||
204 | #define IN_MOVED_TO 0x00000080 | - | ||||||
205 | #define IN_CREATE 0x00000100 | - | ||||||
206 | #define IN_DELETE 0x00000200 | - | ||||||
207 | #define IN_DELETE_SELF 0x00000400 | - | ||||||
208 | #define IN_MOVE_SELF 0x00000800 | - | ||||||
209 | #define IN_UNMOUNT 0x00002000 | - | ||||||
210 | #define IN_Q_OVERFLOW 0x00004000 | - | ||||||
211 | #define IN_IGNORED 0x00008000 | - | ||||||
212 | - | |||||||
213 | #define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) | - | ||||||
214 | #define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) | - | ||||||
215 | } | - | ||||||
216 | - | |||||||
217 | QT_END_NAMESPACE | - | ||||||
218 | - | |||||||
219 | // --------- inotify.h end ---------- | - | ||||||
220 | - | |||||||
221 | #else /* QT_NO_INOTIFY */ | - | ||||||
222 | - | |||||||
223 | #include <sys/inotify.h> | - | ||||||
224 | - | |||||||
225 | #endif | - | ||||||
226 | - | |||||||
227 | QT_BEGIN_NAMESPACE | - | ||||||
228 | - | |||||||
229 | QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent) | - | ||||||
230 | { | - | ||||||
231 | int fd = -1; | - | ||||||
232 | #ifdef IN_CLOEXEC | - | ||||||
233 | fd = inotify_init1(IN_CLOEXEC); | - | ||||||
234 | #endif | - | ||||||
235 | if (fd == -1) { | - | ||||||
236 | fd = inotify_init(); | - | ||||||
237 | if (fd == -1) | - | ||||||
238 | return 0; | - | ||||||
239 | } | - | ||||||
240 | return new QInotifyFileSystemWatcherEngine(fd, parent); | - | ||||||
241 | } | - | ||||||
242 | - | |||||||
243 | QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd, QObject *parent) | - | ||||||
244 | : QFileSystemWatcherEngine(parent), | - | ||||||
245 | inotifyFd(fd), | - | ||||||
246 | notifier(fd, QSocketNotifier::Read, this) | - | ||||||
247 | { | - | ||||||
248 | fcntl(inotifyFd, F_SETFD, FD_CLOEXEC); | - | ||||||
249 | connect(¬ifier, SIGNAL(activated(int)), SLOT(readFromInotify())); | - | ||||||
250 | } | - | ||||||
251 | - | |||||||
252 | QInotifyFileSystemWatcherEngine::~QInotifyFileSystemWatcherEngine() | - | ||||||
253 | { | - | ||||||
254 | notifier.setEnabled(false); | - | ||||||
255 | foreachfor (int id ,: qAsConst(pathToID))) | - | ||||||
256 | inotify_rm_watch(inotifyFd, id < 0 ? -id : id); executed 361 times by 9 tests: inotify_rm_watch(inotifyFd, id < 0 ? -id : id); Executed by:
| 361 | ||||||
257 | - | |||||||
258 | ::close(inotifyFd); | - | ||||||
259 | } executed 354 times by 11 tests: end of block Executed by:
| 354 | ||||||
260 | - | |||||||
261 | QStringList QInotifyFileSystemWatcherEngine::addPaths(const QStringList &paths, | - | ||||||
262 | QStringList *files, | - | ||||||
263 | QStringList *directories) | - | ||||||
264 | { | - | ||||||
265 | QStringList p = paths; | - | ||||||
266 | QMutableListIterator<QString> it(p); | - | ||||||
267 | while (it.hasNext()) {
| 476-478 | ||||||
268 | QString path = it.next(); | - | ||||||
269 | QFileInfo fi(path); | - | ||||||
270 | bool isDir = fi.isDir(); | - | ||||||
271 | if (isDir) {
| 36-442 | ||||||
272 | if (directories->contains(path))
| 1-441 | ||||||
273 | continue; executed 1 time by 1 test: continue; Executed by:
| 1 | ||||||
274 | } else { executed 441 times by 8 tests: end of block Executed by:
| 441 | ||||||
275 | if (files->contains(path))
| 0-36 | ||||||
276 | continue; never executed: continue; | 0 | ||||||
277 | } executed 36 times by 1 test: end of block Executed by:
| 36 | ||||||
278 | - | |||||||
279 | int wd = inotify_add_watch(inotifyFd, | - | ||||||
280 | QFile::encodeName(path), | - | ||||||
281 | (isDir | - | ||||||
282 | ? (0 | - | ||||||
283 | | IN_ATTRIB | - | ||||||
284 | | IN_MOVE | - | ||||||
285 | | IN_CREATE | - | ||||||
286 | | IN_DELETE | - | ||||||
287 | | IN_DELETE_SELF | - | ||||||
288 | ) | - | ||||||
289 | : (0 | - | ||||||
290 | | IN_ATTRIB | - | ||||||
291 | | IN_MODIFY | - | ||||||
292 | | IN_MOVE | - | ||||||
293 | | IN_MOVE_SELF | - | ||||||
294 | | IN_DELETE_SELF | - | ||||||
295 | ))); | - | ||||||
296 | if (wd < 0) {
| 2-475 | ||||||
297 | perrorqWarning().nospace() << "inotify_add_watch("QInotifyFileSystemWatcherEngine::addPaths:" << path << inotify_add_watch") failed");failed: " << QSystemError(errno, QSystemError::NativeError).toString(); | - | ||||||
298 | continue; executed 2 times by 1 test: continue; Executed by:
| 2 | ||||||
299 | } | - | ||||||
300 | - | |||||||
301 | it.remove(); | - | ||||||
302 | - | |||||||
303 | int id = isDir ? -wd : wd;
| 34-441 | ||||||
304 | if (id < 0) {
| 34-441 | ||||||
305 | directories->append(path); | - | ||||||
306 | } else { executed 441 times by 8 tests: end of block Executed by:
| 441 | ||||||
307 | files->append(path); | - | ||||||
308 | } executed 34 times by 1 test: end of block Executed by:
| 34 | ||||||
309 | - | |||||||
310 | pathToID.insert(path, id); | - | ||||||
311 | idToPath.insert(id, path); | - | ||||||
312 | } executed 475 times by 8 tests: end of block Executed by:
| 475 | ||||||
313 | - | |||||||
314 | return p; executed 476 times by 8 tests: return p; Executed by:
| 476 | ||||||
315 | } | - | ||||||
316 | - | |||||||
317 | QStringList QInotifyFileSystemWatcherEngine::removePaths(const QStringList &paths, | - | ||||||
318 | QStringList *files, | - | ||||||
319 | QStringList *directories) | - | ||||||
320 | { | - | ||||||
321 | QStringList p = paths; | - | ||||||
322 | QMutableListIterator<QString> it(p); | - | ||||||
323 | while (it.hasNext()) { | - | ||||||
324 | QString path = it.next(); | - | ||||||
325 | int id = pathToID.take(path); | - | ||||||
326 | QString x = idToPath.take(id); | - | ||||||
327 | if (x.isEmpty() || x != path) | - | ||||||
328 | continue; | - | ||||||
329 | - | |||||||
330 | int wd = id < 0 ? -id : id; | - | ||||||
331 | // qDebug() << "removing watch for path" << path << "wd" << wd; | - | ||||||
332 | inotify_rm_watch(inotifyFd, wd); | - | ||||||
333 | - | |||||||
334 | it.remove(); | - | ||||||
335 | if (id < 0) { | - | ||||||
336 | directories->removeAll(path); | - | ||||||
337 | } else { | - | ||||||
338 | files->removeAll(path); | - | ||||||
339 | } | - | ||||||
340 | } | - | ||||||
341 | - | |||||||
342 | return p; | - | ||||||
343 | } | - | ||||||
344 | - | |||||||
345 | void QInotifyFileSystemWatcherEngine::readFromInotify() | - | ||||||
346 | { | - | ||||||
347 | // qDebug("QInotifyFileSystemWatcherEngine::readFromInotify"); | - | ||||||
348 | - | |||||||
349 | int buffSize = 0; | - | ||||||
350 | ioctl(inotifyFd, FIONREAD, (char *) &buffSize); | - | ||||||
351 | QVarLengthArray<char, 4096> buffer(buffSize); | - | ||||||
352 | buffSize = read(inotifyFd, buffer.data(), buffSize); | - | ||||||
353 | char *at = buffer.data(); | - | ||||||
354 | char * const end = at + buffSize; | - | ||||||
355 | - | |||||||
356 | QHash<int, inotify_event *> eventForId; | - | ||||||
357 | while (at < end) { | - | ||||||
358 | inotify_event *event = reinterpret_cast<inotify_event *>(at); | - | ||||||
359 | - | |||||||
360 | if (eventForId.contains(event->wd)) | - | ||||||
361 | eventForId[event->wd]->mask |= event->mask; | - | ||||||
362 | else | - | ||||||
363 | eventForId.insert(event->wd, event); | - | ||||||
364 | - | |||||||
365 | at += sizeof(inotify_event) + event->len; | - | ||||||
366 | } | - | ||||||
367 | - | |||||||
368 | QHash<int, inotify_event *>::const_iterator it = eventForId.constBegin(); | - | ||||||
369 | while (it != eventForId.constEnd()) { | - | ||||||
370 | const inotify_event &event = **it; | - | ||||||
371 | ++it; | - | ||||||
372 | - | |||||||
373 | // qDebug() << "inotify event, wd" << event.wd << "mask" << hex << event.mask; | - | ||||||
374 | - | |||||||
375 | int id = event.wd; | - | ||||||
376 | QString path = getPathFromID(id); | - | ||||||
377 | if (path.isEmpty()) { | - | ||||||
378 | // perhaps a directory? | - | ||||||
379 | id = -id; | - | ||||||
380 | path = getPathFromID(id); | - | ||||||
381 | if (path.isEmpty()) | - | ||||||
382 | continue; | - | ||||||
383 | } | - | ||||||
384 | - | |||||||
385 | // qDebug() << "event for path" << path; | - | ||||||
386 | - | |||||||
387 | if ((event.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT)) != 0) { | - | ||||||
388 | pathToID.remove(path); | - | ||||||
389 | idToPath.remove(id, getPathFromID(id)); | - | ||||||
390 | if (!idToPath.contains(id)) | - | ||||||
391 | inotify_rm_watch(inotifyFd, event.wd); | - | ||||||
392 | - | |||||||
393 | if (id < 0) | - | ||||||
394 | emit directoryChanged(path, true); | - | ||||||
395 | else | - | ||||||
396 | emit fileChanged(path, true); | - | ||||||
397 | } else { | - | ||||||
398 | if (id < 0) | - | ||||||
399 | emit directoryChanged(path, false); | - | ||||||
400 | else | - | ||||||
401 | emit fileChanged(path, false); | - | ||||||
402 | } | - | ||||||
403 | } | - | ||||||
404 | } | - | ||||||
405 | - | |||||||
406 | QString QInotifyFileSystemWatcherEngine::getPathFromID(int id) const | - | ||||||
407 | { | - | ||||||
408 | QHash<int, QString>::const_iterator i = idToPath.find(id); | - | ||||||
409 | while (i != idToPath.constEnd() && i.key() == id) { | - | ||||||
410 | if ((i + 1) == idToPath.constEnd() || (i + 1).key() != id) { | - | ||||||
411 | return i.value(); | - | ||||||
412 | } | - | ||||||
413 | ++i; | - | ||||||
414 | } | - | ||||||
415 | return QString(); | - | ||||||
416 | } | - | ||||||
417 | - | |||||||
418 | QT_END_NAMESPACE | - | ||||||
419 | - | |||||||
420 | #endif // QT_NO_FILESYSTEMWATCHER | - | ||||||
Source code | Switch to Preprocessed file |