Archived
1
0
Fork 0

inotify: breakup mapping into a module.

This commit is contained in:
Henrik Hautakoski 2011-01-14 15:15:03 +01:00
parent 51dd9eac43
commit cc590370bf
7 changed files with 325 additions and 8 deletions

166
src/inotify-map.c Normal file
View file

@ -0,0 +1,166 @@
/* inotify-map.c
*
* (C) Copyright 2011 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include "xalloc.h"
#include "rbtree.h"
#include "inotify-map.h"
/*
* IMPORTANT: always have the member used for comparison as the first member
* to make it possible to cast a struct pointer to the first member type.
*/
struct map_wd {
int wd;
struct str_list paths;
};
struct map_path {
const char *path;
int wd;
};
static void wd_free(void *ptr);
static int wd_cmp(const void *a, const void *b);
static int path_cmp(const void *a, const void *b);
static rbtree tree_wd_paths = RBTREE_INIT(wd_free, NULL, wd_cmp);
static rbtree tree_path_wd = RBTREE_INIT(xfree, NULL, path_cmp);
static int wd_cmp(const void *a, const void *b) {
return *((int*)a) - *((int*)b);
}
static int path_cmp(const void *a, const void *b) {
return strcmp(*((char**)a), *((char**)b));
}
static void wd_free(void *ptr) {
struct map_wd *m = (struct map_wd*)ptr;
str_list_clear(&m->paths);
xfree(m);
}
static void map_path_wd(const char *path, int wd) {
struct map_path *p = rbtree_search(&tree_path_wd, &path);
if (!p) {
p = xmalloc(sizeof(struct map_path));
p->path = path;
rbtree_insert(&tree_path_wd, p);
}
p->wd = wd;
}
static int unmap_path_wd(int wd, const char *path) {
return rbtree_delete(&tree_path_wd, &path);
}
static void unmap_wd_path(int wd, const char *path) {
struct map_wd *m = rbtree_search(&tree_wd_paths, &wd);
if (m) {
char *p = str_list_remove(&m->paths, path);
if (p) {
xfree(p);
if (str_list_isempty(&m->paths))
rbtree_delete(&tree_wd_paths, m);
}
}
}
/*
* unmaps all paths from the 'struct map_wd' and from the search tree 'tree_path_wd'.
*/
static void unmap_all_paths(struct map_wd *map) {
char *path;
while(path = str_list_reduce(&map->paths)) {
rbtree_delete(&tree_path_wd, &path);
xfree(path);
}
}
void inotify_map(int wd, const char *path) {
struct map_wd *m = rbtree_search(&tree_wd_paths, &wd);
if (!m) {
m = xmalloc(sizeof(struct map_wd));
m->wd = wd;
str_list_init(&m->paths);
rbtree_insert(&tree_wd_paths, m);
}
if (!str_list_has(&m->paths, path)) {
int index = str_list_insert(&m->paths, xstrdup(path));
map_path_wd(m->paths.items[index], wd);
}
}
int inotify_unmap_wd(int wd) {
struct map_wd *m = rbtree_search(&tree_wd_paths, &wd);
if (m) {
unmap_all_paths(m);
return rbtree_delete(&tree_wd_paths, &wd);
}
return 0;
}
int inotify_unmap_path(const char *path) {
struct map_path *m = rbtree_search(&tree_path_wd, &path);
if (m) {
int wd = m->wd;
unmap_path_wd(wd, path);
unmap_wd_path(wd, path);
return 1;
}
return 0;
}
void inotify_unmap_all() {
rbtree_free(&tree_path_wd);
rbtree_free(&tree_wd_paths);
}
int inotify_map_get_wd(const char *path) {
struct map_path *p = rbtree_search(&tree_path_wd, &path);
if (p)
return p->wd;
return 0;
}
const struct str_list* inotify_map_lookup(int wd) {
struct map_wd *m = rbtree_search(&tree_wd_paths, &wd);
return m ? &m->paths : NULL;
}
const struct str_list* inotify_map_lookup_by_path(const char *path) {
struct map_path *p = rbtree_search(&tree_path_wd, &path);
return p ? inotify_map_lookup(p->wd) : NULL;
}
int inotify_map_isempty() {
return rbtree_is_empty(&tree_wd_paths)
&& rbtree_is_empty(&tree_path_wd);
}

32
src/inotify-map.h Normal file
View file

@ -0,0 +1,32 @@
/* inotify-map.h
*
* (C) Copyright 2011 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#ifndef __INOTIFY_MAP_H
#define __INOTIFY_MAP_H
#include "str-list.h"
void inotify_map(int wd, const char *path);
int inotify_unmap_wd(int wd);
int inotify_unmap_path(const char *path);
void inotify_unmap_all();
int inotify_map_get_wd(const char *path);
const struct str_list* inotify_map_lookup(int wd);
const struct str_list* inotify_map_lookup_by_path(const char *path);
int inotify_map_isempty();
#endif /* __INOTIFY_MAP_H */

View file

@ -23,7 +23,7 @@ static void resize(struct str_list *list) {
list->items = xrealloc(list->items, sizeof(list->items) * list->nr);
}
static int get_index(struct str_list *list, const char *str, int *match) {
static int get_index(const struct str_list *list, const char *str, int *match) {
int min = -1, max = list->nr;
@ -134,13 +134,13 @@ char* str_list_reduce(struct str_list *list) {
return item;
}
int str_list_indexof(struct str_list *list, const char *str) {
int str_list_indexof(const struct str_list *list, const char *str) {
int match, index = get_index(list, str, &match);
return match ? index : -1;
}
char* str_list_lookup(struct str_list *list, const char *str) {
char* str_list_lookup(const struct str_list *list, const char *str) {
int match, index = get_index(list, str, &match);
@ -149,7 +149,7 @@ char* str_list_lookup(struct str_list *list, const char *str) {
return list->items[index];
}
int str_list_has(struct str_list *list, const char *str) {
int str_list_has(const struct str_list *list, const char *str) {
int rc;
get_index(list, str, &rc);

View file

@ -34,11 +34,11 @@ char* str_list_remove(struct str_list *list, const char *str);
char* str_list_reduce(struct str_list *list);
int str_list_indexof(struct str_list *list, const char *str);
int str_list_indexof(const struct str_list *list, const char *str);
char* str_list_lookup(struct str_list *list, const char *str);
char* str_list_lookup(const struct str_list *list, const char *str);
int str_list_has(struct str_list *list, const char *str);
int str_list_has(const struct str_list *list, const char *str);
#define str_list_foreach(i, l) \
for(i = (l)->items; i < (l)->items + (l)->nr; ++i)