Archived
1
0
Fork 0

inotify-watch: Adding inotify watch datastructure.

This commit is contained in:
Henrik Hautakoski 2011-02-15 16:33:51 +01:00
parent 9e9321dc65
commit 3b68160892
4 changed files with 146 additions and 0 deletions

75
src/inotify-watch.c Normal file
View file

@ -0,0 +1,75 @@
#include <stdio.h>
#include <string.h>
#include "xalloc.h"
#include "path.h"
#include "inotify-watch.h"
struct watch* inotify_watch_new(int wd, const char *path) {
struct watch *w = xmallocz(sizeof(struct watch));
w->wd = wd;
w->path = path;
return w;
}
static void watch_free(struct tree *tree, void *data) {
void (*fn)(struct watch *) = data;
if (fn)
fn((struct watch*)tree);
xfree(tree);
}
void inotify_watch_destroy(struct watch *watch, void (*fn)(struct watch *)) {
if (watch) {
struct tree *tree = (struct tree*) watch;
if (!tree_is_root(tree))
tree_detach(tree);
tree_traverse(tree, watch_free, fn);
}
}
struct watch* inotify_watch_add(struct watch *parent, struct watch *watch) {
if (parent->tree.child) {
struct watch *it = (struct watch*)parent->tree.child;
/* move nodes that are children of this watch alongside watch */
while(it) {
if (path_isparent(it->path, watch->path)) {
struct watch *next = (struct watch*)it->tree.next;
tree_move(&watch->tree, &it->tree);
it = next;
} else {
it = (struct watch*)it->tree.next;
}
}
}
return (struct watch*)tree_link(&parent->tree, &watch->tree);
}
void inotify_watch_rm(struct watch *watch) {
tree_unlink(&watch->tree);
}
struct watch* inoitfy_watch_find_child(struct watch *watch, const char *path) {
if (watch->tree.child) {
struct watch *it = (struct watch *) watch->tree.child;
while(it) {
if (!strcmp(watch->path, it->path))
return it;
it = (struct watch*) it->tree.next;
}
}
return NULL;
}

21
src/inotify-watch.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef INOTIFY_WATCH_H
#define INOTIFY_WATCH_H
#include "tree.h"
struct watch {
struct tree tree;
int wd;
const char *path;
};
struct watch* inotify_watch_new(int wd, const char *path);
void inotify_watch_destroy(struct watch *watch, void (*fn)(struct watch *));
struct watch* inotify_watch_add(struct watch *parent, struct watch *watch);
void inotify_watch_rm(struct watch *watch);
#endif