Archived
1
0
Fork 0

inotify.c: refactoring addwatch function.

moving the "watch" code to it's own function.
this removes the need for a 'recursive' flag to addwatch.
This commit is contained in:
Henrik Hautakoski 2011-02-08 11:09:33 +01:00
parent 8f1f7c356c
commit a1766126a2

View file

@ -39,32 +39,35 @@ static int fd;
static queue_t event_queue; static queue_t event_queue;
static int addwatch(const char *path, const char *name, unsigned recursive) { static int inotify_watch(const char *path) {
char *npath; int wd = inotify_add_watch(fd, path, WATCH_MASK);
int wd;
npath = path_normalize(path, name, 1); if (wd < 0) {
if (errno != EACCES)
logerrno(LOG_CRIT, "inotify_watch", errno);
return -1;
}
inotify_map(wd, path);
return wd;
}
static int addwatch(const char *path, const char *name) {
fscrawl_t f;
char *npath = path_normalize(path, name, 1);
if (!npath) if (!npath)
return -1; return -1;
wd = inotify_add_watch(fd, npath, WATCH_MASK); if (inotify_watch(npath) < 0)
goto clean;
if (wd < 0) {
if (errno != EACCES)
logerrno(LOG_CRIT, "inotify_add_watch", errno);
free(npath);
return -1;
}
logmsg(LOG_DEBUG, "added wd = %i on %s", wd, npath);
if (recursive) {
fscrawl_t f = fsc_open(npath);
f = fsc_open(npath);
if (!f) if (!f)
return -1; goto clean;
for(;;) { for(;;) {
notify_event *ev; notify_event *ev;
@ -80,16 +83,23 @@ static int addwatch(const char *path, const char *name, unsigned recursive) {
notify_event_set_filename(ev, ent->name); notify_event_set_filename(ev, ent->name);
notify_event_set_dir(ev, ent->dir); notify_event_set_dir(ev, ent->dir);
queue_enqueue(event_queue, ev); if (ent->dir) {
char *fullpath = path_normalize(ev->path, ev->filename, 1);
if (fullpath) {
if (inotify_watch(fullpath) < 0) {
xfree(fullpath);
}
}
}
if (ent->dir) queue_enqueue(event_queue, ev);
addwatch(ent->base, ent->name, 0);
} }
fsc_close(f); fsc_close(f);
}
inotify_map(wd, npath);
return 1; return 1;
clean:
xfree(npath);
return -1;
} }
static int rmwatch(const char *path, const char *name) { static int rmwatch(const char *path, const char *name) {
@ -161,14 +171,14 @@ static void proc_event(inoev *iev) {
case IN_CREATE : case IN_CREATE :
if (event->dir) { if (event->dir) {
logmsg(LOG_DEBUG, "IN_CREATE on directory, adding"); logmsg(LOG_DEBUG, "IN_CREATE on directory, adding");
addwatch(event->path, event->filename, 1); addwatch(event->path, event->filename);
} }
type = NOTIFY_CREATE; type = NOTIFY_CREATE;
break; break;
case IN_MOVED_TO : case IN_MOVED_TO :
if (event->dir) { if (event->dir) {
logmsg(LOG_DEBUG, "IN_MOVED_TO on directory, adding"); logmsg(LOG_DEBUG, "IN_MOVED_TO on directory, adding");
addwatch(event->path, event->filename, 1); addwatch(event->path, event->filename);
} }
type = NOTIFY_CREATE; type = NOTIFY_CREATE;
break; break;
@ -230,7 +240,7 @@ int notify_add_watch(const char *path) {
if (!init) if (!init)
die("inotify is not instantiated."); die("inotify is not instantiated.");
return (addwatch(path, NULL, 1) > 0) ? 1 : -1; return (addwatch(path, NULL) > 0) ? 1 : -1;
} }
int notify_rm_watch(const char *path) { int notify_rm_watch(const char *path) {