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:
parent
8f1f7c356c
commit
a1766126a2
1 changed files with 51 additions and 41 deletions
|
|
@ -39,57 +39,67 @@ 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) {
|
f = fsc_open(npath);
|
||||||
if (errno != EACCES)
|
if (!f)
|
||||||
logerrno(LOG_CRIT, "inotify_add_watch", errno);
|
goto clean;
|
||||||
free(npath);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
logmsg(LOG_DEBUG, "added wd = %i on %s", wd, npath);
|
for(;;) {
|
||||||
|
notify_event *ev;
|
||||||
|
fs_entry *ent = fsc_read(f);
|
||||||
|
|
||||||
if (recursive) {
|
if (!ent)
|
||||||
fscrawl_t f = fsc_open(npath);
|
break;
|
||||||
|
|
||||||
if (!f)
|
ev = notify_event_new();
|
||||||
return -1;
|
|
||||||
|
|
||||||
for(;;) {
|
notify_event_set_type(ev, NOTIFY_CREATE);
|
||||||
notify_event *ev;
|
notify_event_set_path(ev, ent->base);
|
||||||
fs_entry *ent = fsc_read(f);
|
notify_event_set_filename(ev, ent->name);
|
||||||
|
notify_event_set_dir(ev, ent->dir);
|
||||||
|
|
||||||
if (!ent)
|
if (ent->dir) {
|
||||||
break;
|
char *fullpath = path_normalize(ev->path, ev->filename, 1);
|
||||||
|
if (fullpath) {
|
||||||
ev = notify_event_new();
|
if (inotify_watch(fullpath) < 0) {
|
||||||
|
xfree(fullpath);
|
||||||
notify_event_set_type(ev, NOTIFY_CREATE);
|
}
|
||||||
notify_event_set_path(ev, ent->base);
|
}
|
||||||
notify_event_set_filename(ev, ent->name);
|
|
||||||
notify_event_set_dir(ev, ent->dir);
|
|
||||||
|
|
||||||
queue_enqueue(event_queue, ev);
|
|
||||||
|
|
||||||
if (ent->dir)
|
|
||||||
addwatch(ent->base, ent->name, 0);
|
|
||||||
}
|
}
|
||||||
fsc_close(f);
|
|
||||||
}
|
|
||||||
inotify_map(wd, npath);
|
|
||||||
|
|
||||||
return 1;
|
queue_enqueue(event_queue, ev);
|
||||||
|
}
|
||||||
|
fsc_close(f);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
|
||||||
Reference in a new issue