Archived
1
0
Fork 0

notify/inotify.c: first change, uses queue

This commit is contained in:
Henrik Hautakoski 2010-09-26 14:53:14 +02:00
parent dd73cf9422
commit c6f346586e
3 changed files with 83 additions and 104 deletions

View file

@ -7,7 +7,7 @@ CFLAGS = -O2 -Werror
LDFLAGS = LDFLAGS =
ifdef DEBUG ifdef DEBUG
CFLAGS += -g -D__DEBUG__ CFLAGS += -g -D__DEBUG__
endif endif
ifndef V ifndef V
@ -20,7 +20,6 @@ FINDOBJ = find . -name "*.o" -type f -printf "%P\n"
BUILD := ./build BUILD := ./build
PROGRAM := $(BUILD)/arch PROGRAM := $(BUILD)/arch
ifeq ($(output), mysql) ifeq ($(output), mysql)
CFLAGS += `mysql_config --cflags` CFLAGS += `mysql_config --cflags`
LDFLAGS += -L/usr/lib/mysql -lmysqlclient LDFLAGS += -L/usr/lib/mysql -lmysqlclient
@ -44,6 +43,7 @@ obj += src/output/$(output).o
obj += src/notify/inotify.o obj += src/notify/inotify.o
obj += src/notify/event.o obj += src/notify/event.o
obj += src/notify/tree.o obj += src/notify/tree.o
obj += src/notify/queue.o
obj += src/indexer.o obj += src/indexer.o
obj += src/arch.o obj += src/arch.o

View file

@ -9,43 +9,35 @@
* (at your option) any later version. * (at your option) any later version.
*/ */
#include "notify.h" #include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/inotify.h>
#include "../common/util.h"
/* red black tree for watch descriptors */ /* red black tree for watch descriptors */
#include "../common/rbtree.h" #include "../common/rbtree.h"
#include "../common/debug.h" #include "../common/debug.h"
#include "../common/path.h" #include "../common/path.h"
#include <unistd.h> #include "queue.h"
#include <stdlib.h> #include "notify.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/inotify.h>
typedef struct inotify_event inoev; typedef struct inotify_event inoev;
#define MAXEVENT 0x200 #define INOBUFSIZE ((1 << 12) * (sizeof(inoev) + 0x40))
#define PADEVSIZE (sizeof(inoev) + 0x40)
#define INOBUFSIZE (MAXEVENT*PADEVSIZE)
#define WATCH_MASK (IN_MOVE | IN_CREATE | IN_DELETE | IN_ONLYDIR) #define WATCH_MASK (IN_MOVE | IN_CREATE | IN_DELETE | IN_ONLYDIR)
/* Inotify file descriptor */ /* Inotify file descriptor */
static int fd = 0; static int fd = 0;
/* Available bytes on file descriptor */
static unsigned int fd_bytes = 0;
/* redblack tree */ /* redblack tree */
static rbtree tree; static rbtree tree;
/* inotify queue buffer */ static queue_t event_queue;
static char *inobuf = NULL;
/* current event */
static notify_event event;
static int addwatch(const char *path, const char *name) { static int addwatch(const char *path, const char *name) {
@ -55,9 +47,6 @@ static int addwatch(const char *path, const char *name) {
cpath = path_normalize(path, name, 1); cpath = path_normalize(path, name, 1);
if (cpath == NULL)
return -1;
wd = inotify_add_watch(fd, cpath, WATCH_MASK); wd = inotify_add_watch(fd, cpath, WATCH_MASK);
if (wd < 0) { if (wd < 0) {
@ -94,14 +83,14 @@ static int rmwatch(unsigned int wd) {
return 1; return 1;
} }
static int proc_event(inoev *iev) { static void proc_event(inoev *iev) {
rbnode *node; rbnode *node;
notify_event *event;
char buf[4096]; char buf[4096];
uint8_t type = NOTIFY_UNKNOWN; uint8_t type = NOTIFY_UNKNOWN;
if (iev == NULL) event = notify_event_new();
return 0;
#ifdef __DEBUG__ #ifdef __DEBUG__
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask); fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
@ -116,14 +105,14 @@ static int proc_event(inoev *iev) {
if (node == NULL) { if (node == NULL) {
dprint("-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd); dprint("-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd);
return 0; goto cleanup;
} }
notify_event_set_dir(&event, (iev->mask & IN_ISDIR) != 0); notify_event_set_dir(event, (iev->mask & IN_ISDIR) != 0);
/* set path, this is stored in void* node->data */ /* set path, this is stored in void* node->data */
notify_event_set_path(&event, (char*)node->data); notify_event_set_path(event, (char*)node->data);
notify_event_set_filename(&event, iev->name); notify_event_set_filename(event, iev->name);
iev->mask &= ~IN_ISDIR; iev->mask &= ~IN_ISDIR;
@ -131,37 +120,40 @@ static int proc_event(inoev *iev) {
case IN_CREATE : case IN_CREATE :
if (event.dir) { if (event->dir) {
dprint("IN_CREATE on directory, adding\n"); dprint("IN_CREATE on directory, adding\n");
addwatch(event.path, event.filename); 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) {
dprint("IN_MOVED_TO on directory, adding\n"); dprint("IN_MOVED_TO on directory, adding\n");
addwatch(event.path, event.filename); addwatch(event->path, event->filename);
} }
type = NOTIFY_MOVE_TO; type = NOTIFY_MOVE_TO;
break; break;
case IN_MOVED_FROM : case IN_MOVED_FROM :
/* TODO: clean this */ /* TODO: clean this */
sprintf(buf, "%s%s/", event.path, event.filename); sprintf(buf, "%s%s/", event->path, event->filename);
notify_rm_watch(buf); notify_rm_watch(buf);
case IN_DELETE : case IN_DELETE :
type = NOTIFY_DELETE; type = NOTIFY_DELETE;
break; break;
case IN_IGNORED : case IN_IGNORED :
rmwatch(iev->wd); rmwatch(iev->wd);
return 0; goto cleanup;
} }
notify_event_set_type(&event, type); notify_event_set_type(event, type);
return 1; queue_enqueue(event_queue, event);
return;
cleanup:
free(event);
} }
int notify_init() { int notify_init() {
@ -178,12 +170,10 @@ int notify_init() {
return -1; return -1;
} }
dprint("inotify descriptor = %i\n", fd); event_queue = queue_init();
inobuf = malloc(INOBUFSIZE); if (event_queue == NULL) {
perror("EVENT QUEUE");
if (inobuf == NULL) {
perror("NOTIFY BUFFER");
return -1; return -1;
} }
@ -192,15 +182,15 @@ int notify_init() {
void notify_cleanup() { void notify_cleanup() {
fd = 0;
/* free rbtree */ /* free rbtree */
rbtree_free(&tree, NULL); rbtree_free(&tree, NULL);
if (inobuf != NULL) { if (event_queue) {
free(inobuf); queue_destroy(event_queue);
inobuf = NULL; event_queue = NULL;
} }
fd = 0;
} }
/* /*
@ -225,45 +215,48 @@ int notify_rm_watch(const char *path) {
return 0; return 0;
} }
/*
* Get next inotify event
*/
notify_event* notify_read() { notify_event* notify_read() {
int rc; char buf[INOBUFSIZE];
inoev *rev = NULL;
static int offset;
while(! proc_event(rev)) { /* time resolution */
struct timespec tres = { 0, 2000000 };
if (fd_bytes < 1) { unsigned short tcount;
offset = 0; /* bytes ready on the inotify descriptor */
int ioready;
fd_bytes = read(fd, inobuf, INOBUFSIZE); for(tcount = 0; tcount < 10; tcount++) {
if (fd_bytes < 1) { if (ioctl(fd, FIONREAD, &ioready) == -1)
break;
if (fd_bytes == 0) { if (ioready > INOBUFSIZE)
fprintf(stderr, "NOTIFY READ: EOF\n"); break;
}
/* EINTR = call interrupted, silent ignore */
else if (errno != EINTR) {
perror("NOTIFY READ");
}
return NULL; nanosleep(&tres, NULL);
} }
}
rev = (inoev *) &inobuf[offset]; /* read if we have data on fd */
offset += sizeof(inoev) + rev->len; while(ioready > 0) {
if (offset >= fd_bytes) dprint("%i bytes avail\n", ioready);
fd_bytes = 0;
}
return &event; int offset = 0, rbytes = read(fd, buf, INOBUFSIZE);
if (rbytes == -1)
die_errno("INOTIFY");
while(rbytes > offset) {
inoev *rev = (inoev *) &buf[offset];
proc_event(rev);
offset += sizeof(inoev) + rev->len;
}
ioready -= rbytes;
}
return queue_dequeue(event_queue);
} }
static void print_node(rbnode *node) { static void print_node(rbnode *node) {
@ -276,18 +269,3 @@ void notify_stat() {
rbtree_walk(&tree, print_node); rbtree_walk(&tree, print_node);
#endif #endif
} }
int notify_is_ready() {
unsigned int bytes;
if (fd_bytes > 1)
return 1;
if (ioctl(fd, FIONREAD, &bytes) == -1)
return 0;
dprint("%i bytes ready!\n", bytes);
return bytes > 512;
}

View file

@ -40,6 +40,7 @@ inotify :
../src/common/path.c \ ../src/common/path.c \
../src/notify/event.c \ ../src/notify/event.c \
../src/notify/tree.c \ ../src/notify/tree.c \
../src/notify/queue.c \
../src/notify/inotify.c \ ../src/notify/inotify.c \
t_inotify.c -o test_inotify t_inotify.c -o test_inotify