#include #include #include #include #include #include #define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) char buffer[BUF_LEN]; void display_inotify_event(struct inotify_event * e) { printf("inotify_event wd=%d\n", e->wd); // che tipo di evento su file è accaduto? if (e->mask & IN_ACCESS) { printf("IN_ACCESS [read()]\n"); } if (e->mask & IN_ATTRIB) { printf("IN_ATTRIB\n"); } if (e->mask & IN_CLOSE_NOWRITE) { printf("IN_CLOSE_NOWRITE\n"); } if (e->mask & IN_CLOSE_WRITE) { printf("IN_CLOSE_WRITE\n"); } if (e->mask & IN_CREATE) { printf("IN_CREATE\n"); } if (e->mask & IN_DELETE) { printf("IN_DELETE\n"); } if (e->mask & IN_DELETE_SELF) { printf("IN_DELETE_SELF\n"); } if (e->mask & IN_MODIFY) { printf("IN_MODIFY\n"); } if (e->mask & IN_OPEN) { printf("IN_OPEN\n"); } if (e->len > 0) { printf("filename = %s\n", e->name); } else { printf("filename non specificato perchè è il file che sto monitorando\n"); } } int main() { int inotify_fd; char * dir_name = "."; int wd; inotify_fd = inotify_init(); if (inotify_fd == -1) { perror("inotify_init"); exit(1); } wd = inotify_add_watch(inotify_fd, dir_name, IN_ALL_EVENTS); if (wd == -1) { perror("inotify_add_watch"); exit(1); } int bytes_read; char * p; while (1) { bytes_read = read(inotify_fd, buffer, BUF_LEN); if (bytes_read == -1) { perror("read"); exit(1); } printf("read returns %d bytes\n", bytes_read); for (p = buffer; p < buffer + bytes_read; ) { struct inotify_event * event; event = (struct inotify_event *) p; display_inotify_event(event); p = p + sizeof(struct inotify_event) + event->len; } } }