blob: 89f7ecdcc0df88bf5cb6be3c46397b5f077e786b [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cache.c: cache management
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli25105d72006-12-10 22:31:36 +01009#include "cgit.h"
Lars Hjemliee4056b2008-03-27 09:22:13 +010010#include "cache.h"
Lars Hjemli25105d72006-12-10 22:31:36 +010011
12const int NOLOCK = -1;
13
Lars Hjemli2c2047f2007-01-12 00:24:35 +010014char *cache_safe_filename(const char *unsafe)
15{
Lars Hjemli72fa5c62007-05-18 03:54:15 +020016 static char buf[4][PATH_MAX];
17 static int bufidx;
18 char *s;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010019 char c;
20
Lars Hjemli72fa5c62007-05-18 03:54:15 +020021 bufidx++;
22 bufidx &= 3;
23 s = buf[bufidx];
24
Lars Hjemli2c2047f2007-01-12 00:24:35 +010025 while(unsafe && (c = *unsafe++) != 0) {
Lars Hjemli72fa5c62007-05-18 03:54:15 +020026 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
Lars Hjemli2c2047f2007-01-12 00:24:35 +010027 c == '>' || c == '<' || c == '.')
28 c = '_';
29 *s++ = c;
30 }
31 *s = '\0';
Lars Hjemli72fa5c62007-05-18 03:54:15 +020032 return buf[bufidx];
Lars Hjemli2c2047f2007-01-12 00:24:35 +010033}
34
Lars Hjemlif5069d82006-12-11 09:57:58 +010035int cache_exist(struct cacheitem *item)
36{
Lars Hjemli25105d72006-12-10 22:31:36 +010037 if (stat(item->name, &item->st)) {
38 item->st.st_mtime = 0;
39 return 0;
40 }
41 return 1;
42}
43
44int cache_create_dirs()
45{
46 char *path;
47
Lars Hjemlib228d4f2008-02-16 13:07:13 +010048 path = fmt("%s", ctx.cfg.cache_root);
Lars Hjemli7c849d92006-12-16 13:55:58 +010049 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
50 return 0;
51
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010052 if (!ctx.repo)
Lars Hjemli25105d72006-12-10 22:31:36 +010053 return 0;
54
Lars Hjemlib228d4f2008-02-16 13:07:13 +010055 path = fmt("%s/%s", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010056 cache_safe_filename(ctx.repo->url));
Lars Hjemli30ccdca2007-05-18 03:00:54 +020057
Lars Hjemli25105d72006-12-10 22:31:36 +010058 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
59 return 0;
60
Lars Hjemlid14d77f2008-02-16 11:53:40 +010061 if (ctx.qry.page) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010062 path = fmt("%s/%s/%s", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010063 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010064 ctx.qry.page);
Lars Hjemli25105d72006-12-10 22:31:36 +010065 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
66 return 0;
67 }
68 return 1;
69}
70
Lars Hjemli318d1062006-12-11 12:10:12 +010071int cache_refill_overdue(const char *lockfile)
72{
73 struct stat st;
74
75 if (stat(lockfile, &st))
76 return 0;
77 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010078 return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time);
Lars Hjemli318d1062006-12-11 12:10:12 +010079}
80
Lars Hjemli25105d72006-12-10 22:31:36 +010081int cache_lock(struct cacheitem *item)
82{
Lars Hjemli318d1062006-12-11 12:10:12 +010083 int i = 0;
Lars Hjemli58d04f62006-12-12 10:16:41 +010084 char *lockfile = xstrdup(fmt("%s.lock", item->name));
Lars Hjemli25105d72006-12-10 22:31:36 +010085
Lars Hjemli318d1062006-12-11 12:10:12 +010086 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +010087 if (++i > ctx.cfg.max_lock_attempts)
Lars Hjemli318d1062006-12-11 12:10:12 +010088 die("cache_lock: unable to lock %s: %s",
89 item->name, strerror(errno));
90
91 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
92
Lars Hjemli25105d72006-12-10 22:31:36 +010093 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
94 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010095
96 if (item->fd == NOLOCK && errno == EEXIST &&
97 cache_refill_overdue(lockfile) && !unlink(lockfile))
Lars Hjemli25105d72006-12-10 22:31:36 +010098 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010099
Lars Hjemli58d04f62006-12-12 10:16:41 +0100100 free(lockfile);
Lars Hjemli25105d72006-12-10 22:31:36 +0100101 return (item->fd > 0);
102}
103
104int cache_unlock(struct cacheitem *item)
105{
106 close(item->fd);
107 return (rename(fmt("%s.lock", item->name), item->name) == 0);
108}
109
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100110int cache_cancel_lock(struct cacheitem *item)
111{
112 return (unlink(fmt("%s.lock", item->name)) == 0);
113}
114
Lars Hjemli25105d72006-12-10 22:31:36 +0100115int cache_expired(struct cacheitem *item)
116{
117 if (item->ttl < 0)
118 return 0;
119 return item->st.st_mtime + item->ttl * 60 < time(NULL);
120}