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

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • io
kdirwatch_p.h
Go to the documentation of this file.
1 /* Private Header for class of KDirWatchPrivate
2  *
3  * this separate header file is needed for MOC processing
4  * because KDirWatchPrivate has signals and slots
5  *
6  * This file is part of the KDE libraries
7  * Copyright (C) 1998 Sven Radej <sven@lisa.exp.univie.ac.at>
8  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
9  * Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
10  * Copyright (C) 2008 JarosÅ‚aw Staniek <staniek@kde.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License version 2 as published by the Free Software Foundation.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB. If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26 
27 #ifndef KDIRWATCH_P_H
28 #define KDIRWATCH_P_H
29 
30 #include <io/config-kdirwatch.h>
31 #include "kdirwatch.h"
32 
33 #ifndef QT_NO_FILESYSTEMWATCHER
34 #define HAVE_QFILESYSTEMWATCHER
35 #endif
36 
37 #include <QtCore/QList>
38 #include <QtCore/QSet>
39 #include <QtCore/QMap>
40 #include <QtCore/QObject>
41 #include <QtCore/QString>
42 #include <QtCore/QTimer>
43 class QFileSystemWatcher;
44 class QSocketNotifier;
45 
46 #ifdef HAVE_FAM
47 #include <limits.h>
48 #include <fam.h>
49 #endif
50 
51 #ifdef HAVE_SYS_INOTIFY_H
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/inotify.h>
55 
56 #ifndef IN_DONT_FOLLOW
57 #define IN_DONT_FOLLOW 0x02000000
58 #endif
59 
60 #ifndef IN_ONLYDIR
61 #define IN_ONLYDIR 0x01000000
62 #endif
63 
64 #endif
65 
66 #include <sys/time.h>
67 #include <sys/param.h> // ino_t
68 #include <ctime>
69 
70 
71 #define invalid_ctime ((time_t)-1)
72 
73 #ifdef HAVE_QFILESYSTEMWATCHER
74 #include <QtCore/QFileSystemWatcher>
75 
76 #if defined Q_WS_WIN
77 /* Helper implemented as a workaround for limitation on Windows:
78  * the maximum number of object handles is MAXIMUM_WAIT_OBJECTS (64) per thread.
79  *
80  * From http://msdn.microsoft.com/en-us/library/ms687025(VS.85).aspx
81  * "To wait on more than MAXIMUM_WAIT_OBJECTS handles, create a thread to wait
82  * on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles.
83  * Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS."
84  *
85  * QFileSystemWatcher is implemented as thread, so KFileSystemWatcher
86  * allocates more QFileSystemWatcher instances on demand (and deallocates them later).
87  */
88 class KFileSystemWatcher : public QObject
89 {
90  Q_OBJECT
91 public:
92  KFileSystemWatcher();
93  ~KFileSystemWatcher();
94  void addPath(const QString &file);
95  void removePath(const QString &file);
96 
97 Q_SIGNALS:
98  void fileChanged(const QString &path);
99  void directoryChanged(const QString &path);
100 
101 private:
102  QFileSystemWatcher* availableWatcher();
103  QFileSystemWatcher* m_recentWatcher;
104  QList<QFileSystemWatcher*> m_watchers;
105  QHash<QFileSystemWatcher*, uint> m_usedObjects;
106  QHash<QString,QFileSystemWatcher*> m_paths;
107 };
108 #else
109 typedef QFileSystemWatcher KFileSystemWatcher;
110 #endif
111 #endif
112 
113 /* KDirWatchPrivate is a singleton and does the watching
114  * for every KDirWatch instance in the application.
115  */
116 class KDirWatchPrivate : public QObject
117 {
118  Q_OBJECT
119 public:
120 
121  enum entryStatus { Normal = 0, NonExistent };
122  enum entryMode { UnknownMode = 0, StatMode, DNotifyMode, INotifyMode, FAMMode, QFSWatchMode };
123  enum { NoChange=0, Changed=1, Created=2, Deleted=4 };
124 
125 
126  struct Client {
127  KDirWatch* instance;
128  int count;
129  // did the instance stop watching
130  bool watchingStopped;
131  // events blocked when stopped
132  int pending;
133  KDirWatch::WatchModes m_watchModes;
134  };
135 
136  class Entry
137  {
138  public:
139  // the last observed modification time
140  time_t m_ctime;
141  // the last observed link count
142  int m_nlink;
143  // last observed inode
144  ino_t m_ino;
145  entryStatus m_status;
146  entryMode m_mode;
147  bool isDir;
148  // instances interested in events
149  QList<Client *> m_clients;
150  // nonexistent entries of this directory
151  QList<Entry *> m_entries;
152  QString path;
153 
154  int msecLeft, freq;
155 
156  QString parentDirectory() const;
157  void addClient(KDirWatch*, KDirWatch::WatchModes);
158  void removeClient(KDirWatch*);
159  int clientCount() const;
160  bool isValid() { return m_clients.count() || m_entries.count(); }
161 
162  Entry* findSubEntry(const QString& path) const {
163  Q_FOREACH(Entry* sub_entry, m_entries) {
164  if (sub_entry->path == path)
165  return sub_entry;
166  }
167  return 0;
168  }
169 
170  bool dirty;
171  void propagate_dirty();
172 
173  QList<Client *> clientsForFileOrDir(const QString& tpath, bool* isDir) const;
174  QList<Client *> inotifyClientsForFileOrDir(bool isDir) const;
175 
176 #ifdef HAVE_FAM
177  FAMRequest fr;
178 #endif
179 
180 #ifdef HAVE_SYS_INOTIFY_H
181  int wd;
182  // Creation and Deletion of files happens infrequently, so
183  // can safely be reported as they occur. File changes i.e. those that emity "dirty()" can
184  // happen many times per second, though, so maintain a list of files in this directory
185  // that can be emitted and flushed at the next slotRescan(...).
186  // This will be unused if the Entry is not a directory.
187  QList<QString> m_pendingFileChanges;
188 #endif
189  };
190 
191  typedef QMap<QString,Entry> EntryMap;
192 
193  KDirWatchPrivate();
194  ~KDirWatchPrivate();
195 
196  void resetList (KDirWatch*,bool);
197  void useFreq(Entry* e, int newFreq);
198  void addEntry(KDirWatch* instance,const QString& _path, Entry* sub_entry,
199  bool isDir, KDirWatch::WatchModes watchModes = KDirWatch::WatchDirOnly);
200  bool removeEntry(KDirWatch*,const QString&, Entry* sub_entry);
201  void removeEntry(KDirWatch*,Entry* e, Entry* sub_entry);
202  bool stopEntryScan(KDirWatch*, Entry*);
203  bool restartEntryScan(KDirWatch*, Entry*, bool );
204  void stopScan(KDirWatch*);
205  void startScan(KDirWatch*, bool, bool);
206 
207  void removeEntries(KDirWatch*);
208  void statistics();
209 
210  void addWatch(Entry* entry);
211  void removeWatch(Entry* entry);
212  Entry* entry(const QString&);
213  int scanEntry(Entry* e);
214  void emitEvent(const Entry* e, int event, const QString &fileName = QString());
215 
216  // Memory management - delete when last KDirWatch gets deleted
217  void ref() { m_ref++; }
218  bool deref() { return ( --m_ref == 0 ); }
219 
220  static bool isNoisyFile( const char *filename );
221 
222 public Q_SLOTS:
223  void slotRescan();
224  void famEventReceived(); // for FAM
225  void inotifyEventReceived(); // for inotify
226  void slotRemoveDelayed();
227  void fswEventReceived(const QString &path); // for QFileSystemWatcher
228 
229 public:
230  QTimer timer;
231  EntryMap m_mapEntries;
232 
233  KDirWatch::Method m_preferredMethod, m_nfsPreferredMethod;
234  int freq;
235  int statEntries;
236  int m_nfsPollInterval, m_PollInterval;
237  int m_ref;
238  bool useStat(Entry*);
239 
240  // removeList is allowed to contain any entry at most once
241  QSet<Entry *> removeList;
242  bool delayRemove;
243 
244  bool rescan_all;
245  QTimer rescan_timer;
246 
247 #ifdef HAVE_FAM
248  QSocketNotifier *sn;
249  FAMConnection fc;
250  bool use_fam;
251 
252  void checkFAMEvent(FAMEvent*);
253  bool useFAM(Entry*);
254 #endif
255 
256 #ifdef HAVE_SYS_INOTIFY_H
257  QSocketNotifier *mSn;
258  bool supports_inotify;
259  int m_inotify_fd;
260 
261  bool useINotify(Entry*);
262 #endif
263 #ifdef HAVE_QFILESYSTEMWATCHER
264  KFileSystemWatcher *fsWatcher;
265  bool useQFSWatch(Entry* e);
266 #endif
267 
268  bool _isStopped;
269 };
270 
271 QDebug operator<<(QDebug debug, const KDirWatchPrivate::Entry &entry);
272 
273 #endif // KDIRWATCH_P_H
274 
KDirWatchPrivate::ref
void ref()
Definition: kdirwatch_p.h:217
KDirWatchPrivate::Entry::parentDirectory
QString parentDirectory() const
Definition: kdirwatch.cpp:520
KDirWatchPrivate::Entry::inotifyClientsForFileOrDir
QList< Client * > inotifyClientsForFileOrDir(bool isDir) const
Definition: kdirwatch.cpp:549
KDirWatchPrivate::Entry::clientsForFileOrDir
QList< Client * > clientsForFileOrDir(const QString &tpath, bool *isDir) const
Definition: kdirwatch.cpp:525
KDirWatchPrivate::famEventReceived
void famEventReceived()
Definition: kdirwatch.cpp:1658
KFileSystemWatcher::KFileSystemWatcher
KFileSystemWatcher()
Definition: kdirwatch_win.cpp:23
KDirWatchPrivate::Entry::m_clients
QList< Client * > m_clients
Definition: kdirwatch_p.h:149
KDirWatchPrivate::useFreq
void useFreq(Entry *e, int newFreq)
Definition: kdirwatch.cpp:606
KDirWatchPrivate::Entry
Definition: kdirwatch_p.h:136
KDirWatchPrivate::removeEntry
bool removeEntry(KDirWatch *, const QString &, Entry *sub_entry)
Definition: kdirwatch.cpp:994
KDirWatchPrivate::isNoisyFile
static bool isNoisyFile(const char *filename)
Definition: kdirwatch.cpp:1501
KDirWatchPrivate::Entry::isValid
bool isValid()
Definition: kdirwatch_p.h:160
KDirWatchPrivate::rescan_timer
QTimer rescan_timer
Definition: kdirwatch_p.h:245
KDirWatch::WatchDirOnly
Watch just the specified directory.
Definition: kdirwatch.h:74
KDirWatchPrivate::stopScan
void stopScan(KDirWatch *)
Definition: kdirwatch.cpp:1182
KDirWatchPrivate::stopEntryScan
bool stopEntryScan(KDirWatch *, Entry *)
Definition: kdirwatch.cpp:1095
kdirwatch.h
KDirWatchPrivate::INotifyMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::FAMMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::m_mapEntries
EntryMap m_mapEntries
Definition: kdirwatch_p.h:231
KDirWatch::Method
Method
Definition: kdirwatch.h:223
KDirWatchPrivate::Client
Definition: kdirwatch_p.h:126
KDirWatchPrivate::scanEntry
int scanEntry(Entry *e)
Definition: kdirwatch.cpp:1219
KDirWatchPrivate::Entry::m_status
entryStatus m_status
Definition: kdirwatch_p.h:145
KDirWatchPrivate::Created
Definition: kdirwatch_p.h:123
KDirWatchPrivate::restartEntryScan
bool restartEntryScan(KDirWatch *, Entry *, bool)
Definition: kdirwatch.cpp:1121
QString
KDirWatchPrivate::slotRescan
void slotRescan()
Definition: kdirwatch.cpp:1387
KDirWatchPrivate::NoChange
Definition: kdirwatch_p.h:123
QHash< QFileSystemWatcher *, uint >
KDirWatchPrivate::NonExistent
Definition: kdirwatch_p.h:121
KDirWatchPrivate::Entry::m_entries
QList< Entry * > m_entries
Definition: kdirwatch_p.h:151
KDirWatchPrivate::Entry::m_ino
ino_t m_ino
Definition: kdirwatch_p.h:144
KDirWatchPrivate::useStat
bool useStat(Entry *)
Definition: kdirwatch.cpp:734
QObject
KDirWatchPrivate::QFSWatchMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::removeEntries
void removeEntries(KDirWatch *)
Definition: kdirwatch.cpp:1060
KDirWatchPrivate::Client::pending
int pending
Definition: kdirwatch_p.h:132
KDirWatchPrivate::Client::m_watchModes
KDirWatch::WatchModes m_watchModes
Definition: kdirwatch_p.h:133
KDirWatchPrivate::Client::instance
KDirWatch * instance
Definition: kdirwatch_p.h:127
operator<<
QDebug operator<<(QDebug debug, const KDirWatchPrivate::Entry &entry)
Definition: kdirwatch.cpp:561
KFileSystemWatcher::removePath
void removePath(const QString &file)
Definition: kdirwatch_win.cpp:68
KFileSystemWatcher::fileChanged
void fileChanged(const QString &path)
KDirWatchPrivate::Entry::path
QString path
Definition: kdirwatch_p.h:152
KDirWatchPrivate::~KDirWatchPrivate
~KDirWatchPrivate()
Definition: kdirwatch.cpp:250
KDirWatchPrivate::Client::count
int count
Definition: kdirwatch_p.h:128
KDirWatchPrivate::UnknownMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::resetList
void resetList(KDirWatch *, bool)
Definition: kdirwatch.cpp:1205
KDirWatchPrivate::statistics
void statistics()
Definition: kdirwatch.cpp:1665
KDirWatchPrivate::StatMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::useQFSWatch
bool useQFSWatch(Entry *e)
Definition: kdirwatch.cpp:713
KDirWatchPrivate::emitEvent
void emitEvent(const Entry *e, int event, const QString &fileName=QString())
Definition: kdirwatch.cpp:1314
KDirWatchPrivate::m_preferredMethod
KDirWatch::Method m_preferredMethod
Definition: kdirwatch_p.h:233
KDirWatchPrivate::m_nfsPreferredMethod
KDirWatch::Method m_nfsPreferredMethod
Definition: kdirwatch_p.h:233
KDirWatchPrivate::Entry::propagate_dirty
void propagate_dirty()
Definition: kdirwatch.cpp:453
KDirWatchPrivate::fsWatcher
KFileSystemWatcher * fsWatcher
Definition: kdirwatch_p.h:264
KDirWatchPrivate::KDirWatchPrivate
KDirWatchPrivate()
Definition: kdirwatch.cpp:153
KDirWatchPrivate::fswEventReceived
void fswEventReceived(const QString &path)
Definition: kdirwatch.cpp:1706
KDirWatchPrivate::entryStatus
entryStatus
Definition: kdirwatch_p.h:121
KDirWatchPrivate::timer
QTimer timer
Definition: kdirwatch_p.h:230
KDirWatchPrivate::removeWatch
void removeWatch(Entry *entry)
Definition: kdirwatch.cpp:967
KDirWatchPrivate::m_PollInterval
int m_PollInterval
Definition: kdirwatch_p.h:236
KDirWatchPrivate::Entry::m_mode
entryMode m_mode
Definition: kdirwatch_p.h:146
KDirWatchPrivate::DNotifyMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::Normal
Definition: kdirwatch_p.h:121
KDirWatchPrivate::Entry::m_ctime
time_t m_ctime
Definition: kdirwatch_p.h:140
QSet
Definition: k3resolver.h:41
KDirWatchPrivate::startScan
void startScan(KDirWatch *, bool, bool)
Definition: kdirwatch.cpp:1190
KDirWatchPrivate::Entry::msecLeft
int msecLeft
Definition: kdirwatch_p.h:154
KDirWatchPrivate::inotifyEventReceived
void inotifyEventReceived()
Definition: kdirwatch.cpp:271
KDirWatchPrivate::rescan_all
bool rescan_all
Definition: kdirwatch_p.h:244
KFileSystemWatcher
Definition: kdirwatch_p.h:88
KDirWatchPrivate::Entry::removeClient
void removeClient(KDirWatch *)
Definition: kdirwatch.cpp:493
KDirWatchPrivate::Entry::m_nlink
int m_nlink
Definition: kdirwatch_p.h:142
KDirWatchPrivate
Definition: kdirwatch_p.h:116
KDirWatchPrivate::removeList
QSet< Entry * > removeList
Definition: kdirwatch_p.h:241
KFileSystemWatcher::addPath
void addPath(const QString &file)
Definition: kdirwatch_win.cpp:60
KDirWatchPrivate::entryMode
entryMode
Definition: kdirwatch_p.h:122
KDirWatchPrivate::EntryMap
QMap< QString, Entry > EntryMap
Definition: kdirwatch_p.h:191
KDirWatchPrivate::Entry::dirty
bool dirty
Definition: kdirwatch_p.h:170
KDirWatchPrivate::m_nfsPollInterval
int m_nfsPollInterval
Definition: kdirwatch_p.h:236
KDirWatchPrivate::Client::watchingStopped
bool watchingStopped
Definition: kdirwatch_p.h:130
KDirWatchPrivate::entry
Entry * entry(const QString &)
Definition: kdirwatch.cpp:586
KDirWatchPrivate::Entry::freq
int freq
Definition: kdirwatch_p.h:154
KFileSystemWatcher::~KFileSystemWatcher
~KFileSystemWatcher()
Definition: kdirwatch_win.cpp:28
KDirWatch
Class for watching directory and file changes.
Definition: kdirwatch.h:64
KDirWatchPrivate::Entry::clientCount
int clientCount() const
Definition: kdirwatch.cpp:511
KDirWatchPrivate::deref
bool deref()
Definition: kdirwatch_p.h:218
KDirWatchPrivate::slotRemoveDelayed
void slotRemoveDelayed()
Definition: kdirwatch.cpp:1372
KDirWatchPrivate::statEntries
int statEntries
Definition: kdirwatch_p.h:235
KDirWatchPrivate::Entry::isDir
bool isDir
Definition: kdirwatch_p.h:147
KDirWatchPrivate::delayRemove
bool delayRemove
Definition: kdirwatch_p.h:242
KDirWatchPrivate::Deleted
Definition: kdirwatch_p.h:123
KDirWatchPrivate::Entry::findSubEntry
Entry * findSubEntry(const QString &path) const
Definition: kdirwatch_p.h:162
KDirWatchPrivate::freq
int freq
Definition: kdirwatch_p.h:234
KDirWatchPrivate::Entry::addClient
void addClient(KDirWatch *, KDirWatch::WatchModes)
Definition: kdirwatch.cpp:469
KDirWatchPrivate::addEntry
void addEntry(KDirWatch *instance, const QString &_path, Entry *sub_entry, bool isDir, KDirWatch::WatchModes watchModes=KDirWatch::WatchDirOnly)
Definition: kdirwatch.cpp:763
KFileSystemWatcher::directoryChanged
void directoryChanged(const QString &path)
QMap
KDirWatchPrivate::Changed
Definition: kdirwatch_p.h:123
KDirWatchPrivate::addWatch
void addWatch(Entry *entry)
Definition: kdirwatch.cpp:919
QList< QFileSystemWatcher * >
KDirWatchPrivate::_isStopped
bool _isStopped
Definition: kdirwatch_p.h:268
KDirWatchPrivate::m_ref
int m_ref
Definition: kdirwatch_p.h:237
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs API Reference

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

Search



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

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