diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-08-22 17:49:30 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2016-08-25 22:29:47 +0300 |
commit | 6f39bd7e00acee689ce9a8a3916888a815242557 (patch) | |
tree | d6319602bb9f4b171358823a4b0c38270f54f841 | |
parent | cccafb5ff305978b2b7a41fd68ade359aa27007b (diff) | |
download | direvent-6f39bd7e00acee689ce9a8a3916888a815242557.tar.gz direvent-6f39bd7e00acee689ce9a8a3916888a815242557.tar.bz2 |
Rename: struct dirwatcher -> struct watchpoint
-rw-r--r-- | src/config.c | 12 | ||||
-rw-r--r-- | src/direvent.c | 2 | ||||
-rw-r--r-- | src/direvent.h | 55 | ||||
-rw-r--r-- | src/ev_inotify.c | 72 | ||||
-rw-r--r-- | src/ev_kqueue.c | 38 | ||||
-rw-r--r-- | src/handler.c | 20 | ||||
-rw-r--r-- | src/progman.c | 8 | ||||
-rw-r--r-- | src/watcher.c | 274 |
8 files changed, 241 insertions, 240 deletions
diff --git a/src/config.c b/src/config.c index a11e629..1f8d26e 100644 --- a/src/config.c +++ b/src/config.c @@ -173,24 +173,24 @@ eventconf_flush(grecs_locus_t *loc) { struct grecs_list_entry *ep; struct handler *hp = handler_copy(&eventconf.handler); for (ep = eventconf.pathlist->head; ep; ep = ep->next) { struct pathent *pe = ep->data; - struct dirwatcher *dwp; + struct watchpoint *wpt; int isnew; - dwp = dirwatcher_install(pe->path, &isnew); - if (!dwp) + wpt = watchpoint_install(pe->path, &isnew); + if (!wpt) abort(); - if (!isnew && dwp->depth != pe->depth) + if (!isnew && wpt->depth != pe->depth) grecs_error(loc, 0, _("%s: recursion depth does not match previous definition"), pe->path); - dwp->depth = pe->depth; - handler_list_append(dwp->handler_list, hp); + wpt->depth = pe->depth; + handler_list_append(wpt->handler_list, hp); } grecs_list_free(eventconf.pathlist); eventconf_init(); } static int diff --git a/src/direvent.c b/src/direvent.c index 6c6a278..e1775f9 100644 --- a/src/direvent.c +++ b/src/direvent.c @@ -361,13 +361,13 @@ setuser(const char *user) strerror(errno)); exit(2); } } void -ev_log(int flags, struct dirwatcher *dp) +ev_log(int flags, struct watchpoint *dp) { int i; char *p; if (debug_level > 0) { for (p = trans_tokfirst(sysev_transtab, flags, &i); p; diff --git a/src/direvent.h b/src/direvent.h index bc5bb46..faa1657 100644 --- a/src/direvent.h +++ b/src/direvent.h @@ -92,33 +92,34 @@ struct handler { gid_t *gidv; /* Run with these groups' privileges */ size_t gidc; /* Number of elements in gidv */ unsigned timeout; /* Handler timeout */ char **env; /* Environment */ } prog; struct { - struct dirwatcher *watcher; + struct watchpoint *watchpoint; } sentinel; } v; #define prog_flags v.prog.flags #define prog_command v.prog.command #define prog_uid v.prog.uid #define prog_gidv v.prog.gidv #define prog_gidc v.prog.gidc #define prog_timeout v.prog.timeout #define prog_env v.prog.env -#define sentinel_watcher v.sentinel.watcher +#define sentinel_watchpoint v.sentinel.watchpoint }; typedef struct handler_list *handler_list_t; typedef struct handler_iterator *handler_iterator_t; -/* A directory watcher is described by the following structure */ -struct dirwatcher { +/* Watchpoint links the directory being monitored and a list of + handlers for various events: */ +struct watchpoint { size_t refcnt; int wd; /* Watch descriptor */ - struct dirwatcher *parent; /* Points to the parent watcher. + struct watchpoint *parent; /* Points to the parent watcher. NULL for top-level watchers */ char *dirname; /* Pathname being watched */ handler_list_t handler_list; /* List of handlers */ int depth; /* Recursion depth */ char *split_p; /* Points to the deleted directory separator in dirname (see @@ -171,16 +172,16 @@ void debugprt(const char *fmt, ...); #define debug(l, c) do { if (debug_level>=(l)) debugprt c; } while(0) void signal_setup(void (*sf) (int)); int detach(void (*)(void)); -int sysev_filemask(struct dirwatcher *dp); +int sysev_filemask(struct watchpoint *dp); void sysev_init(void); -int sysev_add_watch(struct dirwatcher *dwp, event_mask mask); -void sysev_rm_watch(struct dirwatcher *dwp); +int sysev_add_watch(struct watchpoint *dwp, event_mask mask); +void sysev_rm_watch(struct watchpoint *dwp); int sysev_select(void); int sysev_name_to_code(const char *name); const char *sysev_code_to_name(int code); int defevt(const char *name, event_mask *mask, int line); int getevt(const char *name, event_mask *mask); @@ -237,41 +238,41 @@ void config_help(void); void config_init(void); void config_parse(const char *file); int get_facility(const char *arg); int get_priority(const char *arg); -int dirwatcher_init(struct dirwatcher *dwp); -void dirwatcher_ref(struct dirwatcher *dw); -void dirwatcher_unref(struct dirwatcher *dw); -void dirwatcher_gc(void); +int watchpoint_init(struct watchpoint *dwp); +void watchpoint_ref(struct watchpoint *dw); +void watchpoint_unref(struct watchpoint *dw); +void watchpoint_gc(void); -int dirwatcher_pattern_match(struct dirwatcher *dwp, const char *file_name); +int watchpoint_pattern_match(struct watchpoint *dwp, const char *file_name); void setup_watchers(void); void shutdown_watchers(void); -struct dirwatcher *dirwatcher_lookup(const char *dirname); +struct watchpoint *watchpoint_lookup(const char *dirname); int check_new_watcher(const char *dir, const char *name); -struct dirwatcher *dirwatcher_install(const char *path, int *pnew); -struct dirwatcher *dirwatcher_install_ptr(struct dirwatcher *dw); -void dirwatcher_suspend(struct dirwatcher *dwp); -void dirwatcher_destroy(struct dirwatcher *dwp); -struct dirwatcher *dirwatcher_install_sentinel(struct dirwatcher *dwp); +struct watchpoint *watchpoint_install(const char *path, int *pnew); +struct watchpoint *watchpoint_install_ptr(struct watchpoint *dw); +void watchpoint_suspend(struct watchpoint *dwp); +void watchpoint_destroy(struct watchpoint *dwp); +struct watchpoint *watchpoint_install_sentinel(struct watchpoint *dwp); -int watch_pathname(struct dirwatcher *parent, const char *dirname, int isdir, int notify); +int watch_pathname(struct watchpoint *parent, const char *dirname, int isdir, int notify); -char *split_pathname(struct dirwatcher *dp, char **dirname); -void unsplit_pathname(struct dirwatcher *dp); +char *split_pathname(struct watchpoint *dp, char **dirname); +void unsplit_pathname(struct watchpoint *dp); -void ev_log(int flags, struct dirwatcher *dp); -void deliver_ev_create(struct dirwatcher *dp, const char *name); -int subwatcher_create(struct dirwatcher *parent, const char *dirname, +void ev_log(int flags, struct watchpoint *dp); +void deliver_ev_create(struct watchpoint *dp, const char *name); +int subwatcher_create(struct watchpoint *parent, const char *dirname, int isdir, int notify); -struct handler *handler_itr_first(struct dirwatcher *dp, +struct handler *handler_itr_first(struct watchpoint *dp, handler_iterator_t *itr); struct handler *handler_itr_next(handler_iterator_t *itr); struct handler *handler_itr_current(handler_iterator_t itr); #define for_each_handler(d,i,h) \ for (h = handler_itr_first(d, &(i)); \ @@ -288,13 +289,13 @@ void handler_list_remove(handler_list_t hlist, struct handler *hp); size_t handler_list_size(handler_list_t hlist); struct process *process_lookup(pid_t pid); void process_cleanup(int expect_term); void process_timeouts(void); -int run_handler(struct dirwatcher *dp, struct handler *hp, event_mask *event, +int run_handler(struct watchpoint *dp, struct handler *hp, event_mask *event, const char *dir, const char *file); char **environ_setup(char **hint, char **kve); #define NITEMS(a) ((sizeof(a)/sizeof((a)[0]))) struct sigtab { int signo; diff --git a/src/ev_inotify.c b/src/ev_inotify.c index 6d4c03a..7b2ee1d 100644 --- a/src/ev_inotify.c +++ b/src/ev_inotify.c @@ -42,73 +42,73 @@ event_mask genev_xlat[] = { { 0 } }; static int ifd; -static struct dirwatcher **dwtab; -static size_t dwsize; +static struct watchpoint **wptab; +static size_t wpsize; static int -dwreg(int wd, struct dirwatcher *wp) +wpreg(int wd, struct watchpoint *wpt) { if (wd < 0) abort(); - if (wd >= dwsize) { - size_t n = dwsize; - struct dirwatcher **p; + if (wd >= wpsize) { + size_t n = wpsize; + struct watchpoint **p; if (n == 0) n = sysconf(_SC_OPEN_MAX); while (wd >= n) { n *= 2; - if (n < dwsize) { + if (n < wpsize) { diag(LOG_CRIT, _("can't allocate memory for fd %d"), wd); return -1; } } - p = realloc(dwtab, n * sizeof(dwtab[0])); + p = realloc(wptab, n * sizeof(wptab[0])); if (!p) { diag(LOG_CRIT, _("can't allocate memory for fd %d"), wd); return -1; } - memset(p + dwsize, 0, (n - dwsize) * sizeof(dwtab[0])); - dwtab = p; - dwsize = n; + memset(p + wpsize, 0, (n - wpsize) * sizeof(wptab[0])); + wptab = p; + wpsize = n; } - dirwatcher_ref(wp); - dwtab[wd] = wp; + watchpoint_ref(wpt); + wptab[wd] = wpt; return 0; } static void -dwunreg(int wd) +wpunreg(int wd) { - if (wd < 0 || wd > dwsize) + if (wd < 0 || wd > wpsize) abort(); - if (dwtab[wd]) { - dirwatcher_unref(dwtab[wd]); - dwtab[wd] = NULL; + if (wptab[wd]) { + watchpoint_unref(wptab[wd]); + wptab[wd] = NULL; } } -static struct dirwatcher * -dwget(int wd) +static struct watchpoint * +wpget(int wd) { - if (wd >= 0 && wd < dwsize) - return dwtab[wd]; + if (wd >= 0 && wd < wpsize) + return wptab[wd]; return NULL; } int -sysev_filemask(struct dirwatcher *dp) +sysev_filemask(struct watchpoint *dp) { return 0; } void sysev_init() @@ -118,66 +118,66 @@ sysev_init() diag(LOG_CRIT, "inotify_init: %s", strerror(errno)); exit(1); } } int -sysev_add_watch(struct dirwatcher *dwp, event_mask mask) +sysev_add_watch(struct watchpoint *wpt, event_mask mask) { - int wd = inotify_add_watch(ifd, dwp->dirname, mask.sys_mask); - if (wd >= 0 && dwreg(wd, dwp)) { + int wd = inotify_add_watch(ifd, wpt->dirname, mask.sys_mask); + if (wd >= 0 && wpreg(wd, wpt)) { inotify_rm_watch(ifd, wd); return -1; } return wd; } void -sysev_rm_watch(struct dirwatcher *dwp) +sysev_rm_watch(struct watchpoint *wpt) { - dwunreg(dwp->wd); - inotify_rm_watch(ifd, dwp->wd); + wpunreg(wpt->wd); + inotify_rm_watch(ifd, wpt->wd); } /* Remove a watcher identified by its directory and file name */ void remove_watcher(const char *dir, const char *name) { - struct dirwatcher *dwp; + struct watchpoint *wpt; char *fullname = mkfilename(dir, name); if (!fullname) { diag(LOG_EMERG, "not enough memory: " "cannot look up a watcher to delete"); return; } - dwp = dirwatcher_lookup(fullname); + wpt = watchpoint_lookup(fullname); free(fullname); - if (dwp) - dirwatcher_suspend(dwp); + if (wpt) + watchpoint_suspend(wpt); } static void process_event(struct inotify_event *ep) { - struct dirwatcher *dp; + struct watchpoint *dp; struct handler *h; handler_iterator_t itr; event_mask m; char *dirname, *filename; - dp = dwget(ep->wd); + dp = wpget(ep->wd); if (!dp) { if (!(ep->mask & IN_IGNORED)) diag(LOG_NOTICE, _("watcher not found: %d (%s)"), ep->wd, ep->name); return; } if (ep->mask & IN_IGNORED) { diag(LOG_NOTICE, _("%s deleted"), dp->dirname); - dirwatcher_suspend(dp); + watchpoint_suspend(dp); return; } if (ep->mask & IN_Q_OVERFLOW) { diag(LOG_NOTICE, "event queue overflow"); diff --git a/src/ev_kqueue.c b/src/ev_kqueue.c index a9921c6..680fb42 100644 --- a/src/ev_kqueue.c +++ b/src/ev_kqueue.c @@ -57,81 +57,81 @@ sysev_init() } evtab = calloc(sysconf(_SC_OPEN_MAX), sizeof(evtab[0])); chtab = calloc(sysconf(_SC_OPEN_MAX), sizeof(chtab[0])); } int -sysev_filemask(struct dirwatcher *dp) +sysev_filemask(struct watchpoint *dp) { struct handler *h; handler_iterator_t itr; for_each_handler(dp, itr, h) { if (h->ev_mask.sys_mask) return S_IFMT; } return 0; } int -sysev_add_watch(struct dirwatcher *dwp, event_mask mask) +sysev_add_watch(struct watchpoint *wpt, event_mask mask) { - int wd = open(dwp->dirname, O_RDONLY); + int wd = open(wpt->dirname, O_RDONLY); if (wd >= 0) { struct stat st; int sysmask; if (fstat(wd, &st)) { close(wd); return -1; } - dwp->file_mode = st.st_mode; - dwp->file_ctime = st.st_ctime; + wpt->file_mode = st.st_mode; + wpt->file_ctime = st.st_ctime; sysmask = mask.sys_mask; if (S_ISDIR(st.st_mode) && mask.gen_mask & GENEV_CREATE) sysmask |= NOTE_WRITE; EV_SET(chtab + chcnt, wd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, sysmask, - 0, dwp); + 0, wpt); wd = chcnt++; } return wd; } void -sysev_rm_watch(struct dirwatcher *dwp) +sysev_rm_watch(struct watchpoint *wpt) { - close(chtab[dwp->wd].ident); - chtab[dwp->wd].ident = -1; - if (chclosed == -1 || chclosed > dwp->wd) - chclosed = dwp->wd; + close(chtab[wpt->wd].ident); + chtab[wpt->wd].ident = -1; + if (chclosed == -1 || chclosed > wpt->wd) + chclosed = wpt->wd; } static void chclosed_elim() { int i, j; if (chclosed == -1) return; for (i = chclosed, j = chclosed + 1; j < chcnt; j++) if (chtab[j].ident != -1) { - struct dirwatcher *dwp; + struct watchpoint *wpt; chtab[i] = chtab[j]; - dwp = chtab[i].udata; - dwp->wd = i; + wpt = chtab[i].udata; + wpt->wd = i; i++; } chcnt = i; chclosed = -1; } static void -check_created(struct dirwatcher *dp) +check_created(struct watchpoint *dp) { DIR *dir; struct dirent *ent; struct handler *h; dir = opendir(dp->dirname); @@ -147,13 +147,13 @@ check_created(struct dirwatcher *dp) if (ent->d_name[0] == '.' && (ent->d_name[1] == 0 || (ent->d_name[1] == '.' && ent->d_name[2] == 0))) continue; - if (dirwatcher_pattern_match(dp, ent->d_name)) + if (watchpoint_pattern_match(dp, ent->d_name)) continue; pathname = mkfilename(dp->dirname, ent->d_name); if (!pathname) { diag(LOG_ERR, "cannot stat %s/%s: not enough memory", dp->dirname, ent->d_name); @@ -168,13 +168,13 @@ check_created(struct dirwatcher *dp) the file is new (the timestamp precision leaves a time window long enough for a file to be created) so try the more expensive hash lookup to see if we know about that file. If the file is new, register a watcher for it. */ } else if (st.st_ctime > dp->file_ctime || - !dirwatcher_lookup(pathname)) { + !watchpoint_lookup(pathname)) { deliver_ev_create(dp, ent->d_name); subwatcher_create(dp, pathname, S_ISDIR(st.st_mode), 1); dp->file_ctime = st.st_ctime; } free(pathname); @@ -182,13 +182,13 @@ check_created(struct dirwatcher *dp) closedir(dir); } static void process_event(struct kevent *ep) { - struct dirwatcher *dp = ep->udata; + struct watchpoint *dp = ep->udata; struct handler *h; handler_iterator_t itr; event_mask m; char *filename, *dirname; if (!dp) { @@ -214,13 +214,13 @@ process_event(struct kevent *ep) } } unsplit_pathname(dp); if (ep->fflags & (NOTE_DELETE|NOTE_RENAME)) { debug(1, ("%s deleted", dp->dirname)); - dirwatcher_suspend(dp); + watchpoint_suspend(dp); return; } } int diff --git a/src/handler.c b/src/handler.c index 0e86a44..2d27824 100644 --- a/src/handler.c +++ b/src/handler.c @@ -105,13 +105,13 @@ handler_free(struct handler *hp) case HANDLER_EXTERN: free(hp->prog_command); free(hp->prog_gidv); envfree(hp->prog_env); break; case HANDLER_SENTINEL: - dirwatcher_unref(hp->sentinel_watcher); + watchpoint_unref(hp->sentinel_watchpoint); } } static void handler_unref(struct handler *hp) { @@ -143,35 +143,35 @@ struct handler_iterator { int advanced; }; static struct handler_iterator *itr_avail; struct handler * -handler_itr_first(struct dirwatcher *dwp, handler_iterator_t *ret_itr) +handler_itr_first(struct watchpoint *wpt, handler_iterator_t *ret_itr) { struct handler_iterator *itr; - if (!dwp->handler_list) + if (!wpt->handler_list) return NULL; if (itr_avail) { itr = itr_avail; itr_avail = itr->next; if (itr_avail) itr_avail->prev = NULL; } else itr = emalloc(sizeof *itr); itr->prev = NULL; - itr->next = dwp->handler_list->itr_chain; - itr->hlist = dwp->handler_list; - if (dwp->handler_list->itr_chain) - dwp->handler_list->itr_chain->prev = itr; - dwp->handler_list->itr_chain = itr; + itr->next = wpt->handler_list->itr_chain; + itr->hlist = wpt->handler_list; + if (wpt->handler_list->itr_chain) + wpt->handler_list->itr_chain->prev = itr; + wpt->handler_list->itr_chain = itr; - itr->ent = dwp->handler_list->list->head; + itr->ent = wpt->handler_list->list->head; itr->advanced = 0; *ret_itr = itr; return handler_itr_current(itr); } struct handler * @@ -281,8 +281,8 @@ handler_list_remove(handler_list_t hlist, struct handler *hp) } } grecs_list_remove_entry(hlist->list, ep); if (grecs_list_size(hlist->list) == 0) /* Remove watchers that don't have handlers */ - dirwatcher_gc(); + watchpoint_gc(); } diff --git a/src/progman.c b/src/progman.c index 823d51c..134deaf 100644 --- a/src/progman.c +++ b/src/progman.c @@ -538,22 +538,22 @@ run_handler_prog(struct handler *hp, event_mask *event, break; } return 0; } static int -run_sentinel(struct dirwatcher *dp, struct handler *hp) +run_sentinel(struct watchpoint *dp, struct handler *hp) { - dirwatcher_init(hp->sentinel_watcher); - dirwatcher_install_ptr(hp->sentinel_watcher); + watchpoint_init(hp->sentinel_watchpoint); + watchpoint_install_ptr(hp->sentinel_watchpoint); handler_list_remove(dp->handler_list, hp); return 0; } int -run_handler(struct dirwatcher *dp, struct handler *hp, event_mask *event, +run_handler(struct watchpoint *dp, struct handler *hp, event_mask *event, const char *dirname, const char *file) { int rc; switch (hp->type) { case HANDLER_EXTERN: rc = run_handler_prog(hp, event, dirname, file); diff --git a/src/watcher.c b/src/watcher.c index b625276..e0b4950 100644 --- a/src/watcher.c +++ b/src/watcher.c @@ -16,317 +16,317 @@ #include "direvent.h" #include <dirent.h> #include <sys/stat.h> void -dirwatcher_ref(struct dirwatcher *dw) +watchpoint_ref(struct watchpoint *wpt) { - ++dw->refcnt; + ++wpt->refcnt; } void -dirwatcher_unref(struct dirwatcher *dw) +watchpoint_unref(struct watchpoint *wpt) { - if (--dw->refcnt) + if (--wpt->refcnt) return; - free(dw->dirname); - handler_list_unref(dw->handler_list); - free(dw); + free(wpt->dirname); + handler_list_unref(wpt->handler_list); + free(wpt); } -struct dwref { +struct wpref { int used; - struct dirwatcher *dw; + struct watchpoint *wpt; }; static unsigned -dwname_hash(void *data, unsigned long hashsize) +wpref_hash(void *data, unsigned long hashsize) { - struct dwref *sym = data; - return hash_string(sym->dw->dirname, hashsize); + struct wpref *sym = data; + return hash_string(sym->wpt->dirname, hashsize); } static int -dwname_cmp(const void *a, const void *b) +wpref_cmp(const void *a, const void *b) { - struct dwref const *syma = a; - struct dwref const *symb = b; + struct wpref const *syma = a; + struct wpref const *symb = b; - return strcmp(syma->dw->dirname, symb->dw->dirname); + return strcmp(syma->wpt->dirname, symb->wpt->dirname); } static int -dwname_copy(void *a, void *b) +wpref_copy(void *a, void *b) { - struct dwref *syma = a; - struct dwref *symb = b; + struct wpref *syma = a; + struct wpref *symb = b; syma->used = 1; - syma->dw = symb->dw; + syma->wpt = symb->wpt; return 0; } static void -dwref_free(void *p) +wpref_free(void *p) { - struct dwref *dwref = p; - dirwatcher_unref(dwref->dw); - free(dwref); + struct wpref *wpref = p; + watchpoint_unref(wpref->wpt); + free(wpref); } struct hashtab *nametab; -struct dirwatcher * -dirwatcher_install(const char *path, int *pnew) +struct watchpoint * +watchpoint_install(const char *path, int *pnew) { - struct dirwatcher *dw, dwkey; - struct dwref key; - struct dwref *ent; + struct watchpoint *wpt, wpkey; + struct wpref key; + struct wpref *ent; int install = 1; if (!nametab) { - nametab = hashtab_create(sizeof(struct dwref), - dwname_hash, dwname_cmp, dwname_copy, - NULL, dwref_free); + nametab = hashtab_create(sizeof(struct wpref), + wpref_hash, wpref_cmp, wpref_copy, + NULL, wpref_free); if (!nametab) { diag(LOG_CRIT, _("not enough memory")); exit(1); } } - dwkey.dirname = (char*) path; - key.dw = &dwkey; + wpkey.dirname = (char*) path; + key.wpt = &wpkey; ent = hashtab_lookup_or_install(nametab, &key, &install); if (install) { - dw = ecalloc(1, sizeof(*dw)); - dw->dirname = estrdup(path); - dw->wd = -1; - dw->handler_list = handler_list_create(); - dw->refcnt++; - ent->dw = dw; + wpt = ecalloc(1, sizeof(*wpt)); + wpt->dirname = estrdup(path); + wpt->wd = -1; + wpt->handler_list = handler_list_create(); + wpt->refcnt++; + ent->wpt = wpt; } if (!ent) abort(); /* FIXME */ if (pnew) *pnew = install; - return ent->dw; + return ent->wpt; } -struct dirwatcher * -dirwatcher_install_ptr(struct dirwatcher *dw) +struct watchpoint * +watchpoint_install_ptr(struct watchpoint *wpt) { - struct dwref key; + struct wpref key; int install = 1; - key.dw = dw; + key.wpt = wpt; if (!hashtab_lookup_or_install(nametab, &key, &install)) { diag(LOG_CRIT, _("not enough memory")); exit(1); } - dirwatcher_ref(dw); - return dw; + watchpoint_ref(wpt); + return wpt; } static int -dwref_gc(struct hashent *ent, void *data) +wpref_gc(struct hashent *ent, void *data) { - struct dwref *dwref = (struct dwref *) ent; - struct dirwatcher *dwp = dwref->dw; + struct wpref *wpref = (struct wpref *) ent; + struct watchpoint *wpt = wpref->wpt; - if (handler_list_size(dwp->handler_list) == 0) - dirwatcher_destroy(dwp); + if (handler_list_size(wpt->handler_list) == 0) + watchpoint_destroy(wpt); return 0; } void -dirwatcher_gc(void) +watchpoint_gc(void) { - hashtab_foreach(nametab, dwref_gc, NULL); + hashtab_foreach(nametab, wpref_gc, NULL); } -struct dirwatcher * -dirwatcher_lookup(const char *dirname) +struct watchpoint * +watchpoint_lookup(const char *dirname) { - struct dirwatcher dwkey; - struct dwref key; - struct dwref *ent; + struct watchpoint wpkey; + struct wpref key; + struct wpref *ent; if (!nametab) return NULL; - dwkey.dirname = (char*) dirname; - key.dw = &dwkey; + wpkey.dirname = (char*) dirname; + key.wpt = &wpkey; ent = hashtab_lookup_or_install(nametab, &key, NULL); - return ent ? ent->dw : NULL; + return ent ? ent->wpt : NULL; } static void -dirwatcher_remove(const char *dirname) +watchpoint_remove(const char *dirname) { - struct dirwatcher dwkey; - struct dwref key; + struct watchpoint wpkey; + struct wpref key; if (!nametab) return; - dwkey.dirname = (char*) dirname; - key.dw = &dwkey; + wpkey.dirname = (char*) dirname; + key.wpt = &wpkey; hashtab_remove(nametab, &key); } void -dirwatcher_destroy(struct dirwatcher *dwp) +watchpoint_destroy(struct watchpoint *wpt) { - debug(1, (_("removing watcher %s"), dwp->dirname)); - sysev_rm_watch(dwp); - dirwatcher_remove(dwp->dirname); + debug(1, (_("removing watcher %s"), wpt->dirname)); + sysev_rm_watch(wpt); + watchpoint_remove(wpt->dirname); } // FIXME: Perhaps the check for count is not needed after all void -dirwatcher_suspend(struct dirwatcher *dwp) +watchpoint_suspend(struct watchpoint *wpt) { - struct dirwatcher *sent = dirwatcher_install_sentinel(dwp); - dirwatcher_init(sent);//FIXME: error checking - dirwatcher_destroy(dwp); + struct watchpoint *sent = watchpoint_install_sentinel(wpt); + watchpoint_init(sent);//FIXME: error checking + watchpoint_destroy(wpt); if (hashtab_count(nametab) == 0) { diag(LOG_CRIT, _("no watchers left; exiting now")); stop = 1; } } static int -convert_watcher(struct dirwatcher *dwp) +convert_watcher(struct watchpoint *wpt) { char *dirname; char *filename; char *new_dirname; struct handler *hp; handler_iterator_t itr; - for_each_handler(dwp, itr, hp) { + for_each_handler(wpt, itr, hp) { if (hp->fnames) { /* FIXME: Error message */ return 1; } } - filename = split_pathname(dwp, &dirname); - for_each_handler(dwp, itr, hp) + filename = split_pathname(wpt, &dirname); + for_each_handler(wpt, itr, hp) handler_add_exact_filename(hp, filename); new_dirname = estrdup(dirname); - unsplit_pathname(dwp); + unsplit_pathname(wpt); diag(LOG_NOTICE, _("watcher %s converted to %s"), - dwp->dirname, new_dirname); + wpt->dirname, new_dirname); - free(dwp->dirname); - dwp->dirname = new_dirname; + free(wpt->dirname); + wpt->dirname = new_dirname; return 0; } -struct dirwatcher * -dirwatcher_install_sentinel(struct dirwatcher *dwp) +struct watchpoint * +watchpoint_install_sentinel(struct watchpoint *wpt) { - struct dirwatcher *sent; + struct watchpoint *sent; char *dirname; char *filename; struct handler *hp; - filename = split_pathname(dwp, &dirname); - sent = dirwatcher_install(dirname, NULL); + filename = split_pathname(wpt, &dirname); + sent = watchpoint_install(dirname, NULL); hp = handler_alloc(HANDLER_SENTINEL); getevt("CREATE", &hp->ev_mask); - hp->sentinel_watcher = dwp; - dirwatcher_ref(dwp); + hp->sentinel_watchpoint = wpt; + watchpoint_ref(wpt); handler_add_exact_filename(hp, filename); handler_list_append(sent->handler_list, hp); - unsplit_pathname(dwp); - diag(LOG_NOTICE, _("installing CREATE sentinel for %s"), dwp->dirname); - dirwatcher_init(sent); + unsplit_pathname(wpt); + diag(LOG_NOTICE, _("installing CREATE sentinel for %s"), wpt->dirname); + watchpoint_init(sent); return sent; } int -dirwatcher_init(struct dirwatcher *dwp) +watchpoint_init(struct watchpoint *wpt) { struct stat st; event_mask mask = { 0, 0 }; struct handler *hp; handler_iterator_t itr; int wd; - debug(1, (_("creating watcher %s"), dwp->dirname)); + debug(1, (_("creating watcher %s"), wpt->dirname)); - if (stat(dwp->dirname, &st)) { + if (stat(wpt->dirname, &st)) { if (errno == ENOENT) { - dwp = dirwatcher_install_sentinel(dwp); + wpt = watchpoint_install_sentinel(wpt); return 0; } else { diag(LOG_ERR, _("cannot set watcher on %s: %s"), - dwp->dirname, strerror(errno)); + wpt->dirname, strerror(errno)); return 1; } } else if (!S_ISDIR(st.st_mode)) { - diag(LOG_NOTICE, _("%s is a regular file"), dwp->dirname); - convert_watcher(dwp); + diag(LOG_NOTICE, _("%s is a regular file"), wpt->dirname); + convert_watcher(wpt); } - for_each_handler(dwp, itr, hp) { + for_each_handler(wpt, itr, hp) { mask.sys_mask |= hp->ev_mask.sys_mask; mask.gen_mask |= hp->ev_mask.gen_mask; } - wd = sysev_add_watch(dwp, mask); + wd = sysev_add_watch(wpt, mask); if (wd == -1) { diag(LOG_ERR, _("cannot set watcher on %s: %s"), - dwp->dirname, strerror(errno)); + wpt->dirname, strerror(errno)); return 1; } - dwp->wd = wd; + wpt->wd = wd; return 0; } -static int watch_subdirs(struct dirwatcher *parent, int notify); +static int watch_subdirs(struct watchpoint *parent, int notify); int -subwatcher_create(struct dirwatcher *parent, const char *dirname, +subwatcher_create(struct watchpoint *parent, const char *dirname, int isdir, int notify) { - struct dirwatcher *dwp; + struct watchpoint *wpt; int inst; - dwp = dirwatcher_install(dirname, &inst); + wpt = watchpoint_install(dirname, &inst); if (!inst) return -1; - dwp->handler_list = handler_list_copy(parent->handler_list); - dwp->parent = parent; + wpt->handler_list = handler_list_copy(parent->handler_list); + wpt->parent = parent; if (parent->depth == -1) - dwp->depth = parent->depth; + wpt->depth = parent->depth; else if (parent->depth) - dwp->depth = parent->depth - 1; + wpt->depth = parent->depth - 1; else - dwp->depth = 0; + wpt->depth = 0; - if (dirwatcher_init(dwp)) { - //FIXME dirwatcher_free(dwp); + if (watchpoint_init(wpt)) { + //FIXME watchpoint_free(wpt); return -1; } - return 1 + (isdir ? watch_subdirs(dwp, notify) : 0); + return 1 + (isdir ? watch_subdirs(wpt, notify) : 0); } /* Deliver GENEV_CREATE event */ void -deliver_ev_create(struct dirwatcher *dp, const char *name) +deliver_ev_create(struct watchpoint *dp, const char *name) { event_mask m = { GENEV_CREATE, 0 }; struct handler *h; handler_iterator_t itr; for_each_handler(dp, itr, h) { @@ -349,15 +349,15 @@ deliver_ev_create(struct dirwatcher *dp, const char *name) int check_new_watcher(const char *dir, const char *name) { int rc; char *fname; struct stat st; - struct dirwatcher *parent; + struct watchpoint *parent; - parent = dirwatcher_lookup(dir); + parent = watchpoint_lookup(dir); if (!parent || !parent->depth) return 0; fname = mkfilename(dir, name); if (!fname) { diag(LOG_ERR, @@ -378,28 +378,28 @@ check_new_watcher(const char *dir, const char *name) rc = 0; free(fname); return rc; } int -dirwatcher_pattern_match(struct dirwatcher *dwp, const char *file_name) +watchpoint_pattern_match(struct watchpoint *wpt, const char *file_name) { struct handler *hp; handler_iterator_t itr; - for_each_handler(dwp, itr, hp) { + for_each_handler(wpt, itr, hp) { if (filename_pattern_match(hp->fnames, file_name) == 0) return 0; } return 1; } /* Recursively scan subdirectories of parent and add them to the watcher list, as requested by the parent's recursion depth value. */ static int -watch_subdirs(struct dirwatcher *parent, int notify) +watch_subdirs(struct watchpoint *parent, int notify) { DIR *dir; struct dirent *ent; int filemask = sysev_filemask(parent); int total = 0; @@ -431,13 +431,13 @@ watch_subdirs(struct dirwatcher *parent, int notify) parent->dirname, ent->d_name); continue; } if (stat(dirna |