Debian Codegraph @

src/filesystem/dfilesystemwatcher_linux.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dfilesystemwatcher.h"
6 #include "private/dfilesystemwatcher_linux_p.h"
7
8 #include <QFileInfo>
9 #include <QDir>
10 #include <QSocketNotifier>
11 #include <QDebug>
12 #include <QMultiMap>
13
14 #include <sys/inotify.h>
15 #include <sys/fcntl.h>
16 #include <sys/ioctl.h>
17 #include <unistd.h>
18
19 DCORE_BEGIN_NAMESPACE
20
21 DFileSystemWatcherPrivate::DFileSystemWatcherPrivate(int fd, DFileSystemWatcher *qq)
22 : DObjectPrivate(qq)
23 , inotifyFd(fd)
24 , notifier(fd, QSocketNotifier::Read, qq)
25 {
26 fcntl(inotifyFd, F_SETFD, FD_CLOEXEC);
27 qq->connect(&notifier, SIGNAL(activated(int)), qq, SLOT(_q_readFromInotify()));
28 }
29
30 DFileSystemWatcherPrivate::~DFileSystemWatcherPrivate()
31 {
32 notifier.setEnabled(false);
33 Q_FOREACH (int id, pathToID)
34 inotify_rm_watch(inotifyFd, id < 0 ? -id : id);
35
36 ::close(inotifyFd);
37 }
38
39 QStringList DFileSystemWatcherPrivate::addPaths(const QStringList &paths, QStringList *files, QStringList *directories)
40 {
41 QStringList p = paths;
42 QMutableListIterator<QString> it(p);
43 while (it.hasNext()) {
44 QString path = it.next();
45 QFileInfo fi(path);
46 bool isDir = fi.isDir();
47 if (isDir) {
48 if (directories->contains(path))
49 continue;
50 } else {
51 if (files->contains(path))
52 continue;
53 }
54
55 int wd = inotify_add_watch(inotifyFd,
56 QFile::encodeName(path),
57 (isDir
58 ? (0
59 | IN_ATTRIB
60 | IN_MOVE
61 | IN_MOVE_SELF
62 | IN_CREATE
63 | IN_DELETE
64 | IN_DELETE_SELF
65 | IN_MODIFY
66 )
67 : (0
68 | IN_ATTRIB
69 | IN_CLOSE_WRITE
70 | IN_MODIFY
71 | IN_MOVE
72 | IN_MOVE_SELF
73 | IN_DELETE_SELF
74 )));
75 if (wd < 0) {
76 perror("DFileSystemWatcherPrivate::addPaths: inotify_add_watch failed");
77 continue;
78 }
79
80 it.remove();
81
82 int id = isDir ? -wd : wd;
83 if (id < 0) {
84 directories->append(path);
85 } else {
86 files->append(path);
87 }
88
89 pathToID.insert(path, id);
90 idToPath.insert(id, path);
91 }
92
93 return p;
94 }
95
96 QStringList DFileSystemWatcherPrivate::removePaths(const QStringList &paths, QStringList *files, QStringList *directories)
97 {
98 QStringList p = paths;
99 QMutableListIterator<QString> it(p);
100 while (it.hasNext()) {
101 QString path = it.next();
102 int id = pathToID.take(path);
103 for (auto hit = idToPath.find(id); hit != idToPath.end() && hit.key() == id; ++hit) {
104 if (hit.value() == path) {
105 idToPath.erase(hit);
106 break;
107 }
108 }
109
110 it.remove();
111
112 if (!idToPath.contains(id)) {
113 int wd = id < 0 ? -id : id;
114 //qDebug() << "removing watch for path" << path << "wd" << wd;
115 inotify_rm_watch(inotifyFd, wd);
116 }
117
118 if (id < 0) {
119 directories->removeAll(path);
120 } else {
121 files->removeAll(path);
122 }
123 }
124
125 return p;
126 }
127
128 void DFileSystemWatcherPrivate::_q_readFromInotify()
129 {
130 Q_Q(DFileSystemWatcher);
131 // qDebug() << "QInotifyFileSystemWatcherEngine::readFromInotify";
132
133 int buffSize = 0;
134 ioctl(inotifyFd, FIONREAD, (char *) &buffSize);
135 QVarLengthArray<char, 4096> buffer(buffSize);
136 buffSize = read(inotifyFd, buffer.data(), buffSize);
137 char *at = buffer.data();
138 char * const end = at + buffSize;
139
140 QList<inotify_event *> eventList;
141 QMultiHash<int, QString> batch_pathmap;
142 /// only save event: IN_MOVE_TO
143 QMultiMap<int, QString> cookieToFilePath;
144 QMultiMap<int, QString> cookieToFileName;
145 QSet<int> hasMoveFromByCookie;
146 #ifdef QT_DEBUG
147 int exist_count = 0;
148 #endif
149 while (at < end) {
150 inotify_event *event = reinterpret_cast<inotify_event *>(at);
151 QStringList paths;
152
153 at += sizeof(inotify_event) + event->len;
154
155 int id = event->wd;
156 paths = idToPath.values(id);
157 if (paths.empty()) {
158 // perhaps a directory?
159 id = -id;
160 paths = idToPath.values(id);
161 if (paths.empty())
162 continue;
163 }
164
165 if (!(event->mask & IN_MOVED_TO) || !hasMoveFromByCookie.contains(event->cookie)) {
166 auto it = std::find_if(eventList.begin(), eventList.end(), [event](inotify_event *e){
167 return event->wd == e->wd && event->mask == e->mask &&
168 event->cookie == e->cookie &&
169 event->len == e->len &&
170 !strcmp(event->name, e->name);
171 });
172
173 if (it==eventList.end()) {
174 eventList.append(event);
175 }
176 #ifdef QT_DEBUG
177 else {
178 qDebug() << "exist event:" << "event->wd" << event->wd <<
179 "event->mask" << event->mask <<
180 "event->cookie" << event->cookie << "exist counts " << ++exist_count;
181 }
182 #endif
183 const QList<QString> bps = batch_pathmap.values(id);
184 for (auto &path : paths) {
185 if (!bps.contains(path)) {
186 batch_pathmap.insert(id, path);
187 }
188 }
189 }
190
191 if (event->mask & IN_MOVED_TO) {
192 for (auto &path : paths) {
193 cookieToFilePath.insert(event->cookie, path);
194 }
195 cookieToFileName.insert(event->cookie, QString::fromUtf8(event->name));
196 }
197
198 if (event->mask & IN_MOVED_FROM)
199 hasMoveFromByCookie << event->cookie;
200 }
201
202 // qDebug() << "event count:" << eventList.count();
203
204 QList<inotify_event *>::const_iterator it = eventList.constBegin();
205 while (it != eventList.constEnd()) {
206 const inotify_event &event = **it;
207 ++it;
208
209 // qDebug() << "inotify event, wd" << event.wd << "cookie" << event.cookie << "mask" << hex << event.mask;
210
211 int id = event.wd;
212 QStringList paths = batch_pathmap.values(id);
213
214 if (paths.empty()) {
215 id = -id;
216 paths = batch_pathmap.values(id);
217
218 if (paths.empty())
219 continue;
220 }
221 const QString &name = QString::fromUtf8(event.name);
222
223 for (auto &path : paths) {
224 // qDebug() << "event for path" << path;
225
226 // /// TODO: Existence of invalid utf8 characters QFile can not read the file information
227 // if (event.name != QString::fromLocal8Bit(event.name).toLocal8Bit()) {
228 // if (event.mask & (IN_CREATE | IN_MOVED_TO)) {
229 // DFMGlobal::fileNameCorrection(path);
230 // }
231 // }
232
233 if ((event.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT)) != 0) {
234 do {
235 if (event.mask & IN_MOVE_SELF) {
236 QMultiMap<int, QString>::const_iterator iterator = cookieToFilePath.constBegin();
237
238 bool isMove = false;
239
240 while (iterator != cookieToFilePath.constEnd()) {
241 const QString &_path = iterator.value();
242 const QString &_name = cookieToFileName.value(iterator.key());
243
244 if (QFileInfo(_path + QDir::separator() + _name) == QFileInfo(path)) {
245 isMove = true;
246 break;
247 }
248
249 ++iterator;
250 }
251
252 if (isMove)
253 break;
254 }
255
256 /// Keep watcher
257 // pathToID.remove(path);
258 // idToPath.remove(id, getPathFromID(id));
259 // if (!idToPath.contains(id))
260 // inotify_rm_watch(inotifyFd, event.wd);
261
262 // if (id < 0)
263 // onDirectoryChanged(path, true);
264 // else
265 // onFileChanged(path, true);
266
267 Q_EMIT q->fileDeleted(path, QString(), DFileSystemWatcher::QPrivateSignal());
268 } while (false);
269 } else {
270 if (id < 0)
271 onDirectoryChanged(path, false);
272 else
273 onFileChanged(path, false);
274 }
275
276 QString filePath = path;
277
278 if (id < 0) {
279 if (path.endsWith(QDir::separator()))
280 filePath = path + name;
281 else
282 filePath = path + QDir::separator() + name;
283 }
284
285 if (event.mask & IN_CREATE) {
286 // qDebug() << "IN_CREATE" << filePath << name;
287
288 if (name.isEmpty()) {
289 if (pathToID.contains(path)) {
290 q->removePath(path);
291 q->addPath(path);
292 }
293 } else if (pathToID.contains(filePath)) {
294 q->removePath(filePath);
295 q->addPath(filePath);
296 }
297
298 Q_EMIT q->fileCreated(path, name, DFileSystemWatcher::QPrivateSignal());
299 }
300
301 if (event.mask & IN_DELETE) {
302 // qDebug() << "IN_DELETE" << filePath;
303
304 Q_EMIT q->fileDeleted(path, name, DFileSystemWatcher::QPrivateSignal());
305 }
306
307 if (event.mask & IN_MOVED_FROM) {
308 const QString toName = cookieToFileName.value(event.cookie);
309
310 if (cookieToFilePath.values(event.cookie).empty()) {
311 Q_EMIT q->fileMoved(path, name, QString(), QString(), DFileSystemWatcher::QPrivateSignal());
312 } else {
313 for (QString &toPath : cookieToFilePath.values(event.cookie)) {
314 // qDebug() << "IN_MOVED_FROM" << filePath << "to path:" << toPath << "to name:" << toName;
315
316 Q_EMIT q->fileMoved(path, name, toPath, toName, DFileSystemWatcher::QPrivateSignal());
317 }
318 }
319 }
320
321 if (event.mask & IN_MOVED_TO) {
322 // qDebug() << "IN_MOVED_TO" << filePath;
323
324 if (!hasMoveFromByCookie.contains(event.cookie))
325 Q_EMIT q->fileMoved(QString(), QString(), path, name, DFileSystemWatcher::QPrivateSignal());
326 }
327
328 if (event.mask & IN_ATTRIB) {
329 // qDebug() << "IN_ATTRIB" << event.mask << filePath;
330
331 Q_EMIT q->fileAttributeChanged(path, name, DFileSystemWatcher::QPrivateSignal());
332 }
333
334 /*only monitor file close event which is opend by write mode*/
335 if (event.mask & IN_CLOSE_WRITE) {
336 // qDebug() << "IN_CLOSE_WRITE" << event.mask << filePath;
337
338 Q_EMIT q->fileClosed(path, id < 0 ? name : QString(), DFileSystemWatcher::QPrivateSignal());
339 }
340
341 if (event.mask & IN_MODIFY) {
342 // qDebug() << "IN_MODIFY" << event.mask << filePath << name;
343
344 Q_EMIT q->fileModified(path, name, DFileSystemWatcher::QPrivateSignal());
345 }
346 }
347 }
348 }
349
350 void DFileSystemWatcherPrivate::onFileChanged(const QString &path, bool removed)
351 {
352 Q_Q(DFileSystemWatcher);
353 if (!files.contains(path)) {
354 // the path was removed after a change was detected, but before we delivered the signal
355 return;
356 }
357 if (removed) {
358 files.removeAll(path);
359 }
360 // Q_EMIT q->fileChanged(path, DFileSystemWatcher::QPrivateSignal());
361 }
362
363 void DFileSystemWatcherPrivate::onDirectoryChanged(const QString &path, bool removed)
364 {
365 Q_Q(DFileSystemWatcher);
366 if (!directories.contains(path)) {
367 // perhaps the path was removed after a change was detected, but before we delivered the signal
368 return;
369 }
370 if (removed) {
371 directories.removeAll(path);
372 }
373 // Q_EMIT q->directoryChanged(path, DFileSystemWatcher::QPrivateSignal());
374 }
375
376 /*!
377 \class Dtk::Core::DFileSystemWatcher
378 \inmodule dtkcore
379 \brief The DFileSystemWatcher class provides an interface for monitoring files and directories for modifications.
380
381 DFileSystemWatcher monitors the file system for changes to files
382 and directories by watching a list of specified paths.
383
384 Call addPath() to watch a particular file or directory. Multiple
385 paths can be added using the addPaths() function. Existing paths can
386 be removed by using the removePath() and removePaths() functions.
387
388 DFileSystemWatcher examines each path added to it. Files that have
389 been added to the DFileSystemWatcher can be accessed using the
390 files() function, and directories using the directories() function.
391
392 \note On systems running a Linux kernel without inotify support,
393 file systems that contain watched paths cannot be unmounted.
394
395 \note Windows CE does not support directory monitoring by
396 default as this depends on the file system driver installed.
397
398 \note The act of monitoring files and directories for
399 modifications consumes system resources. This implies there is a
400 limit to the number of files and directories your process can
401 monitor simultaneously. On all BSD variants, for
402 example, an open file descriptor is required for each monitored
403 file. Some system limits the number of open file descriptors to 256
404 by default. This means that addPath() and addPaths() will fail if
405 your process tries to add more than 256 files or directories to
406 the file system monitor. Also note that your process may have
407 other file descriptors open in addition to the ones for files
408 being monitored, and these other open descriptors also count in
409 the total. OS X uses a different backend and does not
410 suffer from this issue.
411 */
412
413
414 /*!
415 Constructs a new file system watcher object with the given \a parent.
416 */
417 DFileSystemWatcher::DFileSystemWatcher(QObject *parent)
418 : QObject(parent)
419 , DObject()
420 {
421 int fd = -1;
422 #ifdef IN_CLOEXEC
423 fd = inotify_init1(IN_CLOEXEC | O_NONBLOCK);
424 #endif
425 if (fd == -1) {
426 fd = inotify_init1(O_NONBLOCK);
427 }
428
429 if (fd != -1) {
430 d_d_ptr.reset(new DFileSystemWatcherPrivate(fd, this));
431 } else {
432 qCritical() << "inotify_init1 failed, and the DFileSystemWatcher is invalid." << strerror(errno);
433 }
434 }
435
436 /*!
437 Constructs a new file system watcher object with the given \a parent
438 which monitors the specified \a paths list.
439 */
440 DFileSystemWatcher::DFileSystemWatcher(const QStringList &paths, QObject *parent)
441 : DFileSystemWatcher(parent)
442 {
443 addPaths(paths);
444 }
445
446 /*!
447 Destroys the file system watcher.
448 */
449 DFileSystemWatcher::~DFileSystemWatcher()
450 { }
451
452 /*!
453 Adds \a path to the file system watcher if \a path exists. The
454 path is not added if it does not exist, or if it is already being
455 monitored by the file system watcher.
456
457 If \a path specifies a directory, the directoryChanged() signal
458 will be emitted when \a path is modified or removed from disk;
459 otherwise the fileChanged() signal is emitted when \a path is
460 modified, renamed or removed.
461
462 If the watch was successful, true is returned.
463
464 Reasons for a watch failure are generally system-dependent, but
465 may include the resource not existing, access failures, or the
466 total watch count limit, if the platform has one.
467
468 \note There may be a system dependent limit to the number of
469 files and directories that can be monitored simultaneously.
470 If this limit is been reached, \a path will not be monitored,
471 and false is returned.
472
473 \sa addPaths(), removePath()
474 */
475 bool DFileSystemWatcher::addPath(const QString &path)
476 {
477 const QStringList &paths = addPaths(QStringList(path));
478 return paths.isEmpty();
479 }
480
481 /*!
482 Adds each path in \a paths to the file system watcher. Paths are
483 not added if they not exist, or if they are already being
484 monitored by the file system watcher.
485
486 If a path specifies a directory, the directoryChanged() signal
487 will be emitted when the path is modified or removed from disk;
488 otherwise the fileChanged() signal is emitted when the path is
489 modified, renamed, or removed.
490
491 The return value is a list of paths that could not be watched.
492
493 Reasons for a watch failure are generally system-dependent, but
494 may include the resource not existing, access failures, or the
495 total watch count limit, if the platform has one.
496
497 \note There may be a system dependent limit to the number of
498 files and directories that can be monitored simultaneously.
499 If this limit has been reached, the excess \a paths will not
500 be monitored, and they will be added to the returned QStringList.
501
502 \sa addPath(), removePaths()
503 */
504 QStringList DFileSystemWatcher::addPaths(const QStringList &paths)
505 {
506 Q_D(DFileSystemWatcher);
507
508 if (!d)
509 return paths;
510
511 QStringList p = paths;
512 QMutableListIterator<QString> it(p);
513
514 while (it.hasNext()) {
515 const QString &path = it.next();
516 if (path.isEmpty()) {
517 qWarning() << Q_FUNC_INFO << "the path is empty and it is not be watched";
518 it.remove();
519 }
520 }
521
522 if (p.isEmpty()) {
523 qWarning() << Q_FUNC_INFO << "all path are filtered and they are not be watched, paths are " << paths;
524 return paths;
525 }
526
527 p = d->addPaths(p, &d->files, &d->directories);
528
529 return p;
530 }
531
532 /*!
533 Removes the specified \a path from the file system watcher.
534
535 If the watch is successfully removed, true is returned.
536
537 Reasons for watch removal failing are generally system-dependent,
538 but may be due to the path having already been deleted, for example.
539
540 \sa removePaths(), addPath()
541 */
542 bool DFileSystemWatcher::removePath(const QString &path)
543 {
544 const QStringList &paths = removePaths(QStringList(path));
545 return paths.isEmpty();
546 }
547
548 /*!
549 Removes the specified \a paths from the file system watcher.
550
551 The return value is a list of paths which were not able to be
552 unwatched successfully.
553
554 Reasons for watch removal failing are generally system-dependent,
555 but may be due to the path having already been deleted, for example.
556
557 \sa removePath(), addPaths()
558 */
559 QStringList DFileSystemWatcher::removePaths(const QStringList &paths)
560 {
561 Q_D(DFileSystemWatcher);
562
563 if (!d)
564 return paths;
565
566 QStringList p = paths;
567 QMutableListIterator<QString> it(p);
568
569 while (it.hasNext()) {
570 const QString &path = it.next();
571 if (path.isEmpty()) {
572 qWarning() << Q_FUNC_INFO << "the path is empty and it is not be removed from watched list";
573 it.remove();
574 }
575 }
576
577 if (p.isEmpty()) {
578 qWarning() << Q_FUNC_INFO << "all path are filtered and they are not be watched, paths are " << paths;
579 return paths;
580 }
581
582 p = d->removePaths(p, &d->files, &d->directories);
583
584 return p;
585 }
586
587 /*!
588 \fn QStringList DFileSystemWatcher::directories() const
589
590 Returns a list of paths to directories that are being watched.
591
592 \sa files()
593 */
594
595 /*!
596 \fn QStringList DFileSystemWatcher::files() const
597
598 Returns a list of paths to files that are being watched.
599
600 \sa directories()
601 */
602
603 QStringList DFileSystemWatcher::directories() const
604 {
605 Q_D(const DFileSystemWatcher);
606
607 if (!d)
608 return QStringList();
609
610 return d->directories;
611 }
612
613 QStringList DFileSystemWatcher::files() const
614 {
615 Q_D(const DFileSystemWatcher);
616
617 if (!d)
618 return QStringList();
619
620 return d->files;
621 }
622
623 DCORE_END_NAMESPACE
624
625 #include "moc_dfilesystemwatcher.cpp"