• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.3 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  // we trust QFSW to get it right, the ctime comparisons above
1269  // fail for example when adding files to directories on Windows
1270  // which doesn't change the mtime of the directory
1271  || e->m_mode == QFSWatchMode
1272  ) {
1273  e->m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
1274  e->m_nlink = stat_buf.st_nlink;
1275  if (e->m_ino != stat_buf.st_ino) {
1276  // The file got deleted and recreated. We need to watch it again.
1277  removeWatch(e);
1278  addWatch(e);
1279  }
1280  e->m_ino = stat_buf.st_ino;
1281  return Changed;
1282  }
1283 
1284  return NoChange;
1285  }
1286 
1287  // dir/file doesn't exist
1288 
1289  e->m_nlink = 0;
1290  e->m_ino = 0;
1291  e->m_status = NonExistent;
1292 
1293  if (e->m_ctime == invalid_ctime) {
1294  return NoChange;
1295  }
1296 
1297  e->m_ctime = invalid_ctime;
1298  return Deleted;
1299 }
1300 
1301 /* Notify all interested KDirWatch instances about a given event on an entry
1302  * and stored pending events. When watching is stopped, the event is
1303  * added to the pending events.
1304  */
1305 void KDirWatchPrivate::emitEvent(const Entry* e, int event, const QString &fileName)
1306 {
1307  QString path (e->path);
1308  if (!fileName.isEmpty()) {
1309  if (!QDir::isRelativePath(fileName))
1310  path = fileName;
1311  else {
1312 #ifdef Q_OS_UNIX
1313  path += QLatin1Char('/') + fileName;
1314 #elif defined(Q_WS_WIN)
1315  //current drive is passed instead of /
1316  path += QDir::currentPath().left(2) + QLatin1Char('/') + fileName;
1317 #endif
1318  }
1319  }
1320 
1321  if (s_verboseDebug) {
1322  kDebug(7001) << event << path << e->m_clients.count() << "clients";
1323  }
1324 
1325  foreach(Client* c, e->m_clients)
1326  {
1327  if (c->instance==0 || c->count==0) continue;
1328 
1329  if (c->watchingStopped) {
1330  // add event to pending...
1331  if (event == Changed)
1332  c->pending |= event;
1333  else if (event == Created || event == Deleted)
1334  c->pending = event;
1335  continue;
1336  }
1337  // not stopped
1338  if (event == NoChange || event == Changed)
1339  event |= c->pending;
1340  c->pending = NoChange;
1341  if (event == NoChange) continue;
1342 
1343  // Emit the signals delayed, to avoid unexpected re-entrancy from the slots (#220153)
1344 
1345  if (event & Deleted) {
1346  QMetaObject::invokeMethod(c->instance, "setDeleted", Qt::QueuedConnection, Q_ARG(QString, path));
1347  // emit only Deleted event...
1348  continue;
1349  }
1350 
1351  if (event & Created) {
1352  QMetaObject::invokeMethod(c->instance, "setCreated", Qt::QueuedConnection, Q_ARG(QString, path));
1353  // possible emit Change event after creation
1354  }
1355 
1356  if (event & Changed) {
1357  QMetaObject::invokeMethod(c->instance, "setDirty", Qt::QueuedConnection, Q_ARG(QString, path));
1358  }
1359  }
1360 }
1361 
1362 // Remove entries which were marked to be removed
1363 void KDirWatchPrivate::slotRemoveDelayed()
1364 {
1365  delayRemove = false;
1366  // Removing an entry could also take care of removing its parent
1367  // (e.g. in FAM or inotify mode), which would remove other entries in removeList,
1368  // so don't use foreach or iterators here...
1369  while (!removeList.isEmpty()) {
1370  Entry* entry = *removeList.begin();
1371  removeEntry(0, entry, 0); // this will remove entry from removeList
1372  }
1373 }
1374 
1375 /* Scan all entries to be watched for changes. This is done regularly
1376  * when polling. FAM and inotify use a single-shot timer to call this slot delayed.
1377  */
1378 void KDirWatchPrivate::slotRescan()
1379 {
1380  if (s_verboseDebug)
1381  kDebug(7001);
1382 
1383  EntryMap::Iterator it;
1384 
1385  // People can do very long things in the slot connected to dirty(),
1386  // like showing a message box. We don't want to keep polling during
1387  // that time, otherwise the value of 'delayRemove' will be reset.
1388  // ### TODO: now the emitEvent delays emission, this can be cleaned up
1389  bool timerRunning = timer.isActive();
1390  if ( timerRunning )
1391  timer.stop();
1392 
1393  // We delay deletions of entries this way.
1394  // removeDir(), when called in slotDirty(), can cause a crash otherwise
1395  // ### TODO: now the emitEvent delays emission, this can be cleaned up
1396  delayRemove = true;
1397 
1398  if (rescan_all)
1399  {
1400  // mark all as dirty
1401  it = m_mapEntries.begin();
1402  for( ; it != m_mapEntries.end(); ++it )
1403  (*it).dirty = true;
1404  rescan_all = false;
1405  }
1406  else
1407  {
1408  // progate dirty flag to dependant entries (e.g. file watches)
1409  it = m_mapEntries.begin();
1410  for( ; it != m_mapEntries.end(); ++it )
1411  if (((*it).m_mode == INotifyMode || (*it).m_mode == QFSWatchMode) && (*it).dirty )
1412  (*it).propagate_dirty();
1413  }
1414 
1415 #ifdef HAVE_SYS_INOTIFY_H
1416  QList<Entry*> cList;
1417 #endif
1418 
1419  it = m_mapEntries.begin();
1420  for( ; it != m_mapEntries.end(); ++it ) {
1421  // we don't check invalid entries (i.e. remove delayed)
1422  Entry* entry = &(*it);
1423  if (!entry->isValid()) continue;
1424 
1425  const int ev = scanEntry(entry);
1426  if (s_verboseDebug)
1427  kDebug(7001) << "scanEntry for" << entry->path << "says" << ev;
1428 
1429  switch(entry->m_mode) {
1430 #ifdef HAVE_SYS_INOTIFY_H
1431  case INotifyMode:
1432  if ( ev == Deleted ) {
1433  if (s_verboseDebug)
1434  kDebug(7001) << "scanEntry says" << entry->path << "was deleted";
1435  addEntry(0, entry->parentDirectory(), entry, true);
1436  } else if (ev == Created) {
1437  if (s_verboseDebug)
1438  kDebug(7001) << "scanEntry says" << entry->path << "was created. wd=" << entry->wd;
1439  if (entry->wd < 0) {
1440  cList.append(entry);
1441  addWatch(entry);
1442  }
1443  }
1444  break;
1445 #endif
1446  case FAMMode:
1447  case QFSWatchMode:
1448  if (ev == Created) {
1449  addWatch(entry);
1450  }
1451  break;
1452  default:
1453  // dunno about StatMode...
1454  break;
1455  }
1456 
1457 #ifdef HAVE_SYS_INOTIFY_H
1458  if (entry->isDir) {
1459  // Report and clear the the list of files that have changed in this directory.
1460  // Remove duplicates by changing to set and back again:
1461  // we don't really care about preserving the order of the
1462  // original changes.
1463  QStringList pendingFileChanges = entry->m_pendingFileChanges;
1464  pendingFileChanges.removeDuplicates();
1465  Q_FOREACH(const QString &changedFilename, pendingFileChanges) {
1466  if (s_verboseDebug) {
1467  kDebug(7001) << "processing pending file change for" << changedFilename;
1468  }
1469  emitEvent(entry, Changed, changedFilename);
1470  }
1471  entry->m_pendingFileChanges.clear();
1472  }
1473 #endif
1474 
1475  if ( ev != NoChange ) {
1476  emitEvent(entry, ev);
1477  }
1478  }
1479 
1480  if ( timerRunning )
1481  timer.start(freq);
1482 
1483 #ifdef HAVE_SYS_INOTIFY_H
1484  // Remove watch of parent of new created directories
1485  Q_FOREACH(Entry* e, cList)
1486  removeEntry(0, e->parentDirectory(), e);
1487 #endif
1488 
1489  QTimer::singleShot(0, this, SLOT(slotRemoveDelayed()));
1490 }
1491 
1492 bool KDirWatchPrivate::isNoisyFile( const char * filename )
1493 {
1494  // $HOME/.X.err grows with debug output, so don't notify change
1495  if ( *filename == '.') {
1496  if (strncmp(filename, ".X.err", 6) == 0) return true;
1497  if (strncmp(filename, ".xsession-errors", 16) == 0) return true;
1498  // fontconfig updates the cache on every KDE app start
1499  // (inclusive kio_thumbnail slaves)
1500  if (strncmp(filename, ".fonts.cache", 12) == 0) return true;
1501  }
1502 
1503  return false;
1504 }
1505 
1506 #ifdef HAVE_FAM
1507 void KDirWatchPrivate::famEventReceived()
1508 {
1509  static FAMEvent fe;
1510 
1511  delayRemove = true;
1512 
1513  //kDebug(7001) << "Fam event received";
1514 
1515  while(use_fam && FAMPending(&fc)) {
1516  if (FAMNextEvent(&fc, &fe) == -1) {
1517  kWarning(7001) << "FAM connection problem, switching to polling.";
1518  use_fam = false;
1519  delete sn; sn = 0;
1520 
1521  // Replace all FAMMode entries with INotify/Stat
1522  EntryMap::Iterator it = m_mapEntries.begin();
1523  for( ; it != m_mapEntries.end(); ++it )
1524  if ((*it).m_mode == FAMMode && (*it).m_clients.count()>0) {
1525  Entry* e = &(*it);
1526  addWatch(e);
1527  }
1528  }
1529  else
1530  checkFAMEvent(&fe);
1531  }
1532 
1533  QTimer::singleShot(0, this, SLOT(slotRemoveDelayed()));
1534 }
1535 
1536 void KDirWatchPrivate::checkFAMEvent(FAMEvent* fe)
1537 {
1538  //kDebug(7001);
1539 
1540  // Don't be too verbose ;-)
1541  if ((fe->code == FAMExists) ||
1542  (fe->code == FAMEndExist) ||
1543  (fe->code == FAMAcknowledge)) return;
1544 
1545  if ( isNoisyFile( fe->filename ) )
1546  return;
1547 
1548  Entry* e = 0;
1549  EntryMap::Iterator it = m_mapEntries.begin();
1550  for( ; it != m_mapEntries.end(); ++it )
1551  if (FAMREQUEST_GETREQNUM(&( (*it).fr )) ==
1552  FAMREQUEST_GETREQNUM(&(fe->fr)) ) {
1553  e = &(*it);
1554  break;
1555  }
1556 
1557  // Entry* e = static_cast<Entry*>(fe->userdata);
1558 
1559  if (s_verboseDebug) { // don't enable this except when debugging, see #88538
1560  kDebug(7001) << "Processing FAM event ("
1561  << ((fe->code == FAMChanged) ? "FAMChanged" :
1562  (fe->code == FAMDeleted) ? "FAMDeleted" :
1563  (fe->code == FAMStartExecuting) ? "FAMStartExecuting" :
1564  (fe->code == FAMStopExecuting) ? "FAMStopExecuting" :
1565  (fe->code == FAMCreated) ? "FAMCreated" :
1566  (fe->code == FAMMoved) ? "FAMMoved" :
1567  (fe->code == FAMAcknowledge) ? "FAMAcknowledge" :
1568  (fe->code == FAMExists) ? "FAMExists" :
1569  (fe->code == FAMEndExist) ? "FAMEndExist" : "Unknown Code")
1570  << ", " << fe->filename
1571  << ", Req " << FAMREQUEST_GETREQNUM(&(fe->fr)) << ") e=" << e;
1572  }
1573 
1574  if (!e) {
1575  // this happens e.g. for FAMAcknowledge after deleting a dir...
1576  // kDebug(7001) << "No entry for FAM event ?!";
1577  return;
1578  }
1579 
1580  if (e->m_status == NonExistent) {
1581  kDebug(7001) << "FAM event for nonExistent entry " << e->path;
1582  return;
1583  }
1584 
1585  // Delayed handling. This rechecks changes with own stat calls.
1586  e->dirty = true;
1587  if (!rescan_timer.isActive())
1588  rescan_timer.start(m_PollInterval); // singleshot
1589 
1590  // needed FAM control actions on FAM events
1591  switch (fe->code) {
1592  case FAMDeleted:
1593  // fe->filename is an absolute path when a watched file-or-dir is deleted
1594  if (!QDir::isRelativePath(QFile::decodeName(fe->filename))) {
1595  FAMCancelMonitor(&fc, &(e->fr) ); // needed ?
1596  kDebug(7001) << "Cancelled FAMReq"
1597  << FAMREQUEST_GETREQNUM(&(e->fr))
1598  << "for" << e->path;
1599  e->m_status = NonExistent;
1600  e->m_ctime = invalid_ctime;
1601  emitEvent(e, Deleted, e->path);
1602  // If the parent dir was already watched, tell it something changed
1603  Entry* parentEntry = entry(e->parentDirectory());
1604  if (parentEntry)
1605  parentEntry->dirty = true;
1606  // Add entry to parent dir to notice if the entry gets recreated
1607  addEntry(0, e->parentDirectory(), e, true /*isDir*/);
1608  } else {
1609  // A file in this directory has been removed, and wasn't explicitly watched.
1610  // We could still inform clients, like inotify does? But stat can't.
1611  // For now we just marked e dirty and slotRescan will emit the dir as dirty.
1612  //kDebug(7001) << "Got FAMDeleted for" << QFile::decodeName(fe->filename) << "in" << e->path << ". Absolute path -> NOOP!";
1613  }
1614  break;
1615 
1616  case FAMCreated: {
1617  // check for creation of a directory we have to watch
1618  QString tpath(e->path + QLatin1Char('/') + QFile::decodeName(fe->filename));
1619 
1620  // This code is very similar to the one in inotifyEventReceived...
1621  Entry* sub_entry = e->findSubEntry(tpath);
1622  if (sub_entry /*&& sub_entry->isDir*/) {
1623  // We were waiting for this new file/dir to be created
1624  emitEvent(sub_entry, Created);
1625  sub_entry->dirty = true;
1626  rescan_timer.start(0); // process this asap, to start watching that dir
1627  } else if (e->isDir && !e->m_clients.empty()) {
1628  bool isDir = false;
1629  const QList<Client *> clients = e->clientsForFileOrDir(tpath, &isDir);
1630  Q_FOREACH(Client *client, clients) {
1631  addEntry (client->instance, tpath, 0, isDir,
1632  isDir ? client->m_watchModes : KDirWatch::WatchDirOnly);
1633  }
1634 
1635  if (!clients.isEmpty()) {
1636  emitEvent(e, Created, tpath);
1637 
1638  kDebug(7001).nospace() << clients.count() << " instance(s) monitoring the new "
1639  << (isDir ? "dir " : "file ") << tpath;
1640  }
1641  }
1642  }
1643  break;
1644  default:
1645  break;
1646  }
1647 }
1648 #else
1649 void KDirWatchPrivate::famEventReceived()
1650 {
1651  kWarning (7001) << "Fam event received but FAM is not supported";
1652 }
1653 #endif
1654 
1655 
1656 void KDirWatchPrivate::statistics()
1657 {
1658  EntryMap::Iterator it;
1659 
1660  kDebug(7001) << "Entries watched:";
1661  if (m_mapEntries.count()==0) {
1662  kDebug(7001) << " None.";
1663  }
1664  else {
1665  it = m_mapEntries.begin();
1666  for( ; it != m_mapEntries.end(); ++it ) {
1667  Entry* e = &(*it);
1668  kDebug(7001) << " " << *e;
1669 
1670  foreach(Client* c, e->m_clients) {
1671  QByteArray pending;
1672  if (c->watchingStopped) {
1673  if (c->pending & Deleted) pending += "deleted ";
1674  if (c->pending & Created) pending += "created ";
1675  if (c->pending & Changed) pending += "changed ";
1676  if (!pending.isEmpty()) pending = " (pending: " + pending + ')';
1677  pending = ", stopped" + pending;
1678  }
1679  kDebug(7001) << " by " << c->instance->objectName()
1680  << " (" << c->count << " times)" << pending;
1681  }
1682  if (e->m_entries.count()>0) {
1683  kDebug(7001) << " dependent entries:";
1684  foreach(Entry *d, e->m_entries) {
1685  kDebug(7001) << " " << d << d->path << (d->m_status == NonExistent ? "NonExistent" : "EXISTS!!! ERROR!");
1686  if (s_verboseDebug) {
1687  Q_ASSERT(d->m_status == NonExistent); // it doesn't belong here otherwise
1688  }
1689  }
1690  }
1691  }
1692  }
1693 }
1694 
1695 #ifdef HAVE_QFILESYSTEMWATCHER
1696 // Slot for QFileSystemWatcher
1697 void KDirWatchPrivate::fswEventReceived(const QString &path)
1698 {
1699  if (s_verboseDebug)
1700  kDebug(7001) << path;
1701  EntryMap::Iterator it = m_mapEntries.find(path);
1702  if(it != m_mapEntries.end()) {
1703  Entry* e = &(*it);
1704  e->dirty = true;
1705  const int ev = scanEntry(e);
1706  if (s_verboseDebug)
1707  kDebug(7001) << "scanEntry for" << e->path << "says" << ev;
1708  if (ev != NoChange)
1709  emitEvent(e, ev);
1710  if(ev == Deleted) {
1711  if (e->isDir)
1712  addEntry(0, e->parentDirectory(), e, true);
1713  else
1714  addEntry(0, QFileInfo(e->path).absolutePath(), e, true);
1715  } else if (ev == Created) {
1716  // We were waiting for it to appear; now watch it
1717  addWatch(e);
1718  } else if (e->isDir) {
1719  // Check if any file or dir was created under this directory, that we were waiting for
1720  Q_FOREACH(Entry* sub_entry, e->m_entries) {
1721  fswEventReceived(sub_entry->path); // recurse, to call scanEntry and see if something changed
1722  }
1723  }
1724  }
1725 }
1726 #else
1727 void KDirWatchPrivate::fswEventReceived(const QString &path)
1728 {
1729  Q_UNUSED(path);
1730  kWarning (7001) << "QFileSystemWatcher event received but QFileSystemWatcher is not supported";
1731 }
1732 #endif // HAVE_QFILESYSTEMWATCHER
1733 
1734 //
1735 // Class KDirWatch
1736 //
1737 
1738 K_GLOBAL_STATIC(KDirWatch, s_pKDirWatchSelf)
1739 KDirWatch* KDirWatch::self()
1740 {
1741  return s_pKDirWatchSelf;
1742 }
1743 
1744 // TODO KDE5: is this used anywhere?
1745 bool KDirWatch::exists()
1746 {
1747  return s_pKDirWatchSelf.exists();
1748 }
1749 
1750 static void cleanupQFSWatcher()
1751 {
1752  s_pKDirWatchSelf->deleteQFSWatcher();
1753 }
1754 
1755 KDirWatch::KDirWatch (QObject* parent)
1756  : QObject(parent), d(createPrivate())
1757 {
1758  static int nameCounter = 0;
1759 
1760  nameCounter++;
1761  setObjectName(QString::fromLatin1("KDirWatch-%1").arg(nameCounter) );
1762 
1763  d->ref();
1764 
1765  d->_isStopped = false;
1766 
1767  static bool cleanupRegistered = false;
1768  if (!cleanupRegistered) {
1769  cleanupRegistered = true;
1770  // Must delete QFileSystemWatcher before qApp is gone - bug 261541
1771  qAddPostRoutine(cleanupQFSWatcher);
1772  }
1773 }
1774 
1775 KDirWatch::~KDirWatch()
1776 {
1777  d->removeEntries(this);
1778  if ( d->deref() )
1779  {
1780  // delete it if it's the last one
1781  delete d;
1782  dwp_self = 0;
1783  }
1784 }
1785 
1786 void KDirWatch::addDir( const QString& _path, WatchModes watchModes)
1787 {
1788  if (d) d->addEntry(this, _path, 0, true, watchModes);
1789 }
1790 
1791 void KDirWatch::addFile( const QString& _path )
1792 {
1793  if ( !d )
1794  return;
1795 
1796  d->addEntry(this, _path, 0, false);
1797 }
1798 
1799 QDateTime KDirWatch::ctime( const QString &_path ) const
1800 {
1801  KDirWatchPrivate::Entry* e = d->entry(_path);
1802 
1803  if (!e)
1804  return QDateTime();
1805 
1806  return QDateTime::fromTime_t(e->m_ctime);
1807 }
1808 
1809 void KDirWatch::removeDir( const QString& _path )
1810 {
1811  if (d) d->removeEntry(this, _path, 0);
1812 }
1813 
1814 void KDirWatch::removeFile( const QString& _path )
1815 {
1816  if (d) d->removeEntry(this, _path, 0);
1817 }
1818 
1819 bool KDirWatch::stopDirScan( const QString& _path )
1820 {
1821  if (d) {
1822  KDirWatchPrivate::Entry *e = d->entry(_path);
1823  if (e && e->isDir) return d->stopEntryScan(this, e);
1824  }
1825  return false;
1826 }
1827 
1828 bool KDirWatch::restartDirScan( const QString& _path )
1829 {
1830  if (d) {
1831  KDirWatchPrivate::Entry *e = d->entry(_path);
1832  if (e && e->isDir)
1833  // restart without notifying pending events
1834  return d->restartEntryScan(this, e, false);
1835  }
1836  return false;
1837 }
1838 
1839 void KDirWatch::stopScan()
1840 {
1841  if (d) {
1842  d->stopScan(this);
1843  d->_isStopped = true;
1844  }
1845 }
1846 
1847 bool KDirWatch::isStopped()
1848 {
1849  return d->_isStopped;
1850 }
1851 
1852 void KDirWatch::startScan( bool notify, bool skippedToo )
1853 {
1854  if (d) {
1855  d->_isStopped = false;
1856  d->startScan(this, notify, skippedToo);
1857  }
1858 }
1859 
1860 
1861 bool KDirWatch::contains( const QString& _path ) const
1862 {
1863  KDirWatchPrivate::Entry* e = d->entry(_path);
1864  if (!e)
1865  return false;
1866 
1867  foreach(KDirWatchPrivate::Client* client, e->m_clients) {
1868  if (client->instance == this)
1869  return true;
1870  }
1871 
1872  return false;
1873 }
1874 
1875 void KDirWatch::deleteQFSWatcher()
1876 {
1877  delete d->fsWatcher;
1878  d->fsWatcher = 0;
1879 }
1880 
1881 void KDirWatch::statistics()
1882 {
1883  if (!dwp_self) {
1884  kDebug(7001) << "KDirWatch not used";
1885  return;
1886  }
1887  dwp_self->statistics();
1888 }
1889 
1890 
1891 void KDirWatch::setCreated( const QString & _file )
1892 {
1893  kDebug(7001) << objectName() << "emitting created" << _file;
1894  emit created( _file );
1895 }
1896 
1897 void KDirWatch::setDirty( const QString & _file )
1898 {
1899  //kDebug(7001) << objectName() << "emitting dirty" << _file;
1900  emit dirty( _file );
1901 }
1902 
1903 void KDirWatch::setDeleted( const QString & _file )
1904 {
1905  kDebug(7001) << objectName() << "emitting deleted" << _file;
1906  emit deleted( _file );
1907 }
1908 
1909 KDirWatch::Method KDirWatch::internalMethod()
1910 {
1911  return d->m_preferredMethod;
1912 }
1913 
1914 
1915 #include "kdirwatch.moc"
1916 #include "kdirwatch_p.moc"
1917 
1918 //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:1891
KDirWatchPrivate::famEventReceived
void famEventReceived()
Definition: kdirwatch.cpp:1649
kdebug.h
KDirWatch::addFile
void addFile(const QString &file)
Adds a file to be watched.
Definition: kdirwatch.cpp:1791
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:1492
KDirWatchPrivate::Entry::isValid
bool isValid()
Definition: kdirwatch_p.h:160
KDirWatch::KDirWatch
KDirWatch(QObject *parent=0)
Constructor.
Definition: kdirwatch.cpp:1755
KDirWatch::stopScan
void stopScan()
Stops scanning of all directories in internal list.
Definition: kdirwatch.cpp:1839
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:1852
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:1909
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:1378
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:1819
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:1656
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:1305
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:1881
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:1697
QStringList
KDirWatch::setDeleted
void setDeleted(const QString &path)
Emits deleted().
Definition: kdirwatch.cpp:1903
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:1799
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:1861
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:1750
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:1875
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:1809
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:1786
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:1745
KDirWatch
Class for watching directory and file changes.
Definition: kdirwatch.h:64
KDirWatch::~KDirWatch
~KDirWatch()
Destructor.
Definition: kdirwatch.cpp:1775
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:1363
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:1897
KDirWatch::isStopped
bool isStopped()
Is scanning stopped? After creation of a KDirWatch instance, this is false.
Definition: kdirwatch.cpp:1847
KDirWatch::removeFile
void removeFile(const QString &file)
Removes a file from the list of watched files.
Definition: kdirwatch.cpp:1814
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:1828
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 Thu Dec 12 2013 07:58:43 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.3 API Reference

Skip menu "kdelibs-4.11.3 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