summaryrefslogtreecommitdiffabout
path: root/cache.c
authorLars Hjemli <hjemli@gmail.com>2006-12-11 08:57:58 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2006-12-11 08:57:58 (UTC)
commitf5069d88dff7a7ed2f4665904b03e906cca75a7c (patch) (side-by-side diff)
tree4c9bfa3aaf931af4a345ffb9563b19825c38b628 /cache.c
parent76827d8679d1d2bd46e8cddf7da2ce4178e1d676 (diff)
downloadcgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.zip
cgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.tar.gz
cgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.tar.bz2
Fix cache algorithm loophole
This closes the door for unneccessary calls to cgit_fill_cache(). Noticed by Linus. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'cache.c') (more/less context) (show whitespace changes)
-rw-r--r--cache.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/cache.c b/cache.c
index b034c48..0ffa502 100644
--- a/cache.c
+++ b/cache.c
@@ -1,80 +1,84 @@
/* cache.c: cache management
*
* Copyright (C) 2006 Lars Hjemli
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
#include "cgit.h"
const int NOLOCK = -1;
-int cache_lookup(struct cacheitem *item)
+void cache_prepare(struct cacheitem *item)
{
if (!cgit_query_repo) {
item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
item->ttl = cgit_cache_root_ttl;
} else if (!cgit_query_page) {
item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
cgit_query_repo));
item->ttl = cgit_cache_repo_ttl;
} else {
item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
cgit_query_repo, cgit_query_page,
cgit_querystring));
if (cgit_query_has_symref)
item->ttl = cgit_cache_dynamic_ttl;
else if (cgit_query_has_sha1)
item->ttl = cgit_cache_static_ttl;
else
item->ttl = cgit_cache_repo_ttl;
}
+}
+
+int cache_exist(struct cacheitem *item)
+{
if (stat(item->name, &item->st)) {
item->st.st_mtime = 0;
return 0;
}
return 1;
}
int cache_create_dirs()
{
char *path;
if (!cgit_query_repo)
return 0;
path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
if (mkdir(path, S_IRWXU) && errno!=EEXIST)
return 0;
if (cgit_query_page) {
path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
cgit_query_page);
if (mkdir(path, S_IRWXU) && errno!=EEXIST)
return 0;
}
return 1;
}
int cache_lock(struct cacheitem *item)
{
int ret;
char *lockfile = fmt("%s.lock", item->name);
top:
item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
goto top;
if (item->fd == NOLOCK && errno == EEXIST) {
struct stat st;
time_t t;
if (stat(lockfile, &st))
return ret;
t = time(NULL);
if (t-st.st_mtime > cgit_cache_max_create_time &&
!unlink(lockfile))
goto top;
return 0;
}
return (item->fd > 0);