48 #include <io/config-kdirwatch.h>
54 #include <QtCore/QDir>
55 #include <QtCore/QFile>
56 #include <QtCore/QSocketNotifier>
57 #include <QtCore/QTimer>
58 #include <QtCore/QCoreApplication>
71 #include <sys/ioctl.h>
74 #include <sys/utsname.h>
89 if (method == QLatin1String(
"Fam")) {
91 }
else if (method == QLatin1String(
"Stat")) {
93 }
else if (method == QLatin1String(
"QFSWatch")) {
158 delayRemove( false ),
162 timer.setObjectName(QLatin1String(
"KDirWatchPrivate::timer"));
177 availableMethods <<
"Stat";
180 rescan_timer.setObjectName(QString::fromLatin1(
"KDirWatchPrivate::rescan_timer"));
186 if (FAMOpen(&fc) ==0) {
187 availableMethods <<
"FAM";
189 sn =
new QSocketNotifier( FAMCONNECTION_GETFD(&fc),
190 QSocketNotifier::Read,
this);
191 connect( sn, SIGNAL(activated(
int)),
195 kDebug(7001) <<
"Can't use FAM (fam daemon not running?)";
200 #ifdef HAVE_SYS_INOTIFY_H
201 supports_inotify =
true;
203 m_inotify_fd = inotify_init();
205 if ( m_inotify_fd <= 0 ) {
206 kDebug(7001) <<
"Can't use Inotify, kernel doesn't support it";
207 supports_inotify =
false;
212 int major, minor, patch;
213 if (uname(&uts) < 0) {
214 supports_inotify =
false;
215 kDebug(7001) <<
"Unable to get uname";
216 }
else if (sscanf(uts.release,
"%d.%d", &major, &minor) != 2) {
217 supports_inotify =
false;
218 kDebug(7001) <<
"The version is malformed: " << uts.release;
219 }
else if(major == 2 && minor == 6) {
220 if (sscanf(uts.release,
"%d.%d.%d", &major, &minor, &patch) != 3) {
221 supports_inotify =
false;
222 kDebug() <<
"Detected 2.6 kernel but can't know more: " << uts.release;
223 }
else if (major * 1000000 + minor * 1000 + patch < 2006014 ){
224 supports_inotify =
false;
225 kDebug(7001) <<
"Can't use INotify, Linux kernel too old " << uts.release;
230 kDebug(7001) <<
"INotify available: " << supports_inotify;
231 if ( supports_inotify ) {
232 availableMethods <<
"INotify";
233 (void)fcntl(m_inotify_fd, F_SETFD, FD_CLOEXEC);
235 mSn =
new QSocketNotifier( m_inotify_fd, QSocketNotifier::Read,
this );
236 connect( mSn, SIGNAL(activated(
int)),
240 #ifdef HAVE_QFILESYSTEMWATCHER
241 availableMethods <<
"QFileSystemWatcher";
262 #ifdef HAVE_SYS_INOTIFY_H
263 if ( supports_inotify )
264 ::close( m_inotify_fd );
266 #ifdef HAVE_QFILESYSTEMWATCHER
274 #ifdef HAVE_SYS_INOTIFY_H
275 if ( !supports_inotify )
279 int offsetStartRead = 0;
281 assert( m_inotify_fd > -1 );
282 ioctl( m_inotify_fd, FIONREAD, &pending );
284 while ( pending > 0 ) {
286 const int bytesToRead = qMin( pending, (
int)
sizeof( buf ) - offsetStartRead );
288 int bytesAvailable = read( m_inotify_fd, &buf[offsetStartRead], bytesToRead );
289 pending -= bytesAvailable;
290 bytesAvailable += offsetStartRead;
293 int offsetCurrent = 0;
294 while ( bytesAvailable >= (
int)
sizeof(
struct inotify_event ) ) {
295 const struct inotify_event *
const event = (
struct inotify_event *) &buf[offsetCurrent];
296 const int eventSize =
sizeof(
struct inotify_event ) + event->len;
297 if ( bytesAvailable < eventSize ) {
301 bytesAvailable -= eventSize;
302 offsetCurrent += eventSize;
305 QByteArray cpath(event->name, event->len);
307 path = QFile::decodeName ( cpath );
319 if ( e->wd == event->wd ) {
326 if( event->mask & IN_DELETE_SELF) {
327 if (s_verboseDebug) {
328 kDebug(7001) <<
"-->got deleteself signal for" << e->
path;
337 if ( event->mask & IN_IGNORED ) {
341 if ( event->mask & (IN_CREATE|IN_MOVED_TO) ) {
342 const QString tpath = e->
path + QLatin1Char(
'/') + path;
345 if (s_verboseDebug) {
346 kDebug(7001) <<
"-->got CREATE signal for" << (tpath) <<
"sub_entry=" << sub_entry;
353 sub_entry->
dirty =
true;
358 Q_FOREACH(
Client *client, clients) {
366 if (!clients.isEmpty()) {
368 kDebug(7001).nospace() << clients.count() <<
" instance(s) monitoring the new "
369 << (isDir ?
"dir " :
"file ") << tpath;
371 e->m_pendingFileChanges.append(e->
path);
376 if (event->mask & (IN_DELETE|IN_MOVED_FROM)) {
377 const QString tpath = e->
path + QLatin1Char(
'/') + path;
378 if (s_verboseDebug) {
379 kDebug(7001) <<
"-->got DELETE signal for" << tpath;
387 KDE_struct_stat stat_buf;
392 bool isDir = S_ISDIR(stat_buf.st_mode);
406 if (event->mask & (IN_MODIFY|IN_ATTRIB)) {
408 const QString tpath = e->
path + QLatin1Char(
'/') + path;
409 if (s_verboseDebug) {
410 kDebug(7001) <<
"-->got MODIFY signal for" << (tpath);
427 e->m_pendingFileChanges.append(tpath);
438 if (bytesAvailable > 0) {
440 memmove(buf, &buf[offsetCurrent], bytesAvailable);
441 offsetStartRead = bytesAvailable;
455 if (!sub_entry->
dirty)
457 sub_entry->
dirty =
true;
468 KDirWatch::WatchModes watchModes)
473 foreach(
Client* client, m_clients) {
488 m_clients.append(client);
495 for ( ; it != end ; ++it ) {
499 if (client->
count == 0) {
512 foreach(
Client* client, m_clients)
513 clients += client->
count;
520 return QDir::cleanPath(path + QLatin1String(
"/.."));
526 KDE_struct_stat stat_buf;
528 *isDir = S_ISDIR(stat_buf.st_mode);
529 const KDirWatch::WatchModes flag =
531 Q_FOREACH(
Client *client, this->m_clients) {
547 debug.nospace() <<
"[ Entry for " << entry.
path <<
", " << (entry.
isDir ?
"dir" :
"file");
549 debug <<
", non-existent";
555 #ifdef HAVE_SYS_INOTIFY_H
557 debug <<
" inotify_wd=" << entry.wd;
559 debug <<
", has " << entry.
m_clients.count() <<
" clients";
562 debug <<
", nonexistent subentries:";
564 debug << subEntry << subEntry->
path;
573 if (_path.isEmpty() || QDir::isRelativePath(_path)) {
579 if ( path.length() > 1 && path.endsWith( QLatin1Char(
'/' ) ) )
580 path.truncate( path.length() - 1 );
598 kDebug(7001) <<
"Global Poll Freq is now" <<
freq <<
"msec";
603 #if defined(HAVE_FAM)
605 bool KDirWatchPrivate::useFAM(Entry* e)
607 if (!use_fam)
return false;
619 addEntry(0, e->parentDirectory(), e,
true);
622 int res =FAMMonitorDirectory(&fc, QFile::encodeName(e->path),
630 kDebug(7001).nospace() <<
" Setup FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
631 <<
") for " << e->path;
637 addEntry(0, QFileInfo(e->path).absolutePath(), e,
true);
640 int res = FAMMonitorFile(&fc, QFile::encodeName(e->path),
649 kDebug(7001).nospace() <<
" Setup FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
650 <<
") for " << e->path;
662 #ifdef HAVE_SYS_INOTIFY_H
664 bool KDirWatchPrivate::useINotify( Entry* e )
671 if (!supports_inotify)
return false;
676 addEntry(0, e->parentDirectory(), e,
true);
681 int mask = IN_DELETE|IN_DELETE_SELF|IN_CREATE|IN_MOVE|IN_MOVE_SELF|IN_DONT_FOLLOW|IN_MOVED_FROM|IN_MODIFY|IN_ATTRIB;
683 if ( ( e->wd = inotify_add_watch( m_inotify_fd,
684 QFile::encodeName( e->path ), mask) ) >= 0)
686 if (s_verboseDebug) {
687 kDebug(7001) <<
"inotify successfully used for monitoring" << e->path <<
"wd=" << e->wd;
692 kDebug(7001) <<
"inotify failed for monitoring" << e->path <<
":" << strerror(errno);
696 #ifdef HAVE_QFILESYSTEMWATCHER
707 kDebug(7001) <<
"fsWatcher->addPath" << e->
path;
732 kDebug(7001) <<
" Started Polling Timer, freq " <<
freq;
736 kDebug(7001) <<
" Setup Stat (freq " << e->
freq <<
") for " << e->
path;
748 Entry* sub_entry,
bool isDir, KDirWatch::WatchModes watchModes)
753 || path == QLatin1String(
"/dev")
754 || (path.startsWith(QLatin1String(
"/dev/")) && !path.startsWith(QLatin1String(
"/dev/.")))
759 if ( path.length() > 1 && path.endsWith( QLatin1Char(
'/' ) ) )
760 path.truncate( path.length() - 1 );
766 (*it).m_entries.append(sub_entry);
767 if (s_verboseDebug) {
768 kDebug(7001) <<
"Added already watched Entry" << path
769 <<
"(for" << sub_entry->
path <<
")";
771 #ifdef HAVE_SYS_INOTIFY_H
774 int mask = IN_DELETE|IN_DELETE_SELF|IN_CREATE|IN_MOVE|IN_MOVE_SELF|IN_DONT_FOLLOW;
776 mask |= IN_MODIFY|IN_ATTRIB;
780 inotify_rm_watch (m_inotify_fd, e->wd);
781 e->wd = inotify_add_watch( m_inotify_fd, QFile::encodeName( e->
path ),
788 (*it).addClient(instance, watchModes);
789 if (s_verboseDebug) {
790 kDebug(7001) <<
"Added already watched Entry" << path
791 <<
"(now" << (*it).clientCount() <<
"clients)"
792 << QString::fromLatin1(
"[%1]").arg(instance->objectName());
800 KDE_struct_stat stat_buf;
801 bool exists = (
KDE::stat(path, &stat_buf) == 0);
805 Entry* e = &(*newIt);
808 e->
isDir = S_ISDIR(stat_buf.st_mode);
810 if (e->
isDir && !isDir) {
812 if (S_ISLNK(stat_buf.st_mode))
816 qWarning() <<
"KDirWatch:" << path <<
"is a directory. Use addDir!";
818 }
else if (!e->
isDir && isDir)
819 qWarning(
"KDirWatch: %s is a file. Use addFile!", qPrintable(path));
822 qWarning() <<
"KDirWatch:" << path <<
"is a file. You can't use recursive or "
823 "watchFiles options";
829 e->
m_ctime = stat_buf.st_mtime;
831 e->
m_ctime = stat_buf.st_ctime;
834 e->
m_nlink = stat_buf.st_nlink;
835 e->
m_ino = stat_buf.st_ino;
851 kDebug(7001).nospace() <<
"Added " << (e->
isDir ?
"Dir " :
"File ") << path
853 <<
" for " << (sub_entry ? sub_entry->
path :
QString())
854 <<
" [" << (instance ? instance->objectName() :
QString()) <<
"]";
864 QFlags<QDir::Filter> filters = QDir::NoDotAndDotDot;
868 filters |= (QDir::Dirs|QDir::Files);
869 }
else if (watchModes & KDirWatch::WatchSubDirs) {
870 filters |= QDir::Dirs;
872 filters |= QDir::Files;
875 #if defined(HAVE_SYS_INOTIFY_H)
882 filters &= ~QDir::Files;
886 QDir basedir (e->
path);
887 const QFileInfoList contents = basedir.entryInfoList(filters);
888 for (QFileInfoList::const_iterator iter = contents.constBegin();
889 iter != contents.constEnd(); ++iter)
891 const QFileInfo &fileInfo = *iter;
893 bool isDir = fileInfo.isDir() && !fileInfo.isSymLink();
895 addEntry (instance, fileInfo.absoluteFilePath(), 0, isDir,
921 bool entryAdded =
false;
922 switch (preferredMethod) {
923 #if defined(HAVE_FAM)
926 #if defined(HAVE_SYS_INOTIFY_H)
929 #if defined(HAVE_QFILESYSTEMWATCHER)
938 #if defined(HAVE_SYS_INOTIFY_H)
939 if (useINotify(e))
return;
941 #if defined(HAVE_FAM)
942 if (useFAM(e))
return;
944 #if defined(HAVE_QFILESYSTEMWATCHER)
955 FAMCancelMonitor(&fc, &(e->fr) );
956 kDebug(7001).nospace() <<
"Cancelled FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
957 <<
") for " << e->
path;
960 #ifdef HAVE_SYS_INOTIFY_H
962 (void) inotify_rm_watch( m_inotify_fd, e->wd );
963 if (s_verboseDebug) {
964 kDebug(7001).nospace() <<
"Cancelled INotify (fd " << m_inotify_fd <<
", "
965 << e->wd <<
") for " << e->
path;
969 #ifdef HAVE_QFILESYSTEMWATCHER
972 kDebug(7001) <<
"fsWatcher->removePath" << e->
path;
982 if (s_verboseDebug) {
983 kDebug(7001) <<
"path=" << _path <<
"sub_entry:" << sub_entry;
987 kWarning(7001) <<
"doesn't know" << _path;
1028 kDebug(7001) <<
" Stopped Polling Timer";
1032 if (s_verboseDebug) {
1033 kDebug(7001).nospace() <<
"Removed " << (e->
isDir ?
"Dir ":
"File ") << e->
path
1034 <<
" for " << (sub_entry ? sub_entry->
path :
QString())
1035 <<
" [" << (instance ? instance->objectName() :
QString()) <<
"]";
1046 int minfreq = 3600000;
1053 foreach(
Client* client, (*it).m_clients) {
1054 if (client->
instance == instance) {
1061 pathList.append((*it).path);
1063 else if ( (*it).m_mode ==
StatMode && (*it).freq < minfreq )
1064 minfreq = (*it).freq;
1067 foreach(
const QString &path, pathList)
1070 if (minfreq >
freq) {
1074 kDebug(7001) <<
"Poll Freq now" <<
freq <<
"msec";
1081 int stillWatching = 0;
1083 if (!instance || instance == client->
instance)
1086 stillWatching += client->
count;
1089 kDebug(7001) << (instance ? instance->objectName() : QString::fromLatin1(
"all"))
1090 <<
"stopped scanning" << e->
path <<
"(now"
1091 << stillWatching <<
"watchers)";
1093 if (stillWatching == 0) {
1108 int wasWatching = 0, newWatching = 0;
1111 wasWatching += client->
count;
1112 else if (!instance || instance == client->
instance) {
1114 newWatching += client->
count;
1117 if (newWatching == 0)
1120 kDebug(7001) << (instance ? instance->objectName() : QString::fromLatin1(
"all"))
1121 <<
"restarted scanning" << e->
path
1122 <<
"(now" << wasWatching+newWatching <<
"watchers)";
1127 if (wasWatching == 0) {
1129 KDE_struct_stat stat_buf;
1134 e->
m_ctime = stat_buf.st_mtime;
1136 e->
m_ctime = stat_buf.st_ctime;
1139 if (s_verboseDebug) {
1140 kDebug(7001) <<
"Setting status to Normal for" << e << e->
path;
1142 e->
m_nlink = stat_buf.st_nlink;
1143 e->
m_ino = stat_buf.st_ino;
1152 if (s_verboseDebug) {
1153 kDebug(7001) <<
"Setting status to NonExistent for" << e << e->
path;
1175 bool notify,
bool skippedToo )
1194 foreach(
Client* client, (*it).m_clients) {
1224 KDE_struct_stat stat_buf;
1231 e->
m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
1233 e->
m_ino = stat_buf.st_ino;
1234 if (s_verboseDebug) {
1235 kDebug(7001) <<
"Setting status to Normal for just created" << e << e->
path;
1243 #if 1 // for debugging the if() below
1244 if (s_verboseDebug) {
1245 struct tm* tmp = localtime(&e->
m_ctime);
1247 strftime(outstr,
sizeof(outstr),
"%T", tmp);
1249 <<
"stat_buf.st_ctime=" << stat_buf.st_ctime
1250 <<
"e->m_nlink=" << e->
m_nlink
1251 <<
"stat_buf.st_nlink=" << stat_buf.st_nlink
1252 <<
"e->m_ino=" << e->
m_ino
1253 <<
"stat_buf.st_ino=" << stat_buf.st_ino;
1258 (qMax(stat_buf.st_ctime, stat_buf.st_mtime) != e->
m_ctime ||
1259 stat_buf.st_ino != e->
m_ino ||
1260 stat_buf.st_nlink != nlink_t(e->
m_nlink)))
1268 e->
m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
1269 e->
m_nlink = stat_buf.st_nlink;
1270 if (e->
m_ino != stat_buf.st_ino) {
1275 e->
m_ino = stat_buf.st_ino;
1303 if (!fileName.isEmpty()) {
1304 if (!QDir::isRelativePath(fileName))
1308 path += QLatin1Char(
'/') + fileName;
1309 #elif defined(Q_WS_WIN)
1311 path += QDir::currentPath().left(2) + QLatin1Char(
'/') + fileName;
1316 if (s_verboseDebug) {
1341 QMetaObject::invokeMethod(c->
instance,
"setDeleted", Qt::QueuedConnection, Q_ARG(
QString, path));
1347 QMetaObject::invokeMethod(c->
instance,
"setCreated", Qt::QueuedConnection, Q_ARG(
QString, path));
1352 QMetaObject::invokeMethod(c->
instance,
"setDirty", Qt::QueuedConnection, Q_ARG(
QString, path));
1378 EntryMap::Iterator it;
1384 bool timerRunning =
timer.isActive();
1407 (*it).propagate_dirty();
1410 #ifdef HAVE_SYS_INOTIFY_H
1418 if (!entry->
isValid())
continue;
1422 kDebug(7001) <<
"scanEntry for" << entry->
path <<
"says" << ev;
1425 #ifdef HAVE_SYS_INOTIFY_H
1429 kDebug(7001) <<
"scanEntry says" << entry->
path <<
"was deleted";
1433 kDebug(7001) <<
"scanEntry says" << entry->
path <<
"was created. wd=" << entry->wd;
1434 if (entry->wd < 0) {
1435 cList.append(entry);
1452 #ifdef HAVE_SYS_INOTIFY_H
1458 QStringList pendingFileChanges = entry->m_pendingFileChanges;
1459 pendingFileChanges.removeDuplicates();
1460 Q_FOREACH(
const QString &changedFilename, pendingFileChanges) {
1461 if (s_verboseDebug) {
1462 kDebug(7001) <<
"processing pending file change for" << changedFilename;
1466 entry->m_pendingFileChanges.clear();
1478 #ifdef HAVE_SYS_INOTIFY_H
1480 Q_FOREACH(
Entry* e, cList)
1490 if ( *filename ==
'.') {
1491 if (strncmp(filename,
".X.err", 6) == 0)
return true;
1492 if (strncmp(filename,
".xsession-errors", 16) == 0)
return true;
1495 if (strncmp(filename,
".fonts.cache", 12) == 0)
return true;
1510 while(use_fam && FAMPending(&fc)) {
1511 if (FAMNextEvent(&fc, &fe) == -1) {
1512 kWarning(7001) <<
"FAM connection problem, switching to polling.";
1519 if ((*it).m_mode ==
FAMMode && (*it).m_clients.count()>0) {
1531 void KDirWatchPrivate::checkFAMEvent(FAMEvent* fe)
1536 if ((fe->code == FAMExists) ||
1537 (fe->code == FAMEndExist) ||
1538 (fe->code == FAMAcknowledge))
return;
1546 if (FAMREQUEST_GETREQNUM(&( (*it).fr )) ==
1547 FAMREQUEST_GETREQNUM(&(fe->fr)) ) {
1554 if (s_verboseDebug) {
1555 kDebug(7001) <<
"Processing FAM event ("
1556 << ((fe->code == FAMChanged) ?
"FAMChanged" :
1557 (fe->code == FAMDeleted) ?
"FAMDeleted" :
1558 (fe->code == FAMStartExecuting) ?
"FAMStartExecuting" :
1559 (fe->code == FAMStopExecuting) ?
"FAMStopExecuting" :
1560 (fe->code == FAMCreated) ?
"FAMCreated" :
1561 (fe->code == FAMMoved) ?
"FAMMoved" :
1562 (fe->code == FAMAcknowledge) ?
"FAMAcknowledge" :
1563 (fe->code == FAMExists) ?
"FAMExists" :
1564 (fe->code == FAMEndExist) ?
"FAMEndExist" :
"Unknown Code")
1565 <<
", " << fe->filename
1566 <<
", Req " << FAMREQUEST_GETREQNUM(&(fe->fr)) <<
") e=" << e;
1576 kDebug(7001) <<
"FAM event for nonExistent entry " << e->path;
1589 if (!QDir::isRelativePath(QFile::decodeName(fe->filename))) {
1590 FAMCancelMonitor(&fc, &(e->fr) );
1591 kDebug(7001) <<
"Cancelled FAMReq"
1592 << FAMREQUEST_GETREQNUM(&(e->fr))
1593 <<
"for" << e->path;
1598 addEntry(0, e->parentDirectory(), e,
true );
1609 QString tpath(e->path + QLatin1Char(
'/') + QFile::decodeName(fe->filename));
1612 Entry* sub_entry = e->findSubEntry(tpath);
1616 sub_entry->dirty =
true;
1618 }
else if (e->isDir && !e->m_clients.empty()) {
1620 const QList<Client *> clients = e->clientsForFileOrDir(tpath, &isDir);
1621 Q_FOREACH(Client *client, clients) {
1622 addEntry (client->instance, tpath, 0, isDir,
1626 if (!clients.isEmpty()) {
1629 kDebug(7001).nospace() << clients.count() <<
" instance(s) monitoring the new "
1630 << (isDir ?
"dir " :
"file ") << tpath;
1642 kWarning (7001) <<
"Fam event received but FAM is not supported";
1649 EntryMap::Iterator it;
1651 kDebug(7001) <<
"Entries watched:";
1653 kDebug(7001) <<
" None.";
1659 kDebug(7001) <<
" " << *e;
1667 if (!pending.isEmpty()) pending =
" (pending: " + pending +
')';
1668 pending =
", stopped" + pending;
1671 <<
" (" << c->
count <<
" times)" << pending;
1674 kDebug(7001) <<
" dependent entries:";
1677 if (s_verboseDebug) {
1686 #ifdef HAVE_QFILESYSTEMWATCHER
1698 kDebug(7001) <<
"scanEntry for" << e->
path <<
"says" << ev;
1705 addEntry(0, QFileInfo(e->
path).absolutePath(), e,
true);
1709 }
else if (e->
isDir) {
1721 kWarning (7001) <<
"QFileSystemWatcher event received but QFileSystemWatcher is not supported";
1723 #endif // HAVE_QFILESYSTEMWATCHER
1732 return s_pKDirWatchSelf;
1738 return s_pKDirWatchSelf.exists();
1743 s_pKDirWatchSelf->deleteQFSWatcher();
1749 static int nameCounter = 0;
1752 setObjectName(QString::fromLatin1(
"KDirWatch-%1").arg(nameCounter) );
1758 static bool cleanupRegistered =
false;
1759 if (!cleanupRegistered) {
1760 cleanupRegistered =
true;
1779 if (d) d->
addEntry(
this, _path, 0,
true, watchModes);
1787 d->
addEntry(
this, _path, 0,
false);
1875 kDebug(7001) <<
"KDirWatch not used";
1884 kDebug(7001) << objectName() <<
"emitting created" << _file;
1891 emit
dirty( _file );
1896 kDebug(7001) << objectName() <<
"emitting deleted" << _file;
1906 #include "kdirwatch.moc"
1907 #include "kdirwatch_p.moc"