-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);
-}
-