summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2007-01-13 21:13:24 +0000
committerMarcel Holtmann <marcel@holtmann.org>2007-01-13 21:13:24 +0000
commit603a99217dc455370b09967e7628412c13b6663b (patch)
tree4ffe7480f7d679e400f8cd29c0e601b9d1d99f26 /common
parenta3418be8059ffc30a0858a738f8955ceae9b39ea (diff)
Allow adding user defined directories
Diffstat (limited to 'common')
-rw-r--r--common/notify-dummy.c8
-rw-r--r--common/notify-inotify.c59
-rw-r--r--common/notify.h2
3 files changed, 58 insertions, 11 deletions
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 <unistd.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
@@ -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);