7 #include <linux/limits.h>
9 #include <sys/inotify.h>
11 #include <sys/types.h>
22 namespace file_watcher {
29 int read_pipe,
int write_pipe) {
33 for (FileWatcher::HandlerMap::const_iterator it = handlers.begin();
34 it != handlers.end(); ++it) {
42 struct pollfd poll_set[2];
44 poll_set[0].fd = read_pipe;
45 poll_set[0].events = POLLHUP | POLLIN;
46 poll_set[0].revents = 0;
48 poll_set[1].events = POLLIN;
49 poll_set[1].revents = 0;
53 int ready = poll(poll_set, 2, -1);
59 "FileWatcherInotify - Could not poll events. Errno: %d", errno);
66 if (poll_set[0].revents & POLLHUP) {
71 if (poll_set[0].revents & POLLIN) {
79 const size_t event_size =
sizeof(
struct inotify_event);
81 const size_t buffer_size = event_size + PATH_MAX + 1;
82 char buffer[buffer_size];
83 if (poll_set[1].revents & POLLIN) {
88 struct inotify_event* inotify_event =
89 reinterpret_cast<struct inotify_event*
>(&buffer[i]);
90 std::map<int, WatchRecord>::const_iterator it =
95 if (inotify_event->mask & IN_DELETE_SELF) {
97 }
else if (inotify_event->mask & IN_CLOSE_WRITE) {
100 }
else if (inotify_event->mask & IN_MOVE_SELF) {
103 }
else if (inotify_event->mask & IN_ATTRIB) {
106 }
else if (inotify_event->mask & IN_IGNORED) {
111 bool clear_handler =
true;
118 "FileWatcherInotify - Unknown event 0x%x\n",
119 inotify_event->mask);
125 if (!clear_handler) {
132 "FileWatcherInotify - Unknown event ident: %d",
136 i += event_size + inotify_event->len;
149 return inotify_add_watch(
151 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")
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< std::string, EventHandler * > HandlerMap
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,...)