Archived
1
0
Fork 0

fs/notify.h: some small API change.

This commit is contained in:
Henrik Hautakoski 2010-06-10 00:04:04 +02:00 committed by Henrik Hautakoski
parent 8f89092faa
commit 20a13a90b4
2 changed files with 36 additions and 39 deletions

6
src/fs/notify.h Normal file → Executable file
View file

@ -10,7 +10,6 @@
*/ */
#ifndef _FS_NOTIFY_H #ifndef _FS_NOTIFY_H
#define _FS_NOTIFY_H #define _FS_NOTIFY_H
/* notify event def's and operations */ /* notify event def's and operations */
@ -22,9 +21,7 @@ void notify_cleanup();
int notify_add_watch(const char *path); int notify_add_watch(const char *path);
void notify_rm_watch(unsigned int wd); int notify_rm_watch(const char *path);
void notify_rm_watch_path(const char *path);
void notify_print_stat(); void notify_print_stat();
@ -32,6 +29,7 @@ notify_event* notify_read();
void notify_stat(); void notify_stat();
/* TODO: context-switch/threading should be implementation specific. */
int notify_is_ready(); int notify_is_ready();
#endif /* _FS_NOTIFY_H */ #endif /* _FS_NOTIFY_H */

69
src/fs/notify_inotify.c Normal file → Executable file
View file

@ -9,6 +9,12 @@
* (at your option) any later version. * (at your option) any later version.
*/ */
#include "notify.h"
/* red black tree for watch descriptors */
#include "../common/rbtree.h"
#include "../common/debug.h"
#include "../common/path.h"
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -18,12 +24,6 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/inotify.h> #include <sys/inotify.h>
/* red black tree for watch descriptors */
#include "../common/rbtree.h"
#include "../common/debug.h"
#include "../common/path.h"
#include "notify.h"
typedef struct inotify_event inoev; typedef struct inotify_event inoev;
#define MAXEVENT 0x200 #define MAXEVENT 0x200
@ -47,18 +47,6 @@ static char *inobuf = NULL;
/* current event */ /* current event */
static notify_event event; static notify_event event;
static int rmwatch(unsigned int wd) {
void *data = rbtree_delete(&tree, wd);
if (data == NULL)
return 0;
dprint("remove watch: %i %s\n", wd, (char*) data);
free(data);
return 1;
}
static int addwatch(const char *path, const char *name) { static int addwatch(const char *path, const char *name) {
rbnode *node; rbnode *node;
@ -93,23 +81,35 @@ static int addwatch(const char *path, const char *name) {
return wd; return wd;
} }
/* REF:1 */ static int rmwatch(unsigned int wd) {
void *data = rbtree_delete(&tree, wd);
if (data == NULL)
return 0;
dprint("remove watch: %i %s\n", wd, (char*) data);
inotify_rm_watch(fd, wd);
free(data);
return 1;
}
static int proc_event(inoev *iev) { static int proc_event(inoev *iev) {
rbnode *node; rbnode *node;
char buf[4096]; char buf[4096];
uint8_t type = NOTIFY_UNKNOWN; uint8_t type = NOTIFY_UNKNOWN;
if (iev == NULL) if (iev == NULL)
return 0; return 0;
/* notify_event_clear(&event); */ #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)
fprintf(stderr, ", %s\n", iev->name); fprintf(stderr, ", %s\n", iev->name);
else else
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif
/* lookup the watch descriptor in rbtree */ /* lookup the watch descriptor in rbtree */
node = rbtree_search(&tree, iev->wd); node = rbtree_search(&tree, iev->wd);
@ -148,9 +148,9 @@ static int proc_event(inoev *iev) {
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_path(buf); notify_rm_watch_path(buf);
case IN_DELETE : case IN_DELETE :
type = NOTIFY_DELETE; type = NOTIFY_DELETE;
break; break;
@ -195,8 +195,10 @@ void notify_cleanup() {
/* free rbtree */ /* free rbtree */
rbtree_free(&tree, NULL); rbtree_free(&tree, NULL);
if (inobuf != NULL) if (inobuf != NULL) {
free(inobuf); free(inobuf);
inobuf = NULL;
}
fd = 0; fd = 0;
} }
@ -209,21 +211,18 @@ int notify_add_watch(const char *path) {
return addwatch(path, NULL); return addwatch(path, NULL);
} }
void notify_rm_watch(unsigned int wd) { int notify_rm_watch(const char *path) {
inotify_rm_watch(fd, wd);
}
void notify_rm_watch_path(const char *path) {
rbnode *node = rbtree_cmp_search(&tree, (void*)path, strlen(path)); rbnode *node = rbtree_cmp_search(&tree, (void*)path, strlen(path));
if (node == NULL) { if (node == NULL) {
dprint("remove watch by path: can't find %s\n", path); dprint("remove watch: can't find %s\n", path);
return; return -1;
} }
dprint("remove watch by path: %i %s\n", node->key, (char*) node->data); dprint("remove watch: %i %s\n", node->key, (char*) node->data);
notify_rm_watch(node->key); rmwatch(node->key);
return 0;
} }
/* /*