Fawkes API  Fawkes Development Version
fam.h
1 
2 /***************************************************************************
3  * fam.h - File Alteration Monitor
4  *
5  * Created: Fri May 23 11:38:41 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef _UTILS_SYSTEM_FAM_H_
24 #define _UTILS_SYSTEM_FAM_H_
25 
26 #include <core/utils/lock_list.h>
27 #include <sys/types.h>
28 
29 #include <map>
30 #include <regex.h>
31 #include <string>
32 
33 namespace fawkes {
34 
36 {
37 public:
38  virtual ~FamListener();
39 
40  static const unsigned int FAM_ACCESS;
41  static const unsigned int FAM_MODIFY;
42  static const unsigned int FAM_ATTRIB;
43  static const unsigned int FAM_CLOSE_WRITE;
44  static const unsigned int FAM_CLOSE_NOWRITE;
45  static const unsigned int FAM_CLOSE;
46  static const unsigned int FAM_OPEN;
47  static const unsigned int FAM_MOVED_FROM;
48  static const unsigned int FAM_MOVED_TO;
49  static const unsigned int FAM_MOVE;
50  static const unsigned int FAM_CREATE;
51  static const unsigned int FAM_DELETE;
52  static const unsigned int FAM_DELETE_SELF;
53  static const unsigned int FAM_MOVE_SELF;
54 
55  static const unsigned int FAM_UNMOUNT;
56  static const unsigned int FAM_Q_OVERFLOW;
57  static const unsigned int FAM_IGNORED;
58 
59  static const unsigned int FAM_ONLYDIR;
60  static const unsigned int FAM_DONT_FOLLOW;
61  static const unsigned int FAM_MASK_ADD;
62  static const unsigned int FAM_ISDIR;
63  static const unsigned int FAM_ONESHOT;
64 
65  static const unsigned int FAM_ALL_EVENTS;
66 
67  virtual void fam_event(const char *filename, unsigned int mask) = 0;
68 };
69 
71 {
72 public:
75 
76  void watch_dir(const char *dirpath);
77  void watch_file(const char *filepath);
78  void add_filter(const char *regex);
79  void reset();
80 
81  void process_events(int timeout = 0);
82  void interrupt();
83 
84  void add_listener(FamListener *listener);
85  void remove_listener(FamListener *listener);
86 
87 private:
88  LockList<FamListener *> listeners_;
90  LockList<regex_t *> regexes_;
92 
93  int inotify_fd_;
94  char * inotify_buf_;
95  size_t inotify_bufsize_;
96 
97  std::map<int, std::string> inotify_watches_;
98  std::map<int, std::string>::iterator inotify_wit_;
99 
100  bool interrupted_;
101  bool interruptible_;
102  int pipe_fds_[2];
103 };
104 
105 } // end of namespace fawkes
106 
107 #endif
fawkes::FileAlterationMonitor::remove_listener
void remove_listener(FamListener *listener)
Remove a listener.
Definition: fam.cpp:272
fawkes::FamListener::FAM_ATTRIB
static const unsigned int FAM_ATTRIB
Metadata changed.
Definition: fam.h:42
fawkes::FamListener::~FamListener
virtual ~FamListener()
Virtual empty destructor.
Definition: fam.cpp:442
fawkes::FileAlterationMonitor
Monitors files for changes.
Definition: fam.h:71
fawkes::FamListener::FAM_DELETE
static const unsigned int FAM_DELETE
Subfile was deleted.
Definition: fam.h:51
fawkes::FileAlterationMonitor::~FileAlterationMonitor
~FileAlterationMonitor()
Destructor.
Definition: fam.cpp:131
fawkes::FamListener
File Alteration Monitor Listener.
Definition: fam.h:36
fawkes::FamListener::FAM_Q_OVERFLOW
static const unsigned int FAM_Q_OVERFLOW
Event queued overflowed.
Definition: fam.h:56
fawkes::FamListener::FAM_ONLYDIR
static const unsigned int FAM_ONLYDIR
Only watch the path if it is a directory.
Definition: fam.h:59
fawkes::FamListener::FAM_CLOSE_WRITE
static const unsigned int FAM_CLOSE_WRITE
Writtable file was closed.
Definition: fam.h:43
fawkes::LockList
List with a lock.
Definition: lock_list.h:45
fawkes::FamListener::FAM_CLOSE
static const unsigned int FAM_CLOSE
Close.
Definition: fam.h:45
fawkes::FamListener::FAM_ONESHOT
static const unsigned int FAM_ONESHOT
Only send event once.
Definition: fam.h:63
fawkes::FileAlterationMonitor::add_filter
void add_filter(const char *regex)
Add a filter.
Definition: fam.cpp:246
fawkes::FamListener::FAM_ACCESS
static const unsigned int FAM_ACCESS
File was accessed.
Definition: fam.h:40
fawkes::FamListener::FAM_IGNORED
static const unsigned int FAM_IGNORED
File was ignored.
Definition: fam.h:57
fawkes::FileAlterationMonitor::watch_file
void watch_file(const char *filepath)
Watch a file.
Definition: fam.cpp:205
fawkes::FamListener::FAM_MASK_ADD
static const unsigned int FAM_MASK_ADD
Add to the mask of an already existing watch.
Definition: fam.h:61
fawkes::FileAlterationMonitor::add_listener
void add_listener(FamListener *listener)
Add a listener.
Definition: fam.cpp:263
fawkes::FamListener::FAM_ISDIR
static const unsigned int FAM_ISDIR
Event occurred against dir.
Definition: fam.h:62
fawkes::FileAlterationMonitor::reset
void reset()
Remove all currently active watches.
Definition: fam.cpp:222
fawkes
Fawkes library namespace.
fawkes::FileAlterationMonitor::process_events
void process_events(int timeout=0)
Process events.
Definition: fam.cpp:283
fawkes::FileAlterationMonitor::watch_dir
void watch_dir(const char *dirpath)
Watch a directory.
Definition: fam.cpp:156
fawkes::FileAlterationMonitor::interrupt
void interrupt()
Interrupt a running process_events().
Definition: fam.cpp:413
fawkes::FamListener::FAM_DELETE_SELF
static const unsigned int FAM_DELETE_SELF
Self was deleted.
Definition: fam.h:52
fawkes::FamListener::FAM_MOVE
static const unsigned int FAM_MOVE
Moves.
Definition: fam.h:49
fawkes::FamListener::FAM_MODIFY
static const unsigned int FAM_MODIFY
File was modified.
Definition: fam.h:41
fawkes::FamListener::FAM_MOVE_SELF
static const unsigned int FAM_MOVE_SELF
Self was moved.
Definition: fam.h:53
fawkes::FamListener::FAM_OPEN
static const unsigned int FAM_OPEN
File was opened.
Definition: fam.h:46
fawkes::FamListener::FAM_MOVED_FROM
static const unsigned int FAM_MOVED_FROM
File was moved from X.
Definition: fam.h:47
fawkes::FileAlterationMonitor::FileAlterationMonitor
FileAlterationMonitor()
Constructor.
Definition: fam.cpp:108
fawkes::FamListener::FAM_MOVED_TO
static const unsigned int FAM_MOVED_TO
File was moved to Y.
Definition: fam.h:48
fawkes::FamListener::FAM_DONT_FOLLOW
static const unsigned int FAM_DONT_FOLLOW
Do not follow a sym link.
Definition: fam.h:60
fawkes::FamListener::FAM_ALL_EVENTS
static const unsigned int FAM_ALL_EVENTS
All events which a program can wait on.
Definition: fam.h:65
fawkes::FamListener::FAM_UNMOUNT
static const unsigned int FAM_UNMOUNT
Backing fs was unmounted.
Definition: fam.h:55
fawkes::FamListener::FAM_CREATE
static const unsigned int FAM_CREATE
Subfile was created.
Definition: fam.h:50
fawkes::FamListener::FAM_CLOSE_NOWRITE
static const unsigned int FAM_CLOSE_NOWRITE
Unwrittable file closed.
Definition: fam.h:44
fawkes::FamListener::fam_event
virtual void fam_event(const char *filename, unsigned int mask)=0
Event has been raised.