Created
September 8, 2015 14:33
-
-
Save dradtke/c521a6a3aa831b3e6400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <errno.h> | |
| #include <sys/types.h> | |
| #include <linux/inotify.h> | |
| #define EVENT_SIZE (sizeof (struct inotify_event)) | |
| #define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) | |
| int main() | |
| { | |
| int fd = inotify_init(); | |
| if (fd < 0) { | |
| perror("inotify_init"); | |
| } | |
| int wd = inotify_add_watch(fd, "hello.txt", IN_ACCESS | IN_ATTRIB | IN_CLOSE_WRITE | IN_CLOSE_NOWRITE | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO | IN_OPEN); | |
| for (;;) { | |
| int length, i = 0; | |
| char buffer[EVENT_BUF_LEN]; | |
| length = read(fd, buffer, EVENT_BUF_LEN); | |
| if (length < 0) { | |
| perror("read"); | |
| } | |
| while (i < length) { | |
| struct inotify_event *event = (struct inotify_event *) &buffer[i]; | |
| if (event->mask & IN_ACCESS) printf("IN_ACCESS "); | |
| if (event->mask & IN_ATTRIB) printf("IN_ATTRIB "); | |
| if (event->mask & IN_CLOSE_WRITE) printf("IN_CLOSE_WRITE "); | |
| if (event->mask & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE "); | |
| if (event->mask & IN_CREATE) printf("IN_CREATE "); | |
| if (event->mask & IN_DELETE) printf("IN_DELETE "); | |
| if (event->mask & IN_DELETE_SELF) printf("IN_DELETE_SELF "); | |
| if (event->mask & IN_MODIFY) printf("IN_MODIFY "); | |
| if (event->mask & IN_MOVE_SELF) printf("IN_MOVE_SELF "); | |
| if (event->mask & IN_MOVED_FROM) printf("IN_MOVED_FROM "); | |
| if (event->mask & IN_MOVED_TO) printf("IN_MOVED_TO "); | |
| if (event->mask & IN_OPEN) printf("IN_OPEN "); | |
| printf("\n"); | |
| i += EVENT_SIZE + event->len; | |
| } | |
| printf("--------\n"); | |
| } | |
| inotify_rm_watch(fd, wd); | |
| close(fd); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment