summaryrefslogtreecommitdiff
path: root/shared/inotify/inotify-cxx.h
blob: 4170972fe44bbc143ce76333673ae99bb5325da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889

/// inotify C++ interface header
/**
 * \file inotify-cxx.h
 * 
 * inotify C++ interface
 * 
 * Copyright (C) 2006, 2007 Lukas Jelinek, <lukas@aiken.cz>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of one of the following licenses:
 *
 * \li 1. X11-style license (see LICENSE-X11)
 * \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
 * \li 3. GNU General Public License, version 2  (see LICENSE-GPL)
 *
 * If you want to help with choosing the best license for you,
 * please visit http://www.gnu.org/licenses/license-list.html.
 * 
 */





#ifndef _INOTIFYCXX_H_
#define _INOTIFYCXX_H_

#include <stdint.h>
#include <string>
#include <deque>
#include <map>

// Please ensure that the following headers take the right place
#include <sys/syscall.h>
#include <sys/inotify.h>

// Use this if syscalls not defined
#ifndef __NR_inotify_init
#include <sys/inotify-syscalls.h>
#endif // __NR_inotify_init

/// Event struct size
#define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))

/// Event buffer length
#define INOTIFY_BUFLEN (1024 * (INOTIFY_EVENT_SIZE + 16))

/// Helper macro for creating exception messages.
/**
 * It prepends the message by the function name.
 */
#define IN_EXC_MSG(msg) (std::string(__PRETTY_FUNCTION__) + ": " + msg)

/// inotify capability/limit identifiers 
typedef enum
{
  IN_MAX_EVENTS     = 0,  ///< max. events in the kernel queue
  IN_MAX_INSTANCES  = 1,  ///< max. inotify file descriptors per process
  IN_MAX_WATCHES    = 2   ///< max. watches per file descriptor
} InotifyCapability_t;

/// inotify-cxx thread safety
/**
 * If this symbol is defined you can use this interface safely
 * threaded applications. Remember that it slightly degrades
 * performance.
 * 
 * Even if INOTIFY_THREAD_SAFE is defined some classes stay
 * unsafe. If you must use them (must you?) in more than one
 * thread concurrently you need to implement explicite locking.
 * 
 * You need not to define INOTIFY_THREAD_SAFE in that cases
 * where the application is multithreaded but all the inotify
 * infrastructure will be managed only in one thread. This is
 * the recommended way.
 * 
 * Locking may fail (it is very rare but not impossible). In this
 * case an exception is thrown. But if unlocking fails in case
 * of an error it does nothing (this failure is ignored).
 */
#ifdef INOTIFY_THREAD_SAFE

#include <pthread.h>

#define IN_LOCK_DECL mutable pthread_rwlock_t __m_lock;

#define IN_LOCK_INIT \
  { \
    pthread_rwlockattr_t attr; \
    int res = 0; \
    if ((res = pthread_rwlockattr_init(&attr)) != 0) \
      throw InotifyException(IN_EXC_MSG("cannot initialize lock attributes"), res, this); \
    if ((res = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP)) != 0) \
      throw InotifyException(IN_EXC_MSG("cannot set lock kind"), res, this); \
    if ((res = pthread_rwlock_init(&__m_lock, &attr)) != 0) \
      throw InotifyException(IN_EXC_MSG("cannot initialize lock"), res, this); \
    pthread_rwlockattr_destroy(&attr); \
  }
 
#define IN_LOCK_DONE pthread_rwlock_destroy(&__m_lock);

#define IN_READ_BEGIN \
  { \
    int res = pthread_rwlock_rdlock(&__m_lock); \
    if (res != 0) \
      throw InotifyException(IN_EXC_MSG("locking for reading failed"), res, (void*) this); \
  }
  
#define IN_READ_END \
  { \
    int res = pthread_rwlock_unlock(&__m_lock); \
    if (res != 0) \
      throw InotifyException(IN_EXC_MSG("unlocking failed"), res, (void*) this); \
  }
  
#define IN_READ_END_NOTHROW pthread_rwlock_unlock(&__m_lock);
  
#define IN_WRITE_BEGIN \
  { \
    int res = pthread_rwlock_wrlock(&__m_lock); \
    if (res != 0) \
      throw InotifyException(IN_EXC_MSG("locking for writing failed"), res, (void*) this); \
  }
  
#define IN_WRITE_END IN_READ_END
#define IN_WRITE_END_NOTHROW IN_READ_END_NOTHROW

#else // INOTIFY_THREAD_SAFE

#define IN_LOCK_DECL
#define IN_LOCK_INIT
#define IN_LOCK_DONE
#define IN_READ_BEGIN
#define IN_READ_END
#define IN_READ_END_NOTHROW
#define IN_WRITE_BEGIN
#define IN_WRITE_END
#define IN_WRITE_END_NOTHROW

#endif // INOTIFY_THREAD_SAFE




// forward declaration
class InotifyWatch;
class Inotify;


/// Class for inotify exceptions
/**
 * This class allows to acquire information about exceptional
 * events. It makes easier to log or display error messages
 * and to identify problematic code locations.
 * 
 * Although this class is basically thread-safe it is not intended
 * to be shared between threads.
 */
class InotifyException
{
public:
  /// Constructor
  /**
   * \param[in] rMsg message
   * \param[in] iErr error number (see errno.h)
   * \param[in] pSrc source
   */
  InotifyException(const std::string& rMsg = "", int iErr = 0, void* pSrc = NULL)
  : m_msg(rMsg),
    m_err(iErr)
  {
    m_pSrc = pSrc;
  }
  
  /// Returns the exception message.
  /**
   * \return message
   */
  inline const std::string& GetMessage() const
  {
    return m_msg;
  }
  
  /// Returns the exception error number.
  /**
   * If not applicable this value is 0 (zero).
   * 
   * \return error number (standardized; see errno.h)
   */
  inline int GetErrorNumber() const
  {
    return m_err;
  } 
  
  /// Returns the exception source.
  /**
   * \return source
   */
  inline void* GetSource() const
  {
    return m_pSrc;
  }

protected:
  std::string m_msg;      ///< message
  int m_err;              ///< error number
  mutable void* m_pSrc;   ///< source
};


/// inotify event class
/**
 * It holds all information about inotify event and provides
 * access to its particular values.
 * 
 * This class is not (and is not intended to be) thread-safe
 * and therefore it must not be used concurrently in multiple
 * threads.
 */
class InotifyEvent
{
public:
  /// Constructor.
  /**
   * Creates a plain event.
   */
  InotifyEvent()
  : m_uMask(0),
    m_uCookie(0)
  {
    m_pWatch = NULL;
  }
  
  /// Constructor.
  /**
   * Creates an event based on inotify event data.
   * For NULL pointers it works the same way as InotifyEvent().
   * 
   * \param[in] pEvt event data
   * \param[in] pWatch inotify watch
   */
  InotifyEvent(const struct inotify_event* pEvt, InotifyWatch* pWatch)
  : m_uMask(0),
    m_uCookie(0)
  {
    if (pEvt != NULL) {
      m_uMask = (uint32_t) pEvt->mask;
      m_uCookie = (uint32_t) pEvt->cookie;
      if (pEvt->name != NULL) {
        m_name = pEvt->len > 0
            ? pEvt->name
            : "";
      }
      m_pWatch = pWatch;
    }
    else {
      m_pWatch = NULL;
    }
  }
  
  /// Destructor.
  ~InotifyEvent() {}
  
  /// Returns the event watch descriptor.
  /**
   * \return watch descriptor
   * 
   * \sa InotifyWatch::GetDescriptor()
   */
  int32_t GetDescriptor() const;
  
  /// Returns the event mask.
  /**
   * \return event mask
   * 
   * \sa InotifyWatch::GetMask()
   */
  inline uint32_t GetMask() const
  {
    return m_uMask;
  }
  
  /// Checks a value for the event type.
  /**
   * \param[in] uValue checked value
   * \param[in] uType type which is checked for
   * \return true = the value contains the given type, false = otherwise
   */
  inline static bool IsType(uint32_t uValue, uint32_t uType)
  {
    return ((uValue & uType) != 0) && ((~uValue & uType) == 0);
  }
  
  /// Checks for the event type.
  /**
   * \param[in] uType type which is checked for
   * \return true = event mask contains the given type, false = otherwise
   */
  inline bool IsType(uint32_t uType) const
  {
    return IsType(m_uMask, uType);
  }
  
  /// Returns the event cookie.
  /**
   * \return event cookie
   */
  inline uint32_t GetCookie() const
  {
    return m_uCookie;
  }
  
  /// Returns the event name length.
  /**
   * \return event name length
   */
  inline uint32_t GetLength() const
  {
    return (uint32_t) m_name.length();
  }
  
  /// Returns the event name.
  /**
   * \return event name
   */
  inline const std::string& GetName() const
  {
    return m_name;
  }
  
  /// Extracts the event name.
  /**
   * \param[out] rName event name
   */
  inline void GetName(std::string& rName) const
  {
    rName = GetName();
  }
  
  /// Returns the source watch.
  /**
   * \return source watch
   */
  inline InotifyWatch* GetWatch()
  {
    return m_pWatch;
  }
  
  /// Finds the appropriate mask for a name.
  /**
   * \param[in] rName mask name
   * \return mask for name; 0 on failure
   */
  static uint32_t GetMaskByName(const std::string& rName);
  
  /// Fills the string with all types contained in an event mask value.
  /**
   * \param[in] uValue event mask value
   * \param[out] rStr dumped event types
   */
  static void DumpTypes(uint32_t uValue, std::string& rStr);
  
  /// Fills the string with all types contained in the event mask.
  /**
   * \param[out] rStr dumped event types
   */
  void DumpTypes(std::string& rStr) const;
  
private:
  uint32_t m_uMask;           ///< mask
  uint32_t m_uCookie;         ///< cookie
  std::string m_name;         ///< name
  InotifyWatch* m_pWatch;     ///< source watch
};



/// inotify watch class
/**
 * It holds information about the inotify watch on a particular
 * inode.
 * 
 * If the INOTIFY_THREAD_SAFE is defined this class is thread-safe.
 */
class InotifyWatch
{
public:
  /// Constructor.
  /**
   * Creates an inotify watch. Because this watch is
   * inactive it has an invalid descriptor (-1).
   * 
   * \param[in] rPath watched file path
   * \param[in] uMask mask for events
   * \param[in] fEnabled events enabled yes/no
   */
  InotifyWatch(const std::string& rPath, int32_t uMask, bool fEnabled = true)
  : m_path(rPath),
    m_uMask(uMask),
    m_wd((int32_t) -1),
    m_fEnabled(fEnabled)
  {
    IN_LOCK_INIT
  }
  
  /// Destructor.
  ~InotifyWatch()
  {
    IN_LOCK_DONE
  }
  
  /// Returns the watch descriptor.
  /**
   * \return watch descriptor; -1 for inactive watch
   */
  inline int32_t GetDescriptor() const
  {
    return m_wd;
  }
  
  /// Returns the watched file path.
  /**
   * \return file path
   */
  inline const std::string& GetPath() const
  {
    return m_path;
  }
  
  /// Returns the watch event mask.
  /**
   * \return event mask
   */
  inline uint32_t GetMask() const
  {
    return (uint32_t) m_uMask;
  }
  
  /// Sets the watch event mask.
  /**
   * If the watch is active (added to an instance of Inotify)
   * this method may fail due to unsuccessful re-setting
   * the watch in the kernel.
   * 
   * \param[in] uMask event mask
   * 
   * \throw InotifyException thrown if changing fails
   */
  void SetMask(uint32_t uMask) throw (InotifyException);   
  
  /// Returns the appropriate inotify class instance.
  /**
   * \return inotify instance
   */
  inline Inotify* GetInotify()
  {
    return m_pInotify;
  }
  
  /// Enables/disables the watch.
  /**
   * If the watch is active (added to an instance of Inotify)
   * this method may fail due to unsuccessful re-setting
   * the watch in the kernel.
   * 
   * Re-setting the current state has no effect.
   * 
   * \param[in] fEnabled set enabled yes/no
   * 
   * \throw InotifyException thrown if enabling/disabling fails
   */
  void SetEnabled(bool fEnabled) throw (InotifyException);
  
  /// Checks whether the watch is enabled.
  /**
   * \return true = enables, false = disabled
   */
  inline bool IsEnabled() const
  {
    return m_fEnabled;
  }
  
  /// Checks whether the watch is recursive.
  /**
   * A recursive watch monitors a directory itself and all
   * its subdirectories. This watch is a logical object
   * which may have many underlying kernel watches.
   * 
   * \return currently always false (recursive watches not yet supported)
   * \attention Recursive watches are currently NOT supported.
   *            They are planned for future versions.
   */
  inline bool IsRecursive() const
  {
    return false;    
  }
  
private:
  friend class Inotify;

  std::string m_path;   ///< watched file path
  uint32_t m_uMask;     ///< event mask
  int32_t m_wd;         ///< watch descriptor
  Inotify* m_pInotify;  ///< inotify object
  bool m_fEnabled;      ///< events enabled yes/no
  
  IN_LOCK_DECL
  
  /// Disables the watch (due to removing by the kernel).
  /**
   * This method must be called after receiving an event.
   * It ensures the watch object is consistent with the kernel
   * data. 
   */
  void __Disable();
};


/// Mapping from watch descriptors to watch objects.
typedef std::map<int32_t, InotifyWatch*> IN_WATCH_MAP;

/// Mapping from paths to watch objects.
typedef std::map<std::string, InotifyWatch*> IN_WP_MAP;


/// inotify class
/**
 * It holds information about the inotify device descriptor
 * and manages the event queue.
 * 
 * If the INOTIFY_THREAD_SAFE is defined this class is thread-safe.
 */
class Inotify
{
public:
  /// Constructor.
  /**
   * Creates and initializes an instance of inotify communication
   * object (opens the inotify device).
   * 
   * \throw InotifyException thrown if inotify isn't available
   */
  Inotify() throw (InotifyException);
  
  /// Destructor.
  /**
   * Calls Close() due to clean-up.
   */
  ~Inotify();
  
  /// Removes all watches and closes the inotify device.
  void Close();
    
  /// Adds a new watch.
  /**
   * \param[in] pWatch inotify watch
   * 
   * \throw InotifyException thrown if adding failed
   */
  void Add(InotifyWatch* pWatch) throw (InotifyException);
  
  /// Adds a new watch.
  /**
   * \param[in] rWatch inotify watch
   * 
   * \throw InotifyException thrown if adding failed
   */
  inline void Add(InotifyWatch& rWatch) throw (InotifyException)
  {
    Add(&rWatch);
  }
  
  /// Removes a watch.
  /**
   * If the given watch is not present it does nothing.
   * 
   * \param[in] pWatch inotify watch
   * 
   * \throw InotifyException thrown if removing failed
   */
  void Remove(InotifyWatch* pWatch) throw (InotifyException);
  
  /// Removes a watch.
  /**
   * If the given watch is not present it does nothing.
   * 
   * \param[in] rWatch inotify watch
   * 
   * \throw InotifyException thrown if removing failed
   */
  inline void Remove(InotifyWatch& rWatch) throw (InotifyException)
  {
    Remove(&rWatch);
  }
  
  /// Removes all watches.
  void RemoveAll();
  
  /// Returns the count of watches.
  /**
   * This is the total count of all watches (regardless whether
   * enabled or not).
   * 
   * \return count of watches
   * 
   * \sa GetEnabledCount()
   */
  inline size_t GetWatchCount() const
  {
    IN_READ_BEGIN
    size_t n = (size_t) m_paths.size();
    IN_READ_END
    return n;
  }
  
  /// Returns the count of enabled watches.
  /**
   * \return count of enabled watches
   * 
   * \sa GetWatchCount()
   */  
  inline size_t GetEnabledCount() const
  {
    IN_READ_BEGIN
    size_t n = (size_t) m_watches.size();
    IN_READ_END
    return n;
  }
  
  /// Waits for inotify events.
  /**
   * It waits until one or more events occur. When called
   * in nonblocking mode it only retrieves occurred events
   * to the internal queue and exits.
   * 
   * \param[in] fNoIntr if true it re-calls the system call after a handled signal
   * 
   * \throw InotifyException thrown if reading events failed
   * 
   * \sa SetNonBlock()
   */
  void WaitForEvents(bool fNoIntr = false) throw (InotifyException);
  
  /// Returns the count of received and queued events.
  /**
   * This number is related to the events in the queue inside
   * this object, not to the events pending in the kernel.
   * 
   * \return count of events
   */
  inline size_t GetEventCount()
  {
    IN_READ_BEGIN
    size_t n = (size_t) m_events.size();
    IN_READ_END
    return n;
  }
  
  /// Extracts a queued inotify event.
  /**
   * The extracted event is removed from the queue.
   * If the pointer is NULL it does nothing.
   * 
   * \param[in,out] pEvt event object
   * 
   * \throw InotifyException thrown if the provided pointer is NULL
   */
  bool GetEvent(InotifyEvent* pEvt) throw (InotifyException);
  
  /// Extracts a queued inotify event.
  /**
   * The extracted event is removed from the queue.
   * 
   * \param[in,out] rEvt event object
   * 
   * \throw InotifyException thrown only in very anomalous cases
   */
  bool GetEvent(InotifyEvent& rEvt) throw (InotifyException)
  {
    return GetEvent(&rEvt);
  }
  
  /// Extracts a queued inotify event (without removing).
  /**
   * The extracted event stays in the queue.
   * If the pointer is NULL it does nothing.
   * 
   * \param[in,out] pEvt event object
   * 
   * \throw InotifyException thrown if the provided pointer is NULL
   */
  bool PeekEvent(InotifyEvent* pEvt) throw (InotifyException);
  
  /// Extracts a queued inotify event (without removing).
  /**
   * The extracted event stays in the queue.
   * 
   * \param[in,out] rEvt event object
   * 
   * \throw InotifyException thrown only in very anomalous cases
   */
  bool PeekEvent(InotifyEvent& rEvt) throw (InotifyException)
  {
    return PeekEvent(&rEvt);
  }
  
  /// Searches for a watch by a watch descriptor.
  /**
   * It tries to find a watch by the given descriptor.
   * 
   * \param[in] iDescriptor watch descriptor
   * \return pointer to a watch; NULL if no such watch exists
   */
  InotifyWatch* FindWatch(int iDescriptor);
  
  /// Searches for a watch by a filesystem path.
  /**
   * It tries to find a watch by the given filesystem path.
   * 
   * \param[in] rPath filesystem path
   * \return pointer to a watch; NULL if no such watch exists
   * 
   * \attention The path must be exactly identical to the one
   *            used for the searched watch. Be careful about
   *            absolute/relative and case-insensitive paths.
   */
   InotifyWatch* FindWatch(const std::string& rPath);
  
  /// Returns the file descriptor.
  /**
   * The descriptor can be used in standard low-level file
   * functions (poll(), select(), fcntl() etc.).
   * 
   * \return valid file descriptor or -1 for inactive object
   * 
   * \sa SetNonBlock()
   */
  inline int GetDescriptor() const
  {
    return m_fd;
  }
  
  /// Enables/disables non-blocking mode.
  /**
   * Use this mode if you want to monitor the descriptor
   * (acquired thru GetDescriptor()) in functions such as
   * poll(), select() etc.
   * 
   * Non-blocking mode is disabled by default.
   * 
   * \param[in] fNonBlock enable/disable non-blocking mode
   * 
   * \throw InotifyException thrown if setting mode failed
   * 
   * \sa GetDescriptor(), SetCloseOnExec()
   */
  void SetNonBlock(bool fNonBlock) throw (InotifyException);
  
  /// Enables/disables closing on exec.
  /**
   * Enable this if you want to close the descriptor when
   * executing another program. Otherwise, the descriptor
   * will be inherited.
   * 
   * Closing on exec is disabled by default.
   * 
   * \param[in] fClOnEx enable/disable closing on exec
   * 
   * \throw InotifyException thrown if setting failed
   * 
   * \sa GetDescriptor(), SetNonBlock()
   */
  void SetCloseOnExec(bool fClOnEx) throw (InotifyException);
  
  /// Acquires a particular inotify capability/limit.
  /**
   * \param[in] cap capability/limit identifier
   * \return capability/limit value
   * \throw InotifyException thrown if the given value cannot be acquired
   */
  static uint32_t GetCapability(InotifyCapability_t cap) throw (InotifyException);
  
  /// Modifies a particular inotify capability/limit.
  /**
   * \param[in] cap capability/limit identifier
   * \param[in] val new capability/limit value
   * \throw InotifyException thrown if the given value cannot be set
   * \attention Using this function requires root privileges.
   *            Beware of setting extensive values - it may seriously
   *            affect system performance and/or stability.
   */
  static void SetCapability(InotifyCapability_t cap, uint32_t val) throw (InotifyException);
  
  /// Returns the maximum number of events in the kernel queue.
  /**
   * \return maximum number of events in the kernel queue
   * \throw InotifyException thrown if the given value cannot be acquired
   */
  inline static uint32_t GetMaxEvents() throw (InotifyException)
  {
    return GetCapability(IN_MAX_EVENTS);
  }
  
  /// Sets the maximum number of events in the kernel queue.
  /**
   * \param[in] val new value
   * \throw InotifyException thrown if the given value cannot be set
   * \attention Using this function requires root privileges.
   *            Beware of setting extensive values - the greater value
   *            is set here the more physical memory may be used for the inotify
   *            infrastructure.
   */
  inline static void SetMaxEvents(uint32_t val) throw (InotifyException)
  {
    SetCapability(IN_MAX_EVENTS, val);
  }
  
  /// Returns the maximum number of inotify instances per process.
  /**
   * It means the maximum number of open inotify file descriptors
   * per running process.
   * 
   * \return maximum number of inotify instances
   * \throw InotifyException thrown if the given value cannot be acquired
   */
  inline static uint32_t GetMaxInstances() throw (InotifyException)
  {
    return GetCapability(IN_MAX_INSTANCES);
  }
  
  /// Sets the maximum number of inotify instances per process.
  /**
   * \param[in] val new value
   * \throw InotifyException thrown if the given value cannot be set
   * \attention Using this function requires root privileges.
   *            Beware of setting extensive values - the greater value
   *            is set here the more physical memory may be used for the inotify
   *            infrastructure.
   */
  inline static void SetMaxInstances(uint32_t val) throw (InotifyException)
  {
    SetCapability(IN_MAX_INSTANCES, val);
  }
  
  /// Returns the maximum number of inotify watches per instance.
  /**
   * It means the maximum number of inotify watches per inotify
   * file descriptor.
   * 
   * \return maximum number of inotify watches
   * \throw InotifyException thrown if the given value cannot be acquired
   */
  inline static uint32_t GetMaxWatches() throw (InotifyException)
  {
    return GetCapability(IN_MAX_WATCHES);
  }
  
  /// Sets the maximum number of inotify watches per instance.
  /**
   * \param[in] val new value
   * \throw InotifyException thrown if the given value cannot be set
   * \attention Using this function requires root privileges.
   *            Beware of setting extensive values - the greater value
   *            is set here the more physical memory may be used for the inotify
   *            infrastructure.
   */
  inline static void SetMaxWatches(uint32_t val) throw (InotifyException)
  {
    SetCapability(IN_MAX_WATCHES, val);
  }

private: 
  int m_fd;                             ///< file descriptor
  IN_WATCH_MAP m_watches;               ///< watches (by descriptors)
  IN_WP_MAP m_paths;                    ///< watches (by paths)
  unsigned char m_buf[INOTIFY_BUFLEN];  ///< buffer for events
  std::deque<InotifyEvent> m_events;    ///< event queue
  
  IN_LOCK_DECL
  
  friend class InotifyWatch;
  
  static std::string GetCapabilityPath(InotifyCapability_t cap) throw (InotifyException);
};


#endif //_INOTIFYCXX_H_

bgstack15