diff options
Diffstat (limited to 'common/inotify_helpers.h')
| -rw-r--r-- | common/inotify_helpers.h | 66 |
1 files changed, 17 insertions, 49 deletions
diff --git a/common/inotify_helpers.h b/common/inotify_helpers.h index 5bb7647..a0d884a 100644 --- a/common/inotify_helpers.h +++ b/common/inotify_helpers.h @@ -16,65 +16,33 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* The usage is pretty simple: - * - * int my_filedesc = inotify_init(); - * ... - * struct inotify_event *my_event; - * for (INOTIFY_ITR(my_filedesc, my_event)) { - * ... - * } - * - * Easy, right? - */ - #ifndef _INOTIFY_HELPER_H #define _INOTIFY_HELPER_H -#include <sys/inotify.h> -#include <limits.h> /* for NAME_MAX */ -#include <string.h> /* for memset(3) */ -#include <unistd.h> /* for read(3) */ +#include <sys/inotify.h> /* for 'struct inotify_event' */ +#include <sys/types.h> /* for ssize_t, uint32_t */ +#include <limits.h> /* for NAME_MAX */ + +char *inotify_mask2str(uint32_t mask); +int inotify_print_event(struct inotify_event *event); -struct _inotify_buffer { +struct inotify_buffer { ssize_t len; ssize_t pos; char dat[sizeof(struct inotify_event)+NAME_MAX+1]; }; -#define _INOTIFY_ITR_EVENT(BUF) \ - ((struct inotify_event *)&((BUF).dat[(BUF).pos])) -#define _INOTIFY_ITR_EVENT_SIZE(BUF) \ - (sizeof(struct inotify_event) + _INOTIFY_ITR_EVENT(BUF)->len) +struct inotify_event *inotify_next_event_r(int fd, struct inotify_buffer *buf); +struct inotify_event *inotify_next_event(int fd); -#define _INOTIFY_ITR_INIT(FD, BUF, EVENT) \ - struct _inotify_buffer BUF = ({ \ - struct _inotify_buffer tmp; \ - memset(&tmp, 0, sizeof(tmp)); \ - do { \ - tmp.len = read(FD, tmp.dat, sizeof(tmp.dat)); \ - } while (tmp.len == 0); \ - EVENT = (struct inotify_event *)&BUF; \ - tmp; \ - }) -#define _INOTIFY_ITR_CHECK(FD, BUF, EVENT) \ - BUF.len > -1 -#define _INOTIFY_ITR_UPDATE(FD, BUF, EVENT) \ - EVENT = ({ \ - if (BUF.len-(BUF.pos+_INOTIFY_ITR_EVENT_SIZE(BUF)) < 1) { \ - do { \ - BUF.len = read(FD, BUF.dat, sizeof(BUF.dat)); \ - } while (BUF.len == 0); \ - BUF.pos = 0; \ - } else { \ - BUF.pos += _INOTIFY_ITR_EVENT_SIZE(BUF); \ - } \ - _INOTIFY_ITR_EVENT(BUF); \ - }) +#define INOTIFY_ITERATOR(FD, EVENT) \ + struct inotify_event *EVENT = inotify_next_event(FD); \ + EVENT != NULL; \ + EVENT = inotify_next_event(FD) -#define INOTIFY_ITR(FD, EVENT) \ - _INOTIFY_ITR_INIT(FD, _buf, EVENT); \ - _INOTIFY_ITR_CHECK(FD, _buf, EVENT); \ - _INOTIFY_ITR_UPDATE(FD, _buf, EVENT) +#define INOTIFY_ITERATOR_R(FD, EVENT, BUF) \ + struct inotify_event *EVENT = inotify_next_event_r(FD, BUF); \ + EVENT != NULL; \ + EVENT = inotify_next_event_r(FD, BUF) #endif |
