#include #include #include #include #include #define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) int main (int arg, char * argv []) { int notify_d, watch_id; fd_set rfds; int sel_ret; FD_ZERO (&rfds); notify_d = inotify_init (); if (notify_d == -1) { perror ("could not init inotify"); return 1; } FD_SET (notify_d, &rfds); watch_id = inotify_add_watch (notify_d, argv[1], IN_CREATE|IN_OPEN|IN_MODIFY|IN_CLOSE); if (watch_id == -1) { perror ("could not add watch"); return 2; } while (sel_ret = select (notify_d+1, &rfds, NULL, NULL, NULL)) { ssize_t numRead; char buf[BUF_LEN] __attribute__ ((aligned(8))); char * p; struct inotify_event *event; char * ev_name = NULL; numRead = read(notify_d, buf, BUF_LEN); if (numRead == 0) { perror("read() from inotify fd returned 0!"); return 3; } if (numRead == -1) { perror("read"); return 3; } for (p = buf; p < buf + numRead; ) { event = (struct inotify_event *) p; printf ("Watch Descriptor: %d\n", event->wd); switch (event->mask) { case IN_ACCESS: ev_name = "IN_ACCESS"; break; case IN_ATTRIB: ev_name = "IN_ATTRIB"; break; case IN_CLOSE_WRITE: ev_name = "IN_CLOSE_WRITE"; break; case IN_CLOSE_NOWRITE: ev_name = "IN_CLOSE_NOWRITE"; break; case IN_CREATE: ev_name = "IN_CREATE"; break; case IN_DELETE: ev_name = "IN_DELETE"; break; case IN_DELETE_SELF: ev_name = "IN_DELETE_SELF"; break; case IN_MODIFY: ev_name = "IN_MODIFY"; break; case IN_MOVE_SELF: ev_name = "IN_MOVE_SELF"; break; case IN_MOVED_FROM: ev_name = "IN_MOVED_FROM"; break; case IN_MOVED_TO: ev_name = "IN_MOVED_TO"; break; case IN_OPEN: ev_name = "IN_OPEN"; break; } printf ("Mask of Events: %s(%d)\n", ev_name, event->mask); printf ("Events Cookie: %u\n", event->cookie); printf ("Length of name: %u\n", event->len); printf ("Event Name: %s\n", event->name); p += sizeof(struct inotify_event) + event->len; } puts ("---"); FD_SET (notify_d, &rfds); } return 0; }