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#include "private/qcore_unix_p.h"
44#include "private/qsystemerror_p.h"
45
46#include <qdebug.h>
47#include <qfile.h>
48#include <qfileinfo.h>
49#include <qscopeguard.h>
50#include <qsocketnotifier.h>
51#include <qvarlengtharray.h>
52
53#if defined(Q_OS_LINUX)
54#include <sys/syscall.h>
55#include <sys/ioctl.h>
56#include <unistd.h>
57#include <fcntl.h>
58#endif
59
60#if defined(QT_NO_INOTIFY)
61
62#if defined(Q_OS_QNX)
63// These files should only be compiled on QNX if the inotify headers are found
64#error "Should not get here."
65#endif
66
67#include <linux/types.h>
68
69#if defined(__i386__)
70# define __NR_inotify_init 291
71# define __NR_inotify_add_watch 292
72# define __NR_inotify_rm_watch 293
73# define __NR_inotify_init1 332
74#elif defined(__x86_64__)
75# define __NR_inotify_init 253
76# define __NR_inotify_add_watch 254
77# define __NR_inotify_rm_watch 255
78# define __NR_inotify_init1 294
79#elif defined(__powerpc__) || defined(__powerpc64__)
80# define __NR_inotify_init 275
81# define __NR_inotify_add_watch 276
82# define __NR_inotify_rm_watch 277
83# define __NR_inotify_init1 318
84#elif defined (__ia64__)
85# define __NR_inotify_init 1277
86# define __NR_inotify_add_watch 1278
87# define __NR_inotify_rm_watch 1279
88# define __NR_inotify_init1 1318
89#elif defined (__s390__) || defined (__s390x__)
90# define __NR_inotify_init 284
91# define __NR_inotify_add_watch 285
92# define __NR_inotify_rm_watch 286
93# define __NR_inotify_init1 324
94#elif defined (__alpha__)
95# define __NR_inotify_init 444
96# define __NR_inotify_add_watch 445
97# define __NR_inotify_rm_watch 446
98// no inotify_init1 for the Alpha
99#elif defined (__sparc__) || defined (__sparc64__)
100# define __NR_inotify_init 151
101# define __NR_inotify_add_watch 152
102# define __NR_inotify_rm_watch 156
103# define __NR_inotify_init1 322
104#elif defined (__arm__)
105# define __NR_inotify_init 316
106# define __NR_inotify_add_watch 317
107# define __NR_inotify_rm_watch 318
108# define __NR_inotify_init1 360
109#elif defined (__sh__)
110# define __NR_inotify_init 290
111# define __NR_inotify_add_watch 291
112# define __NR_inotify_rm_watch 292
113# define __NR_inotify_init1 332
114#elif defined (__sh64__)
115# define __NR_inotify_init 318
116# define __NR_inotify_add_watch 319
117# define __NR_inotify_rm_watch 320
118# define __NR_inotify_init1 360
119#elif defined (__mips__)
120# define __NR_inotify_init 284
121# define __NR_inotify_add_watch 285
122# define __NR_inotify_rm_watch 286
123# define __NR_inotify_init1 329
124#elif defined (__hppa__)
125# define __NR_inotify_init 269
126# define __NR_inotify_add_watch 270
127# define __NR_inotify_rm_watch 271
128# define __NR_inotify_init1 314
129#elif defined (__avr32__)
130# define __NR_inotify_init 240
131# define __NR_inotify_add_watch 241
132# define __NR_inotify_rm_watch 242
133// no inotify_init1 for AVR32
134#elif defined (__mc68000__)
135# define __NR_inotify_init 284
136# define __NR_inotify_add_watch 285
137# define __NR_inotify_rm_watch 286
138# define __NR_inotify_init1 328
139#elif defined (__aarch64__)
140# define __NR_inotify_init1 26
141# define __NR_inotify_add_watch 27
142# define __NR_inotify_rm_watch 28
143// no inotify_init for aarch64
144#else
145# error "This architecture is not supported. Please see http://www.qt-project.org/"
146#endif
147
148#if !defined(IN_CLOEXEC) && defined(O_CLOEXEC) && defined(__NR_inotify_init1)
149# define IN_CLOEXEC O_CLOEXEC
150#endif
151
152QT_BEGIN_NAMESPACE
153
154#ifdef QT_LINUXBASE
155// ### the LSB doesn't standardize syscall, need to wait until glib2.4 is standardized
156static inline int syscall(...) { return -1; }
157#endif
158
159static inline int inotify_init()
160{
161#ifdef __NR_inotify_init
162 return syscall(__NR_inotify_init);
163#else
164 return syscall(__NR_inotify_init1, 0);
165#endif
166}
167
168static inline int inotify_add_watch(int fd, const char *name, __u32 mask)
169{
170 return syscall(__NR_inotify_add_watch, fd, name, mask);
171}
172
173static inline int inotify_rm_watch(int fd, __u32 wd)
174{
175 return syscall(__NR_inotify_rm_watch, fd, wd);
176}
177
178#ifdef IN_CLOEXEC
179static inline int inotify_init1(int flags)
180{
181 return syscall(__NR_inotify_init1, flags);
182}
183#endif
184
185// the following struct and values are documented in linux/inotify.h
186extern "C" {
187
188struct inotify_event {
189 __s32 wd;
190 __u32 mask;
191 __u32 cookie;
192 __u32 len;
193 char name[0];
194};
195
196#define IN_ACCESS 0x00000001
197#define IN_MODIFY 0x00000002
198#define IN_ATTRIB 0x00000004
199#define IN_CLOSE_WRITE 0x00000008
200#define IN_CLOSE_NOWRITE 0x00000010
201#define IN_OPEN 0x00000020
202#define IN_MOVED_FROM 0x00000040
203#define IN_MOVED_TO 0x00000080
204#define IN_CREATE 0x00000100
205#define IN_DELETE 0x00000200
206#define IN_DELETE_SELF 0x00000400
207#define IN_MOVE_SELF 0x00000800
208#define IN_UNMOUNT 0x00002000
209#define IN_Q_OVERFLOW 0x00004000
210#define IN_IGNORED 0x00008000
211
212#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
213#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO)
214}
215
216QT_END_NAMESPACE
217
218// --------- inotify.h end ----------
219
220#else /* QT_NO_INOTIFY */
221
222#include <sys/inotify.h>
223
224// see https://github.com/android-ndk/ndk/issues/394
225# if defined(Q_OS_ANDROID) && (__ANDROID_API__ < 21)
226static inline int inotify_init1(int flags)
227{
228 return syscall(__NR_inotify_init1, flags);
229}
230# endif
231
232#endif
233
234QT_BEGIN_NAMESPACE
235
236QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent)
237{
238 int fd = -1;
239#if defined(IN_CLOEXEC)
240 fd = inotify_init1(IN_CLOEXEC);
241#endif
242 if (fd == -1) {
243 fd = inotify_init();
244 if (fd == -1)
245 return 0;
246 }
247 return new QInotifyFileSystemWatcherEngine(fd, parent);
248}
249
250QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd, QObject *parent)
251 : QFileSystemWatcherEngine(parent),
252 inotifyFd(fd),
253 notifier(fd, QSocketNotifier::Read, this)
254{
255 fcntl(inotifyFd, F_SETFD, FD_CLOEXEC);
256 connect(&notifier, SIGNAL(activated(int)), SLOT(readFromInotify()));
257}
258
259QInotifyFileSystemWatcherEngine::~QInotifyFileSystemWatcherEngine()
260{
261 notifier.setEnabled(false);
262 for (int id : qAsConst(pathToID))
263 inotify_rm_watch(inotifyFd, id < 0 ? -id : id);
264
265 ::close(inotifyFd);
266}
267
268QStringList QInotifyFileSystemWatcherEngine::addPaths(const QStringList &paths,
269 QStringList *files,
270 QStringList *directories)
271{
272 QStringList unhandled;
273 for (const QString &path : paths) {
274 QFileInfo fi(path);
275 bool isDir = fi.isDir();
276 auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
277 if (isDir) {
278 if (directories->contains(path))
279 continue;
280 } else {
281 if (files->contains(path))
282 continue;
283 }
284
285 int wd = inotify_add_watch(inotifyFd,
286 QFile::encodeName(path),
287 (isDir
288 ? (0
289 | IN_ATTRIB
290 | IN_MOVE
291 | IN_CREATE
292 | IN_DELETE
293 | IN_DELETE_SELF
294 )
295 : (0
296 | IN_ATTRIB
297 | IN_MODIFY
298 | IN_MOVE
299 | IN_MOVE_SELF
300 | IN_DELETE_SELF
301 )));
302 if (wd < 0) {
303 if (errno != ENOENT)
304 qErrnoWarning("inotify_add_watch(%ls) failed:", path.constData());
305 continue;
306 }
307
308 sg.dismiss();
309
310 int id = isDir ? -wd : wd;
311 if (id < 0) {
312 directories->append(path);
313 } else {
314 files->append(path);
315 }
316
317 pathToID.insert(path, id);
318 idToPath.insert(id, path);
319 }
320
321 return unhandled;
322}
323
324QStringList QInotifyFileSystemWatcherEngine::removePaths(const QStringList &paths,
325 QStringList *files,
326 QStringList *directories)
327{
328 QStringList unhandled;
329 for (const QString &path : paths) {
330 int id = pathToID.take(path);
331
332 auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
333
334 // Multiple paths could be associated to the same watch descriptor
335 // when a file is moved and added with the new name.
336 // So we should find and delete the correct one by using
337 // both id and path
338 auto path_range = idToPath.equal_range(id);
339 auto path_it = std::find(path_range.first, path_range.second, path);
340 if (path_it == idToPath.end())
341 continue;
342
343 const ssize_t num_elements = std::distance(path_range.first, path_range.second);
344 idToPath.erase(path_it);
345
346 // If there was only one path associated to the given id we should remove the watch
347 if (num_elements == 1) {
348 int wd = id < 0 ? -id : id;
349 inotify_rm_watch(inotifyFd, wd);
350 }
351
352 sg.dismiss();
353
354 if (id < 0) {
355 directories->removeAll(path);
356 } else {
357 files->removeAll(path);
358 }
359 }
360
361 return unhandled;
362}
363
364void QInotifyFileSystemWatcherEngine::readFromInotify()
365{
366 // qDebug("QInotifyFileSystemWatcherEngine::readFromInotify");
367
368 int buffSize = 0;
369 ioctl(inotifyFd, FIONREAD, (char *) &buffSize);
370 QVarLengthArray<char, 4096> buffer(buffSize);
371 buffSize = read(inotifyFd, buffer.data(), buffSize);
372 char *at = buffer.data();
373 char * const end = at + buffSize;
374
375 QHash<int, inotify_event *> eventForId;
376 while (at < end) {
377 inotify_event *event = reinterpret_cast<inotify_event *>(at);
378
379 if (eventForId.contains(event->wd))
380 eventForId[event->wd]->mask |= event->mask;
381 else
382 eventForId.insert(event->wd, event);
383
384 at += sizeof(inotify_event) + event->len;
385 }
386
387 QHash<int, inotify_event *>::const_iterator it = eventForId.constBegin();
388 while (it != eventForId.constEnd()) {
389 const inotify_event &event = **it;
390 ++it;
391
392 // qDebug() << "inotify event, wd" << event.wd << "mask" << Qt::hex << event.mask;
393
394 int id = event.wd;
395 QString path = getPathFromID(id);
396 if (path.isEmpty()) {
397 // perhaps a directory?
398 id = -id;
399 path = getPathFromID(id);
400 if (path.isEmpty())
401 continue;
402 }
403
404 // qDebug() << "event for path" << path;
405
406 if ((event.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT)) != 0) {
407 pathToID.remove(path);
408 idToPath.remove(id, getPathFromID(id));
409 if (!idToPath.contains(id))
410 inotify_rm_watch(inotifyFd, event.wd);
411
412 if (id < 0)
413 emit directoryChanged(path, true);
414 else
415 emit fileChanged(path, true);
416 } else {
417 if (id < 0)
418 emit directoryChanged(path, false);
419 else
420 emit fileChanged(path, false);
421 }
422 }
423}
424
425template <typename Hash, typename Key>
426typename Hash::const_iterator
427find_last_in_equal_range(const Hash &c, const Key &key)
428{
429 // find c.equal_range(key).second - 1 without backwards iteration:
430 auto i = c.find(key);
431 const auto end = c.cend();
432 if (i == end)
433 return end;
434 decltype(i) prev;
435 do {
436 prev = i;
437 ++i;
438 } while (i != end && i.key() == key);
439 return prev;
440}
441
442QString QInotifyFileSystemWatcherEngine::getPathFromID(int id) const
443{
444 auto i = find_last_in_equal_range(idToPath, id);
445 return i == idToPath.cend() ? QString() : i.value() ;
446}
447
448QT_END_NAMESPACE
449
450#include "moc_qfilesystemwatcher_inotify_p.cpp"
451