summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c35
-rw-r--r--cgit.c9
-rw-r--r--cgit.h1
3 files changed, 31 insertions, 14 deletions
diff --git a/cache.c b/cache.c
index 0ffa502..2e1ef8c 100644
--- a/cache.c
+++ b/cache.c
@@ -58,32 +58,41 @@ int cache_create_dirs()
if (mkdir(path, S_IRWXU) && errno!=EEXIST)
return 0;
}
return 1;
}
+int cache_refill_overdue(const char *lockfile)
+{
+ struct stat st;
+
+ if (stat(lockfile, &st))
+ return 0;
+ else
+ return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
+}
+
int cache_lock(struct cacheitem *item)
{
- int ret;
+ int i = 0;
char *lockfile = fmt("%s.lock", item->name);
- top:
- item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
+ top:
+ if (++i > cgit_max_lock_attempts)
+ die("cache_lock: unable to lock %s: %s",
+ item->name, strerror(errno));
+
+ 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))
+
+ if (item->fd == NOLOCK && errno == EEXIST &&
+ cache_refill_overdue(lockfile) && !unlink(lockfile))
goto top;
- return 0;
- }
+
return (item->fd > 0);
}
int cache_unlock(struct cacheitem *item)
{
close(item->fd);
diff --git a/cgit.c b/cgit.c
index 7f14016..dc91125 100644
--- a/cgit.c
+++ b/cgit.c
@@ -28,12 +28,13 @@ char *cgit_css = "/cgit.css";
char *cgit_logo = "/git-logo.png";
char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
char *cgit_virtual_root = NULL;
char *cgit_cache_root = "/var/cache/cgit";
+int cgit_max_lock_attempts = 5;
int cgit_cache_root_ttl = 5;
int cgit_cache_repo_ttl = 5;
int cgit_cache_dynamic_ttl = 5;
int cgit_cache_static_ttl = -1;
int cgit_cache_max_create_time = 5;
@@ -462,17 +463,23 @@ static void cgit_fill_cache(struct cacheitem *item)
else
cgit_print_repolist();
}
static void cgit_refresh_cache(struct cacheitem *item)
{
+ int i = 0;
+
cache_prepare(item);
top:
+ if (++i > cgit_max_lock_attempts) {
+ die("cgit_refresh_cache: unable to lock %s: %s",
+ item->name, strerror(errno));
+ }
if (!cache_exist(item)) {
if (!cache_lock(item)) {
- sched_yield();
+ sleep(1);
goto top;
}
if (!cache_exist(item))
cgit_fill_cache(item);
cache_unlock(item);
} else if (cache_expired(item) && cache_lock(item)) {
diff --git a/cgit.h b/cgit.h
index 3b0994a..7e4bfef 100644
--- a/cgit.h
+++ b/cgit.h
@@ -20,12 +20,13 @@ extern char *cgit_root_title;
extern char *cgit_css;
extern char *cgit_logo;
extern char *cgit_logo_link;
extern char *cgit_virtual_root;
extern char *cgit_cache_root;
+extern int cgit_max_lock_attempts;
extern int cgit_cache_root_ttl;
extern int cgit_cache_repo_ttl;
extern int cgit_cache_dynamic_ttl;
extern int cgit_cache_static_ttl;
extern int cgit_cache_max_create_time;