From 603a99217dc455370b09967e7628412c13b6663b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 13 Jan 2007 21:13:24 +0000 Subject: Allow adding user defined directories --- common/notify-dummy.c | 8 +++++++ common/notify-inotify.c | 59 ++++++++++++++++++++++++++++++++++++++++--------- common/notify.h | 2 ++ 3 files changed, 58 insertions(+), 11 deletions(-) (limited to 'common') diff --git a/common/notify-dummy.c b/common/notify-dummy.c index f199e972..3443a467 100644 --- a/common/notify-dummy.c +++ b/common/notify-dummy.c @@ -34,3 +34,11 @@ void notify_init(void) void notify_close(void) { } + +void notify_add(const char *pathname) +{ +} + +void notify_remove(const char *pathname) +{ +} diff --git a/common/notify-inotify.c b/common/notify-inotify.c index a1e12a6a..2b14b744 100644 --- a/common/notify-inotify.c +++ b/common/notify-inotify.c @@ -26,6 +26,7 @@ #endif #include +#include #include #include @@ -38,6 +39,8 @@ static GIOChannel *io = NULL; static int fd = -1; static int wd = -1; +static char *name = NULL; + static gboolean io_event(GIOChannel *chan, GIOCondition cond, gpointer data) { unsigned char buf[129]; @@ -53,20 +56,26 @@ static gboolean io_event(GIOChannel *chan, GIOCondition cond, gpointer data) if (len < sizeof(struct inotify_event)) return TRUE; + if (evt->wd != wd) + return TRUE; + if (evt->mask & (IN_CREATE | IN_MOVED_TO)) - debug("File %s/%s created", CONFIGDIR, evt->name); + debug("File %s/%s created", name, evt->name); if (evt->mask & (IN_DELETE | IN_MOVED_FROM)) - debug("File %s/%s deleted", CONFIGDIR, evt->name); + debug("File %s/%s deleted", name, evt->name); if (evt->mask & IN_MODIFY) - debug("File %s/%s modified", CONFIGDIR, evt->name); + debug("File %s/%s modified", name, evt->name); return TRUE; } void notify_init(void) { + if (fd != -1) + return; + fd = inotify_init(); if (fd < 0) { error("Creation of inotify context failed"); @@ -80,15 +89,13 @@ void notify_init(void) } g_io_add_watch(io, G_IO_IN, io_event, NULL); - - wd = inotify_add_watch(fd, CONFIGDIR, - IN_ONLYDIR | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE); - if (wd < 0) - error("Creation of watch for %s failed", CONFIGDIR); } void notify_close(void) { + if (fd == -1) + return; + if (wd != -1) { inotify_rm_watch(fd, wd); wd = -1; @@ -99,8 +106,38 @@ void notify_close(void) io = NULL; } - if (fd != -1) { - close(fd); - fd = -1; + close(fd); + fd = -1; + + if (name) { + free(name); + name = NULL; } } + +void notify_add(const char *pathname) +{ + if (fd == -1 || wd != -1) + return; + + if (name) + free(name); + + name = strdup(pathname); + if (!name) + return; + + wd = inotify_add_watch(fd, pathname, + IN_ONLYDIR | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE); + if (wd < 0) + error("Creation of watch for %s failed", pathname); +} + +void notify_remove(const char *pathname) +{ + if (fd == -1 || wd == -1) + return; + + inotify_rm_watch(fd, wd); + wd = -1; +} diff --git a/common/notify.h b/common/notify.h index d82a7ca2..b6b40034 100644 --- a/common/notify.h +++ b/common/notify.h @@ -23,3 +23,5 @@ void notify_init(void); void notify_close(void); +void notify_add(const char *pathname); +void notify_remove(const char *pathname); -- cgit