]> gitweb.ps.run Git - ps-cgit/blobdiff - cgit.c
Add support for --scan-path command line option
[ps-cgit] / cgit.c
diff --git a/cgit.c b/cgit.c
index 66dd1402c23185154f67f55e41506e16a3152682..a792fe46eaf73d842a16dbca127ddbe6b43a96f0 100644 (file)
--- a/cgit.c
+++ b/cgit.c
@@ -40,6 +40,8 @@ struct cgit_filter *new_filter(const char *cmd, int extra_args)
        return f;
 }
 
+static void process_cached_repolist(const char *path);
+
 void config_cb(const char *name, const char *value)
 {
        if (!strcmp(name, "root-title"))
@@ -96,6 +98,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.cache_root_ttl = atoi(value);
        else if (!strcmp(name, "cache-repo-ttl"))
                ctx.cfg.cache_repo_ttl = atoi(value);
+       else if (!strcmp(name, "cache-scanrc-ttl"))
+               ctx.cfg.cache_scanrc_ttl = atoi(value);
        else if (!strcmp(name, "cache-static-ttl"))
                ctx.cfg.cache_static_ttl = atoi(value);
        else if (!strcmp(name, "cache-dynamic-ttl"))
@@ -114,6 +118,11 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.max_repo_count = atoi(value);
        else if (!strcmp(name, "max-commit-count"))
                ctx.cfg.max_commit_count = atoi(value);
+       else if (!strcmp(name, "scan-path"))
+               if (!ctx.cfg.nocache && ctx.cfg.cache_size)
+                       process_cached_repolist(value);
+               else
+                       scan_tree(value);
        else if (!strcmp(name, "source-filter"))
                ctx.cfg.source_filter = new_filter(value, 1);
        else if (!strcmp(name, "summary-log"))
@@ -177,6 +186,9 @@ void config_cb(const char *name, const char *value)
 
 static void querystring_cb(const char *name, const char *value)
 {
+       if (!value)
+               value = "";
+
        if (!strcmp(name,"r")) {
                ctx.qry.repo = xstrdup(value);
                ctx.repo = cgit_get_repoinfo(value);
@@ -231,6 +243,7 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.cache_repo_ttl = 5;
        ctx->cfg.cache_root = CGIT_CACHE_ROOT;
        ctx->cfg.cache_root_ttl = 5;
+       ctx->cfg.cache_scanrc_ttl = 15;
        ctx->cfg.cache_static_ttl = -1;
        ctx->cfg.css = "/cgit.css";
        ctx->cfg.logo = "/cgit.png";
@@ -411,28 +424,93 @@ int cmp_repos(const void *a, const void *b)
        return strcmp(ra->url, rb->url);
 }
 
-void print_repo(struct cgit_repo *repo)
+void print_repo(FILE *f, struct cgit_repo *repo)
 {
-       printf("repo.url=%s\n", repo->url);
-       printf("repo.name=%s\n", repo->name);
-       printf("repo.path=%s\n", repo->path);
+       fprintf(f, "repo.url=%s\n", repo->url);
+       fprintf(f, "repo.name=%s\n", repo->name);
+       fprintf(f, "repo.path=%s\n", repo->path);
        if (repo->owner)
-               printf("repo.owner=%s\n", repo->owner);
+               fprintf(f, "repo.owner=%s\n", repo->owner);
        if (repo->desc)
-               printf("repo.desc=%s\n", repo->desc);
+               fprintf(f, "repo.desc=%s\n", repo->desc);
        if (repo->readme)
-               printf("repo.readme=%s\n", repo->readme);
-       printf("\n");
+               fprintf(f, "repo.readme=%s\n", repo->readme);
+       fprintf(f, "\n");
 }
 
-void print_repolist(struct cgit_repolist *list)
+void print_repolist(FILE *f, struct cgit_repolist *list, int start)
 {
        int i;
 
-       for(i = 0; i < list->count; i++)
-               print_repo(&list->repos[i]);
+       for(i = start; i < list->count; i++)
+               print_repo(f, &list->repos[i]);
+}
+
+/* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc'
+ * and return 0 on success.
+ */
+static int generate_cached_repolist(const char *path, const char *cached_rc)
+{
+       char *locked_rc;
+       int idx;
+       FILE *f;
+
+       locked_rc = xstrdup(fmt("%s.lock", cached_rc));
+       f = fopen(locked_rc, "wx");
+       if (!f) {
+               /* Inform about the error unless the lockfile already existed,
+                * since that only means we've got concurrent requests.
+                */
+               if (errno != EEXIST)
+                       fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
+                               locked_rc, strerror(errno), errno);
+               return errno;
+       }
+       idx = cgit_repolist.count;
+       scan_tree(path);
+       print_repolist(f, &cgit_repolist, idx);
+       if (rename(locked_rc, cached_rc))
+               fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
+                       locked_rc, cached_rc, strerror(errno), errno);
+       fclose(f);
+       return 0;
 }
 
+static void process_cached_repolist(const char *path)
+{
+       struct stat st;
+       char *cached_rc;
+       time_t age;
+
+       cached_rc = xstrdup(fmt("%s/rc-%8x", ctx.cfg.cache_root,
+               hash_str(path)));
+
+       if (stat(cached_rc, &st)) {
+               /* Nothing is cached, we need to scan without forking. And
+                * if we fail to generate a cached repolist, we need to
+                * invoke scan_tree manually.
+                */
+               if (generate_cached_repolist(path, cached_rc))
+                       scan_tree(path);
+               return;
+       }
+
+       parse_configfile(cached_rc, config_cb);
+
+       /* If the cached configfile hasn't expired, lets exit now */
+       age = time(NULL) - st.st_mtime;
+       if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
+               return;
+
+       /* The cached repolist has been parsed, but it was old. So lets
+        * rescan the specified path and generate a new cached repolist
+        * in a child-process to avoid latency for the current request.
+        */
+       if (fork())
+               return;
+
+       exit(generate_cached_repolist(path, cached_rc));
+}
 
 static void cgit_parse_args(int argc, const char **argv)
 {
@@ -469,7 +547,8 @@ static void cgit_parse_args(int argc, const char **argv)
                if (!strncmp(argv[i], "--ofs=", 6)) {
                        ctx.qry.ofs = atoi(argv[i]+6);
                }
-               if (!strncmp(argv[i], "--scan-tree=", 12)) {
+               if (!strncmp(argv[i], "--scan-tree=", 12) ||
+                   !strncmp(argv[i], "--scan-path=", 12)) {
                        scan++;
                        scan_tree(argv[i] + 12);
                }
@@ -477,7 +556,7 @@ static void cgit_parse_args(int argc, const char **argv)
        if (scan) {
                qsort(cgit_repolist.repos, cgit_repolist.count,
                        sizeof(struct cgit_repo), cmp_repos);
-               print_repolist(&cgit_repolist);
+               print_repolist(stdout, &cgit_repolist, 0);
                exit(0);
        }
 }
@@ -536,7 +615,7 @@ int main(int argc, const char **argv)
                        ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
                        free(qry);
                } else
-                       ctx.qry.raw = ctx.qry.url;
+                       ctx.qry.raw = xstrdup(ctx.qry.url);
                cgit_parse_url(ctx.qry.url);
        }