From 36358cf9d075411b71c8c81708ace159e7ddfc58 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 3 Mar 2011 08:50:14 +0100 Subject: [PATCH] inotify.c: trigger correct types for symlinks. --- Makefile.include | 2 +- src/inotify.c | 52 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Makefile.include b/Makefile.include index 98affbb..55ddaa0 100644 --- a/Makefile.include +++ b/Makefile.include @@ -32,7 +32,7 @@ obj-log = src/log.o $(obj-strbuf) $(obj-xalloc) obj-inotify-backend = src/inotify-backend.o $(obj-log) obj-inotify-watch = src/inotify-watch.o $(obj-tree) obj-inotify-map = src/inotify-map.o $(obj-inotify-watch) $(obj-path) $(obj-rbtree) $(obj-list) -obj-inotify = src/inotify.o src/queue.o $(obj-inotify-backend) $(obj-inotify-map) +obj-inotify = src/inotify.o src/queue.o $(obj-path) $(obj-inotify-backend) $(obj-inotify-map) obj-notify-inotify = src/notify-inotify.o $(obj-inotify) $(obj-xalloc) $(obj-fscrawl) obj-notify = src/event.o $(obj-notify-inotify) diff --git a/src/inotify.c b/src/inotify.c index f1e7906..8108237 100644 --- a/src/inotify.c +++ b/src/inotify.c @@ -3,6 +3,7 @@ #include "log.h" #include "path.h" #include "queue.h" +#include "path.h" #include "inotify-backend.h" #include "inotify-map.h" #include "inotify.h" @@ -43,7 +44,7 @@ int inotify_ignore(const char *path) { int wd = inotify_map_get_wd(path); - if (wd < 0) + if (wd <= 0) return -1; /* unmap and remove watch */ @@ -60,7 +61,6 @@ static void proc_event(struct inotify_event *iev) { int i; struct list *watch_list; - int isdir; logmsg(LOG_DEBUG, "RAW EVENT: %i, %x, %s", iev->wd, iev->mask, iev->name); @@ -77,10 +77,6 @@ static void proc_event(struct inotify_event *iev) { return; } - /* set dir and drop that bit off (so its not in the way) */ - isdir = (iev->mask & IN_ISDIR) != 0; - iev->mask &= ~IN_ISDIR; - for(i=0; i < watch_list->nr; i++) { struct watch *watch = watch_list->items[i]; @@ -88,19 +84,43 @@ static void proc_event(struct inotify_event *iev) { notify_event_set_path(event, watch->path); notify_event_set_filename(event, iev->name); - event->dir = isdir; - switch(iev->mask) { - case IN_CREATE : - case IN_MOVED_TO : + /* + * Because of limitation on the information we get from inotify + * We must do something else to find out what type a "file/link" should be. + * + * if CREATE or MOVED_TO: + * The file is present on the filesystem, so we + * only need to perform a simple system call. + * + * if DELETE or MOVED_FROM: + * The file is no longer present on the filesystem, but + * we will treat the symlink as a directory if we are + * watching the path. since we only watch directories. + * otherwise it may be a symlink we simple do not care about + * or some other file. + */ + if (iev->mask & IN_CREATE || iev->mask & IN_MOVED_TO) { + event->dir = (iev->mask & IN_ISDIR) ? 1 : + is_dir(mkpath("%s/%s", event->path, event->filename)); + event->type = NOTIFY_CREATE; - break; - case IN_DELETE : - case IN_MOVED_FROM : + } else if (iev->mask & IN_DELETE || iev->mask & IN_MOVED_FROM) { + + if (iev->mask & IN_ISDIR) { + event->dir = 1; + } else { + const char *path = mkpath("%s%s/", event->path, event->filename); + + if (inotify_map_get_wd(path) > 0) { + inotify_unmap_path(path); + event->dir = 1; + } else { + event->dir = 0; + } + } event->type = NOTIFY_DELETE; - inotify_ignore(mkpath("%s%s/", event->path, event->filename)); - break; - default: + } else { event->type = NOTIFY_UNKNOWN; }