int main();
static void watch_spawn_all_basic_test();
static void watch_functionality_basic_test();
+static void watch_functionality_multi_level_test();
int main() {
setup_env();
- watch_spawn_all_basic_test();
watch_functionality_basic_test();
+ watch_functionality_multi_level_test();
+ watch_spawn_all_basic_test();
clean_env();
reset_env();
}
+static void watch_functionality_multi_level_test() {
+ /* see `man inotify` for explanation of alignment modifiers */
+ char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
+ const struct inotify_event *event;
+ int fd, watch_fd, watch_fd2;
+ ssize_t len;
+ char *p;
+
+ fd = inotify_init();
+ assert(fd>=0);
+
+ watch_fd = inotify_add_watch(fd,TEST_DIRECTORY,WATCH_FLAGS);
+ assert(watch_fd>=0);
+
+ system("mkdir -p " TEST_DIRECTORY "/directory1/directory2"); // IN_CREATE
+ system("echo hello > " TEST_DIRECTORY "/directory1/test.txt"); // NO EVENT
+
+ watch_fd2 = inotify_add_watch(fd,TEST_DIRECTORY "/directory1/directory2",WATCH_FLAGS);
+ assert(watch_fd2>=0);
+
+ system("echo hello > " TEST_DIRECTORY "/directory1/directory2/test2.txt"); // IN_CREATE + IN_MODIFY + IN_CLOSE_WRITE
+ system("rm " TEST_DIRECTORY "/directory1/directory2/test2.txt"); // IN_DELETE
+ system("rmdir " TEST_DIRECTORY "/directory1/directory2"); // IN_IGNORED (since directory [subject of watch2] is removed)
+ system("rm " TEST_DIRECTORY "/directory1/test.txt"); // NO EVENT
+ system("rmdir " TEST_DIRECTORY "/directory1"); // IN_ISDIR
+
+ len = read(fd,buf,sizeof(buf));
+ assert(len==208);
+
+ p = buf;
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_CREATE);
+ assert(event->wd==watch_fd);
+ assert(strcmp(event->name,"directory1")==0);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_CREATE);
+ assert(event->wd==watch_fd2);
+ assert(strcmp(event->name,"test2.txt")==0);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_MODIFY);
+ assert(event->wd==watch_fd2);
+ assert(strcmp(event->name,"test2.txt")==0);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_CLOSE_WRITE);
+ assert(event->wd==watch_fd2);
+ assert(strcmp(event->name,"test2.txt")==0);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_DELETE);
+ assert(event->wd==watch_fd2);
+ assert(strcmp(event->name,"test2.txt")==0);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_IGNORED);
+ assert(event->wd==watch_fd2);
+
+ p += (sizeof(struct inotify_event)+event->len);
+ event = (const struct inotify_event*) p;
+ assert(event->mask & IN_ISDIR);
+ assert(event->mask & IN_DELETE);
+ assert(event->wd==watch_fd);
+ assert(strcmp(event->name,"directory1")==0);
+
+ assert((p+(sizeof(struct inotify_event)+event->len)-buf)==208);
+
+ close(fd);
+
+ reset_env();
+}
+
static void watch_spawn_all_basic_test() {
struct torrent *p;
int fd;