Added global config files to dirplex and patplex.
[ashd.git] / src / dirplex.c
index 4634283..6595201 100644 (file)
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <dirent.h>
 #include <fnmatch.h>
+#include <time.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -45,7 +46,7 @@
 struct config {
     struct config *next, *prev;
     char *path;
-    time_t mtime;
+    time_t mtime, lastck;
     struct child *children;
     struct pattern *patterns;
 };
@@ -61,7 +62,9 @@ struct pattern {
     struct rule **rules;
 };
 
-struct config *cflist;
+static struct config *cflist;
+static struct config *gconfig, *lconfig;
+static time_t now;
 
 static void freepattern(struct pattern *pat)
 {
@@ -88,7 +91,8 @@ static void freeconfig(struct config *cf)
        cf->next->prev = cf->prev;
     if(cf == cflist)
        cflist = cf->next;
-    free(cf->path);
+    if(cf->path != NULL)
+       free(cf->path);
     for(ch = cf->children; ch != NULL; ch = nch) {
        nch = ch->next;
        freechild(ch);
@@ -196,27 +200,28 @@ static struct pattern *parsepattern(struct cfstate *s)
     return(pat);
 }
 
-static struct config *readconfig(char *path)
+static struct config *emptyconfig(void)
+{
+    struct config *cf;
+    
+    omalloc(cf);
+    return(cf);
+}
+
+static struct config *readconfig(char *file)
 {
     struct cfstate *s;
     FILE *in;
     struct config *cf;
     struct child *child;
     struct pattern *pat;
-    struct stat sb;
-    char *p;
     
-    p = sprintf3("%s/.htrc", path);
-    if(stat(p, &sb))
-       return(NULL);
-    if((in = fopen(p, "r")) == NULL) {
-       flog(LOG_WARNING, "%s: %s", p, strerror(errno));
+    if((in = fopen(file, "r")) == NULL) {
+       flog(LOG_WARNING, "%s: %s", file, strerror(errno));
        return(NULL);
     }
-    s = mkcfparser(in, p);
-    omalloc(cf);
-    cf->mtime = sb.st_mtime;
-    cf->path = sstrdup(path);
+    s = mkcfparser(in, file);
+    cf = emptyconfig();
     
     while(1) {
        getcfline(s);
@@ -242,22 +247,35 @@ static struct config *getconfig(char *path)
 {
     struct config *cf;
     struct stat sb;
+    char *fn;
+    time_t mtime;
     
+    fn = sprintf3("%s/.htrc", path);
     for(cf = cflist; cf != NULL; cf = cf->next) {
        if(!strcmp(cf->path, path)) {
-           if(stat(sprintf3("%s/.htrc", path), &sb))
-               return(NULL);
-           if(sb.st_mtime != cf->mtime) {
-               freeconfig(cf);
-               break;
+           if(now - cf->lastck > 5) {
+               if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
+                   freeconfig(cf);
+                   break;
+               }
            }
+           cf->lastck = now;
            return(cf);
        }
     }
-    if((cf = readconfig(path)) != NULL) {
-       cf->next = cflist;
-       cflist = cf;
+    if(access(fn, R_OK) || stat(fn, &sb)) {
+       cf = emptyconfig();
+       mtime = 0;
+    } else {
+       if((cf = readconfig(fn)) == NULL)
+           return(NULL);
+       mtime = sb.st_mtime;
     }
+    cf->path = sstrdup(path);
+    cf->mtime = mtime;
+    cf->lastck = now;
+    cf->next = cflist;
+    cflist = cf;
     return(cf);
 }
 
@@ -285,6 +303,10 @@ static struct config **getconfigs(char *file)
     free(tmp);
     if((cf = getconfig(".")) != NULL)
        bufadd(buf, cf);
+    if(lconfig != NULL)
+       bufadd(buf, lconfig);
+    if(gconfig != NULL)
+       bufadd(buf, gconfig);
     bufadd(buf, NULL);
     return(ret = buf.b);
 }
@@ -376,6 +398,7 @@ static void serve(struct hthead *req, int fd)
     DIR *dir;
     struct dirent *dent;
     
+    now = time(NULL);
     nm = req->rest;
     path = sstrdup(".");
     p = nm;
@@ -489,17 +512,53 @@ out:
     free(path);
 }
 
+static void usage(FILE *out)
+{
+    fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
+}
+
 int main(int argc, char **argv)
 {
+    int c;
+    int nodef;
+    char *gcf, *lcf;
     struct hthead *req;
     int fd;
     
-    if(argc < 2) {
-       flog(LOG_ERR, "usage: dirplex DIR");
+    nodef = 0;
+    lcf = NULL;
+    while((c = getopt(argc, argv, "hNc:")) >= 0) {
+       switch(c) {
+       case 'h':
+           usage(stdout);
+           exit(0);
+       case 'N':
+           nodef = 1;
+           break;
+       case 'c':
+           lcf = optarg;
+           break;
+       default:
+           usage(stderr);
+           exit(1);
+       }
+    }
+    if(argc - optind < 1) {
+       usage(stderr);
        exit(1);
     }
-    if(chdir(argv[1])) {
-       flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
+    if(!nodef) {
+       if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
+           gconfig = readconfig(gcf);
+           free(gcf);
+       }
+    }
+    if(lcf != NULL) {
+       if((lconfig = readconfig(lcf)) == NULL)
+           exit(1);
+    }
+    if(chdir(argv[optind])) {
+       flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
        exit(1);
     }
     signal(SIGCHLD, SIG_IGN);