summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c1
1 files changed, 1 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index 763242a..6ec763f 100644
--- a/cgit.c
+++ b/cgit.c
@@ -347,112 +347,113 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
347 chk_zero(close(stdout2), "Closing temporary STDOUT"); 347 chk_zero(close(stdout2), "Closing temporary STDOUT");
348 } 348 }
349} 349}
350 350
351static void cgit_check_cache(struct cacheitem *item) 351static void cgit_check_cache(struct cacheitem *item)
352{ 352{
353 int i = 0; 353 int i = 0;
354 354
355 top: 355 top:
356 if (++i > ctx.cfg.max_lock_attempts) { 356 if (++i > ctx.cfg.max_lock_attempts) {
357 die("cgit_refresh_cache: unable to lock %s: %s", 357 die("cgit_refresh_cache: unable to lock %s: %s",
358 item->name, strerror(errno)); 358 item->name, strerror(errno));
359 } 359 }
360 if (!cache_exist(item)) { 360 if (!cache_exist(item)) {
361 if (!cache_lock(item)) { 361 if (!cache_lock(item)) {
362 sleep(1); 362 sleep(1);
363 goto top; 363 goto top;
364 } 364 }
365 if (!cache_exist(item)) { 365 if (!cache_exist(item)) {
366 cgit_fill_cache(item, 1); 366 cgit_fill_cache(item, 1);
367 cache_unlock(item); 367 cache_unlock(item);
368 } else { 368 } else {
369 cache_cancel_lock(item); 369 cache_cancel_lock(item);
370 } 370 }
371 } else if (cache_expired(item) && cache_lock(item)) { 371 } else if (cache_expired(item) && cache_lock(item)) {
372 if (cache_expired(item)) { 372 if (cache_expired(item)) {
373 cgit_fill_cache(item, 1); 373 cgit_fill_cache(item, 1);
374 cache_unlock(item); 374 cache_unlock(item);
375 } else { 375 } else {
376 cache_cancel_lock(item); 376 cache_cancel_lock(item);
377 } 377 }
378 } 378 }
379} 379}
380 380
381static void cgit_print_cache(struct cacheitem *item) 381static void cgit_print_cache(struct cacheitem *item)
382{ 382{
383 static char buf[4096]; 383 static char buf[4096];
384 ssize_t i; 384 ssize_t i;
385 385
386 int fd = open(item->name, O_RDONLY); 386 int fd = open(item->name, O_RDONLY);
387 if (fd<0) 387 if (fd<0)
388 die("Unable to open cached file %s", item->name); 388 die("Unable to open cached file %s", item->name);
389 389
390 while((i=read(fd, buf, sizeof(buf))) > 0) 390 while((i=read(fd, buf, sizeof(buf))) > 0)
391 write(STDOUT_FILENO, buf, i); 391 write(STDOUT_FILENO, buf, i);
392 392
393 close(fd); 393 close(fd);
394} 394}
395 395
396static void cgit_parse_args(int argc, const char **argv) 396static void cgit_parse_args(int argc, const char **argv)
397{ 397{
398 int i; 398 int i;
399 399
400 for (i = 1; i < argc; i++) { 400 for (i = 1; i < argc; i++) {
401 if (!strncmp(argv[i], "--cache=", 8)) { 401 if (!strncmp(argv[i], "--cache=", 8)) {
402 ctx.cfg.cache_root = xstrdup(argv[i]+8); 402 ctx.cfg.cache_root = xstrdup(argv[i]+8);
403 } 403 }
404 if (!strcmp(argv[i], "--nocache")) { 404 if (!strcmp(argv[i], "--nocache")) {
405 ctx.cfg.nocache = 1; 405 ctx.cfg.nocache = 1;
406 } 406 }
407 if (!strncmp(argv[i], "--query=", 8)) { 407 if (!strncmp(argv[i], "--query=", 8)) {
408 ctx.qry.raw = xstrdup(argv[i]+8); 408 ctx.qry.raw = xstrdup(argv[i]+8);
409 } 409 }
410 if (!strncmp(argv[i], "--repo=", 7)) { 410 if (!strncmp(argv[i], "--repo=", 7)) {
411 ctx.qry.repo = xstrdup(argv[i]+7); 411 ctx.qry.repo = xstrdup(argv[i]+7);
412 } 412 }
413 if (!strncmp(argv[i], "--page=", 7)) { 413 if (!strncmp(argv[i], "--page=", 7)) {
414 ctx.qry.page = xstrdup(argv[i]+7); 414 ctx.qry.page = xstrdup(argv[i]+7);
415 } 415 }
416 if (!strncmp(argv[i], "--head=", 7)) { 416 if (!strncmp(argv[i], "--head=", 7)) {
417 ctx.qry.head = xstrdup(argv[i]+7); 417 ctx.qry.head = xstrdup(argv[i]+7);
418 ctx.qry.has_symref = 1; 418 ctx.qry.has_symref = 1;
419 } 419 }
420 if (!strncmp(argv[i], "--sha1=", 7)) { 420 if (!strncmp(argv[i], "--sha1=", 7)) {
421 ctx.qry.sha1 = xstrdup(argv[i]+7); 421 ctx.qry.sha1 = xstrdup(argv[i]+7);
422 ctx.qry.has_sha1 = 1; 422 ctx.qry.has_sha1 = 1;
423 } 423 }
424 if (!strncmp(argv[i], "--ofs=", 6)) { 424 if (!strncmp(argv[i], "--ofs=", 6)) {
425 ctx.qry.ofs = atoi(argv[i]+6); 425 ctx.qry.ofs = atoi(argv[i]+6);
426 } 426 }
427 } 427 }
428} 428}
429 429
430int main(int argc, const char **argv) 430int main(int argc, const char **argv)
431{ 431{
432 struct cacheitem item; 432 struct cacheitem item;
433 const char *cgit_config_env = getenv("CGIT_CONFIG"); 433 const char *cgit_config_env = getenv("CGIT_CONFIG");
434 434
435 prepare_context(&ctx); 435 prepare_context(&ctx);
436 item.st.st_mtime = time(NULL); 436 item.st.st_mtime = time(NULL);
437 cgit_repolist.length = 0; 437 cgit_repolist.length = 0;
438 cgit_repolist.count = 0; 438 cgit_repolist.count = 0;
439 cgit_repolist.repos = NULL; 439 cgit_repolist.repos = NULL;
440 440
441 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 441 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
442 config_cb); 442 config_cb);
443 ctx.repo = NULL;
443 if (getenv("SCRIPT_NAME")) 444 if (getenv("SCRIPT_NAME"))
444 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 445 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
445 if (getenv("QUERY_STRING")) 446 if (getenv("QUERY_STRING"))
446 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 447 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
447 cgit_parse_args(argc, argv); 448 cgit_parse_args(argc, argv);
448 http_parse_querystring(ctx.qry.raw, querystring_cb); 449 http_parse_querystring(ctx.qry.raw, querystring_cb);
449 if (!cgit_prepare_cache(&item)) 450 if (!cgit_prepare_cache(&item))
450 return 0; 451 return 0;
451 if (ctx.cfg.nocache) { 452 if (ctx.cfg.nocache) {
452 cgit_fill_cache(&item, 0); 453 cgit_fill_cache(&item, 0);
453 } else { 454 } else {
454 cgit_check_cache(&item); 455 cgit_check_cache(&item);
455 cgit_print_cache(&item); 456 cgit_print_cache(&item);
456 } 457 }
457 return 0; 458 return 0;
458} 459}