21 namespace file_watcher {
28 int read_pipe,
int write_pipe) {
33 struct kevent watch_event;
34 EV_SET(&watch_event, read_pipe, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
36 assert(kevent(
kq_, &watch_event, 1, NULL, 0, NULL) != -1);
38 for (FileWatcher::HandlerMap::const_iterator it = handlers.begin();
39 it != handlers.end(); ++it) {
49 std::vector<struct kevent> triggered_events(handlers.size() + 1);
50 int nev = kevent(
kq_, NULL, 0, &triggered_events[0],
51 triggered_events.size(), NULL);
54 "FileWatcherKqueue - Could not poll events. Errno: %d", errno);
60 for (
int i = 0; i < nev && !stop; ++i) {
61 struct kevent& current_ev = triggered_events[i];
62 if (current_ev.ident == static_cast<uint64_t>(read_pipe)) {
70 std::map<int, WatchRecord>::const_iterator it =
73 int current_fd = current_ev.ident;
76 if (current_ev.fflags & NOTE_DELETE) {
78 }
else if (current_ev.fflags & NOTE_LINK) {
81 }
else if (current_ev.fflags & NOTE_WRITE) {
84 }
else if (current_ev.fflags & NOTE_RENAME) {
87 }
else if (current_ev.fflags & NOTE_ATTRIB) {
91 bool clear_handler =
true;
97 "FileWatcherKqueue - Unknown event 0x%x\n",
104 if (!clear_handler) {
111 "FileWatcherKqueue - Unknown kevent ident: %ld",
118 for (std::map<int, WatchRecord>::const_iterator it =
watch_records_.begin();
130 struct kevent remove_event;
131 EV_SET(&remove_event, fd, EVFILT_VNODE, EV_DELETE, NULL, 0, 0);
132 assert(kevent(
kq_, &remove_event, 1, NULL, 0, NULL) != -1);
138 int fd = open(file_path.c_str(), O_RDONLY);
140 struct kevent watch_event;
141 EV_SET(&watch_event, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
142 NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK |
143 NOTE_RENAME | NOTE_REVOKE,
146 assert(kevent(
kq_, &watch_event, 1, NULL, 0, NULL) != -1);
void RegisterFilter(const std::string &file_path, EventHandler *handler)
virtual int TryRegisterFilter(const std::string &file_path)
virtual bool RunEventLoop(const FileWatcher::HandlerMap &handler, int read_pipe, int write_pipe)
assert((mem||(size==0))&&"Out Of Memory")
file_watcher::EventHandler * handler_
virtual ~FileWatcherKqueue()
virtual bool Handle(const std::string &file_path, Event event, bool *clear_handler)=0
std::map< std::string, EventHandler * > HandlerMap
void RemoveFilter(int fd)
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)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)