9 #include <linux/limits.h>
11 #include <sys/inotify.h>
13 #include <sys/types.h>
23 namespace file_watcher {
30 int read_pipe,
int write_pipe) {
34 for (FileWatcher::HandlerMap::const_iterator it = handlers.begin();
44 struct pollfd poll_set[2];
46 poll_set[0].fd = read_pipe;
47 poll_set[0].events = POLLHUP | POLLIN;
48 poll_set[0].revents = 0;
50 poll_set[1].events = POLLIN;
51 poll_set[1].revents = 0;
55 int ready = poll(poll_set, 2, -1);
61 "FileWatcherInotify - Could not poll events. Errno: %d", errno);
68 if (poll_set[0].revents & POLLHUP) {
73 if (poll_set[0].revents & POLLIN) {
81 const size_t event_size =
sizeof(
struct inotify_event);
83 const size_t buffer_size = event_size + PATH_MAX + 1;
84 char buffer[buffer_size];
85 if (poll_set[1].revents & POLLIN) {
91 *inotify_event =
reinterpret_cast<struct inotify_event *
>(
93 std::map<int, WatchRecord>::const_iterator it =
watch_records_.find(
98 if (inotify_event->mask & IN_DELETE_SELF) {
100 }
else if (inotify_event->mask & IN_CLOSE_WRITE) {
103 }
else if (inotify_event->mask & IN_MOVE_SELF) {
106 }
else if (inotify_event->mask & IN_ATTRIB) {
109 }
else if (inotify_event->mask & IN_IGNORED) {
114 bool clear_handler =
true;
121 "FileWatcherInotify - Unknown event 0x%x\n",
122 inotify_event->mask);
128 if (!clear_handler) {
135 "FileWatcherInotify - Unknown event ident: %d",
139 i += event_size + inotify_event->len;
152 return inotify_add_watch(
154 IN_ATTRIB | IN_CLOSE_WRITE | IN_DELETE_SELF | IN_MOVE_SELF);
void RegisterFilter(const std::string &file_path, EventHandler *handler)
virtual ~FileWatcherInotify()
assert((mem||(size==0))&&"Out Of Memory")
std::map< std::string, EventHandler * > HandlerMap
file_watcher::EventHandler * handler_
virtual int TryRegisterFilter(const std::string &file_path)
virtual bool Handle(const std::string &file_path, Event event, bool *clear_handler)=0
std::map< int, WatchRecord > watch_records_
void WritePipe(int fd, const void *buf, size_t nbyte)
void ReadPipe(int fd, void *buf, size_t nbyte)
virtual bool RunEventLoop(const FileWatcher::HandlerMap &handler, int read_pipe, int write_pipe)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)