inotify: make notify-inotify.c use inotify.c
This commit is contained in:
parent
49c04b5a6a
commit
0d53ab7bbc
2 changed files with 38 additions and 138 deletions
|
|
@ -33,9 +33,9 @@ obj-inotify-backend = src/inotify-backend.o $(obj-log)
|
||||||
obj-inotify-watch = src/inotify-watch.o $(obj-tree)
|
obj-inotify-watch = src/inotify-watch.o $(obj-tree)
|
||||||
obj-inotify-map = src/inotify-map.o $(obj-inotify-watch) $(obj-path) $(obj-rbtree) $(obj-list)
|
obj-inotify-map = src/inotify-map.o $(obj-inotify-watch) $(obj-path) $(obj-rbtree) $(obj-list)
|
||||||
obj-inotify = src/inotify.o src/queue.o $(obj-inotify-backend) $(obj-inotify-map)
|
obj-inotify = src/inotify.o src/queue.o $(obj-inotify-backend) $(obj-inotify-map)
|
||||||
obj-notify-inotify = src/notify-inotify.o $(obj-inotify)
|
obj-notify-inotify = src/notify-inotify.o $(obj-inotify) $(obj-xalloc) $(obj-fscrawl)
|
||||||
|
|
||||||
obj-notify = src/event.o $(obj-notify-inotify) $(obj-xalloc) $(obj-fscrawl)
|
obj-notify = src/event.o $(obj-notify-inotify)
|
||||||
obj-ini = lib/ini/iniparser.o lib/ini/dictionary.o
|
obj-ini = lib/ini/iniparser.o lib/ini/dictionary.o
|
||||||
obj-mongo = src/database/mongo.o $(obj-path) $(obj-ini)
|
obj-mongo = src/database/mongo.o $(obj-path) $(obj-ini)
|
||||||
obj-mysql = src/database/mysql.o $(obj-ini) $(obj-xalloc)
|
obj-mysql = src/database/mysql.o $(obj-ini) $(obj-xalloc)
|
||||||
|
|
|
||||||
|
|
@ -24,24 +24,12 @@
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
#include "fscrawl.h"
|
#include "fscrawl.h"
|
||||||
|
#include "inotify.h"
|
||||||
#include "notify.h"
|
#include "notify.h"
|
||||||
|
|
||||||
static int init = 0;
|
static int init;
|
||||||
|
|
||||||
/* Inotify file descriptor */
|
static queue_t init_ev_q;
|
||||||
static int fd;
|
|
||||||
|
|
||||||
static queue_t event_queue;
|
|
||||||
|
|
||||||
static int watch(const char *path) {
|
|
||||||
|
|
||||||
int wd = inotify_backend_watch(path);
|
|
||||||
|
|
||||||
if (wd >= 0)
|
|
||||||
inotify_map(wd, path);
|
|
||||||
|
|
||||||
return wd;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int addwatch(const char *path, const char *name) {
|
static int addwatch(const char *path, const char *name) {
|
||||||
|
|
||||||
|
|
@ -51,7 +39,7 @@ static int addwatch(const char *path, const char *name) {
|
||||||
if (!npath)
|
if (!npath)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (watch(npath) < 0)
|
if (inotify_watch(npath) < 0)
|
||||||
goto clean;
|
goto clean;
|
||||||
|
|
||||||
f = fsc_open(npath);
|
f = fsc_open(npath);
|
||||||
|
|
@ -66,22 +54,19 @@ static int addwatch(const char *path, const char *name) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
ev = notify_event_new();
|
ev = notify_event_new();
|
||||||
|
|
||||||
notify_event_set_type(ev, NOTIFY_CREATE);
|
notify_event_set_type(ev, NOTIFY_CREATE);
|
||||||
notify_event_set_path(ev, ent->base);
|
notify_event_set_path(ev, ent->base);
|
||||||
notify_event_set_filename(ev, ent->name);
|
notify_event_set_filename(ev, ent->name);
|
||||||
notify_event_set_dir(ev, ent->dir);
|
notify_event_set_dir(ev, ent->dir);
|
||||||
|
|
||||||
if (ent->dir) {
|
if (ev->dir) {
|
||||||
char *fullpath = path_normalize(ev->path, ev->filename, 1);
|
char *fullpath = path_normalize(ev->path, ent->name, 1);
|
||||||
if (fullpath) {
|
if (fullpath && inotify_watch(fullpath) < 0)
|
||||||
if (watch(fullpath) < 0) {
|
xfree(fullpath);
|
||||||
xfree(fullpath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_enqueue(event_queue, ev);
|
queue_enqueue(init_ev_q, ev);
|
||||||
}
|
}
|
||||||
fsc_close(f);
|
fsc_close(f);
|
||||||
|
|
||||||
|
|
@ -94,122 +79,41 @@ clean:
|
||||||
static int rmwatch(const char *path, const char *name) {
|
static int rmwatch(const char *path, const char *name) {
|
||||||
|
|
||||||
char *fpath;
|
char *fpath;
|
||||||
int wd;
|
int ret;
|
||||||
|
|
||||||
fpath = path_normalize(path, name, 1);
|
fpath = path_normalize(path, name, 1);
|
||||||
|
|
||||||
if (!fpath)
|
if (!fpath)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
wd = inotify_map_get_wd(fpath);
|
ret = inotify_ignore(fpath);
|
||||||
|
|
||||||
if (wd <= 0) {
|
|
||||||
logmsg(LOG_DEBUG, "remove watch: can't find %s", fpath);
|
|
||||||
free(fpath);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inotify_unmap_path(fpath) == 0) {
|
|
||||||
|
|
||||||
logmsg(LOG_DEBUG, "remove watch: %i %s", wd, fpath);
|
|
||||||
if (inotify_rm_watch(fd, wd) < 0) {
|
|
||||||
logerrno(LOG_CRIT, "intotify_rm_watch", errno);
|
|
||||||
free(fpath);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(fpath);
|
free(fpath);
|
||||||
return 0;
|
return ret;
|
||||||
}
|
|
||||||
|
|
||||||
static void proc_event(struct inotify_event *iev) {
|
|
||||||
|
|
||||||
int i;
|
|
||||||
struct list *watch_list;
|
|
||||||
int isdir;
|
|
||||||
|
|
||||||
logmsg(LOG_DEBUG, "RAW EVENT: %i, %x, %s", iev->wd, iev->mask, iev->name);
|
|
||||||
|
|
||||||
if (iev->mask & IN_IGNORED) {
|
|
||||||
inotify_unmap_wd(iev->wd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* lookup watch descriptors */
|
|
||||||
watch_list = inotify_map_get_path(iev->wd);
|
|
||||||
|
|
||||||
if (!watch_list) {
|
|
||||||
logmsg(LOG_WARN, "-- IGNORING EVENT -- invalid watchdescriptor %i", iev->wd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set dir and drop that bit off (so its not in the way) */
|
|
||||||
isdir = (iev->mask & IN_ISDIR) != 0;
|
|
||||||
iev->mask &= ~IN_ISDIR;
|
|
||||||
|
|
||||||
for(i=0; i < watch_list->nr; i++) {
|
|
||||||
|
|
||||||
uint8_t type = NOTIFY_UNKNOWN;
|
|
||||||
struct watch *watch = watch_list->items[i];
|
|
||||||
notify_event *event = notify_event_new();
|
|
||||||
|
|
||||||
notify_event_set_path(event, watch->path);
|
|
||||||
notify_event_set_filename(event, iev->name);
|
|
||||||
event->dir = isdir;
|
|
||||||
|
|
||||||
/* queue event before doing any fscrawl on a subdirectory
|
|
||||||
to prevent messing up the order */
|
|
||||||
queue_enqueue(event_queue, event);
|
|
||||||
|
|
||||||
switch(iev->mask) {
|
|
||||||
case IN_CREATE :
|
|
||||||
case IN_MOVED_TO :
|
|
||||||
addwatch(event->path, event->filename);
|
|
||||||
type = NOTIFY_CREATE;
|
|
||||||
break;
|
|
||||||
case IN_DELETE :
|
|
||||||
case IN_MOVED_FROM :
|
|
||||||
rmwatch(event->path, event->filename);
|
|
||||||
type = NOTIFY_DELETE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
event->type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
list_destroy(watch_list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int notify_init() {
|
int notify_init() {
|
||||||
|
|
||||||
if (init)
|
if (!init) {
|
||||||
return 0;
|
if (inotify_init() < 0)
|
||||||
|
return -1;
|
||||||
inotify_backend_init();
|
init_ev_q = queue_init();
|
||||||
|
init = 1;
|
||||||
event_queue = queue_init();
|
}
|
||||||
|
|
||||||
if (event_queue == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
init = 1;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify_exit() {
|
void notify_exit() {
|
||||||
|
|
||||||
if (!init)
|
if (init) {
|
||||||
return;
|
|
||||||
|
|
||||||
inotify_unmap_all();
|
|
||||||
|
|
||||||
if (event_queue) {
|
|
||||||
notify_event *e;
|
notify_event *e;
|
||||||
while((e = queue_dequeue(event_queue)))
|
while((e = queue_dequeue(init_ev_q)))
|
||||||
notify_event_del(e);
|
notify_event_del(e);
|
||||||
queue_destroy(event_queue);
|
queue_destroy(init_ev_q);
|
||||||
event_queue = NULL;
|
init_ev_q = NULL;
|
||||||
|
|
||||||
|
inotify_exit();
|
||||||
|
|
||||||
|
init = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -230,26 +134,22 @@ int notify_rm_watch(const char *path) {
|
||||||
return rmwatch(path, NULL);
|
return rmwatch(path, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BUFSZ (IN_EVENT_SIZE * (1 << 10))
|
|
||||||
|
|
||||||
notify_event* notify_read() {
|
notify_event* notify_read() {
|
||||||
|
|
||||||
|
notify_event *ev;
|
||||||
|
|
||||||
if (!init)
|
if (!init)
|
||||||
die("inotify is not instantiated.");
|
die("inotify is not instantiated.");
|
||||||
|
|
||||||
if (queue_isempty(event_queue)) {
|
if (!queue_isempty(init_ev_q))
|
||||||
|
return queue_dequeue(init_ev_q);
|
||||||
|
|
||||||
|
ev = inotify_read();
|
||||||
|
|
||||||
char buf[BUFSZ];
|
if (ev && ev->dir && ev->type == NOTIFY_CREATE)
|
||||||
int offset = 0, read = inotify_backend_read(buf, BUFSZ);
|
addwatch(ev->path, ev->filename);
|
||||||
|
|
||||||
while(read > offset) {
|
return ev;
|
||||||
struct inotify_event *ev = (struct inotify_event*) &buf[offset];
|
|
||||||
proc_event(ev);
|
|
||||||
offset += sizeof(struct inotify_event) + ev->len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return queue_dequeue(event_queue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify_stat() {
|
void notify_stat() {
|
||||||
|
|
|
||||||
Reference in a new issue