blob: 7860fc75dc3f6aef61277658a87e5a14d0d5c9a1 [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"
10
11const int NOLOCK = -1;
12
Lars Hjemli2c2047f2007-01-12 00:24:35 +010013char *cache_safe_filename(const char *unsafe)
14{
Lars Hjemli72fa5c62007-05-18 03:54:15 +020015 static char buf[4][PATH_MAX];
16 static int bufidx;
17 char *s;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010018 char c;
19
Lars Hjemli72fa5c62007-05-18 03:54:15 +020020 bufidx++;
21 bufidx &= 3;
22 s = buf[bufidx];
23
Lars Hjemli2c2047f2007-01-12 00:24:35 +010024 while(unsafe && (c = *unsafe++) != 0) {
Lars Hjemli72fa5c62007-05-18 03:54:15 +020025 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
Lars Hjemli2c2047f2007-01-12 00:24:35 +010026 c == '>' || c == '<' || c == '.')
27 c = '_';
28 *s++ = c;
29 }
30 *s = '\0';
Lars Hjemli72fa5c62007-05-18 03:54:15 +020031 return buf[bufidx];
Lars Hjemli2c2047f2007-01-12 00:24:35 +010032}
33
Lars Hjemlif5069d82006-12-11 09:57:58 +010034int cache_exist(struct cacheitem *item)
35{
Lars Hjemli25105d72006-12-10 22:31:36 +010036 if (stat(item->name, &item->st)) {
37 item->st.st_mtime = 0;
38 return 0;
39 }
40 return 1;
41}
42
43int cache_create_dirs()
44{
45 char *path;
46
Lars Hjemlib228d4f2008-02-16 13:07:13 +010047 path = fmt("%s", ctx.cfg.cache_root);
Lars Hjemli7c849d92006-12-16 13:55:58 +010048 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
49 return 0;
50
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010051 if (!ctx.repo)
Lars Hjemli25105d72006-12-10 22:31:36 +010052 return 0;
53
Lars Hjemlib228d4f2008-02-16 13:07:13 +010054 path = fmt("%s/%s", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010055 cache_safe_filename(ctx.repo->url));
Lars Hjemli30ccdca2007-05-18 03:00:54 +020056
Lars Hjemli25105d72006-12-10 22:31:36 +010057 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
58 return 0;
59
Lars Hjemlid14d77f2008-02-16 11:53:40 +010060 if (ctx.qry.page) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010061 path = fmt("%s/%s/%s", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010062 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010063 ctx.qry.page);
Lars Hjemli25105d72006-12-10 22:31:36 +010064 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
65 return 0;
66 }
67 return 1;
68}
69
Lars Hjemli318d1062006-12-11 12:10:12 +010070int cache_refill_overdue(const char *lockfile)
71{
72 struct stat st;
73
74 if (stat(lockfile, &st))
75 return 0;
76 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010077 return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time);
Lars Hjemli318d1062006-12-11 12:10:12 +010078}
79
Lars Hjemli25105d72006-12-10 22:31:36 +010080int cache_lock(struct cacheitem *item)
81{
Lars Hjemli318d1062006-12-11 12:10:12 +010082 int i = 0;
Lars Hjemli58d04f62006-12-12 10:16:41 +010083 char *lockfile = xstrdup(fmt("%s.lock", item->name));
Lars Hjemli25105d72006-12-10 22:31:36 +010084
Lars Hjemli318d1062006-12-11 12:10:12 +010085 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +010086 if (++i > ctx.cfg.max_lock_attempts)
Lars Hjemli318d1062006-12-11 12:10:12 +010087 die("cache_lock: unable to lock %s: %s",
88 item->name, strerror(errno));
89
90 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
91
Lars Hjemli25105d72006-12-10 22:31:36 +010092 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
93 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010094
95 if (item->fd == NOLOCK && errno == EEXIST &&
96 cache_refill_overdue(lockfile) && !unlink(lockfile))
Lars Hjemli25105d72006-12-10 22:31:36 +010097 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010098
Lars Hjemli58d04f62006-12-12 10:16:41 +010099 free(lockfile);
Lars Hjemli25105d72006-12-10 22:31:36 +0100100 return (item->fd > 0);
101}
102
103int cache_unlock(struct cacheitem *item)
104{
105 close(item->fd);
106 return (rename(fmt("%s.lock", item->name), item->name) == 0);
107}
108
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100109int cache_cancel_lock(struct cacheitem *item)
110{
111 return (unlink(fmt("%s.lock", item->name)) == 0);
112}
113
Lars Hjemli25105d72006-12-10 22:31:36 +0100114int cache_expired(struct cacheitem *item)
115{
116 if (item->ttl < 0)
117 return 0;
118 return item->st.st_mtime + item->ttl * 60 < time(NULL);
119}