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;
} }
@ -36,7 +36,7 @@ notify_event* notify_event_new() {
notify_event *ev = malloc(sizeof(notify_event)); notify_event *ev = malloc(sizeof(notify_event));
if(ev == NULL) if (ev == NULL)
return NULL; return NULL;
init_event(ev); init_event(ev);
@ -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);
@ -61,12 +61,10 @@ void notify_event_del(notify_event *event) {
*/ */
void notify_event_clear(notify_event *event) { void notify_event_clear(notify_event *event) {
if(event == NULL) if (event == NULL)
return; return;
dealloc_data(event); dealloc_data(event);
// set init values
init_event(event); init_event(event);
} }
@ -77,17 +75,17 @@ void notify_event_set_path(notify_event *event, const char *path) {
char *ptr; char *ptr;
if(event == NULL || path == NULL) if (event == NULL || path == NULL)
return; return;
ptr = realloc(event->path, sizeof(char) * (strlen(path)+1)); ptr = realloc(event->path, sizeof(char) * (strlen(path)+1));
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);
} }
/* /*
@ -97,13 +95,13 @@ void notify_event_set_filename(notify_event *event, const char *filename) {
char *tmp; char *tmp;
if(event == NULL || filename == NULL) if (event == NULL || filename == NULL)
return; return;
tmp = realloc(event->filename, sizeof(char) * (strlen(filename)+1)); tmp = realloc(event->filename, sizeof(char) * (strlen(filename)+1));
if(tmp == NULL) if (tmp == NULL)
return; return;
memcpy(tmp, filename, strlen(filename)+1); memcpy(tmp, filename, strlen(filename)+1);
@ -114,7 +112,7 @@ void notify_event_set_filename(notify_event *event, const char *filename) {
/* set directory */ /* set directory */
void notify_event_set_dir(notify_event *event, uint8_t dir) { void notify_event_set_dir(notify_event *event, uint8_t dir) {
if(event == NULL) if (event == NULL)
return; return;
event->dir = dir; 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) { void notify_event_set_type(notify_event *event, uint8_t type) {
if(event == NULL) if (event == NULL)
return; return;
event->type = type; event->type = 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

@ -47,14 +47,12 @@ static int addwatch(const char *path, const char *name) {
int wd; int wd;
npath = path_normalize(path, name, 1); npath = path_normalize(path, name, 1);
wd = inotify_add_watch(fd, npath, WATCH_MASK); wd = inotify_add_watch(fd, npath, WATCH_MASK);
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) */
@ -93,8 +91,6 @@ static void proc_event(inoev *iev) {
notify_event *event; notify_event *event;
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);
@ -103,30 +99,36 @@ static void proc_event(inoev *iev) {
else else
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();
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_path(event, node->data);
notify_event_set_filename(event, iev->name); 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 /* queue event before doing any fscrawl on a subdirectory
to prevent messing up the order */ to prevent messing up the order */
queue_enqueue(event_queue, event); queue_enqueue(event_queue, event);
@ -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) {
@ -209,18 +207,14 @@ 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;
} }