diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-01-01 13:25:55 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-01-01 13:33:04 +0200 |
commit | 24e6dfa7cffceea0cac0f3cc349192788f040939 (patch) | |
tree | c2bd53e9bc58873c8187e6bd622ae152b35d1d51 /src/watcher.c | |
parent | 2bdd70d698c63d32f25b4f1142e09f5eaef4812a (diff) | |
download | wydawca-24e6dfa7cffceea0cac0f3cc349192788f040939.tar.gz wydawca-24e6dfa7cffceea0cac0f3cc349192788f040939.tar.bz2 |
Update copyright years. Switch to a familiar style.
Diffstat (limited to 'src/watcher.c')
-rw-r--r-- | src/watcher.c | 379 |
1 files changed, 184 insertions, 195 deletions
diff --git a/src/watcher.c b/src/watcher.c index f6245db..0c587f1 100644 --- a/src/watcher.c +++ b/src/watcher.c @@ -1,8 +1,8 @@ /* wydawca - automatic release submission daemon - Copyright (C) 2007, 2009-2012 Sergey Poznyakoff + Copyright (C) 2007, 2009-2013 Sergey Poznyakoff Wydawca 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. @@ -16,243 +16,232 @@ #include "wydawca.h" #include <sys/inotify.h> #include <sys/ioctl.h> /* A directory watcher is described by the following structure */ -struct dirwatcher -{ - struct dirwatcher *next, *prev; - struct dirwatcher *parent; /* Points to the parent watcher. - NULL for top-level watchers */ - struct spool *spool; - int wd; /* Watch descriptor */ +struct dirwatcher { + struct dirwatcher *next, *prev; + struct dirwatcher *parent; /* Points to the parent watcher. + NULL for top-level watchers */ + struct spool *spool; + int wd; /* Watch descriptor */ }; static struct dirwatcher *dirwatcher_list; struct dirwatcher * dirwatcher_unlink(struct dirwatcher **root, struct dirwatcher *p) { - if (p->prev) - p->prev->next = p->next; - else - *root = p->next; - if (p->next) - p->next->prev = p->prev; - p->next = p->prev = NULL; - return p; + if (p->prev) + p->prev->next = p->next; + else + *root = p->next; + if (p->next) + p->next->prev = p->prev; + p->next = p->prev = NULL; + return p; } struct dirwatcher * dirwatcher_pop(struct dirwatcher **pp) { - if (*pp) - return dirwatcher_unlink(pp, *pp); - return NULL; + if (*pp) + return dirwatcher_unlink(pp, *pp); + return NULL; } static void dirwatcher_push(struct dirwatcher **pp, struct dirwatcher *p) { - p->prev = NULL; - p->next = *pp; - if (*pp) - (*pp)->prev = p; - *pp = p; + p->prev = NULL; + p->next = *pp; + if (*pp) + (*pp)->prev = p; + *pp = p; } /* Find a watcher with the given descriptor */ static struct dirwatcher * -dirwatcher_find_wd (int wd) +dirwatcher_find_wd(int wd) { - struct dirwatcher *dwp; - - for (dwp = dirwatcher_list; dwp; dwp = dwp->next) - if (dwp->wd == wd) - break; - return dwp; + struct dirwatcher *dwp; + + for (dwp = dirwatcher_list; dwp; dwp = dwp->next) + if (dwp->wd == wd) + break; + return dwp; } static int -create_watcher (struct spool *sp, void *data) +create_watcher(struct spool *sp, void *data) { - int ifd = *(int*)data; - struct dirwatcher *dwp; - int wd; - const char *path = sp->source_dir; - - if (!sp) - return 0; - - if (!sp->inotify_enable) - { - if (debug_level > 1) - logmsg (LOG_DEBUG, "disabling inotify support for spool %s", sp->tag); - return 0; - } - - if (debug_level > 1) - logmsg (LOG_DEBUG, "spool %s: creating watcher %s", sp->tag, path); - dwp = malloc (sizeof(*dwp)); - if (!dwp) - { - logmsg (LOG_ERR, "not enough memory"); - return 1; - } - dwp->spool = sp; - dwp->parent = NULL; - - wd = inotify_add_watch (ifd, path, IN_DELETE|IN_CREATE|IN_CLOSE_WRITE| - IN_MOVED_FROM|IN_MOVED_TO); - if (wd == -1) - { - logmsg (LOG_ERR, "cannot set watch on %s: %s", path, strerror (errno)); - free (dwp); - return 1; - } - - dwp->wd = wd; - dirwatcher_push (&dirwatcher_list, dwp); - return 0; + int ifd = *(int *)data; + struct dirwatcher *dwp; + int wd; + const char *path = sp->source_dir; + + if (!sp) + return 0; + + if (!sp->inotify_enable) { + if (debug_level > 1) + logmsg(LOG_DEBUG, + "disabling inotify support for spool %s", + sp->tag); + return 0; + } + + if (debug_level > 1) + logmsg(LOG_DEBUG, "spool %s: creating watcher %s", sp->tag, + path); + dwp = malloc(sizeof(*dwp)); + if (!dwp) { + logmsg(LOG_ERR, "not enough memory"); + return 1; + } + dwp->spool = sp; + dwp->parent = NULL; + + wd = inotify_add_watch(ifd, path, + IN_DELETE | IN_CREATE | IN_CLOSE_WRITE | + IN_MOVED_FROM | IN_MOVED_TO); + if (wd == -1) { + logmsg(LOG_ERR, "cannot set watch on %s: %s", path, + strerror(errno)); + free(dwp); + return 1; + } + + dwp->wd = wd; + dirwatcher_push(&dirwatcher_list, dwp); + return 0; } int -watcher_init () +watcher_init() { - int ifd, rc; - - if (!inotify_enable) - { - if (debug_level > 1) - logmsg (LOG_DEBUG, "disabling inotify support"); - return -1; - } - - if (debug_level > 1) - logmsg (LOG_DEBUG, "setting up inotify"); - ifd = inotify_init (); - if (ifd == -1) - { - logmsg (LOG_ERR, "inotify_init: %s", strerror (errno)); - return -1; - } - - rc = for_each_spool (create_watcher, &ifd); - if (rc) - exit (EX_OSERR); - if (!dirwatcher_list) - { - if (debug_level > 1) - logmsg (LOG_DEBUG, "inotify: nothing to watch"); - close (ifd); - ifd = -1; - } - else if (debug_level > 1) - logmsg (LOG_DEBUG, "inotify initialized successfully"); - - return ifd; + int ifd, rc; + + if (!inotify_enable) { + if (debug_level > 1) + logmsg(LOG_DEBUG, "disabling inotify support"); + return -1; + } + + if (debug_level > 1) + logmsg(LOG_DEBUG, "setting up inotify"); + ifd = inotify_init(); + if (ifd == -1) { + logmsg(LOG_ERR, "inotify_init: %s", strerror(errno)); + return -1; + } + + rc = for_each_spool(create_watcher, &ifd); + if (rc) + exit(EX_OSERR); + if (!dirwatcher_list) { + if (debug_level > 1) + logmsg(LOG_DEBUG, "inotify: nothing to watch"); + close(ifd); + ifd = -1; + } else if (debug_level > 1) + logmsg(LOG_DEBUG, "inotify initialized successfully"); + + return ifd; } static void -process_event (struct inotify_event *ep) +process_event(struct inotify_event *ep) { - static struct dirwatcher *dwp; - dwp = dirwatcher_find_wd (ep->wd); - - if (ep->mask & IN_IGNORED) - /* nothing */; - else if (ep->mask & IN_Q_OVERFLOW) - logmsg (LOG_NOTICE, "event queue overflow"); - else if (ep->mask & IN_UNMOUNT) - /* FIXME: not sure if there's - anything to do. Perhaps we should - deregister the watched dirs that - were located under the mountpoint - */; - else if (!dwp) - { - if (ep->name) - logmsg (LOG_NOTICE, "unrecognized event %x for %s", - ep->mask, ep->name); - else - logmsg (LOG_NOTICE, "unrecognized event %x", ep->mask); - } - else if (ep->mask & IN_CREATE) - { - if (debug_level > 0) - logmsg (LOG_DEBUG, "%s/%s created", dwp->spool->source_dir, ep->name); - } - else if (ep->mask & (IN_DELETE|IN_MOVED_FROM)) - { - if (debug_level > 0) - logmsg (LOG_DEBUG, "%s/%s %s", dwp->spool->source_dir, ep->name, - ep->mask & IN_DELETE ? "deleted" : "moved out"); - triplet_remove_file (dwp->spool, ep->name); - } - else if (ep->mask & (IN_CLOSE_WRITE|IN_MOVED_TO)) - { - if (debug_level > 0) - logmsg (LOG_DEBUG, "%s/%s written", dwp->spool->source_dir, ep->name); - if (spool_add_new_file (dwp->spool, ep->name, 0, NULL) == 0 && - count_processable_triplets ()) - schedule_job (&inotify_spool, getuid ()); - } - else - logmsg (LOG_NOTICE, "%s/%s: unexpected event %x", - dwp->spool->source_dir, ep->name, ep->mask); - + static struct dirwatcher *dwp; + dwp = dirwatcher_find_wd(ep->wd); + + if (ep->mask & IN_IGNORED) + /* nothing */ ; + else if (ep->mask & IN_Q_OVERFLOW) + logmsg(LOG_NOTICE, "event queue overflow"); + else if (ep->mask & IN_UNMOUNT) + /* FIXME: not sure if there's + anything to do. Perhaps we should + deregister the watched dirs that + were located under the mountpoint + */ ; + else if (!dwp) { + if (ep->name) + logmsg(LOG_NOTICE, "unrecognized event %x for %s", + ep->mask, ep->name); + else + logmsg(LOG_NOTICE, "unrecognized event %x", ep->mask); + } else if (ep->mask & IN_CREATE) { + if (debug_level > 0) + logmsg(LOG_DEBUG, "%s/%s created", + dwp->spool->source_dir, ep->name); + } else if (ep->mask & (IN_DELETE | IN_MOVED_FROM)) { + if (debug_level > 0) + logmsg(LOG_DEBUG, "%s/%s %s", dwp->spool->source_dir, + ep->name, + ep->mask & IN_DELETE ? "deleted" : "moved out"); + triplet_remove_file(dwp->spool, ep->name); + } else if (ep->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) { + if (debug_level > 0) + logmsg(LOG_DEBUG, "%s/%s written", + dwp->spool->source_dir, ep->name); + if (spool_add_new_file(dwp->spool, ep->name, 0, NULL) == 0 + && count_processable_triplets()) + schedule_job(&inotify_spool, getuid()); + } else + logmsg(LOG_NOTICE, "%s/%s: unexpected event %x", + dwp->spool->source_dir, ep->name, ep->mask); + } static char buffer[4096]; static int offset; int -watcher_run (int ifd) +watcher_run(int ifd) { - int n; - int rdbytes; - - if (ioctl (ifd, FIONREAD, &n)) - { - logmsg (LOG_ERR, "ioctl: %s", strerror (errno)); - return -1; - } - if (offset + n > sizeof buffer) - n = sizeof buffer - offset; - if (n) - { - rdbytes = read (ifd, buffer + offset, n); - if (rdbytes == -1) - { - if (errno == EINTR) - { - //FIXME logmsg (LOG_NOTICE, "got signal %d", signo); - return 0; - } - - logmsg (LOG_NOTICE, "read failed: %s", strerror (errno)); - return -1; + int n; + int rdbytes; + + if (ioctl(ifd, FIONREAD, &n)) { + logmsg(LOG_ERR, "ioctl: %s", strerror(errno)); + return -1; + } + if (offset + n > sizeof buffer) + n = sizeof buffer - offset; + if (n) { + rdbytes = read(ifd, buffer + offset, n); + if (rdbytes == -1) { + if (errno == EINTR) { + /*FIXME + logmsg (LOG_NOTICE, "got signal %d", signo); + */ + return 0; + } + + logmsg(LOG_NOTICE, "read failed: %s", strerror(errno)); + return -1; + } + } + offset += n; + + for (n = 0; offset - n >= sizeof(struct inotify_event);) { + struct inotify_event *ep; + size_t size; + + ep = (struct inotify_event *)(buffer + n); + size = sizeof(*ep) + ep->len; + if (offset - n < size) + break; + + process_event(ep); + + n += size; } - } - offset += n; - - for (n = 0; offset - n >= sizeof (struct inotify_event); ) - { - struct inotify_event *ep; - size_t size; - - ep = (struct inotify_event *) (buffer + n); - size = sizeof(*ep) + ep->len; - if (offset - n < size) - break; - - process_event (ep); - - n += size; - } - if (n > 0 && offset - n > 0) - memmove (buffer, buffer + n, offset - n); - offset -= n; - - return 0; + if (n > 0 && offset - n > 0) + memmove(buffer, buffer + n, offset - n); + offset -= n; + + return 0; } |