| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qfilesystemwatcher_inotify.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||
| 2 | - | |||||||
| 3 | - | |||||||
| 4 | - | |||||||
| 5 | - | |||||||
| 6 | - | |||||||
| 7 | - | |||||||
| 8 | - | |||||||
| 9 | - | |||||||
| 10 | - | |||||||
| 11 | - | |||||||
| 12 | QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent) | - | ||||||
| 13 | { | - | ||||||
| 14 | int fd = -1; | - | ||||||
| 15 | - | |||||||
| 16 | fd = inotify_init1(IN_CLOEXEC); | - | ||||||
| 17 | - | |||||||
| 18 | if (fd == -1) { | - | ||||||
| 19 | fd = inotify_init(); | - | ||||||
| 20 | if (fd == -1) | - | ||||||
| 21 | return 0; | - | ||||||
| 22 | } | - | ||||||
| 23 | return new QInotifyFileSystemWatcherEngine(fd, parent); | - | ||||||
| 24 | } | - | ||||||
| 25 | - | |||||||
| 26 | QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd, QObject *parent) | - | ||||||
| 27 | : QFileSystemWatcherEngine(parent), | - | ||||||
| 28 | inotifyFd(fd), | - | ||||||
| 29 | notifier(fd, QSocketNotifier::Read, this) | - | ||||||
| 30 | { | - | ||||||
| 31 | fcntl(inotifyFd, 2, 1); | - | ||||||
| 32 | connect(¬ifier, qFlagLocation("2""activated(int)" "\0" __FILE__ ":" "242""249"), qFlagLocation("1""readFromInotify()" "\0" __FILE__ ":" "242""249")); | - | ||||||
| 33 | } | - | ||||||
| 34 | - | |||||||
| 35 | QInotifyFileSystemWatcherEngine::~QInotifyFileSystemWatcherEngine() | - | ||||||
| 36 | { | - | ||||||
| 37 | notifier.setEnabled(false); | - | ||||||
| 38 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(pathToID)>::type> _container_((pathToID)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1)for (int id = *_container_.i; _container_.control; _container_.control = 0): qAsConst(pathToID)) | - | ||||||
| 39 | 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 | ||||||
| 40 | - | |||||||
| 41 | ::close(inotifyFd); | - | ||||||
| 42 | } executed 354 times by 11 tests: end of blockExecuted by:
| 354 | ||||||
| 43 | - | |||||||
| 44 | QStringList QInotifyFileSystemWatcherEngine::addPaths(const QStringList &paths, | - | ||||||
| 45 | QStringList *files, | - | ||||||
| 46 | QStringList *directories) | - | ||||||
| 47 | { | - | ||||||
| 48 | QStringList p = paths; | - | ||||||
| 49 | QMutableListIterator<QString> it(p); | - | ||||||
| 50 | while (it.hasNext()
| 476-478 | ||||||
| 51 | QString path = it.next(); | - | ||||||
| 52 | QFileInfo fi(path); | - | ||||||
| 53 | bool isDir = fi.isDir(); | - | ||||||
| 54 | if (isDir
| 36-442 | ||||||
| 55 | if (directories->contains(path)
| 1-441 | ||||||
| 56 | continue; executed 1 time by 1 test: continue;Executed by:
| 1 | ||||||
| 57 | } executed 441 times by 8 tests: else {end of blockExecuted by:
| 441 | ||||||
| 58 | if (files->contains(path)
| 0-36 | ||||||
| 59 | continue; never executed: continue; | 0 | ||||||
| 60 | } executed 36 times by 1 test: end of blockExecuted by:
| 36 | ||||||
| 61 | - | |||||||
| 62 | int wd = inotify_add_watch(inotifyFd, | - | ||||||
| 63 | QFile::encodeName(path), | - | ||||||
| 64 | (isDir | - | ||||||
| 65 | ? (0 | - | ||||||
| 66 | | 0x00000004 | - | ||||||
| 67 | | (0x00000040 | 0x00000080) | - | ||||||
| 68 | | 0x00000100 | - | ||||||
| 69 | | 0x00000200 | - | ||||||
| 70 | | 0x00000400 | - | ||||||
| 71 | ) | - | ||||||
| 72 | : (0 | - | ||||||
| 73 | | 0x00000004 | - | ||||||
| 74 | | 0x00000002 | - | ||||||
| 75 | | (0x00000040 | 0x00000080) | - | ||||||
| 76 | | 0x00000800 | - | ||||||
| 77 | | 0x00000400 | - | ||||||
| 78 | ))); | - | ||||||
| 79 | if (wd < 0
| 2-475 | ||||||
| 80 | perrorQMessageLogger("QInotifyFileSystemWatcherEngine::addPaths:__FILE__, 297, __PRETTY_FUNCTION__).warning().nospace() << inotify_add_watch"inotify_add_watch(" << path << failed");") failed: " << QSystemError((*__errno_location ()), QSystemError::NativeError).toString(); | - | ||||||
| 81 | continue; executed 2 times by 1 test: continue;Executed by:
| 2 | ||||||
| 82 | } | - | ||||||
| 83 | - | |||||||
| 84 | it.remove(); | - | ||||||
| 85 | - | |||||||
| 86 | int id = isDir
| 34-441 | ||||||
| 87 | if (id < 0
| 34-441 | ||||||
| 88 | directories->append(path); | - | ||||||
| 89 | } executed 441 times by 8 tests: else {end of blockExecuted by:
| 441 | ||||||
| 90 | files->append(path); | - | ||||||
| 91 | } executed 34 times by 1 test: end of blockExecuted by:
| 34 | ||||||
| 92 | - | |||||||
| 93 | pathToID.insert(path, id); | - | ||||||
| 94 | idToPath.insert(id, path); | - | ||||||
| 95 | } executed 475 times by 8 tests: end of blockExecuted by:
| 475 | ||||||
| 96 | - | |||||||
| 97 | return executed 476 times by 8 tests: p;return p;Executed by:
executed 476 times by 8 tests: return p;Executed by:
| 476 | ||||||
| 98 | } | - | ||||||
| 99 | - | |||||||
| 100 | QStringList QInotifyFileSystemWatcherEngine::removePaths(const QStringList &paths, | - | ||||||
| 101 | QStringList *files, | - | ||||||
| 102 | QStringList *directories) | - | ||||||
| 103 | { | - | ||||||
| 104 | QStringList p = paths; | - | ||||||
| 105 | QMutableListIterator<QString> it(p); | - | ||||||
| 106 | while (it.hasNext()) { | - | ||||||
| 107 | QString path = it.next(); | - | ||||||
| 108 | int id = pathToID.take(path); | - | ||||||
| 109 | QString x = idToPath.take(id); | - | ||||||
| 110 | if (x.isEmpty() || x != path) | - | ||||||
| 111 | continue; | - | ||||||
| 112 | - | |||||||
| 113 | int wd = id < 0 ? -id : id; | - | ||||||
| 114 | - | |||||||
| 115 | inotify_rm_watch(inotifyFd, wd); | - | ||||||
| 116 | - | |||||||
| 117 | it.remove(); | - | ||||||
| 118 | if (id < 0) { | - | ||||||
| 119 | directories->removeAll(path); | - | ||||||
| 120 | } else { | - | ||||||
| 121 | files->removeAll(path); | - | ||||||
| 122 | } | - | ||||||
| 123 | } | - | ||||||
| 124 | - | |||||||
| 125 | return p; | - | ||||||
| 126 | } | - | ||||||
| 127 | - | |||||||
| 128 | void QInotifyFileSystemWatcherEngine::readFromInotify() | - | ||||||
| 129 | { | - | ||||||
| 130 | - | |||||||
| 131 | - | |||||||
| 132 | int buffSize = 0; | - | ||||||
| 133 | ioctl(inotifyFd, 0x541B, (char *) &buffSize); | - | ||||||
| 134 | QVarLengthArray<char, 4096> buffer(buffSize); | - | ||||||
| 135 | buffSize = read(inotifyFd, buffer.data(), buffSize); | - | ||||||
| 136 | char *at = buffer.data(); | - | ||||||
| 137 | char * const end = at + buffSize; | - | ||||||
| 138 | - | |||||||
| 139 | QHash<int, inotify_event *> eventForId; | - | ||||||
| 140 | while (at < end) { | - | ||||||
| 141 | inotify_event *event = reinterpret_cast<inotify_event *>(at); | - | ||||||
| 142 | - | |||||||
| 143 | if (eventForId.contains(event->wd)) | - | ||||||
| 144 | eventForId[event->wd]->mask |= event->mask; | - | ||||||
| 145 | else | - | ||||||
| 146 | eventForId.insert(event->wd, event); | - | ||||||
| 147 | - | |||||||
| 148 | at += sizeof(inotify_event) + event->len; | - | ||||||
| 149 | } | - | ||||||
| 150 | - | |||||||
| 151 | QHash<int, inotify_event *>::const_iterator it = eventForId.constBegin(); | - | ||||||
| 152 | while (it != eventForId.constEnd()) { | - | ||||||
| 153 | const inotify_event &event = **it; | - | ||||||
| 154 | ++it; | - | ||||||
| 155 | - | |||||||
| 156 | - | |||||||
| 157 | - | |||||||
| 158 | int id = event.wd; | - | ||||||
| 159 | QString path = getPathFromID(id); | - | ||||||
| 160 | if (path.isEmpty()) { | - | ||||||
| 161 | - | |||||||
| 162 | id = -id; | - | ||||||
| 163 | path = getPathFromID(id); | - | ||||||
| 164 | if (path.isEmpty()) | - | ||||||
| 165 | continue; | - | ||||||
| 166 | } | - | ||||||
| 167 | - | |||||||
| 168 | - | |||||||
| 169 | - | |||||||
| 170 | if ((event.mask & (0x00000400 | 0x00000800 | 0x00002000)) != 0) { | - | ||||||
| 171 | pathToID.remove(path); | - | ||||||
| 172 | idToPath.remove(id, getPathFromID(id)); | - | ||||||
| 173 | if (!idToPath.contains(id)) | - | ||||||
| 174 | inotify_rm_watch(inotifyFd, event.wd); | - | ||||||
| 175 | - | |||||||
| 176 | if (id < 0) | - | ||||||
| 177 | directoryChanged(path, true); | - | ||||||
| 178 | else | - | ||||||
| 179 | fileChanged(path, true); | - | ||||||
| 180 | } else { | - | ||||||
| 181 | if (id < 0) | - | ||||||
| 182 | directoryChanged(path, false); | - | ||||||
| 183 | else | - | ||||||
| 184 | fileChanged(path, false); | - | ||||||
| 185 | } | - | ||||||
| 186 | } | - | ||||||
| 187 | } | - | ||||||
| 188 | - | |||||||
| 189 | QString QInotifyFileSystemWatcherEngine::getPathFromID(int id) const | - | ||||||
| 190 | { | - | ||||||
| 191 | QHash<int, QString>::const_iterator i = idToPath.find(id); | - | ||||||
| 192 | while (i != idToPath.constEnd() && i.key() == id) { | - | ||||||
| 193 | if ((i + 1) == idToPath.constEnd() || (i + 1).key() != id) { | - | ||||||
| 194 | return i.value(); | - | ||||||
| 195 | } | - | ||||||
| 196 | ++i; | - | ||||||
| 197 | } | - | ||||||
| 198 | return QString(); | - | ||||||
| 199 | } | - | ||||||
| 200 | - | |||||||
| 201 | - | |||||||
| Switch to Source code | Preprocessed file |