From: Lars Hjemli Date: Sat, 3 May 2008 08:10:07 +0000 (+0200) Subject: Merge branch 'lh/cache' X-Git-Url: https://gitweb.ps.run/ps-cgit/commitdiff_plain/e19683bedebc74593cb4c4518e47a334a5478e1e?hp=-c Merge branch 'lh/cache' * lh/cache: Add page 'ls_cache' Redesign the caching layer --- e19683bedebc74593cb4c4518e47a334a5478e1e diff --combined cgit.c index a402758,4dc8eec..ccd61f4 --- a/cgit.c +++ b/cgit.c @@@ -19,10 -19,6 +19,10 @@@ void config_cb(const char *name, const { if (!strcmp(name, "root-title")) ctx.cfg.root_title = xstrdup(value); + else if (!strcmp(name, "root-desc")) + ctx.cfg.root_desc = xstrdup(value); + else if (!strcmp(name, "root-readme")) + ctx.cfg.root_readme = xstrdup(value); else if (!strcmp(name, "css")) ctx.cfg.css = xstrdup(value); else if (!strcmp(name, "logo")) @@@ -49,6 -45,8 +49,8 @@@ ctx.cfg.enable_log_filecount = atoi(value); else if (!strcmp(name, "enable-log-linecount")) ctx.cfg.enable_log_linecount = atoi(value); + else if (!strcmp(name, "cache-size")) + ctx.cfg.cache_size = atoi(value); else if (!strcmp(name, "cache-root")) ctx.cfg.cache_root = xstrdup(value); else if (!strcmp(name, "cache-root-ttl")) @@@ -147,6 -145,8 +149,8 @@@ static void prepare_context(struct cgit { memset(ctx, 0, sizeof(ctx)); ctx->cfg.agefile = "info/web/last-modified"; + ctx->cfg.nocache = 0; + ctx->cfg.cache_size = 0; ctx->cfg.cache_dynamic_ttl = 5; ctx->cfg.cache_max_create_time = 5; ctx->cfg.cache_repo_ttl = 5; @@@ -163,52 -163,12 +167,13 @@@ ctx->cfg.renamelimit = -1; ctx->cfg.robots = "index, nofollow"; ctx->cfg.root_title = "Git repository browser"; + ctx->cfg.root_desc = "a fast webinterface for the git dscm"; ctx->cfg.script_name = CGIT_SCRIPT_NAME; ctx->page.mimetype = "text/html"; ctx->page.charset = PAGE_ENCODING; ctx->page.filename = NULL; - } - - static int cgit_prepare_cache(struct cacheitem *item) - { - if (!ctx.repo && ctx.qry.repo) { - ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, - "Bad request"); - cgit_print_http_headers(&ctx); - cgit_print_docstart(&ctx); - cgit_print_pageheader(&ctx); - cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); - cgit_print_docend(); - return 0; - } - - if (!ctx.repo) { - item->name = xstrdup(fmt("%s/index.%s.html", - ctx.cfg.cache_root, - cache_safe_filename(ctx.qry.raw))); - item->ttl = ctx.cfg.cache_root_ttl; - return 1; - } - - if (!ctx.qry.page) { - item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, - cache_safe_filename(ctx.repo->url), - cache_safe_filename(ctx.qry.raw))); - item->ttl = ctx.cfg.cache_repo_ttl; - } else { - item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, - cache_safe_filename(ctx.repo->url), - ctx.qry.page, - cache_safe_filename(ctx.qry.raw))); - if (ctx.qry.has_symref) - item->ttl = ctx.cfg.cache_dynamic_ttl; - else if (ctx.qry.has_sha1) - item->ttl = ctx.cfg.cache_static_ttl; - else - item->ttl = ctx.cfg.cache_repo_ttl; - } - return 1; + ctx->page.modified = time(NULL); + ctx->page.expires = ctx->page.modified; } struct refmatch { @@@ -293,8 -253,9 +258,9 @@@ static int prepare_repo_cmd(struct cgit return 0; } - static void process_request(struct cgit_context *ctx) + static void process_request(void *cbdata) { + struct cgit_context *ctx = cbdata; struct cgit_cmd *cmd; cmd = cgit_get_cmd(ctx); @@@ -309,16 -270,7 +275,16 @@@ return; } - if (cmd->want_repo && prepare_repo_cmd(ctx)) + if (cmd->want_repo && !ctx->repo) { + cgit_print_http_headers(ctx); + cgit_print_docstart(ctx); + cgit_print_pageheader(ctx); + cgit_print_error(fmt("No repository selected")); + cgit_print_docend(); + return; + } + + if (ctx->repo && prepare_repo_cmd(ctx)) return; if (cmd->want_layout) { @@@ -333,82 -285,6 +299,6 @@@ cgit_print_docend(); } - static long ttl_seconds(long ttl) - { - if (ttl<0) - return 60 * 60 * 24 * 365; - else - return ttl * 60; - } - - static void cgit_fill_cache(struct cacheitem *item, int use_cache) - { - int stdout2; - - if (use_cache) { - stdout2 = chk_positive(dup(STDOUT_FILENO), - "Preserving STDOUT"); - chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); - chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); - } - - ctx.page.modified = time(NULL); - ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); - process_request(&ctx); - - if (use_cache) { - chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); - chk_positive(dup2(stdout2, STDOUT_FILENO), - "Restoring original STDOUT"); - chk_zero(close(stdout2), "Closing temporary STDOUT"); - } - } - - static void cgit_check_cache(struct cacheitem *item) - { - int i = 0; - - top: - if (++i > ctx.cfg.max_lock_attempts) { - die("cgit_refresh_cache: unable to lock %s: %s", - item->name, strerror(errno)); - } - if (!cache_exist(item)) { - if (!cache_lock(item)) { - sleep(1); - goto top; - } - if (!cache_exist(item)) { - cgit_fill_cache(item, 1); - cache_unlock(item); - } else { - cache_cancel_lock(item); - } - } else if (cache_expired(item) && cache_lock(item)) { - if (cache_expired(item)) { - cgit_fill_cache(item, 1); - cache_unlock(item); - } else { - cache_cancel_lock(item); - } - } - } - - static void cgit_print_cache(struct cacheitem *item) - { - static char buf[4096]; - ssize_t i; - - int fd = open(item->name, O_RDONLY); - if (fd<0) - die("Unable to open cached file %s", item->name); - - while((i=read(fd, buf, sizeof(buf))) > 0) - write(STDOUT_FILENO, buf, i); - - close(fd); - } - static void cgit_parse_args(int argc, const char **argv) { int i; @@@ -443,13 -319,29 +333,29 @@@ } } + static int calc_ttl() + { + if (!ctx.repo) + return ctx.cfg.cache_root_ttl; + + if (!ctx.qry.page) + return ctx.cfg.cache_repo_ttl; + + if (ctx.qry.has_symref) + return ctx.cfg.cache_dynamic_ttl; + + if (ctx.qry.has_sha1) + return ctx.cfg.cache_static_ttl; + + return ctx.cfg.cache_repo_ttl; + } + int main(int argc, const char **argv) { - struct cacheitem item; const char *cgit_config_env = getenv("CGIT_CONFIG"); + int err, ttl; prepare_context(&ctx); - item.st.st_mtime = time(NULL); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; @@@ -463,13 -355,15 +369,15 @@@ ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); cgit_parse_args(argc, argv); http_parse_querystring(ctx.qry.raw, querystring_cb); - if (!cgit_prepare_cache(&item)) - return 0; - if (ctx.cfg.nocache) { - cgit_fill_cache(&item, 0); - } else { - cgit_check_cache(&item); - cgit_print_cache(&item); - } - return 0; + + ttl = calc_ttl(); + ctx.page.expires += ttl*60; + if (ctx.cfg.nocache) + ctx.cfg.cache_size = 0; + err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, + ctx.qry.raw, ttl, process_request, &ctx); + if (err) + cache_log("[cgit] error %d - %s\n", + err, strerror(err)); + return err; } diff --combined cgit.h index daebeff,f04d227..bbb404e --- a/cgit.h +++ b/cgit.h @@@ -132,10 -132,9 +132,11 @@@ struct cgit_config char *repo_group; char *robots; char *root_title; + char *root_desc; + char *root_readme; char *script_name; char *virtual_root; + int cache_size; int cache_dynamic_ttl; int cache_max_create_time; int cache_repo_ttl; diff --combined cmd.c index 6cc91e6,07f4707..4edca6b --- a/cmd.c +++ b/cmd.c @@@ -8,6 -8,8 +8,8 @@@ #include "cgit.h" #include "cmd.h" + #include "cache.h" + #include "ui-shared.h" #include "ui-blob.h" #include "ui-commit.h" #include "ui-diff.h" @@@ -20,14 -22,6 +22,14 @@@ #include "ui-tag.h" #include "ui-tree.h" +static void about_fn(struct cgit_context *ctx) +{ + if (ctx->repo) + cgit_print_repo_readme(); + else + cgit_print_site_readme(); +} + static void blob_fn(struct cgit_context *ctx) { cgit_print_blob(ctx->qry.sha1, ctx->qry.path); @@@ -43,17 -37,25 +45,25 @@@ static void diff_fn(struct cgit_contex cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); } - static void repolist_fn(struct cgit_context *ctx) - { - cgit_print_repolist(); - } - static void log_fn(struct cgit_context *ctx) { cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); } + static void ls_cache_fn(struct cgit_context *ctx) + { + ctx->page.mimetype = "text/plain"; + ctx->page.filename = "ls-cache.txt"; + cgit_print_http_headers(ctx); + cache_ls(ctx->cfg.cache_root); + } + + static void repolist_fn(struct cgit_context *ctx) + { + cgit_print_repolist(); + } + static void patch_fn(struct cgit_context *ctx) { cgit_print_patch(ctx->qry.sha1); @@@ -92,11 -94,11 +102,12 @@@ static void tree_fn(struct cgit_contex struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) { static struct cgit_cmd cmds[] = { + def_cmd(about, 0, 1), def_cmd(blob, 1, 0), def_cmd(commit, 1, 1), def_cmd(diff, 1, 1), def_cmd(log, 1, 1), + def_cmd(ls_cache, 0, 0), def_cmd(patch, 1, 0), def_cmd(refs, 1, 1), def_cmd(repolist, 0, 0),