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

KDECore

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

Skip menu "kdelibs-4.11.2 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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