notify: refactoring in inotify and minor fixes in event/fscrawl.
This commit is contained in:
parent
c18d548afc
commit
b6b355f6ce
3 changed files with 48 additions and 53 deletions
|
|
@ -16,16 +16,16 @@
|
|||
#include "../common/path.h"
|
||||
|
||||
#define dealloc_data(ev) \
|
||||
if (ev->path != NULL) \
|
||||
if (ev->path) \
|
||||
free(ev->path); \
|
||||
if (ev->filename != NULL) \
|
||||
if (ev->filename) \
|
||||
free(ev->filename)
|
||||
|
||||
static void init_event(notify_event* ev) {
|
||||
|
||||
ev->filename = NULL;
|
||||
ev->path = NULL;
|
||||
ev->type = 0;
|
||||
ev->type = NOTIFY_UNKNOWN;
|
||||
ev->dir = 0;
|
||||
}
|
||||
|
||||
|
|
@ -36,7 +36,7 @@ notify_event* notify_event_new() {
|
|||
|
||||
notify_event *ev = malloc(sizeof(notify_event));
|
||||
|
||||
if(ev == NULL)
|
||||
if (ev == NULL)
|
||||
return NULL;
|
||||
|
||||
init_event(ev);
|
||||
|
|
@ -49,7 +49,7 @@ notify_event* notify_event_new() {
|
|||
*/
|
||||
void notify_event_del(notify_event *event) {
|
||||
|
||||
if(event != NULL)
|
||||
if (event)
|
||||
return;
|
||||
|
||||
dealloc_data(event);
|
||||
|
|
@ -61,12 +61,10 @@ void notify_event_del(notify_event *event) {
|
|||
*/
|
||||
void notify_event_clear(notify_event *event) {
|
||||
|
||||
if(event == NULL)
|
||||
if (event == NULL)
|
||||
return;
|
||||
|
||||
dealloc_data(event);
|
||||
|
||||
// set init values
|
||||
init_event(event);
|
||||
}
|
||||
|
||||
|
|
@ -77,17 +75,17 @@ void notify_event_set_path(notify_event *event, const char *path) {
|
|||
|
||||
char *ptr;
|
||||
|
||||
if(event == NULL || path == NULL)
|
||||
if (event == NULL || path == NULL)
|
||||
return;
|
||||
|
||||
ptr = realloc(event->path, sizeof(char) * (strlen(path)+1));
|
||||
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
|
||||
memcpy(ptr, path, strlen(path)+1);
|
||||
|
||||
|
||||
event->path = ptr;
|
||||
|
||||
memcpy(event->path, path, strlen(path)+1);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -97,13 +95,13 @@ void notify_event_set_filename(notify_event *event, const char *filename) {
|
|||
|
||||
char *tmp;
|
||||
|
||||
if(event == NULL || filename == NULL)
|
||||
if (event == NULL || filename == NULL)
|
||||
return;
|
||||
|
||||
|
||||
tmp = realloc(event->filename, sizeof(char) * (strlen(filename)+1));
|
||||
|
||||
if(tmp == NULL)
|
||||
if (tmp == NULL)
|
||||
return;
|
||||
|
||||
memcpy(tmp, filename, strlen(filename)+1);
|
||||
|
|
@ -114,7 +112,7 @@ void notify_event_set_filename(notify_event *event, const char *filename) {
|
|||
/* set directory */
|
||||
void notify_event_set_dir(notify_event *event, uint8_t dir) {
|
||||
|
||||
if(event == NULL)
|
||||
if (event == NULL)
|
||||
return;
|
||||
|
||||
event->dir = dir;
|
||||
|
|
@ -122,14 +120,17 @@ void notify_event_set_dir(notify_event *event, uint8_t dir) {
|
|||
|
||||
void notify_event_set_type(notify_event *event, uint8_t type) {
|
||||
|
||||
if(event == NULL)
|
||||
if (event == NULL)
|
||||
return;
|
||||
|
||||
event->type = type;
|
||||
}
|
||||
|
||||
const char* notify_event_typetostr(notify_event *event) {
|
||||
|
||||
|
||||
if (!event)
|
||||
return "(null)";
|
||||
|
||||
switch(event->type) {
|
||||
case NOTIFY_CREATE :
|
||||
return "CREATE";
|
||||
|
|
|
|||
|
|
@ -32,14 +32,15 @@ struct __fscrawl {
|
|||
static int mvup(struct __fscrawl *f) {
|
||||
|
||||
if (closedir(f->dirs[f->depth]) == -1) {
|
||||
perror("TREE");
|
||||
perror("closedir");
|
||||
errno = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (f->depth == 0)
|
||||
return 0;
|
||||
|
||||
dprint("tree_next_ent: tree depth is: %i \n", f->depth);
|
||||
dprint("fscrawl: tree depth is: %i \n", f->depth);
|
||||
|
||||
f->depth--;
|
||||
|
||||
|
|
@ -90,7 +91,6 @@ fscrawl_t fsc_open(const char *path) {
|
|||
|
||||
strbuf_init(&f->path);
|
||||
|
||||
|
||||
char *npath = path_normalize(path, NULL, 1);
|
||||
|
||||
if (!npath)
|
||||
|
|
|
|||
|
|
@ -47,14 +47,12 @@ static int addwatch(const char *path, const char *name) {
|
|||
int wd;
|
||||
|
||||
npath = path_normalize(path, name, 1);
|
||||
|
||||
|
||||
wd = inotify_add_watch(fd, npath, WATCH_MASK);
|
||||
|
||||
if (wd < 0) {
|
||||
free(npath);
|
||||
wd = -errno;
|
||||
errno = 0;
|
||||
return wd;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* we must update to not introduce duplicated wd's (keys) */
|
||||
|
|
@ -93,8 +91,6 @@ static void proc_event(inoev *iev) {
|
|||
notify_event *event;
|
||||
char buf[4096];
|
||||
uint8_t type = NOTIFY_UNKNOWN;
|
||||
|
||||
event = notify_event_new();
|
||||
|
||||
#ifdef __DEBUG__
|
||||
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
|
||||
|
|
@ -103,30 +99,36 @@ static void proc_event(inoev *iev) {
|
|||
else
|
||||
fprintf(stderr, "\n");
|
||||
#endif
|
||||
|
||||
|
||||
/* this event is triggered when a watch descriptor is removed.
|
||||
so we can do a binary search instead of useing the IN_DELETE
|
||||
event (that may be triggered on a parent wd) to do a traverse search */
|
||||
if (iev->mask & IN_IGNORED) {
|
||||
rmwatch(iev->wd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* lookup the watch descriptor in rbtree */
|
||||
node = rbtree_search(&tree, iev->wd);
|
||||
|
||||
if (node == NULL) {
|
||||
if (!node) {
|
||||
dprint("-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd);
|
||||
goto cleanup;
|
||||
return;
|
||||
}
|
||||
|
||||
/* set path, this is stored in void* node->data */
|
||||
event = notify_event_new();
|
||||
|
||||
if (event == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* set dir and drop that bit off (so its not in the way) */
|
||||
event->dir = (iev->mask & IN_ISDIR) != 0;
|
||||
iev->mask &= ~IN_ISDIR;
|
||||
|
||||
notify_event_set_path(event, node->data);
|
||||
notify_event_set_filename(event, iev->name);
|
||||
|
||||
notify_event_set_dir(event, (iev->mask & IN_ISDIR) != 0);
|
||||
|
||||
iev->mask &= ~IN_ISDIR;
|
||||
|
||||
/* this event is always generated and works better
|
||||
for removing the node in the binary tree */
|
||||
if (iev->mask == IN_IGNORED) {
|
||||
rmwatch(iev->wd);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* queue event before doing any fscrawl on a subdirectory
|
||||
to prevent messing up the order */
|
||||
queue_enqueue(event_queue, event);
|
||||
|
|
@ -162,11 +164,7 @@ static void proc_event(inoev *iev) {
|
|||
break;
|
||||
}
|
||||
|
||||
notify_event_set_type(event, type);
|
||||
|
||||
return;
|
||||
cleanup:
|
||||
free(event);
|
||||
event->type = type;
|
||||
}
|
||||
|
||||
static void addrecursive(const char *path) {
|
||||
|
|
@ -209,18 +207,14 @@ int notify_init() {
|
|||
}
|
||||
|
||||
fd = inotify_init();
|
||||
|
||||
if (fd < 1) {
|
||||
perror("NOTIFY INIT");
|
||||
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
event_queue = queue_init();
|
||||
|
||||
if (event_queue == NULL) {
|
||||
perror("EVENT QUEUE");
|
||||
if (event_queue == NULL)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue