/* cache.c: cache management * * Copyright (C) 2006-2014 cgit Development Team * * Licensed under GNU General Public License v2 * (see COPYING for full license text) * * * The cache is just a directory structure where each file is a cache slot, * and each filename is based on the hash of some key (e.g. the cgit url). * Each file contains the full key followed by the cached content for that * key. * */ #ifdef HAVE_LINUX_SENDFILE #include #endif #include "cgit.h" #include "cache.h" #include "html.h" #define CACHE_BUFSIZE (1024 * 4) struct cache_slot { const char *key; int keylen; int ttl; cache_fill_fn fn; int cache_fd; int lock_fd; const char *cache_name; const char *lock_name; int match; struct stat cache_st; int bufsize; char buf[CACHE_BUFSIZE]; }; /* Open an existing cache slot and fill the cache buffer with * (part of) the content of the cache file. Return 0 on success * and errno otherwise. */ static int open_slot(struct cache_slot *slot) { char *bufz; int bufkeylen = -1; slot->cache_fd = open(slot->cache_name, O_RDONLY); if (slot->cache_fd == -1) return errno; if (fstat(slot->cache_fd, &slot->cache_st)) return errno; slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf)); if (slot->bufsize < 0) return errno; bufz = memchr(slot->buf, 0, slot->bufsize); if (bufz) bufkeylen = bufz - slot->buf; slot->match = bufkeylen == slot->keylen && !memcmp(slot->key, slot->buf, bufkeylen + 1); return 0; } /* Close the active cache slot */ static int close_slot(struct cache_slot *slot) { int err = 0; if (slot->cache_fd > 0) { if (close(slot->cache_fd)) err = errno; else slot->cache_fd = -1; } return err; } /* Print the content of the active cache slot (but skip the key). */ static int print_slot(struct cache_slot *slot) { #ifdef HAVE_LINUX_SENDFILE off_t start_off; int ret; start_off = slot->keylen + 1; do { ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off, slot->cache_st.st_size - start_off); if (ret < 0) { if (errno == EAGAIN || errno == EINTR) continue; return errno; } return 0; } while (1); #else ssize_t i, j; i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET); if (i != slot->keylen + 1) return errno; do { i = j = xread(slot->cache_fd, slot->buf, sizeof(slot->buf)); if (i > 0) j = xwrite(STDOUT_FILENO, slot->buf, i); } while (i > 0 && j == i); if (i < 0 || j != i) return errno; else return 0; #endif } /* Check if the slot has expired */ static int is_expired(struct cache_slot *slot) { if (slot->ttl < 0) return 0; else return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL); } /* Check if the slot has been modified since we opened it. * NB: If stat() fails, we pretend the file is modified. */ static int is_modified(struct cache_slot *slot) { struct stat st; if (s
#ifndef UI_SUMMARY_H
#define UI_SUMMARY_H

extern void cgit_print_summary(void);
extern void cgit_print_repo_readme(char *path);

#endif /* UI_SUMMARY_H */
his function is to shared memory */ static char *sprintftime(const char *format, time_t time) { static char buf[64]; struct tm *tm; if (!time) return NULL; tm = gmtime(&time); strftime(buf, sizeof(buf)-1, format, tm); return buf; } int cache_ls(const char *path) { DIR *dir; struct dirent *ent; int err = 0; struct cache_slot slot = { NULL }; struct strbuf fullname = STRBUF_INIT; size_t prefixlen; if (!path) { cache_log("[cgit] cache path not specified\n"); return -1; } dir = opendir(path); if (!dir) { err = errno; cache_log("[cgit] unable to open path %s: %s (%d)\n", path, strerror(err), err); return err; } strbuf_addstr(&fullname, path); strbuf_ensure_end(&fullname, '/'); prefixlen = fullname.len; while ((ent = readdir(dir)) != NULL) { if (strlen(ent->d_name) != 8) continue; strbuf_setlen(&fullname, prefixlen); strbuf_addstr(&fullname, ent->d_name); slot.cache_name = fullname.buf; if ((err = open_slot(&slot)) != 0) { cache_log("[cgit] unable to open path %s: %s (%d)\n", fullname.buf, strerror(err), err); continue; } htmlf("%s %s %10"PRIuMAX" %s\n", fullname.buf, sprintftime("%Y-%m-%d %H:%M:%S", slot.cache_st.st_mtime), (uintmax_t)slot.cache_st.st_size, slot.buf); close_slot(&slot); } closedir(dir); strbuf_release(&fullname); return 0; } /* Print a message to stdout */ void cache_log(const char *format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); }