Archived
1
0
Fork 0

notify: refactoring in inotify and minor fixes in event/fscrawl.

This commit is contained in:
Henrik Hautakoski 2010-10-08 19:22:27 +02:00
parent c18d548afc
commit b6b355f6ce
3 changed files with 48 additions and 53 deletions

View file

@ -16,16 +16,16 @@
#include "../common/path.h" #include "../common/path.h"
#define dealloc_data(ev) \ #define dealloc_data(ev) \
if (ev->path != NULL) \ if (ev->path) \
free(ev->path); \ free(ev->path); \
if (ev->filename != NULL) \ if (ev->filename) \
free(ev->filename) free(ev->filename)
static void init_event(notify_event* ev) { static void init_event(notify_event* ev) {
ev->filename = NULL; ev->filename = NULL;
ev->path = NULL; ev->path = NULL;
ev->type = 0; ev->type = NOTIFY_UNKNOWN;
ev->dir = 0; ev->dir = 0;
} }
@ -49,7 +49,7 @@ notify_event* notify_event_new() {
*/ */
void notify_event_del(notify_event *event) { void notify_event_del(notify_event *event) {
if(event != NULL) if (event)
return; return;
dealloc_data(event); dealloc_data(event);
@ -65,8 +65,6 @@ void notify_event_clear(notify_event *event) {
return; return;
dealloc_data(event); dealloc_data(event);
// set init values
init_event(event); init_event(event);
} }
@ -85,9 +83,9 @@ void notify_event_set_path(notify_event *event, const char *path) {
if (ptr == NULL) if (ptr == NULL)
return; return;
memcpy(ptr, path, strlen(path)+1);
event->path = ptr; event->path = ptr;
memcpy(event->path, path, strlen(path)+1);
} }
/* /*
@ -130,6 +128,9 @@ void notify_event_set_type(notify_event *event, uint8_t type) {
const char* notify_event_typetostr(notify_event *event) { const char* notify_event_typetostr(notify_event *event) {
if (!event)
return "(null)";
switch(event->type) { switch(event->type) {
case NOTIFY_CREATE : case NOTIFY_CREATE :
return "CREATE"; return "CREATE";

View file

@ -32,14 +32,15 @@ struct __fscrawl {
static int mvup(struct __fscrawl *f) { static int mvup(struct __fscrawl *f) {
if (closedir(f->dirs[f->depth]) == -1) { if (closedir(f->dirs[f->depth]) == -1) {
perror("TREE"); perror("closedir");
errno = 0;
return -1; return -1;
} }
if (f->depth == 0) if (f->depth == 0)
return 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--; f->depth--;
@ -90,7 +91,6 @@ fscrawl_t fsc_open(const char *path) {
strbuf_init(&f->path); strbuf_init(&f->path);
char *npath = path_normalize(path, NULL, 1); char *npath = path_normalize(path, NULL, 1);
if (!npath) if (!npath)

View file

@ -52,9 +52,7 @@ static int addwatch(const char *path, const char *name) {
if (wd < 0) { if (wd < 0) {
free(npath); free(npath);
wd = -errno; return -1;
errno = 0;
return wd;
} }
/* we must update to not introduce duplicated wd's (keys) */ /* we must update to not introduce duplicated wd's (keys) */
@ -94,8 +92,6 @@ static void proc_event(inoev *iev) {
char buf[4096]; char buf[4096];
uint8_t type = NOTIFY_UNKNOWN; uint8_t type = NOTIFY_UNKNOWN;
event = notify_event_new();
#ifdef __DEBUG__ #ifdef __DEBUG__
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask); fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
if (iev->len) if (iev->len)
@ -104,28 +100,34 @@ static void proc_event(inoev *iev) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #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 */ /* lookup the watch descriptor in rbtree */
node = rbtree_search(&tree, iev->wd); node = rbtree_search(&tree, iev->wd);
if (node == NULL) { if (!node) {
dprint("-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd); 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();
notify_event_set_path(event, node->data);
notify_event_set_filename(event, iev->name);
notify_event_set_dir(event, (iev->mask & IN_ISDIR) != 0); 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; iev->mask &= ~IN_ISDIR;
/* this event is always generated and works better notify_event_set_path(event, node->data);
for removing the node in the binary tree */ notify_event_set_filename(event, iev->name);
if (iev->mask == IN_IGNORED) {
rmwatch(iev->wd);
goto cleanup;
}
/* queue event before doing any fscrawl on a subdirectory /* queue event before doing any fscrawl on a subdirectory
to prevent messing up the order */ to prevent messing up the order */
@ -162,11 +164,7 @@ static void proc_event(inoev *iev) {
break; break;
} }
notify_event_set_type(event, type); event->type = type;
return;
cleanup:
free(event);
} }
static void addrecursive(const char *path) { static void addrecursive(const char *path) {
@ -210,17 +208,13 @@ int notify_init() {
fd = inotify_init(); fd = inotify_init();
if (fd < 1) { if (fd < 0)
perror("NOTIFY INIT");
return -1; return -1;
}
event_queue = queue_init(); event_queue = queue_init();
if (event_queue == NULL) { if (event_queue == NULL)
perror("EVENT QUEUE");
return -1; return -1;
}
return 1; return 1;
} }