inotify.c: more robust checking if we have instantiated properly.
This commit is contained in:
parent
917666b012
commit
1f1e9f7764
1 changed files with 22 additions and 8 deletions
|
|
@ -31,8 +31,10 @@ typedef struct inotify_event inoev;
|
||||||
|
|
||||||
#define WATCH_MASK (IN_MOVE | IN_CREATE | IN_DELETE | IN_ONLYDIR)
|
#define WATCH_MASK (IN_MOVE | IN_CREATE | IN_DELETE | IN_ONLYDIR)
|
||||||
|
|
||||||
|
static int init = 0;
|
||||||
|
|
||||||
/* Inotify file descriptor */
|
/* Inotify file descriptor */
|
||||||
static int fd = 0;
|
static int fd;
|
||||||
|
|
||||||
/* redblack tree */
|
/* redblack tree */
|
||||||
static rbtree tree = RBTREE_INIT(free, NULL, strcmp);
|
static rbtree tree = RBTREE_INIT(free, NULL, strcmp);
|
||||||
|
|
@ -195,10 +197,8 @@ static void proc_event(inoev *iev) {
|
||||||
|
|
||||||
int notify_init() {
|
int notify_init() {
|
||||||
|
|
||||||
if (fd > 0) {
|
if (init)
|
||||||
fprintf(stderr, "inotify already instantiated.");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
fd = inotify_init();
|
fd = inotify_init();
|
||||||
|
|
||||||
|
|
@ -209,13 +209,17 @@ int notify_init() {
|
||||||
|
|
||||||
if (event_queue == NULL)
|
if (event_queue == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return 1;
|
init = 1;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify_exit() {
|
void notify_exit() {
|
||||||
|
|
||||||
fd = 0;
|
if (!init)
|
||||||
|
return;
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
rbtree_free(&tree);
|
rbtree_free(&tree);
|
||||||
|
|
||||||
|
|
@ -230,11 +234,15 @@ void notify_exit() {
|
||||||
*/
|
*/
|
||||||
int notify_add_watch(const char *path) {
|
int notify_add_watch(const char *path) {
|
||||||
|
|
||||||
|
if (!init)
|
||||||
|
die("inotify is not instantiated.");
|
||||||
return (addwatch(path, NULL, 1) > 0) ? 1 : -1;
|
return (addwatch(path, NULL, 1) > 0) ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int notify_rm_watch(const char *path) {
|
int notify_rm_watch(const char *path) {
|
||||||
|
|
||||||
|
if (!init)
|
||||||
|
die("inotify is not instantiated.");
|
||||||
return rmwatch(path, NULL);
|
return rmwatch(path, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,6 +250,9 @@ notify_event* notify_read() {
|
||||||
|
|
||||||
/* bytes ready on the inotify descriptor */
|
/* bytes ready on the inotify descriptor */
|
||||||
int ioready;
|
int ioready;
|
||||||
|
|
||||||
|
if (!init)
|
||||||
|
die("inotify is not instantiated.");
|
||||||
|
|
||||||
/* if we don't have pending events, wait for more data on fd */
|
/* if we don't have pending events, wait for more data on fd */
|
||||||
if (queue_isempty(event_queue)) {
|
if (queue_isempty(event_queue)) {
|
||||||
|
|
@ -298,6 +309,9 @@ static void print_node(rbnode *node) {
|
||||||
|
|
||||||
void notify_stat() {
|
void notify_stat() {
|
||||||
|
|
||||||
|
if (!init)
|
||||||
|
die("inotify is not instantiated.");
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
rbtree_walk(&tree, print_node);
|
rbtree_walk(&tree, print_node);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Reference in a new issue