00001
00003
00026 #ifndef _INOTIFYCXX_H_
00027 #define _INOTIFYCXX_H_
00028
00029 #include <string>
00030 #include <deque>
00031 #include <map>
00032
00033
00034 #include <sys/inotify.h>
00035 #include <sys/inotify-syscalls.h>
00036
00038 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
00039
00041 #define INOTIFY_BUFLEN (1024 * (INOTIFY_EVENT_SIZE + 16))
00042
00044
00048 class InotifyEvent
00049 {
00050 public:
00052
00055 InotifyEvent()
00056 {
00057 memset(&m_evt, 0, sizeof(m_evt));
00058 m_evt.wd = (int32_t) -1;
00059 }
00060
00062
00068 InotifyEvent(const struct inotify_event* pEvt)
00069 {
00070 if (pEvt != NULL) {
00071 memcpy(&m_evt, pEvt, sizeof(m_evt));
00072 if (pEvt->name != NULL)
00073 m_name = pEvt->name;
00074 }
00075 else {
00076 memset(&m_evt, 0, sizeof(m_evt));
00077 m_evt.wd = (int32_t) -1;
00078 }
00079 }
00080
00082 ~InotifyEvent() {}
00083
00085
00090 inline int32_t GetDescriptor() const
00091 {
00092 return (int32_t) m_evt.wd;
00093 }
00094
00096
00101 inline uint32_t GetMask() const
00102 {
00103 return (uint32_t) m_evt.mask;
00104 }
00105
00107
00111 inline bool IsType(uint32_t uType) const
00112 {
00113 return (((uint32_t) m_evt.mask) & uType) != 0;
00114 }
00115
00117
00120 inline uint32_t GetCookie() const
00121 {
00122 return (uint32_t) m_evt.cookie;
00123 }
00124
00126
00129 inline uint32_t GetLength() const
00130 {
00131 return (uint32_t) m_evt.len;
00132 }
00133
00135
00138 inline const std::string& GetName() const
00139 {
00140 return m_name;
00141 }
00142
00144
00147 inline void GetName(std::string& rName) const
00148 {
00149 rName = GetName();
00150 }
00151
00153
00158 inline void GetData(struct inotify_event* pEvt)
00159 {
00160 if (pEvt != NULL)
00161 memcpy(pEvt, &m_evt, sizeof(m_evt));
00162 }
00163
00165
00168 inline void GetData(struct inotify_event& rEvt)
00169 {
00170 memcpy(&rEvt, &m_evt, sizeof(m_evt));
00171 }
00172
00174
00177 void DumpTypes(std::string& rStr) const;
00178
00179 private:
00180 struct inotify_event m_evt;
00181 std::string m_name;
00182 };
00183
00184
00185 class Inotify;
00186
00187
00189 class InotifyWatch
00190 {
00191 public:
00193
00200 InotifyWatch(const std::string& rPath, int32_t uMask)
00201 {
00202 m_path = rPath;
00203 m_uMask = uMask;
00204 m_wd = (int32_t) -1;
00205 }
00206
00208 ~InotifyWatch() {}
00209
00211
00214 inline int32_t GetDescriptor() const
00215 {
00216 return m_wd;
00217 }
00218
00220
00223 inline const std::string& GetPath() const
00224 {
00225 return m_path;
00226 }
00227
00229
00232 inline uint32_t GetMask() const
00233 {
00234 return (uint32_t) m_uMask;
00235 }
00236
00237 private:
00238 friend class Inotify;
00239
00240 std::string m_path;
00241 uint32_t m_uMask;
00242 int32_t m_wd;
00243 };
00244
00245
00247 typedef std::map<int32_t, InotifyWatch*> IN_WATCH_MAP;
00248
00249
00251 class Inotify
00252 {
00253 public:
00255
00259 Inotify();
00260
00262
00265 ~Inotify();
00266
00268 void Close();
00269
00271
00274 inline bool IsReady() const
00275 {
00276 return m_fd != -1;
00277 }
00278
00280
00284 bool Add(InotifyWatch* pWatch);
00285
00287
00291 inline bool Add(InotifyWatch& rWatch)
00292 {
00293 return Add(&rWatch);
00294 }
00295
00297
00302 void Remove(InotifyWatch* pWatch);
00303
00305
00310 inline void Remove(InotifyWatch& rWatch)
00311 {
00312 Remove(&rWatch);
00313 }
00314
00316 void RemoveAll();
00317
00319
00322 inline size_t GetWatchCount() const
00323 {
00324 return (size_t) m_watches.size();
00325 }
00326
00328
00334 bool WaitForEvents(bool fNoIntr = false);
00335
00337
00343 int GetEventCount();
00344
00346
00353 bool GetEvent(InotifyEvent* pEvt);
00354
00356
00362 bool GetEvent(InotifyEvent& rEvt)
00363 {
00364 return GetEvent(&rEvt);
00365 }
00366
00368
00375 bool PeekEvent(InotifyEvent* pEvt);
00376
00378
00384 bool PeekEvent(InotifyEvent& rEvt)
00385 {
00386 return PeekEvent(&rEvt);
00387 }
00388
00390
00396 InotifyWatch* FindWatch(int iDescriptor);
00397
00398 private:
00399 int m_fd;
00400 IN_WATCH_MAP m_watches;
00401 unsigned char m_buf[INOTIFY_BUFLEN];
00402 std::deque<InotifyEvent> m_events;
00403 };
00404
00405
00406 #endif //_INOTIFYCXX_H_