• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • io
kdirwatch.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1998 Sven Radej <sven@lisa.exp.univie.ac.at>
3  Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
4  Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
5  Copyright (C) 2008 Rafal Rzepecki <divided.mind@gmail.com>
6  Copyright (C) 2010 David Faure <faure@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License version 2 as published by the Free Software Foundation.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 
24 // CHANGES:
25 // Jul 30, 2008 - Don't follow symlinks when recursing to avoid loops (Rafal)
26 // Aug 6, 2007 - KDirWatch::WatchModes support complete, flags work fine also
27 // when using FAMD (Flavio Castelli)
28 // Aug 3, 2007 - Handled KDirWatch::WatchModes flags when using inotify, now
29 // recursive and file monitoring modes are implemented (Flavio Castelli)
30 // Jul 30, 2007 - Substituted addEntry boolean params with KDirWatch::WatchModes
31 // flag (Flavio Castelli)
32 // Oct 4, 2005 - Inotify support (Dirk Mueller)
33 // Februar 2002 - Add file watching and remote mount check for STAT
34 // Mar 30, 2001 - Native support for Linux dir change notification.
35 // Jan 28, 2000 - Usage of FAM service on IRIX (Josef.Weidendorfer@in.tum.de)
36 // May 24. 1998 - List of times introduced, and some bugs are fixed. (sven)
37 // May 23. 1998 - Removed static pointer - you can have more instances.
38 // It was Needed for KRegistry. KDirWatch now emits signals and doesn't
39 // call (or need) KFM. No more URL's - just plain paths. (sven)
40 // Mar 29. 1998 - added docs, stop/restart for particular Dirs and
41 // deep copies for list of dirs. (sven)
42 // Mar 28. 1998 - Created. (sven)
43 
44 #include "kdirwatch.h"
45 #include "kdirwatch_p.h"
46 #include "kfilesystemtype_p.h"
47 
48 #include <io/config-kdirwatch.h>
49 #include <config.h>
50 
51 #include <sys/stat.h>
52 #include <assert.h>
53 #include <errno.h>
54 #include <QtCore/QDir>
55 #include <QtCore/QFile>
56 #include <QtCore/QSocketNotifier>
57 #include <QtCore/QTimer>
58 #include <QtCore/QCoreApplication>
59 
60 #include <ksharedconfig.h>
61 #include <kdebug.h>
62 #include <kconfig.h>
63 #include <kglobal.h>
64 #include <kde_file.h>
65 #include <kconfiggroup.h>
66 
67 #include <stdlib.h>
68 #include <string.h>
69 
70 // debug
71 #include <sys/ioctl.h>
72 
73 
74 #include <sys/utsname.h>
75 
76 // set this to true for much more verbose debug output
77 static const bool s_verboseDebug = false;
78 
79 // The KDirWatchPrivate instance is refcounted, and deleted by the last KDirWatch instance
80 static KDirWatchPrivate* dwp_self = 0;
81 static KDirWatchPrivate* createPrivate() {
82  if (!dwp_self)
83  dwp_self = new KDirWatchPrivate;
84  return dwp_self;
85 }
86 
87 // Convert a string into a watch Method
88 static KDirWatch::Method methodFromString(const QString& method) {
89  if (method == QLatin1String("Fam")) {
90  return KDirWatch::FAM;
91  } else if (method == QLatin1String("Stat")) {
92  return KDirWatch::Stat;
93  } else if (method == QLatin1String("QFSWatch")) {
94  return KDirWatch::QFSWatch;
95  } else {
96 #ifdef Q_OS_LINUX
97  // inotify supports delete+recreate+modify, which QFSWatch doesn't support
98  return KDirWatch::INotify;
99 #else
100  return KDirWatch::QFSWatch;
101 #endif
102  }
103 }
104 
105 #ifndef NDEBUG
106 static const char* methodToString(KDirWatch::Method method)
107 {
108  switch (method) {
109  case KDirWatch::FAM:
110  return "Fam";
111  case KDirWatch::INotify:
112  return "INotify";
113  case KDirWatch::DNotify:
114  return "DNotify";
115  case KDirWatch::Stat:
116  return "Stat";
117  case KDirWatch::QFSWatch:
118  return "QFSWatch";
119  default:
120  return "ERROR!";
121  }
122 }
123 #endif
124 
125 //
126 // Class KDirWatchPrivate (singleton)
127 //
128 
129 /* All entries (files/directories) to be watched in the
130  * application (coming from multiple KDirWatch instances)
131  * are registered in a single KDirWatchPrivate instance.
132  *
133  * At the moment, the following methods for file watching
134  * are supported:
135  * - Polling: All files to be watched are polled regularly
136  * using stat (more precise: QFileInfo.lastModified()).
137  * The polling frequency is determined from global kconfig
138  * settings, defaulting to 500 ms for local directories
139  * and 5000 ms for remote mounts
140  * - FAM (File Alternation Monitor): first used on IRIX, SGI
141  * has ported this method to LINUX. It uses a kernel part
142  * (IMON, sending change events to /dev/imon) and a user
143  * level damon (fam), to which applications connect for
144  * notification of file changes. For NFS, the fam damon
145  * on the NFS server machine is used; if IMON is not built
146  * into the kernel, fam uses polling for local files.
147  * - INOTIFY: In LINUX 2.6.13, inode change notification was
148  * introduced. You're now able to watch arbitrary inode's
149  * for changes, and even get notification when they're
150  * unmounted.
151  */
152 
153 KDirWatchPrivate::KDirWatchPrivate()
154  : timer(),
155  freq( 3600000 ), // 1 hour as upper bound
156  statEntries( 0 ),
157  m_ref( 0 ),
158  delayRemove( false ),
159  rescan_all( false ),
160  rescan_timer()
161 {
162  timer.setObjectName(QLatin1String("KDirWatchPrivate::timer"));
163  connect (&timer, SIGNAL(timeout()), this, SLOT(slotRescan()));
164 
165  KConfigGroup config(KGlobal::config(), "DirWatch");
166  m_nfsPollInterval = config.readEntry("NFSPollInterval", 5000);
167  m_PollInterval = config.readEntry("PollInterval", 500);
168 
169  QString method = config.readEntry("PreferredMethod", "inotify");
170  m_preferredMethod = methodFromString(method);
171 
172  // The nfs method defaults to the normal (local) method
173  m_nfsPreferredMethod = methodFromString(config.readEntry("nfsPreferredMethod", "Fam"));
174 
175  QList<QByteArray> availableMethods;
176 
177  availableMethods << "Stat";
178 
179  // used for FAM and inotify
180  rescan_timer.setObjectName(QString::fromLatin1("KDirWatchPrivate::rescan_timer"));
181  rescan_timer.setSingleShot( true );
182  connect(&rescan_timer, SIGNAL(timeout()), this, SLOT(slotRescan()));
183 
184 #ifdef HAVE_FAM
185  // It's possible that FAM server can't be started
186  if (FAMOpen(&fc) ==0) {
187  availableMethods << "FAM";
188  use_fam=true;
189  sn = new QSocketNotifier( FAMCONNECTION_GETFD(&fc),
190  QSocketNotifier::Read, this);
191  connect( sn, SIGNAL(activated(int)),
192  this, SLOT(famEventReceived()) );
193  }
194  else {
195  kDebug(7001) << "Can't use FAM (fam daemon not running?)";
196  use_fam=false;
197  }
198 #endif
199 
200 #ifdef HAVE_SYS_INOTIFY_H
201  supports_inotify = true;
202 
203  m_inotify_fd = inotify_init();
204 
205  if ( m_inotify_fd <= 0 ) {
206  kDebug(7001) << "Can't use Inotify, kernel doesn't support it";
207  supports_inotify = false;
208  }
209 
210  {
211  struct utsname uts;
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) { // If it is 2.6 check further...
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;
226  }
227  }
228  }
229 
230  kDebug(7001) << "INotify available: " << supports_inotify;
231  if ( supports_inotify ) {
232  availableMethods << "INotify";
233  (void)fcntl(m_inotify_fd, F_SETFD, FD_CLOEXEC);
234 
235  mSn = new QSocketNotifier( m_inotify_fd, QSocketNotifier::Read, this );
236  connect( mSn, SIGNAL(activated(int)),
237  this, SLOT(inotifyEventReceived()) );
238  }
239 #endif
240 #ifdef HAVE_QFILESYSTEMWATCHER
241  availableMethods << "QFileSystemWatcher";
242  fsWatcher = 0;
243 #endif
244 #ifndef NDEBUG
245  kDebug(7001) << "Available methods: " << availableMethods << "preferred=" << methodToString(m_preferredMethod);
246 #endif
247 }
248 
249 // This is called on app exit (when K_GLOBAL_STATIC deletes KDirWatch::self)
250 KDirWatchPrivate::~KDirWatchPrivate()
251 {
252  timer.stop();
253 
254  /* remove all entries being watched */
255  removeEntries(0);
256 
257 #ifdef HAVE_FAM
258  if (use_fam) {
259  FAMClose(&fc);
260  }
261 #endif
262 #ifdef HAVE_SYS_INOTIFY_H
263  if ( supports_inotify )
264  ::close( m_inotify_fd );
265 #endif
266 #ifdef HAVE_QFILESYSTEMWATCHER
267  delete fsWatcher;
268 #endif
269 }
270 
271 void KDirWatchPrivate::inotifyEventReceived()
272 {
273  //kDebug(7001);
274 #ifdef HAVE_SYS_INOTIFY_H
275  if ( !supports_inotify )
276  return;
277 
278  int pending = -1;
279  int offsetStartRead = 0; // where we read into buffer
280  char buf[8192];
281  assert( m_inotify_fd > -1 );
282  ioctl( m_inotify_fd, FIONREAD, &pending );
283 
284  while ( pending > 0 ) {
285 
286  const int bytesToRead = qMin( pending, (int)sizeof( buf ) - offsetStartRead );
287 
288  int bytesAvailable = read( m_inotify_fd, &buf[offsetStartRead], bytesToRead );
289  pending -= bytesAvailable;
290  bytesAvailable += offsetStartRead;
291  offsetStartRead = 0;
292 
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 ) {
298  break;
299  }
300 
301  bytesAvailable -= eventSize;
302  offsetCurrent += eventSize;
303 
304  QString path;
305  QByteArray cpath(event->name, event->len);
306  if(event->len)
307  path = QFile::decodeName ( cpath );
308 
309  if ( path.length() && isNoisyFile( cpath ) )
310  continue;
311 
312  // Is set to true if the new event is a directory, false otherwise. This prevents a stat call in clientsForFileOrDir
313  const bool isDir = (event->mask & (IN_ISDIR));
314 
315  // now we're in deep trouble of finding the
316  // associated entries
317  // for now, we suck and iterate
318  for ( EntryMap::Iterator it = m_mapEntries.begin();
319  it != m_mapEntries.end(); ) {
320 
321  Entry* e = &( *it );
322  ++it;
323  if ( e->wd == event->wd ) {
324  const bool wasDirty = e->dirty;
325  e->dirty = true;
326 
327  const QString tpath = e->path + QLatin1Char('/') + path;
328 
329  //if (s_verboseDebug) {
330  // kDebug(7001) << "got event" << "0x"+QString::number(event->mask, 16) << "for" << e->path;
331  //}
332 
333  if( event->mask & IN_DELETE_SELF) {
334  if (s_verboseDebug) {
335  kDebug(7001) << "-->got deleteself signal for" << e->path;
336  }
337  e->m_status = NonExistent;
338  e->wd = -1;
339  e->m_ctime = invalid_ctime;
340  emitEvent(e, Deleted, e->path);
341  // If the parent dir was already watched, tell it something changed
342  Entry* parentEntry = entry(e->parentDirectory());
343  if (parentEntry)
344  parentEntry->dirty = true;
345  // Add entry to parent dir to notice if the entry gets recreated
346  addEntry(0, e->parentDirectory(), e, true /*isDir*/);
347  }
348  if ( event->mask & IN_IGNORED ) {
349  // Causes bug #207361 with kernels 2.6.31 and 2.6.32!
350  //e->wd = -1;
351  }
352  if ( event->mask & (IN_CREATE|IN_MOVED_TO) ) {
353  Entry* sub_entry = e->findSubEntry(tpath);
354 
355  if (s_verboseDebug) {
356  kDebug(7001) << "-->got CREATE signal for" << (tpath) << "sub_entry=" << sub_entry;
357  kDebug(7001) << *e;
358  }
359 
360  // The code below is very similar to the one in checkFAMEvent...
361  if (sub_entry) {
362  // We were waiting for this new file/dir to be created
363  sub_entry->dirty = true;
364  rescan_timer.start(0); // process this asap, to start watching that dir
365  } else if (e->isDir && !e->m_clients.empty()) {
366  const QList<Client *> clients = e->inotifyClientsForFileOrDir(isDir);
367  Q_FOREACH(Client *client, clients) {
368  // See discussion in addEntry for why we don't addEntry for individual
369  // files in WatchFiles mode with inotify.
370  if (isDir) {
371  addEntry(client->instance, tpath, 0, isDir,
372  isDir ? client->m_watchModes : KDirWatch::WatchDirOnly);
373  }
374  }
375  if (!clients.isEmpty()) {
376  emitEvent(e, Created, tpath);
377  kDebug(7001).nospace() << clients.count() << " instance(s) monitoring the new "
378  << (isDir ? "dir " : "file ") << tpath;
379  }
380  e->m_pendingFileChanges.append(e->path);
381  if (!rescan_timer.isActive())
382  rescan_timer.start(m_PollInterval); // singleshot
383  }
384  }
385  if (event->mask & (IN_DELETE|IN_MOVED_FROM)) {
386  if (s_verboseDebug) {
387  kDebug(7001) << "-->got DELETE signal for" << tpath;
388  }
389  if ((e->isDir) && (!e->m_clients.empty())) {
390  Client* client = 0;
391  // A file in this directory has been removed. It wasn't an explicitly
392  // watched file as it would have its own watch descriptor, so
393  // no addEntry/ removeEntry bookkeeping should be required. Emit
394  // the event immediately if any clients are interested.
395  KDirWatch::WatchModes flag = isDir ? KDirWatch::WatchSubDirs : KDirWatch::WatchFiles;
396  int counter = 0;
397  Q_FOREACH(client, e->m_clients) { // krazy:exclude=foreach
398  if (client->m_watchModes & flag) {
399  counter++;
400  }
401  }
402  if (counter != 0) {
403  emitEvent(e, Deleted, tpath);
404  }
405  }
406  }
407  if (event->mask & (IN_MODIFY|IN_ATTRIB)) {
408  if ((e->isDir) && (!e->m_clients.empty())) {
409  if (s_verboseDebug) {
410  kDebug(7001) << "-->got MODIFY signal for" << (tpath);
411  }
412  // A file in this directory has been changed. No
413  // addEntry/ removeEntry bookkeeping should be required.
414  // Add the path to the list of pending file changes if
415  // there are any interested clients.
416  //KDE_struct_stat stat_buf;
417  //QByteArray tpath = QFile::encodeName(e->path+'/'+path);
418  //KDE_stat(tpath, &stat_buf);
419  //bool isDir = S_ISDIR(stat_buf.st_mode);
420 
421  // The API doc is somewhat vague as to whether we should emit
422  // dirty() for implicitly watched files when WatchFiles has
423  // not been specified - we'll assume they are always interested,
424  // regardless.
425  // Don't worry about duplicates for the time
426  // being; this is handled in slotRescan.
427  e->m_pendingFileChanges.append(tpath);
428  // Avoid stat'ing the directory if only an entry inside it changed.
429  e->dirty = (wasDirty || (path.isEmpty() && (event->mask & IN_ATTRIB)));
430  }
431  }
432 
433  if (!rescan_timer.isActive())
434  rescan_timer.start(m_PollInterval); // singleshot
435 
436  break;
437  }
438  }
439  }
440  if (bytesAvailable > 0) {
441  // copy partial event to beginning of buffer
442  memmove(buf, &buf[offsetCurrent], bytesAvailable);
443  offsetStartRead = bytesAvailable;
444  }
445  }
446 #endif
447 }
448 
449 /* In FAM mode, only entries which are marked dirty are scanned.
450  * We first need to mark all yet nonexistent, but possible created
451  * entries as dirty...
452  */
453 void KDirWatchPrivate::Entry::propagate_dirty()
454 {
455  foreach(Entry *sub_entry, m_entries)
456  {
457  if (!sub_entry->dirty)
458  {
459  sub_entry->dirty = true;
460  sub_entry->propagate_dirty();
461  }
462  }
463 }
464 
465 
466 /* A KDirWatch instance is interested in getting events for
467  * this file/Dir entry.
468  */
469 void KDirWatchPrivate::Entry::addClient(KDirWatch* instance,
470  KDirWatch::WatchModes watchModes)
471 {
472  if (instance == 0)
473  return;
474 
475  foreach(Client* client, m_clients) {
476  if (client->instance == instance) {
477  client->count++;
478  client->m_watchModes = watchModes;
479  return;
480  }
481  }
482 
483  Client* client = new Client;
484  client->instance = instance;
485  client->count = 1;
486  client->watchingStopped = instance->isStopped();
487  client->pending = NoChange;
488  client->m_watchModes = watchModes;
489 
490  m_clients.append(client);
491 }
492 
493 void KDirWatchPrivate::Entry::removeClient(KDirWatch* instance)
494 {
495  QList<Client *>::iterator it = m_clients.begin();
496  const QList<Client *>::iterator end = m_clients.end();
497  for ( ; it != end ; ++it ) {
498  Client* client = *it;
499  if (client->instance == instance) {
500  client->count--;
501  if (client->count == 0) {
502  m_clients.erase(it);
503  delete client;
504  }
505  return;
506  }
507  }
508 }
509 
510 /* get number of clients */
511 int KDirWatchPrivate::Entry::clientCount() const
512 {
513  int clients = 0;
514  foreach(Client* client, m_clients)
515  clients += client->count;
516 
517  return clients;
518 }
519 
520 QString KDirWatchPrivate::Entry::parentDirectory() const
521 {
522  return QDir::cleanPath(path + QLatin1String("/.."));
523 }
524 
525 QList<KDirWatchPrivate::Client *> KDirWatchPrivate::Entry::clientsForFileOrDir(const QString& tpath, bool* isDir) const
526 {
527  QList<Client *> ret;
528  KDE_struct_stat stat_buf;
529  if (KDE::stat(tpath, &stat_buf) == 0) {
530  *isDir = S_ISDIR(stat_buf.st_mode);
531  const KDirWatch::WatchModes flag =
532  *isDir ? KDirWatch::WatchSubDirs : KDirWatch::WatchFiles;
533  Q_FOREACH(Client *client, this->m_clients) {
534  if (client->m_watchModes & flag) {
535  ret.append(client);
536  }
537  }
538  } else {
539  // Happens frequently, e.g. ERROR: couldn't stat "/home/dfaure/.viminfo.tmp"
540  //kDebug(7001) << "ERROR: couldn't stat" << tpath;
541  }
542  // If KDE_stat fails then isDir is not set, but ret is empty anyway
543  // so isDir won't be used.
544  return ret;
545 }
546 
547 // inotify specific function that doesn't call KDE::stat to figure out if we have a file or folder.
548 // isDir is determined through inotify's "IN_ISDIR" flag in KDirWatchPrivate::inotifyEventReceived
549 QList<KDirWatchPrivate::Client *> KDirWatchPrivate::Entry::inotifyClientsForFileOrDir(bool isDir) const
550 {
551  QList<Client *> ret;
552  const KDirWatch::WatchModes flag = isDir ? KDirWatch::WatchSubDirs : KDirWatch::WatchFiles;
553  Q_FOREACH(Client *client, this->m_clients) {
554  if (client->m_watchModes & flag) {
555  ret.append(client);
556  }
557  }
558  return ret;
559 }
560 
561 QDebug operator<<(QDebug debug, const KDirWatchPrivate::Entry &entry)
562 {
563  debug.nospace() << "[ Entry for " << entry.path << ", " << (entry.isDir ? "dir" : "file");
564  if (entry.m_status == KDirWatchPrivate::NonExistent)
565  debug << ", non-existent";
566  debug << ", using " << ((entry.m_mode == KDirWatchPrivate::FAMMode) ? "FAM" :
567  (entry.m_mode == KDirWatchPrivate::INotifyMode) ? "INotify" :
568  (entry.m_mode == KDirWatchPrivate::DNotifyMode) ? "DNotify" :
569  (entry.m_mode == KDirWatchPrivate::QFSWatchMode) ? "QFSWatch" :
570  (entry.m_mode == KDirWatchPrivate::StatMode) ? "Stat" : "Unknown Method");
571 #ifdef HAVE_SYS_INOTIFY_H
572  if (entry.m_mode == KDirWatchPrivate::INotifyMode)
573  debug << " inotify_wd=" << entry.wd;
574 #endif
575  debug << ", has " << entry.m_clients.count() << " clients";
576  debug.space();
577  if (!entry.m_entries.isEmpty()) {
578  debug << ", nonexistent subentries:";
579  Q_FOREACH(KDirWatchPrivate::Entry* subEntry, entry.m_entries)
580  debug << subEntry << subEntry->path;
581  }
582  debug << ']';
583  return debug;
584 }
585 
586 KDirWatchPrivate::Entry* KDirWatchPrivate::entry(const QString& _path)
587 {
588 // we only support absolute paths
589  if (_path.isEmpty() || QDir::isRelativePath(_path)) {
590  return 0;
591  }
592 
593  QString path (_path);
594 
595  if ( path.length() > 1 && path.endsWith( QLatin1Char( '/' ) ) )
596  path.truncate( path.length() - 1 );
597 
598  EntryMap::Iterator it = m_mapEntries.find( path );
599  if ( it == m_mapEntries.end() )
600  return 0;
601  else
602  return &(*it);
603 }
604 
605 // set polling frequency for a entry and adjust global freq if needed
606 void KDirWatchPrivate::useFreq(Entry* e, int newFreq)
607 {
608  e->freq = newFreq;
609 
610  // a reasonable frequency for the global polling timer
611  if (e->freq < freq) {
612  freq = e->freq;
613  if (timer.isActive()) timer.start(freq);
614  kDebug(7001) << "Global Poll Freq is now" << freq << "msec";
615  }
616 }
617 
618 
619 #if defined(HAVE_FAM)
620 // setup FAM notification, returns false if not possible
621 bool KDirWatchPrivate::useFAM(Entry* e)
622 {
623  if (!use_fam) return false;
624 
625  // handle FAM events to avoid deadlock
626  // (FAM sends back all files in a directory when monitoring)
627  famEventReceived();
628 
629  e->m_mode = FAMMode;
630  e->dirty = false;
631 
632  if (e->isDir) {
633  if (e->m_status == NonExistent) {
634  // If the directory does not exist we watch the parent directory
635  addEntry(0, e->parentDirectory(), e, true);
636  }
637  else {
638  int res =FAMMonitorDirectory(&fc, QFile::encodeName(e->path),
639  &(e->fr), e);
640  if (res<0) {
641  e->m_mode = UnknownMode;
642  use_fam=false;
643  delete sn; sn = 0;
644  return false;
645  }
646  kDebug(7001).nospace() << " Setup FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
647  << ") for " << e->path;
648  }
649  }
650  else {
651  if (e->m_status == NonExistent) {
652  // If the file does not exist we watch the directory
653  addEntry(0, QFileInfo(e->path).absolutePath(), e, true);
654  }
655  else {
656  int res = FAMMonitorFile(&fc, QFile::encodeName(e->path),
657  &(e->fr), e);
658  if (res<0) {
659  e->m_mode = UnknownMode;
660  use_fam=false;
661  delete sn; sn = 0;
662  return false;
663  }
664 
665  kDebug(7001).nospace() << " Setup FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
666  << ") for " << e->path;
667  }
668  }
669 
670  // handle FAM events to avoid deadlock
671  // (FAM sends back all files in a directory when monitoring)
672  famEventReceived();
673 
674  return true;
675 }
676 #endif
677 
678 #ifdef HAVE_SYS_INOTIFY_H
679 // setup INotify notification, returns false if not possible
680 bool KDirWatchPrivate::useINotify( Entry* e )
681 {
682  //kDebug (7001) << "trying to use inotify for monitoring";
683 
684  e->wd = -1;
685  e->dirty = false;
686 
687  if (!supports_inotify) return false;
688 
689  e->m_mode = INotifyMode;
690 
691  if ( e->m_status == NonExistent ) {
692  addEntry(0, e->parentDirectory(), e, true);
693  return true;
694  }
695 
696  // May as well register for almost everything - it's free!
697  int mask = IN_DELETE|IN_DELETE_SELF|IN_CREATE|IN_MOVE|IN_MOVE_SELF|IN_DONT_FOLLOW|IN_MOVED_FROM|IN_MODIFY|IN_ATTRIB;
698 
699  if ( ( e->wd = inotify_add_watch( m_inotify_fd,
700  QFile::encodeName( e->path ), mask) ) >= 0)
701  {
702  if (s_verboseDebug) {
703  kDebug(7001) << "inotify successfully used for monitoring" << e->path << "wd=" << e->wd;
704  }
705  return true;
706  }
707 
708  kDebug(7001) << "inotify failed for monitoring" << e->path << ":" << strerror(errno);
709  return false;
710 }
711 #endif
712 #ifdef HAVE_QFILESYSTEMWATCHER
713 bool KDirWatchPrivate::useQFSWatch(Entry* e)
714 {
715  e->m_mode = QFSWatchMode;
716  e->dirty = false;
717 
718  if ( e->m_status == NonExistent ) {
719  addEntry(0, e->parentDirectory(), e, true /*isDir*/);
720  return true;
721  }
722 
723  kDebug(7001) << "fsWatcher->addPath" << e->path;
724  if (!fsWatcher) {
725  fsWatcher = new KFileSystemWatcher();
726  connect(fsWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(fswEventReceived(QString)));
727  connect(fsWatcher, SIGNAL(fileChanged(QString)), this, SLOT(fswEventReceived(QString)));
728  }
729  fsWatcher->addPath( e->path );
730  return true;
731 }
732 #endif
733 
734 bool KDirWatchPrivate::useStat(Entry* e)
735 {
736  if (KFileSystemType::fileSystemType(e->path) == KFileSystemType::Nfs) // TODO: or Smbfs?
737  useFreq(e, m_nfsPollInterval);
738  else
739  useFreq(e, m_PollInterval);
740 
741  if (e->m_mode != StatMode) {
742  e->m_mode = StatMode;
743  statEntries++;
744 
745  if ( statEntries == 1 ) {
746  // if this was first STAT entry (=timer was stopped)
747  timer.start(freq); // then start the timer
748  kDebug(7001) << " Started Polling Timer, freq " << freq;
749  }
750  }
751 
752  kDebug(7001) << " Setup Stat (freq " << e->freq << ") for " << e->path;
753 
754  return true;
755 }
756 
757 
758 /* If <instance> !=0, this KDirWatch instance wants to watch at <_path>,
759  * providing in <isDir> the type of the entry to be watched.
760  * Sometimes, entries are dependant on each other: if <sub_entry> !=0,
761  * this entry needs another entry to watch himself (when notExistent).
762  */
763 void KDirWatchPrivate::addEntry(KDirWatch* instance, const QString& _path,
764  Entry* sub_entry, bool isDir, KDirWatch::WatchModes watchModes)
765 {
766  QString path (_path);
767  if (path.isEmpty()
768 #ifndef Q_WS_WIN
769  || path == QLatin1String("/dev")
770  || (path.startsWith(QLatin1String("/dev/")) && !path.startsWith(QLatin1String("/dev/.")))
771 #endif
772  )
773  return; // Don't even go there.
774 
775  if ( path.length() > 1 && path.endsWith( QLatin1Char( '/' ) ) )
776  path.truncate( path.length() - 1 );
777 
778  EntryMap::Iterator it = m_mapEntries.find( path );
779  if ( it != m_mapEntries.end() )
780  {
781  if (sub_entry) {
782  (*it).m_entries.append(sub_entry);
783  if (s_verboseDebug) {
784  kDebug(7001) << "Added already watched Entry" << path
785  << "(for" << sub_entry->path << ")";
786  }
787 #ifdef HAVE_SYS_INOTIFY_H
788  Entry* e = &(*it);
789  if( (e->m_mode == INotifyMode) && (e->wd >= 0) ) {
790  int mask = IN_DELETE|IN_DELETE_SELF|IN_CREATE|IN_MOVE|IN_MOVE_SELF|IN_DONT_FOLLOW;
791  if(!e->isDir)
792  mask |= IN_MODIFY|IN_ATTRIB;
793  else
794  mask |= IN_ONLYDIR;
795 
796  inotify_rm_watch (m_inotify_fd, e->wd);
797  e->wd = inotify_add_watch( m_inotify_fd, QFile::encodeName( e->path ),
798  mask);
799  //Q_ASSERT(e->wd >= 0); // fails in KDirListerTest::testDeleteCurrentDir
800  }
801 #endif
802  }
803  else {
804  (*it).addClient(instance, watchModes);
805  if (s_verboseDebug) {
806  kDebug(7001) << "Added already watched Entry" << path
807  << "(now" << (*it).clientCount() << "clients)"
808  << QString::fromLatin1("[%1]").arg(instance->objectName());
809  }
810  }
811  return;
812  }
813 
814  // we have a new path to watch
815 
816  KDE_struct_stat stat_buf;
817  bool exists = (KDE::stat(path, &stat_buf) == 0);
818 
819  EntryMap::iterator newIt = m_mapEntries.insert( path, Entry() );
820  // the insert does a copy, so we have to use <e> now
821  Entry* e = &(*newIt);
822 
823  if (exists) {
824  e->isDir = S_ISDIR(stat_buf.st_mode);
825 
826  if (e->isDir && !isDir) {
827  if (KDE::lstat(path, &stat_buf) == 0) {
828  if (S_ISLNK(stat_buf.st_mode))
829  // if it's a symlink, don't follow it
830  e->isDir = false;
831  else
832  qWarning() << "KDirWatch:" << path << "is a directory. Use addDir!";
833  }
834  } else if (!e->isDir && isDir)
835  qWarning("KDirWatch: %s is a file. Use addFile!", qPrintable(path));
836 
837  if (!e->isDir && ( watchModes != KDirWatch::WatchDirOnly)) {
838  qWarning() << "KDirWatch:" << path << "is a file. You can't use recursive or "
839  "watchFiles options";
840  watchModes = KDirWatch::WatchDirOnly;
841  }
842 
843 #ifdef Q_OS_WIN
844  // ctime is the 'creation time' on windows - use mtime instead
845  e->m_ctime = stat_buf.st_mtime;
846 #else
847  e->m_ctime = stat_buf.st_ctime;
848 #endif
849  e->m_status = Normal;
850  e->m_nlink = stat_buf.st_nlink;
851  e->m_ino = stat_buf.st_ino;
852  }
853  else {
854  e->isDir = isDir;
855  e->m_ctime = invalid_ctime;
856  e->m_status = NonExistent;
857  e->m_nlink = 0;
858  e->m_ino = 0;
859  }
860 
861  e->path = path;
862  if (sub_entry)
863  e->m_entries.append(sub_entry);
864  else
865  e->addClient(instance, watchModes);
866 
867  kDebug(7001).nospace() << "Added " << (e->isDir ? "Dir " : "File ") << path
868  << (e->m_status == NonExistent ? " NotExisting" : "")
869  << " for " << (sub_entry ? sub_entry->path : QString())
870  << " [" << (instance ? instance->objectName() : QString()) << "]";
871 
872  // now setup the notification method
873  e->m_mode = UnknownMode;
874  e->msecLeft = 0;
875 
876  if ( isNoisyFile( QFile::encodeName( path ) ) )
877  return;
878 
879  if (exists && e->isDir && (watchModes != KDirWatch::WatchDirOnly)) {
880  QFlags<QDir::Filter> filters = QDir::NoDotAndDotDot;
881 
882  if ((watchModes & KDirWatch::WatchSubDirs) &&
883  (watchModes & KDirWatch::WatchFiles)) {
884  filters |= (QDir::Dirs|QDir::Files);
885  } else if (watchModes & KDirWatch::WatchSubDirs) {
886  filters |= QDir::Dirs;
887  } else if (watchModes & KDirWatch::WatchFiles) {
888  filters |= QDir::Files;
889  }
890 
891 #if defined(HAVE_SYS_INOTIFY_H)
892  if (e->m_mode == INotifyMode || (e->m_mode == UnknownMode && m_preferredMethod == KDirWatch::INotify) )
893  {
894  //kDebug(7001) << "Ignoring WatchFiles directive - this is implicit with inotify";
895  // Placing a watch on individual files is redundant with inotify
896  // (inotify gives us WatchFiles functionality "for free") and indeed
897  // actively harmful, so prevent it. WatchSubDirs is necessary, though.
898  filters &= ~QDir::Files;
899  }
900 #endif
901 
902  QDir basedir (e->path);
903  const QFileInfoList contents = basedir.entryInfoList(filters);
904  for (QFileInfoList::const_iterator iter = contents.constBegin();
905  iter != contents.constEnd(); ++iter)
906  {
907  const QFileInfo &fileInfo = *iter;
908  // treat symlinks as files--don't follow them.
909  bool isDir = fileInfo.isDir() && !fileInfo.isSymLink();
910 
911  addEntry (instance, fileInfo.absoluteFilePath(), 0, isDir,
912  isDir ? watchModes : KDirWatch::WatchDirOnly);
913  }
914  }
915 
916  addWatch(e);
917 }
918 
919 void KDirWatchPrivate::addWatch(Entry* e)
920 {
921  // If the watch is on a network filesystem use the nfsPreferredMethod as the
922  // default, otherwise use preferredMethod as the default, if the methods are
923  // the same we can skip the mountpoint check
924 
925  // This allows to configure a different method for NFS mounts, since inotify
926  // cannot detect changes made by other machines. However as a default inotify
927  // is fine, since the most common case is a NFS-mounted home, where all changes
928  // are made locally. #177892.
929  KDirWatch::Method preferredMethod = m_preferredMethod;
930  if (m_nfsPreferredMethod != m_preferredMethod) {
931  if (KFileSystemType::fileSystemType(e->path) == KFileSystemType::Nfs) {
932  preferredMethod = m_nfsPreferredMethod;
933  }
934  }
935 
936  // Try the appropriate preferred method from the config first
937  bool entryAdded = false;
938  switch (preferredMethod) {
939 #if defined(HAVE_FAM)
940  case KDirWatch::FAM: entryAdded = useFAM(e); break;
941 #endif
942 #if defined(HAVE_SYS_INOTIFY_H)
943  case KDirWatch::INotify: entryAdded = useINotify(e); break;
944 #endif
945 #if defined(HAVE_QFILESYSTEMWATCHER)
946  case KDirWatch::QFSWatch: entryAdded = useQFSWatch(e); break;
947 #endif
948  case KDirWatch::Stat: entryAdded = useStat(e); break;
949  default: break;
950  }
951 
952  // Failing that try in order INotify, FAM, QFSWatch, Stat
953  if (!entryAdded) {
954 #if defined(HAVE_SYS_INOTIFY_H)
955  if (useINotify(e)) return;
956 #endif
957 #if defined(HAVE_FAM)
958  if (useFAM(e)) return;
959 #endif
960 #if defined(HAVE_QFILESYSTEMWATCHER)
961  if (useQFSWatch(e)) return;
962 #endif
963  useStat(e);
964  }
965 }
966 
967 void KDirWatchPrivate::removeWatch(Entry* e)
968 {
969 #ifdef HAVE_FAM
970  if (e->m_mode == FAMMode) {
971  FAMCancelMonitor(&fc, &(e->fr) );
972  kDebug(7001).nospace() << "Cancelled FAM (Req " << FAMREQUEST_GETREQNUM(&(e->fr))
973  << ") for " << e->path;
974  }
975 #endif
976 #ifdef HAVE_SYS_INOTIFY_H
977  if (e->m_mode == INotifyMode) {
978  (void) inotify_rm_watch( m_inotify_fd, e->wd );
979  if (s_verboseDebug) {
980  kDebug(7001).nospace() << "Cancelled INotify (fd " << m_inotify_fd << ", "
981  << e->wd << ") for " << e->path;
982  }
983  }
984 #endif
985 #ifdef HAVE_QFILESYSTEMWATCHER
986  if (e->m_mode == QFSWatchMode && fsWatcher) {
987  if (s_verboseDebug)
988  kDebug(7001) << "fsWatcher->removePath" << e->path;
989  fsWatcher->removePath(e->path);
990  }
991 #endif
992 }
993 
994 bool KDirWatchPrivate::removeEntry(KDirWatch* instance,
995  const QString& _path,
996  Entry* sub_entry)
997 {
998  if (s_verboseDebug) {
999  kDebug(7001) << "path=" << _path << "sub_entry:" << sub_entry;
1000  }
1001  Entry* e = entry(_path);
1002  if (!e) {
1003  return false;
1004  }
1005 
1006  removeEntry(instance, e, sub_entry);
1007  return true;
1008 }
1009 
1010 void KDirWatchPrivate::removeEntry(KDirWatch* instance,
1011  Entry* e,
1012  Entry* sub_entry)
1013 {
1014  removeList.remove(e);
1015 
1016  if (sub_entry)
1017  e->m_entries.removeAll(sub_entry);
1018  else
1019  e->removeClient(instance);
1020 
1021  if (e->m_clients.count() || e->m_entries.count())
1022  return;
1023 
1024  if (delayRemove) {
1025  removeList.insert(e);
1026  // now e->isValid() is false
1027  return;
1028  }
1029 
1030  if ( e->m_status == Normal) {
1031  removeWatch(e);
1032  } else {
1033  // Removed a NonExistent entry - we just remove it from the parent
1034  if (e->isDir)
1035  removeEntry(0, e->parentDirectory(), e);
1036  else
1037  removeEntry(0, QFileInfo(e->path).absolutePath(), e);
1038  }
1039 
1040  if (e->m_mode == StatMode) {
1041  statEntries--;
1042  if ( statEntries == 0 ) {
1043  timer.stop(); // stop timer if lists are empty
1044  kDebug(7001) << " Stopped Polling Timer";
1045  }
1046  }
1047 
1048  if (s_verboseDebug) {
1049  kDebug(7001).nospace() << "Removed " << (e->isDir ? "Dir ":"File ") << e->path
1050  << " for " << (sub_entry ? sub_entry->path : QString())
1051  << " [" << (instance ? instance->objectName() : QString()) << "]";
1052  }
1053  m_mapEntries.remove( e->path ); // <e> not valid any more
1054 }
1055 
1056 
1057 /* Called from KDirWatch destructor:
1058  * remove <instance> as client from all entries
1059  */
1060 void KDirWatchPrivate::removeEntries( KDirWatch* instance )
1061 {
1062  int minfreq = 3600000;
1063 
1064  QStringList pathList;
1065  // put all entries where instance is a client in list
1066  EntryMap::Iterator it = m_mapEntries.begin();
1067  for( ; it != m_mapEntries.end(); ++it ) {
1068  Client* c = 0;
1069  foreach(Client* client, (*it).m_clients) {
1070  if (client->instance == instance) {
1071  c = client;
1072  break;
1073  }
1074  }
1075  if (c) {
1076  c->count = 1; // forces deletion of instance as client
1077  pathList.append((*it).path);
1078  }
1079  else if ( (*it).m_mode == StatMode && (*it).freq < minfreq )
1080  minfreq = (*it).freq;
1081  }
1082 
1083  foreach(const QString &path, pathList)
1084  removeEntry(instance, path, 0);
1085 
1086  if (minfreq > freq) {
1087  // we can decrease the global polling frequency
1088  freq = minfreq;
1089  if (timer.isActive()) timer.start(freq);
1090  kDebug(7001) << "Poll Freq now" << freq << "msec";
1091  }
1092 }
1093 
1094 // instance ==0: stop scanning for all instances
1095 bool KDirWatchPrivate::stopEntryScan( KDirWatch* instance, Entry* e)
1096 {
1097  int stillWatching = 0;
1098  foreach(Client* client, e->m_clients) {
1099  if (!instance || instance == client->instance)
1100  client->watchingStopped = true;
1101  else if (!client->watchingStopped)
1102  stillWatching += client->count;
1103  }
1104 
1105  kDebug(7001) << (instance ? instance->objectName() : QString::fromLatin1("all"))
1106  << "stopped scanning" << e->path << "(now"
1107  << stillWatching << "watchers)";
1108 
1109  if (stillWatching == 0) {
1110  // if nobody is interested, we don't watch
1111  if ( e->m_mode != INotifyMode ) {
1112  e->m_ctime = invalid_ctime; // invalid
1113  e->m_status = NonExistent;
1114  }
1115  // e->m_status = Normal;
1116  }
1117  return true;
1118 }
1119 
1120 // instance ==0: start scanning for all instances
1121 bool KDirWatchPrivate::restartEntryScan( KDirWatch* instance, Entry* e,
1122  bool notify)
1123 {
1124  int wasWatching = 0, newWatching = 0;
1125  foreach(Client* client, e->m_clients) {
1126  if (!client->watchingStopped)
1127  wasWatching += client->count;
1128  else if (!instance || instance == client->instance) {
1129  client->watchingStopped = false;
1130  newWatching += client->count;
1131  }
1132  }
1133  if (newWatching == 0)
1134  return false;
1135 
1136  kDebug(7001) << (instance ? instance->objectName() : QString::fromLatin1("all"))
1137  << "restarted scanning" << e->path
1138  << "(now" << wasWatching+newWatching << "watchers)";
1139 
1140  // restart watching and emit pending events
1141 
1142  int ev = NoChange;
1143  if (wasWatching == 0) {
1144  if (!notify) {
1145  KDE_struct_stat stat_buf;
1146  bool exists = (KDE::stat(e->path, &stat_buf) == 0);
1147  if (exists) {
1148 #ifdef Q_OS_WIN
1149  // ctime is the 'creation time' on windows - use mtime instead
1150  e->m_ctime = stat_buf.st_mtime;
1151 #else
1152  e->m_ctime = stat_buf.st_ctime;
1153 #endif
1154  e->m_status = Normal;
1155  if (s_verboseDebug) {
1156  kDebug(7001) << "Setting status to Normal for" << e << e->path;
1157  }
1158  e->m_nlink = stat_buf.st_nlink;
1159  e->m_ino = stat_buf.st_ino;
1160 
1161  // Same as in scanEntry: ensure no subentry in parent dir
1162  removeEntry(0, e->parentDirectory(), e);
1163  }
1164  else {
1165  e->m_ctime = invalid_ctime;
1166  e->m_status = NonExistent;
1167  e->m_nlink = 0;
1168  if (s_verboseDebug) {
1169  kDebug(7001) << "Setting status to NonExistent for" << e << e->path;
1170  }
1171  }
1172  }
1173  e->msecLeft = 0;
1174  ev = scanEntry(e);
1175  }
1176  emitEvent(e,ev);
1177 
1178  return true;
1179 }
1180 
1181 // instance ==0: stop scanning for all instances
1182 void KDirWatchPrivate::stopScan(KDirWatch* instance)
1183 {
1184  EntryMap::Iterator it = m_mapEntries.begin();
1185  for( ; it != m_mapEntries.end(); ++it )
1186  stopEntryScan(instance, &(*it));
1187 }
1188 
1189 
1190 void KDirWatchPrivate::startScan(KDirWatch* instance,
1191  bool notify, bool skippedToo )
1192 {
1193  if (!notify)
1194  resetList(instance,skippedToo);
1195 
1196  EntryMap::Iterator it = m_mapEntries.begin();
1197  for( ; it != m_mapEntries.end(); ++it )
1198  restartEntryScan(instance, &(*it), notify);
1199 
1200  // timer should still be running when in polling mode
1201 }
1202 
1203 
1204 // clear all pending events, also from stopped
1205 void KDirWatchPrivate::resetList( KDirWatch* /*instance*/, bool skippedToo )
1206 {
1207  EntryMap::Iterator it = m_mapEntries.begin();
1208  for( ; it != m_mapEntries.end(); ++it ) {
1209 
1210  foreach(Client* client, (*it).m_clients) {
1211  if (!client->watchingStopped || skippedToo)
1212  client->pending = NoChange;
1213  }
1214  }
1215 }
1216 
1217 // Return event happened on <e>
1218 //
1219 int KDirWatchPrivate::scanEntry(Entry* e)
1220 {
1221  // Shouldn't happen: Ignore "unknown" notification method
1222  if (e->m_mode == UnknownMode) return NoChange;
1223 
1224  if (e->m_mode == FAMMode || e->m_mode == INotifyMode) {
1225  // we know nothing has changed, no need to stat
1226  if(!e->dirty) return NoChange;
1227  e->dirty = false;
1228  }
1229 
1230  if (e->m_mode == StatMode) {
1231  // only scan if timeout on entry timer happens;
1232  // e.g. when using 500msec global timer, a entry
1233  // with freq=5000 is only watched every 10th time
1234 
1235  e->msecLeft -= freq;
1236  if (e->msecLeft>0) return NoChange;
1237  e->msecLeft += e->freq;
1238  }
1239 
1240  KDE_struct_stat stat_buf;
1241  const bool exists = (KDE::stat(e->path, &stat_buf) == 0);
1242  if (exists) {
1243 
1244  if (e->m_status == NonExistent) {
1245  // ctime is the 'creation time' on windows, but with qMax
1246  // we get the latest change of any kind, on any platform.
1247  e->m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
1248  e->m_status = Normal;
1249  e->m_ino = stat_buf.st_ino;
1250  if (s_verboseDebug) {
1251  kDebug(7001) << "Setting status to Normal for just created" << e << e->path;
1252  }
1253  // We need to make sure the entry isn't listed in its parent's subentries... (#222974, testMoveTo)
1254  removeEntry(0, e->parentDirectory(), e);
1255 
1256  return Created;
1257  }
1258 
1259 #if 1 // for debugging the if() below
1260  if (s_verboseDebug) {
1261  struct tm* tmp = localtime(&e->m_ctime);
1262  char outstr[200];
1263  strftime(outstr, sizeof(outstr), "%T", tmp);
1264  kDebug(7001) << "e->m_ctime=" << e->m_ctime << outstr
1265  << "stat_buf.st_ctime=" << stat_buf.st_ctime
1266  << "e->m_nlink=" << e->m_nlink
1267  << "stat_buf.st_nlink=" << stat_buf.st_nlink
1268  << "e->m_ino=" << e->m_ino
1269  << "stat_buf.st_ino=" << stat_buf.st_ino;
1270  }
1271 #endif
1272 
1273  if ( ((e->m_ctime != invalid_ctime) &&
1274  (qMax(stat_buf.st_ctime, stat_buf.st_mtime) != e->m_ctime ||
1275  stat_buf.st_ino != e->m_ino ||
1276  stat_buf.st_nlink != nlink_t(e->m_nlink)))
1277  // we trust QFSW to get it right, the ctime comparisons above
1278  // fail for example when adding files to directories on Windows
1279  // which doesn't change the mtime of the directory
1280  || e->m_mode == QFSWatchMode
1281  ) {
1282  e->m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
1283  e->m_nlink = stat_buf.st_nlink;
1284  if (e->m_ino != stat_buf.st_ino) {
1285  // The file got deleted and recreated. We need to watch it again.
1286  removeWatch(e);
1287  addWatch(e);
1288  }
1289  e->m_ino = stat_buf.st_ino;
1290  return Changed;
1291  }
1292 
1293  return NoChange;
1294  }
1295 
1296  // dir/file doesn't exist
1297 
1298  e->m_nlink = 0;
1299  e->m_ino = 0;
1300  e->m_status = NonExistent;
1301 
1302  if (e->m_ctime == invalid_ctime) {
1303  return NoChange;
1304  }
1305 
1306  e->m_ctime = invalid_ctime;
1307  return Deleted;
1308 }
1309 
1310 /* Notify all interested KDirWatch instances about a given event on an entry
1311  * and stored pending events. When watching is stopped, the event is
1312  * added to the pending events.
1313  */
1314 void KDirWatchPrivate::emitEvent(const Entry* e, int event, const QString &fileName)
1315 {
1316  QString path (e->path);
1317  if (!fileName.isEmpty()) {
1318  if (!QDir::isRelativePath(fileName))
1319  path = fileName;
1320  else {
1321 #ifdef Q_OS_UNIX
1322  path += QLatin1Char('/') + fileName;
1323 #elif defined(Q_WS_WIN)
1324  //current drive is passed instead of /
1325  path += QDir::currentPath().left(2) + QLatin1Char('/') + fileName;
1326 #endif
1327  }
1328  }
1329 
1330  if (s_verboseDebug) {
1331  kDebug(7001) << event << path << e->m_clients.count() << "clients";
1332  }
1333 
1334  foreach(Client* c, e->m_clients)
1335  {
1336  if (c->instance==0 || c->count==0) continue;
1337 
1338  if (c->watchingStopped) {
1339  // add event to pending...
1340  if (event == Changed)
1341  c->pending |= event;
1342  else if (event == Created || event == Deleted)
1343  c->pending = event;
1344  continue;
1345  }
1346  // not stopped
1347  if (event == NoChange || event == Changed)
1348  event |= c->pending;
1349  c->pending = NoChange;
1350  if (event == NoChange) continue;
1351 
1352  // Emit the signals delayed, to avoid unexpected re-entrancy from the slots (#220153)
1353 
1354  if (event & Deleted) {
1355  QMetaObject::invokeMethod(c->instance, "setDeleted", Qt::QueuedConnection, Q_ARG(QString, path));
1356  // emit only Deleted event...
1357  continue;
1358  }
1359 
1360  if (event & Created) {
1361  QMetaObject::invokeMethod(c->instance, "setCreated", Qt::QueuedConnection, Q_ARG(QString, path));
1362  // possible emit Change event after creation
1363  }
1364 
1365  if (event & Changed) {
1366  QMetaObject::invokeMethod(c->instance, "setDirty", Qt::QueuedConnection, Q_ARG(QString, path));
1367  }
1368  }
1369 }
1370 
1371 // Remove entries which were marked to be removed
1372 void KDirWatchPrivate::slotRemoveDelayed()
1373 {
1374  delayRemove = false;
1375  // Removing an entry could also take care of removing its parent
1376  // (e.g. in FAM or inotify mode), which would remove other entries in removeList,
1377  // so don't use foreach or iterators here...
1378  while (!removeList.isEmpty()) {
1379  Entry* entry = *removeList.begin();
1380  removeEntry(0, entry, 0); // this will remove entry from removeList
1381  }
1382 }
1383 
1384 /* Scan all entries to be watched for changes. This is done regularly
1385  * when polling. FAM and inotify use a single-shot timer to call this slot delayed.
1386  */
1387 void KDirWatchPrivate::slotRescan()
1388 {
1389  if (s_verboseDebug)
1390  kDebug(7001);
1391 
1392  EntryMap::Iterator it;
1393 
1394  // People can do very long things in the slot connected to dirty(),
1395  // like showing a message box. We don't want to keep polling during
1396  // that time, otherwise the value of 'delayRemove' will be reset.
1397  // ### TODO: now the emitEvent delays emission, this can be cleaned up
1398  bool timerRunning = timer.isActive();
1399  if ( timerRunning )
1400  timer.stop();
1401 
1402  // We delay deletions of entries this way.
1403  // removeDir(), when called in slotDirty(), can cause a crash otherwise
1404  // ### TODO: now the emitEvent delays emission, this can be cleaned up
1405  delayRemove = true;
1406 
1407  if (rescan_all)
1408  {
1409  // mark all as dirty
1410  it = m_mapEntries.begin();
1411  for( ; it != m_mapEntries.end(); ++it )
1412  (*it).dirty = true;
1413  rescan_all = false;
1414  }
1415  else
1416  {
1417  // progate dirty flag to dependant entries (e.g. file watches)
1418  it = m_mapEntries.begin();
1419  for( ; it != m_mapEntries.end(); ++it )
1420  if (((*it).m_mode == INotifyMode || (*it).m_mode == QFSWatchMode) && (*it).dirty )
1421  (*it).propagate_dirty();
1422  }
1423 
1424 #ifdef HAVE_SYS_INOTIFY_H
1425  QList<Entry*> cList;
1426 #endif
1427 
1428  it = m_mapEntries.begin();
1429  for( ; it != m_mapEntries.end(); ++it ) {
1430  // we don't check invalid entries (i.e. remove delayed)
1431  Entry* entry = &(*it);
1432  if (!entry->isValid()) continue;
1433 
1434  const int ev = scanEntry(entry);
1435  if (s_verboseDebug)
1436  kDebug(7001) << "scanEntry for" << entry->path << "says" << ev;
1437 
1438  switch(entry->m_mode) {
1439 #ifdef HAVE_SYS_INOTIFY_H
1440  case INotifyMode:
1441  if ( ev == Deleted ) {
1442  if (s_verboseDebug)
1443  kDebug(7001) << "scanEntry says" << entry->path << "was deleted";
1444  addEntry(0, entry->parentDirectory(), entry, true);
1445  } else if (ev == Created) {
1446  if (s_verboseDebug)
1447  kDebug(7001) << "scanEntry says" << entry->path << "was created. wd=" << entry->wd;
1448  if (entry->wd < 0) {
1449  cList.append(entry);
1450  addWatch(entry);
1451  }
1452  }
1453  break;
1454 #endif
1455  case FAMMode:
1456  case QFSWatchMode:
1457  if (ev == Created) {
1458  addWatch(entry);
1459  }
1460  break;
1461  default:
1462  // dunno about StatMode...
1463  break;
1464  }
1465 
1466 #ifdef HAVE_SYS_INOTIFY_H
1467  if (entry->isDir) {
1468  // Report and clear the the list of files that have changed in this directory.
1469  // Remove duplicates by changing to set and back again:
1470  // we don't really care about preserving the order of the
1471  // original changes.
1472  QStringList pendingFileChanges = entry->m_pendingFileChanges;
1473  pendingFileChanges.removeDuplicates();
1474  Q_FOREACH(const QString &changedFilename, pendingFileChanges) {
1475  if (s_verboseDebug) {
1476  kDebug(7001) << "processing pending file change for" << changedFilename;
1477  }
1478  emitEvent(entry, Changed, changedFilename);
1479  }
1480  entry->m_pendingFileChanges.clear();
1481  }
1482 #endif
1483 
1484  if ( ev != NoChange ) {
1485  emitEvent(entry, ev);
1486  }
1487  }
1488 
1489  if ( timerRunning )
1490  timer.start(freq);
1491 
1492 #ifdef HAVE_SYS_INOTIFY_H
1493  // Remove watch of parent of new created directories
1494  Q_FOREACH(Entry* e, cList)
1495  removeEntry(0, e->parentDirectory(), e);
1496 #endif
1497 
1498  QTimer::singleShot(0, this, SLOT(slotRemoveDelayed()));
1499 }
1500 
1501 bool KDirWatchPrivate::isNoisyFile( const char * filename )
1502 {
1503  // $HOME/.X.err grows with debug output, so don't notify change
1504  if ( *filename == '.') {
1505  if (strncmp(filename, ".X.err", 6) == 0) return true;
1506  if (strncmp(filename, ".xsession-errors", 16) == 0) return true;
1507  // fontconfig updates the cache on every KDE app start
1508  // (inclusive kio_thumbnail slaves)
1509  if (strncmp(filename, ".fonts.cache", 12) == 0) return true;
1510  }
1511 
1512  return false;
1513 }
1514 
1515 #ifdef HAVE_FAM
1516 void KDirWatchPrivate::famEventReceived()
1517 {
1518  static FAMEvent fe;
1519 
1520  delayRemove = true;
1521 
1522  //kDebug(7001) << "Fam event received";
1523 
1524  while(use_fam && FAMPending(&fc)) {
1525  if (FAMNextEvent(&fc, &fe) == -1) {
1526  kWarning(7001) << "FAM connection problem, switching to polling.";
1527  use_fam = false;
1528  delete sn; sn = 0;
1529 
1530  // Replace all FAMMode entries with INotify/Stat
1531  EntryMap::Iterator it = m_mapEntries.begin();
1532  for( ; it != m_mapEntries.end(); ++it )
1533  if ((*it).m_mode == FAMMode && (*it).m_clients.count()>0) {
1534  Entry* e = &(*it);
1535  addWatch(e);
1536  }
1537  }
1538  else
1539  checkFAMEvent(&fe);
1540  }
1541 
1542  QTimer::singleShot(0, this, SLOT(slotRemoveDelayed()));
1543 }
1544 
1545 void KDirWatchPrivate::checkFAMEvent(FAMEvent* fe)
1546 {
1547  //kDebug(7001);
1548 
1549  // Don't be too verbose ;-)
1550  if ((fe->code == FAMExists) ||
1551  (fe->code == FAMEndExist) ||
1552  (fe->code == FAMAcknowledge)) return;
1553 
1554  if ( isNoisyFile( fe->filename ) )
1555  return;
1556 
1557  Entry* e = 0;
1558  EntryMap::Iterator it = m_mapEntries.begin();
1559  for( ; it != m_mapEntries.end(); ++it )
1560  if (FAMREQUEST_GETREQNUM(&( (*it).fr )) ==
1561  FAMREQUEST_GETREQNUM(&(fe->fr)) ) {
1562  e = &(*it);
1563  break;
1564  }
1565 
1566  // Entry* e = static_cast<Entry*>(fe->userdata);
1567 
1568  if (s_verboseDebug) { // don't enable this except when debugging, see #88538
1569  kDebug(7001) << "Processing FAM event ("
1570  << ((fe->code == FAMChanged) ? "FAMChanged" :
1571  (fe->code == FAMDeleted) ? "FAMDeleted" :
1572  (fe->code == FAMStartExecuting) ? "FAMStartExecuting" :
1573  (fe->code == FAMStopExecuting) ? "FAMStopExecuting" :
1574  (fe->code == FAMCreated) ? "FAMCreated" :
1575  (fe->code == FAMMoved) ? "FAMMoved" :
1576  (fe->code == FAMAcknowledge) ? "FAMAcknowledge" :
1577  (fe->code == FAMExists) ? "FAMExists" :
1578  (fe->code == FAMEndExist) ? "FAMEndExist" : "Unknown Code")
1579  << ", " << fe->filename
1580  << ", Req " << FAMREQUEST_GETREQNUM(&(fe->fr)) << ") e=" << e;
1581  }
1582 
1583  if (!e) {
1584  // this happens e.g. for FAMAcknowledge after deleting a dir...
1585  // kDebug(7001) << "No entry for FAM event ?!";
1586  return;
1587  }
1588 
1589  if (e->m_status == NonExistent) {
1590  kDebug(7001) << "FAM event for nonExistent entry " << e->path;
1591  return;
1592  }
1593 
1594  // Delayed handling. This rechecks changes with own stat calls.
1595  e->dirty = true;
1596  if (!rescan_timer.isActive())
1597  rescan_timer.start(m_PollInterval); // singleshot
1598 
1599  // needed FAM control actions on FAM events
1600  switch (fe->code) {
1601  case FAMDeleted:
1602  // fe->filename is an absolute path when a watched file-or-dir is deleted
1603  if (!QDir::isRelativePath(QFile::decodeName(fe->filename))) {
1604  FAMCancelMonitor(&fc, &(e->fr) ); // needed ?
1605  kDebug(7001) << "Cancelled FAMReq"
1606  << FAMREQUEST_GETREQNUM(&(e->fr))
1607  << "for" << e->path;
1608  e->m_status = NonExistent;
1609  e->m_ctime = invalid_ctime;
1610  emitEvent(e, Deleted, e->path);
1611  // If the parent dir was already watched, tell it something changed
1612  Entry* parentEntry = entry(e->parentDirectory());
1613  if (parentEntry)
1614  parentEntry->dirty = true;
1615  // Add entry to parent dir to notice if the entry gets recreated
1616  addEntry(0, e->parentDirectory(), e, true /*isDir*/);
1617  } else {
1618  // A file in this directory has been removed, and wasn't explicitly watched.
1619  // We could still inform clients, like inotify does? But stat can't.
1620  // For now we just marked e dirty and slotRescan will emit the dir as dirty.
1621  //kDebug(7001) << "Got FAMDeleted for" << QFile::decodeName(fe->filename) << "in" << e->path << ". Absolute path -> NOOP!";
1622  }
1623  break;
1624 
1625  case FAMCreated: {
1626  // check for creation of a directory we have to watch
1627  QString tpath(e->path + QLatin1Char('/') + QFile::decodeName(fe->filename));
1628 
1629  // This code is very similar to the one in inotifyEventReceived...
1630  Entry* sub_entry = e->findSubEntry(tpath);
1631  if (sub_entry /*&& sub_entry->isDir*/) {
1632  // We were waiting for this new file/dir to be created
1633  emitEvent(sub_entry, Created);
1634  sub_entry->dirty = true;
1635  rescan_timer.start(0); // process this asap, to start watching that dir
1636  } else if (e->isDir && !e->m_clients.empty()) {
1637  bool isDir = false;
1638  const QList<Client *> clients = e->clientsForFileOrDir(tpath, &isDir);
1639  Q_FOREACH(Client *client, clients) {
1640  addEntry (client->instance, tpath, 0, isDir,
1641  isDir ? client->m_watchModes : KDirWatch::WatchDirOnly);
1642  }
1643 
1644  if (!clients.isEmpty()) {
1645  emitEvent(e, Created, tpath);
1646 
1647  kDebug(7001).nospace() << clients.count() << " instance(s) monitoring the new "
1648  << (isDir ? "dir " : "file ") << tpath;
1649  }
1650  }
1651  }
1652  break;
1653  default:
1654  break;
1655  }
1656 }
1657 #else
1658 void KDirWatchPrivate::famEventReceived()
1659 {
1660  kWarning (7001) << "Fam event received but FAM is not supported";
1661 }
1662 #endif
1663 
1664 
1665 void KDirWatchPrivate::statistics()
1666 {
1667  EntryMap::Iterator it;
1668 
1669  kDebug(7001) << "Entries watched:";
1670  if (m_mapEntries.count()==0) {
1671  kDebug(7001) << " None.";
1672  }
1673  else {
1674  it = m_mapEntries.begin();
1675  for( ; it != m_mapEntries.end(); ++it ) {
1676  Entry* e = &(*it);
1677  kDebug(7001) << " " << *e;
1678 
1679  foreach(Client* c, e->m_clients) {
1680  QByteArray pending;
1681  if (c->watchingStopped) {
1682  if (c->pending & Deleted) pending += "deleted ";
1683  if (c->pending & Created) pending += "created ";
1684  if (c->pending & Changed) pending += "changed ";
1685  if (!pending.isEmpty()) pending = " (pending: " + pending + ')';
1686  pending = ", stopped" + pending;
1687  }
1688  kDebug(7001) << " by " << c->instance->objectName()
1689  << " (" << c->count << " times)" << pending;
1690  }
1691  if (e->m_entries.count()>0) {
1692  kDebug(7001) << " dependent entries:";
1693  foreach(Entry *d, e->m_entries) {
1694  kDebug(7001) << " " << d << d->path << (d->m_status == NonExistent ? "NonExistent" : "EXISTS!!! ERROR!");
1695  if (s_verboseDebug) {
1696  Q_ASSERT(d->m_status == NonExistent); // it doesn't belong here otherwise
1697  }
1698  }
1699  }
1700  }
1701  }
1702 }
1703 
1704 #ifdef HAVE_QFILESYSTEMWATCHER
1705 // Slot for QFileSystemWatcher
1706 void KDirWatchPrivate::fswEventReceived(const QString &path)
1707 {
1708  if (s_verboseDebug)
1709  kDebug(7001) << path;
1710  EntryMap::Iterator it = m_mapEntries.find(path);
1711  if(it != m_mapEntries.end()) {
1712  Entry* e = &(*it);
1713  e->dirty = true;
1714  const int ev = scanEntry(e);
1715  if (s_verboseDebug)
1716  kDebug(7001) << "scanEntry for" << e->path << "says" << ev;
1717  if (ev != NoChange)
1718  emitEvent(e, ev);
1719  if(ev == Deleted) {
1720  if (e->isDir)
1721  addEntry(0, e->parentDirectory(), e, true);
1722  else
1723  addEntry(0, QFileInfo(e->path).absolutePath(), e, true);
1724  } else if (ev == Created) {
1725  // We were waiting for it to appear; now watch it
1726  addWatch(e);
1727  } else if (e->isDir) {
1728  // Check if any file or dir was created under this directory, that we were waiting for
1729  Q_FOREACH(Entry* sub_entry, e->m_entries) {
1730  fswEventReceived(sub_entry->path); // recurse, to call scanEntry and see if something changed
1731  }
1732  }
1733  }
1734 }
1735 #else
1736 void KDirWatchPrivate::fswEventReceived(const QString &path)
1737 {
1738  Q_UNUSED(path);
1739  kWarning (7001) << "QFileSystemWatcher event received but QFileSystemWatcher is not supported";
1740 }
1741 #endif // HAVE_QFILESYSTEMWATCHER
1742 
1743 //
1744 // Class KDirWatch
1745 //
1746 
1747 K_GLOBAL_STATIC(KDirWatch, s_pKDirWatchSelf)
1748 KDirWatch* KDirWatch::self()
1749 {
1750  return s_pKDirWatchSelf;
1751 }
1752 
1753 // TODO KDE5: is this used anywhere?
1754 bool KDirWatch::exists()
1755 {
1756  return s_pKDirWatchSelf.exists();
1757 }
1758 
1759 static void cleanupQFSWatcher()
1760 {
1761  s_pKDirWatchSelf->deleteQFSWatcher();
1762 }
1763 
1764 KDirWatch::KDirWatch (QObject* parent)
1765  : QObject(parent), d(createPrivate())
1766 {
1767  static int nameCounter = 0;
1768 
1769  nameCounter++;
1770  setObjectName(QString::fromLatin1("KDirWatch-%1").arg(nameCounter) );
1771 
1772  d->ref();
1773 
1774  d->_isStopped = false;
1775 
1776  static bool cleanupRegistered = false;
1777  if (!cleanupRegistered) {
1778  cleanupRegistered = true;
1779  // Must delete QFileSystemWatcher before qApp is gone - bug 261541
1780  qAddPostRoutine(cleanupQFSWatcher);
1781  }
1782 }
1783 
1784 KDirWatch::~KDirWatch()
1785 {
1786  d->removeEntries(this);
1787  if ( d->deref() )
1788  {
1789  // delete it if it's the last one
1790  delete d;
1791  dwp_self = 0;
1792  }
1793 }
1794 
1795 void KDirWatch::addDir( const QString& _path, WatchModes watchModes)
1796 {
1797  if (d) d->addEntry(this, _path, 0, true, watchModes);
1798 }
1799 
1800 void KDirWatch::addFile( const QString& _path )
1801 {
1802  if ( !d )
1803  return;
1804 
1805  d->addEntry(this, _path, 0, false);
1806 }
1807 
1808 QDateTime KDirWatch::ctime( const QString &_path ) const
1809 {
1810  KDirWatchPrivate::Entry* e = d->entry(_path);
1811 
1812  if (!e)
1813  return QDateTime();
1814 
1815  return QDateTime::fromTime_t(e->m_ctime);
1816 }
1817 
1818 void KDirWatch::removeDir( const QString& _path )
1819 {
1820  if (d) {
1821  if (!d->removeEntry(this, _path, 0)) {
1822  kWarning() << "doesn't know" << _path;
1823  }
1824  }
1825 }
1826 
1827 void KDirWatch::removeFile( const QString& _path )
1828 {
1829  if (d) {
1830  if (!d->removeEntry(this, _path, 0)) {
1831  kWarning() << "doesn't know" << _path;
1832  }
1833  }
1834 }
1835 
1836 bool KDirWatch::stopDirScan( const QString& _path )
1837 {
1838  if (d) {
1839  KDirWatchPrivate::Entry *e = d->entry(_path);
1840  if (e && e->isDir) return d->stopEntryScan(this, e);
1841  }
1842  return false;
1843 }
1844 
1845 bool KDirWatch::restartDirScan( const QString& _path )
1846 {
1847  if (d) {
1848  KDirWatchPrivate::Entry *e = d->entry(_path);
1849  if (e && e->isDir)
1850  // restart without notifying pending events
1851  return d->restartEntryScan(this, e, false);
1852  }
1853  return false;
1854 }
1855 
1856 void KDirWatch::stopScan()
1857 {
1858  if (d) {
1859  d->stopScan(this);
1860  d->_isStopped = true;
1861  }
1862 }
1863 
1864 bool KDirWatch::isStopped()
1865 {
1866  return d->_isStopped;
1867 }
1868 
1869 void KDirWatch::startScan( bool notify, bool skippedToo )
1870 {
1871  if (d) {
1872  d->_isStopped = false;
1873  d->startScan(this, notify, skippedToo);
1874  }
1875 }
1876 
1877 
1878 bool KDirWatch::contains( const QString& _path ) const
1879 {
1880  KDirWatchPrivate::Entry* e = d->entry(_path);
1881  if (!e)
1882  return false;
1883 
1884  foreach(KDirWatchPrivate::Client* client, e->m_clients) {
1885  if (client->instance == this)
1886  return true;
1887  }
1888 
1889  return false;
1890 }
1891 
1892 void KDirWatch::deleteQFSWatcher()
1893 {
1894  delete d->fsWatcher;
1895  d->fsWatcher = 0;
1896 }
1897 
1898 void KDirWatch::statistics()
1899 {
1900  if (!dwp_self) {
1901  kDebug(7001) << "KDirWatch not used";
1902  return;
1903  }
1904  dwp_self->statistics();
1905 }
1906 
1907 
1908 void KDirWatch::setCreated( const QString & _file )
1909 {
1910  kDebug(7001) << objectName() << "emitting created" << _file;
1911  emit created( _file );
1912 }
1913 
1914 void KDirWatch::setDirty( const QString & _file )
1915 {
1916  //kDebug(7001) << objectName() << "emitting dirty" << _file;
1917  emit dirty( _file );
1918 }
1919 
1920 void KDirWatch::setDeleted( const QString & _file )
1921 {
1922  kDebug(7001) << objectName() << "emitting deleted" << _file;
1923  emit deleted( _file );
1924 }
1925 
1926 KDirWatch::Method KDirWatch::internalMethod()
1927 {
1928  return d->m_preferredMethod;
1929 }
1930 
1931 
1932 #include "kdirwatch.moc"
1933 #include "kdirwatch_p.moc"
1934 
1935 //sven
KDirWatchPrivate::ref
void ref()
Definition: kdirwatch_p.h:217
KDirWatchPrivate::Entry::parentDirectory
QString parentDirectory() const
Definition: kdirwatch.cpp:520
methodToString
static const char * methodToString(KDirWatch::Method method)
Definition: kdirwatch.cpp:106
KDirWatchPrivate::Entry::inotifyClientsForFileOrDir
QList< Client * > inotifyClientsForFileOrDir(bool isDir) const
Definition: kdirwatch.cpp:549
createPrivate
static KDirWatchPrivate * createPrivate()
Definition: kdirwatch.cpp:81
KDirWatchPrivate::Entry::clientsForFileOrDir
QList< Client * > clientsForFileOrDir(const QString &tpath, bool *isDir) const
Definition: kdirwatch.cpp:525
KDirWatch::setCreated
void setCreated(const QString &path)
Emits created().
Definition: kdirwatch.cpp:1908
KDirWatchPrivate::famEventReceived
void famEventReceived()
Definition: kdirwatch.cpp:1658
kdebug.h
KDirWatch::addFile
void addFile(const QString &file)
Adds a file to be watched.
Definition: kdirwatch.cpp:1800
KDirWatchPrivate::Entry::m_clients
QList< Client * > m_clients
Definition: kdirwatch_p.h:149
KDirWatchPrivate::useFreq
void useFreq(Entry *e, int newFreq)
Definition: kdirwatch.cpp:606
KDirWatchPrivate::Entry
Definition: kdirwatch_p.h:136
KDirWatchPrivate::removeEntry
bool removeEntry(KDirWatch *, const QString &, Entry *sub_entry)
Definition: kdirwatch.cpp:994
KDirWatchPrivate::isNoisyFile
static bool isNoisyFile(const char *filename)
Definition: kdirwatch.cpp:1501
KDirWatchPrivate::Entry::isValid
bool isValid()
Definition: kdirwatch_p.h:160
KDirWatch::KDirWatch
KDirWatch(QObject *parent=0)
Constructor.
Definition: kdirwatch.cpp:1764
KDirWatch::stopScan
void stopScan()
Stops scanning of all directories in internal list.
Definition: kdirwatch.cpp:1856
KDirWatchPrivate::rescan_timer
QTimer rescan_timer
Definition: kdirwatch_p.h:245
KDirWatch::WatchDirOnly
Watch just the specified directory.
Definition: kdirwatch.h:74
KDirWatchPrivate::stopScan
void stopScan(KDirWatch *)
Definition: kdirwatch.cpp:1182
KDirWatchPrivate::stopEntryScan
bool stopEntryScan(KDirWatch *, Entry *)
Definition: kdirwatch.cpp:1095
kdirwatch.h
KDirWatchPrivate::INotifyMode
Definition: kdirwatch_p.h:122
timeout
int timeout
Definition: kkernel_mac.cpp:46
mask
#define mask
KDirWatch::INotify
Definition: kdirwatch.h:223
fromTime_t
static QDateTime fromTime_t(qint32 seconds)
Definition: ktzfiletimezone.cpp:41
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221
KDirWatchPrivate::FAMMode
Definition: kdirwatch_p.h:122
KDirWatch::startScan
void startScan(bool notify=false, bool skippedToo=false)
Starts scanning of all dirs in list.
Definition: kdirwatch.cpp:1869
kconfig.h
KDirWatchPrivate::m_mapEntries
EntryMap m_mapEntries
Definition: kdirwatch_p.h:231
KDirWatchPrivate::Client
Definition: kdirwatch_p.h:126
KDirWatch::Method
Method
Definition: kdirwatch.h:223
KDirWatch::internalMethod
Method internalMethod()
Returns the preferred internal method to watch for changes.
Definition: kdirwatch.cpp:1926
KDE::stat
int stat(const QString &path, KDE_struct_stat *buf)
Definition: kde_file_win.cpp:175
KDirWatchPrivate::scanEntry
int scanEntry(Entry *e)
Definition: kdirwatch.cpp:1219
KDirWatchPrivate::Entry::m_status
entryStatus m_status
Definition: kdirwatch_p.h:145
invalid_ctime
#define invalid_ctime
Definition: kdirwatch_p.h:71
KDirWatchPrivate::Created
Definition: kdirwatch_p.h:123
KDirWatchPrivate::restartEntryScan
bool restartEntryScan(KDirWatch *, Entry *, bool)
Definition: kdirwatch.cpp:1121
QString
KDirWatchPrivate::slotRescan
void slotRescan()
Definition: kdirwatch.cpp:1387
KDirWatchPrivate::NoChange
Definition: kdirwatch_p.h:123
KDirWatchPrivate::NonExistent
Definition: kdirwatch_p.h:121
KDirWatchPrivate::Entry::m_entries
QList< Entry * > m_entries
Definition: kdirwatch_p.h:151
KDirWatchPrivate::Entry::m_ino
ino_t m_ino
Definition: kdirwatch_p.h:144
KDirWatchPrivate::useStat
bool useStat(Entry *)
Definition: kdirwatch.cpp:734
QObject
KDirWatchPrivate::QFSWatchMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::removeEntries
void removeEntries(KDirWatch *)
Definition: kdirwatch.cpp:1060
KDirWatchPrivate::Client::pending
int pending
Definition: kdirwatch_p.h:132
KDirWatchPrivate::Client::m_watchModes
KDirWatch::WatchModes m_watchModes
Definition: kdirwatch_p.h:133
KDirWatchPrivate::Client::instance
KDirWatch * instance
Definition: kdirwatch_p.h:127
KFileSystemWatcher::removePath
void removePath(const QString &file)
Definition: kdirwatch_win.cpp:68
KFileSystemType::fileSystemType
Type fileSystemType(const QString &path)
KDirWatchPrivate::Entry::path
QString path
Definition: kdirwatch_p.h:152
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:138
KDirWatch::stopDirScan
bool stopDirScan(const QString &path)
Stops scanning the specified path.
Definition: kdirwatch.cpp:1836
KDirWatch::deleted
void deleted(const QString &path)
Emitted when a file or directory is deleted.
KDirWatchPrivate::~KDirWatchPrivate
~KDirWatchPrivate()
Definition: kdirwatch.cpp:250
KDirWatchPrivate::Client::count
int count
Definition: kdirwatch_p.h:128
KDirWatchPrivate::UnknownMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::resetList
void resetList(KDirWatch *, bool)
Definition: kdirwatch.cpp:1205
KDirWatchPrivate::statistics
void statistics()
Definition: kdirwatch.cpp:1665
KDirWatchPrivate::StatMode
Definition: kdirwatch_p.h:122
kglobal.h
KDirWatchPrivate::useQFSWatch
bool useQFSWatch(Entry *e)
Definition: kdirwatch.cpp:713
KDirWatchPrivate::emitEvent
void emitEvent(const Entry *e, int event, const QString &fileName=QString())
Definition: kdirwatch.cpp:1314
KDirWatchPrivate::m_preferredMethod
KDirWatch::Method m_preferredMethod
Definition: kdirwatch_p.h:233
KDirWatchPrivate::m_nfsPreferredMethod
KDirWatch::Method m_nfsPreferredMethod
Definition: kdirwatch_p.h:233
KDirWatchPrivate::Entry::propagate_dirty
void propagate_dirty()
Definition: kdirwatch.cpp:453
KDirWatch::statistics
static void statistics()
Dump statistic information about the KDirWatch::self() instance.
Definition: kdirwatch.cpp:1898
KDirWatchPrivate::fsWatcher
KFileSystemWatcher * fsWatcher
Definition: kdirwatch_p.h:264
KDirWatch::DNotify
Definition: kdirwatch.h:223
kdirwatch_p.h
KDirWatchPrivate::KDirWatchPrivate
KDirWatchPrivate()
Definition: kdirwatch.cpp:153
KDirWatchPrivate::fswEventReceived
void fswEventReceived(const QString &path)
Definition: kdirwatch.cpp:1706
QStringList
KDirWatch::setDeleted
void setDeleted(const QString &path)
Emits deleted().
Definition: kdirwatch.cpp:1920
KDirWatch::QFSWatch
Definition: kdirwatch.h:223
KDirWatchPrivate::timer
QTimer timer
Definition: kdirwatch_p.h:230
KDirWatchPrivate::removeWatch
void removeWatch(Entry *entry)
Definition: kdirwatch.cpp:967
KDirWatchPrivate::m_PollInterval
int m_PollInterval
Definition: kdirwatch_p.h:236
dwp_self
static KDirWatchPrivate * dwp_self
Definition: kdirwatch.cpp:80
KDirWatchPrivate::Entry::m_mode
entryMode m_mode
Definition: kdirwatch_p.h:146
ksharedconfig.h
KDirWatchPrivate::DNotifyMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::Normal
Definition: kdirwatch_p.h:121
KDirWatch::ctime
QDateTime ctime(const QString &path) const
Returns the time the directory/file was last changed.
Definition: kdirwatch.cpp:1808
KDirWatch::created
void created(const QString &path)
Emitted when a file or directory is created.
KDirWatchPrivate::Entry::m_ctime
time_t m_ctime
Definition: kdirwatch_p.h:140
kWarning
#define kWarning
Definition: kdebug.h:322
KDirWatchPrivate::startScan
void startScan(KDirWatch *, bool, bool)
Definition: kdirwatch.cpp:1190
KDE::lstat
int lstat(const QString &path, KDE_struct_stat *buf)
Definition: kde_file_win.cpp:148
KDirWatch::WatchFiles
Watch also all files contained by the directory.
Definition: kdirwatch.h:75
KDirWatch::contains
bool contains(const QString &path) const
Check if a directory is being watched by this KDirWatch instance.
Definition: kdirwatch.cpp:1878
QDateTime
KDirWatchPrivate::Entry::msecLeft
int msecLeft
Definition: kdirwatch_p.h:154
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:53
KDirWatchPrivate::inotifyEventReceived
void inotifyEventReceived()
Definition: kdirwatch.cpp:271
cleanupQFSWatcher
static void cleanupQFSWatcher()
Definition: kdirwatch.cpp:1759
KDirWatchPrivate::rescan_all
bool rescan_all
Definition: kdirwatch_p.h:244
kfilesystemtype_p.h
KFileSystemWatcher
Definition: kdirwatch_p.h:88
KFileSystemType::Nfs
Definition: kfilesystemtype_p.h:30
KDirWatchPrivate::Entry::removeClient
void removeClient(KDirWatch *)
Definition: kdirwatch.cpp:493
KDirWatch::deleteQFSWatcher
void deleteQFSWatcher()
Definition: kdirwatch.cpp:1892
operator<<
QDebug operator<<(QDebug debug, const KDirWatchPrivate::Entry &entry)
Definition: kdirwatch.cpp:561
KDirWatchPrivate::Entry::m_nlink
int m_nlink
Definition: kdirwatch_p.h:142
KDirWatchPrivate
Definition: kdirwatch_p.h:116
KDirWatchPrivate::removeList
QSet< Entry * > removeList
Definition: kdirwatch_p.h:241
KFileSystemWatcher::addPath
void addPath(const QString &file)
Definition: kdirwatch_win.cpp:60
KDirWatch::removeDir
void removeDir(const QString &path)
Removes a directory from the list of scanned directories.
Definition: kdirwatch.cpp:1818
KDirWatchPrivate::Entry::dirty
bool dirty
Definition: kdirwatch_p.h:170
KDirWatch::addDir
void addDir(const QString &path, WatchModes watchModes=WatchDirOnly)
Adds a directory to be watched.
Definition: kdirwatch.cpp:1795
KDirWatch::Stat
Definition: kdirwatch.h:223
KDirWatchPrivate::m_nfsPollInterval
int m_nfsPollInterval
Definition: kdirwatch_p.h:236
kDebug
#define kDebug
Definition: kdebug.h:316
KDirWatchPrivate::Client::watchingStopped
bool watchingStopped
Definition: kdirwatch_p.h:130
KDirWatchPrivate::entry
Entry * entry(const QString &)
Definition: kdirwatch.cpp:586
KDirWatchPrivate::Entry::freq
int freq
Definition: kdirwatch_p.h:154
KDirWatch::FAM
Definition: kdirwatch.h:223
KDirWatch::exists
static bool exists()
Returns true if there is an instance of KDirWatch.
Definition: kdirwatch.cpp:1754
KDirWatch
Class for watching directory and file changes.
Definition: kdirwatch.h:64
KDirWatch::~KDirWatch
~KDirWatch()
Destructor.
Definition: kdirwatch.cpp:1784
KDirWatchPrivate::Entry::clientCount
int clientCount() const
Definition: kdirwatch.cpp:511
KDirWatchPrivate::deref
bool deref()
Definition: kdirwatch_p.h:218
KDirWatchPrivate::slotRemoveDelayed
void slotRemoveDelayed()
Definition: kdirwatch.cpp:1372
KDirWatch::dirty
void dirty(const QString &path)
Emitted when a watched object is changed.
KDirWatchPrivate::statEntries
int statEntries
Definition: kdirwatch_p.h:235
KDirWatch::WatchSubDirs
Watch also all the subdirs contained by the directory.
Definition: kdirwatch.h:76
KDirWatch::setDirty
void setDirty(const QString &path)
Emits dirty().
Definition: kdirwatch.cpp:1914
KDirWatch::isStopped
bool isStopped()
Is scanning stopped? After creation of a KDirWatch instance, this is false.
Definition: kdirwatch.cpp:1864
KDirWatch::removeFile
void removeFile(const QString &file)
Removes a file from the list of watched files.
Definition: kdirwatch.cpp:1827
KDirWatchPrivate::Entry::isDir
bool isDir
Definition: kdirwatch_p.h:147
KDirWatchPrivate::delayRemove
bool delayRemove
Definition: kdirwatch_p.h:242
KDirWatchPrivate::Deleted
Definition: kdirwatch_p.h:123
KDirWatchPrivate::Entry::findSubEntry
Entry * findSubEntry(const QString &path) const
Definition: kdirwatch_p.h:162
methodFromString
static KDirWatch::Method methodFromString(const QString &method)
Definition: kdirwatch.cpp:88
KDirWatchPrivate::freq
int freq
Definition: kdirwatch_p.h:234
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:248
KDirWatchPrivate::Entry::addClient
void addClient(KDirWatch *, KDirWatch::WatchModes)
Definition: kdirwatch.cpp:469
KDirWatchPrivate::addEntry
void addEntry(KDirWatch *instance, const QString &_path, Entry *sub_entry, bool isDir, KDirWatch::WatchModes watchModes=KDirWatch::WatchDirOnly)
Definition: kdirwatch.cpp:763
KDirWatch::restartDirScan
bool restartDirScan(const QString &path)
Restarts scanning for specified path.
Definition: kdirwatch.cpp:1845
kconfiggroup.h
KDirWatchPrivate::Changed
Definition: kdirwatch_p.h:123
KDirWatchPrivate::addWatch
void addWatch(Entry *entry)
Definition: kdirwatch.cpp:919
QList< QByteArray >
KDirWatchPrivate::_isStopped
bool _isStopped
Definition: kdirwatch_p.h:268
s_verboseDebug
static const bool s_verboseDebug
Definition: kdirwatch.cpp:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal