aboutsummaryrefslogtreecommitdiffstats
path: root/cgit.c
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2008-05-03 17:10:07 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2008-05-03 17:10:07 (JST)
commite19683bedebc74593cb4c4518e47a334a5478e1e (patch)
tree8b4f231327d27b9451a6d9ab4b2af47558c61352 /cgit.c
parent112b2080626c62fff27cf8aaa9ac2fb07eb50b74 (diff)
parent9000bbf865cb3578ba5ed3810dc44253cb46ec7f (diff)
downloadcgit-e19683bedebc74593cb4c4518e47a334a5478e1e.zip
cgit-e19683bedebc74593cb4c4518e47a334a5478e1e.tar.gz
Merge branch 'lh/cache'
* lh/cache: Add page 'ls_cache' Redesign the caching layer
Diffstat (limited to 'cgit.c')
-rw-r--r--cgit.c166
1 files changed, 37 insertions, 129 deletions
diff --git a/cgit.c b/cgit.c
index a402758..ccd61f4 100644
--- a/cgit.c
+++ b/cgit.c
@@ -49,6 +49,8 @@ void config_cb(const char *name, const char *value)
49 ctx.cfg.enable_log_filecount = atoi(value); 49 ctx.cfg.enable_log_filecount = atoi(value);
50 else if (!strcmp(name, "enable-log-linecount")) 50 else if (!strcmp(name, "enable-log-linecount"))
51 ctx.cfg.enable_log_linecount = atoi(value); 51 ctx.cfg.enable_log_linecount = atoi(value);
52 else if (!strcmp(name, "cache-size"))
53 ctx.cfg.cache_size = atoi(value);
52 else if (!strcmp(name, "cache-root")) 54 else if (!strcmp(name, "cache-root"))
53 ctx.cfg.cache_root = xstrdup(value); 55 ctx.cfg.cache_root = xstrdup(value);
54 else if (!strcmp(name, "cache-root-ttl")) 56 else if (!strcmp(name, "cache-root-ttl"))
@@ -147,6 +149,8 @@ static void prepare_context(struct cgit_context *ctx)
147{ 149{
148 memset(ctx, 0, sizeof(ctx)); 150 memset(ctx, 0, sizeof(ctx));
149 ctx->cfg.agefile = "info/web/last-modified"; 151 ctx->cfg.agefile = "info/web/last-modified";
152 ctx->cfg.nocache = 0;
153 ctx->cfg.cache_size = 0;
150 ctx->cfg.cache_dynamic_ttl = 5; 154 ctx->cfg.cache_dynamic_ttl = 5;
151 ctx->cfg.cache_max_create_time = 5; 155 ctx->cfg.cache_max_create_time = 5;
152 ctx->cfg.cache_repo_ttl = 5; 156 ctx->cfg.cache_repo_ttl = 5;
@@ -168,47 +172,8 @@ static void prepare_context(struct cgit_context *ctx)
168 ctx->page.mimetype = "text/html"; 172 ctx->page.mimetype = "text/html";
169 ctx->page.charset = PAGE_ENCODING; 173 ctx->page.charset = PAGE_ENCODING;
170 ctx->page.filename = NULL; 174 ctx->page.filename = NULL;
171} 175 ctx->page.modified = time(NULL);
172 176 ctx->page.expires = ctx->page.modified;
173static int cgit_prepare_cache(struct cacheitem *item)
174{
175 if (!ctx.repo && ctx.qry.repo) {
176 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
177 "Bad request");
178 cgit_print_http_headers(&ctx);
179 cgit_print_docstart(&ctx);
180 cgit_print_pageheader(&ctx);
181 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
182 cgit_print_docend();
183 return 0;
184 }
185
186 if (!ctx.repo) {
187 item->name = xstrdup(fmt("%s/index.%s.html",
188 ctx.cfg.cache_root,
189 cache_safe_filename(ctx.qry.raw)));
190 item->ttl = ctx.cfg.cache_root_ttl;
191 return 1;
192 }
193
194 if (!ctx.qry.page) {
195 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
196 cache_safe_filename(ctx.repo->url),
197 cache_safe_filename(ctx.qry.raw)));
198 item->ttl = ctx.cfg.cache_repo_ttl;
199 } else {
200 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
201 cache_safe_filename(ctx.repo->url),
202 ctx.qry.page,
203 cache_safe_filename(ctx.qry.raw)));
204 if (ctx.qry.has_symref)
205 item->ttl = ctx.cfg.cache_dynamic_ttl;
206 else if (ctx.qry.has_sha1)
207 item->ttl = ctx.cfg.cache_static_ttl;
208 else
209 item->ttl = ctx.cfg.cache_repo_ttl;
210 }
211 return 1;
212} 177}
213 178
214struct refmatch { 179struct refmatch {
@@ -293,8 +258,9 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
293 return 0; 258 return 0;
294} 259}
295 260
296static void process_request(struct cgit_context *ctx) 261static void process_request(void *cbdata)
297{ 262{
263 struct cgit_context *ctx = cbdata;
298 struct cgit_cmd *cmd; 264 struct cgit_cmd *cmd;
299 265
300 cmd = cgit_get_cmd(ctx); 266 cmd = cgit_get_cmd(ctx);
@@ -333,82 +299,6 @@ static void process_request(struct cgit_context *ctx)
333 cgit_print_docend(); 299 cgit_print_docend();
334} 300}
335 301
336static long ttl_seconds(long ttl)
337{
338 if (ttl<0)
339 return 60 * 60 * 24 * 365;
340 else
341 return ttl * 60;
342}
343
344static void cgit_fill_cache(struct cacheitem *item, int use_cache)
345{
346 int stdout2;
347
348 if (use_cache) {
349 stdout2 = chk_positive(dup(STDOUT_FILENO),
350 "Preserving STDOUT");
351 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
352 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
353 }
354
355 ctx.page.modified = time(NULL);
356 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
357 process_request(&ctx);
358
359 if (use_cache) {
360 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
361 chk_positive(dup2(stdout2, STDOUT_FILENO),
362 "Restoring original STDOUT");
363 chk_zero(close(stdout2), "Closing temporary STDOUT");
364 }
365}
366
367static void cgit_check_cache(struct cacheitem *item)
368{
369 int i = 0;
370
371 top:
372 if (++i > ctx.cfg.max_lock_attempts) {
373 die("cgit_refresh_cache: unable to lock %s: %s",
374 item->name, strerror(errno));
375 }
376 if (!cache_exist(item)) {
377 if (!cache_lock(item)) {
378 sleep(1);
379 goto top;
380 }
381 if (!cache_exist(item)) {
382 cgit_fill_cache(item, 1);
383 cache_unlock(item);
384 } else {
385 cache_cancel_lock(item);
386 }
387 } else if (cache_expired(item) && cache_lock(item)) {
388 if (cache_expired(item)) {
389 cgit_fill_cache(item, 1);
390 cache_unlock(item);
391 } else {
392 cache_cancel_lock(item);
393 }
394 }
395}
396
397static void cgit_print_cache(struct cacheitem *item)
398{
399 static char buf[4096];
400 ssize_t i;
401
402 int fd = open(item->name, O_RDONLY);
403 if (fd<0)
404 die("Unable to open cached file %s", item->name);
405
406 while((i=read(fd, buf, sizeof(buf))) > 0)
407 write(STDOUT_FILENO, buf, i);
408
409 close(fd);
410}
411
412static void cgit_parse_args(int argc, const char **argv) 302static void cgit_parse_args(int argc, const char **argv)
413{ 303{
414 int i; 304 int i;
@@ -443,13 +333,29 @@ static void cgit_parse_args(int argc, const char **argv)
443 } 333 }
444} 334}
445 335
336static int calc_ttl()
337{
338 if (!ctx.repo)
339 return ctx.cfg.cache_root_ttl;
340
341 if (!ctx.qry.page)
342 return ctx.cfg.cache_repo_ttl;
343
344 if (ctx.qry.has_symref)
345 return ctx.cfg.cache_dynamic_ttl;
346
347 if (ctx.qry.has_sha1)
348 return ctx.cfg.cache_static_ttl;
349
350 return ctx.cfg.cache_repo_ttl;
351}
352
446int main(int argc, const char **argv) 353int main(int argc, const char **argv)
447{ 354{
448 struct cacheitem item;
449 const char *cgit_config_env = getenv("CGIT_CONFIG"); 355 const char *cgit_config_env = getenv("CGIT_CONFIG");
356 int err, ttl;
450 357
451 prepare_context(&ctx); 358 prepare_context(&ctx);
452 item.st.st_mtime = time(NULL);
453 cgit_repolist.length = 0; 359 cgit_repolist.length = 0;
454 cgit_repolist.count = 0; 360 cgit_repolist.count = 0;
455 cgit_repolist.repos = NULL; 361 cgit_repolist.repos = NULL;
@@ -463,13 +369,15 @@ int main(int argc, const char **argv)
463 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 369 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
464 cgit_parse_args(argc, argv); 370 cgit_parse_args(argc, argv);
465 http_parse_querystring(ctx.qry.raw, querystring_cb); 371 http_parse_querystring(ctx.qry.raw, querystring_cb);
466 if (!cgit_prepare_cache(&item)) 372
467 return 0; 373 ttl = calc_ttl();
468 if (ctx.cfg.nocache) { 374 ctx.page.expires += ttl*60;
469 cgit_fill_cache(&item, 0); 375 if (ctx.cfg.nocache)
470 } else { 376 ctx.cfg.cache_size = 0;
471 cgit_check_cache(&item); 377 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
472 cgit_print_cache(&item); 378 ctx.qry.raw, ttl, process_request, &ctx);
473 } 379 if (err)
474 return 0; 380 cache_log("[cgit] error %d - %s\n",
381 err, strerror(err));
382 return err;
475} 383}