5 int cache_lookup(struct cacheitem *item)
7 if (!cgit_query_repo) {
8 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
9 item->ttl = cgit_cache_root_ttl;
10 } else if (!cgit_query_page) {
11 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
13 item->ttl = cgit_cache_repo_ttl;
15 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
16 cgit_query_repo, cgit_query_page,
18 if (cgit_query_has_symref)
19 item->ttl = cgit_cache_dynamic_ttl;
20 else if (cgit_query_has_sha1)
21 item->ttl = cgit_cache_static_ttl;
23 item->ttl = cgit_cache_repo_ttl;
25 if (stat(item->name, &item->st)) {
26 item->st.st_mtime = 0;
32 int cache_create_dirs()
39 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
40 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
43 if (cgit_query_page) {
44 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
46 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
52 int cache_lock(struct cacheitem *item)
55 char *lockfile = fmt("%s.lock", item->name);
58 item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
59 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
61 if (item->fd == NOLOCK && errno == EEXIST) {
64 if (stat(lockfile, &st))
67 if (t-st.st_mtime > cgit_cache_max_create_time &&
72 return (item->fd > 0);
75 int cache_unlock(struct cacheitem *item)
78 return (rename(fmt("%s.lock", item->name), item->name) == 0);
81 int cache_expired(struct cacheitem *item)
85 return item->st.st_mtime + item->ttl * 60 < time(NULL);