blob: 8df7c2677bc24f6980888f95523931b7538bd74a [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{
15 static char buf[PATH_MAX];
16 char *s = buf;
17 char c;
18
19 while(unsafe && (c = *unsafe++) != 0) {
20 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
21 c == '>' || c == '<' || c == '.')
22 c = '_';
23 *s++ = c;
24 }
25 *s = '\0';
26 return buf;
27}
28
Lars Hjemlif5069d82006-12-11 09:57:58 +010029int cache_exist(struct cacheitem *item)
30{
Lars Hjemli25105d72006-12-10 22:31:36 +010031 if (stat(item->name, &item->st)) {
32 item->st.st_mtime = 0;
33 return 0;
34 }
35 return 1;
36}
37
38int cache_create_dirs()
39{
40 char *path;
41
Lars Hjemli7c849d92006-12-16 13:55:58 +010042 path = fmt("%s", cgit_cache_root);
43 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44 return 0;
45
Lars Hjemli25105d72006-12-10 22:31:36 +010046 if (!cgit_query_repo)
47 return 0;
48
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
51 return 0;
52
53 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
55 cgit_query_page);
56 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
57 return 0;
58 }
59 return 1;
60}
61
Lars Hjemli318d1062006-12-11 12:10:12 +010062int cache_refill_overdue(const char *lockfile)
63{
64 struct stat st;
65
66 if (stat(lockfile, &st))
67 return 0;
68 else
69 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
70}
71
Lars Hjemli25105d72006-12-10 22:31:36 +010072int cache_lock(struct cacheitem *item)
73{
Lars Hjemli318d1062006-12-11 12:10:12 +010074 int i = 0;
Lars Hjemli58d04f62006-12-12 10:16:41 +010075 char *lockfile = xstrdup(fmt("%s.lock", item->name));
Lars Hjemli25105d72006-12-10 22:31:36 +010076
Lars Hjemli318d1062006-12-11 12:10:12 +010077 top:
78 if (++i > cgit_max_lock_attempts)
79 die("cache_lock: unable to lock %s: %s",
80 item->name, strerror(errno));
81
82 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
83
Lars Hjemli25105d72006-12-10 22:31:36 +010084 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
85 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010086
87 if (item->fd == NOLOCK && errno == EEXIST &&
88 cache_refill_overdue(lockfile) && !unlink(lockfile))
Lars Hjemli25105d72006-12-10 22:31:36 +010089 goto top;
Lars Hjemli318d1062006-12-11 12:10:12 +010090
Lars Hjemli58d04f62006-12-12 10:16:41 +010091 free(lockfile);
Lars Hjemli25105d72006-12-10 22:31:36 +010092 return (item->fd > 0);
93}
94
95int cache_unlock(struct cacheitem *item)
96{
97 close(item->fd);
98 return (rename(fmt("%s.lock", item->name), item->name) == 0);
99}
100
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100101int cache_cancel_lock(struct cacheitem *item)
102{
103 return (unlink(fmt("%s.lock", item->name)) == 0);
104}
105
Lars Hjemli25105d72006-12-10 22:31:36 +0100106int cache_expired(struct cacheitem *item)
107{
108 if (item->ttl < 0)
109 return 0;
110 return item->st.st_mtime + item->ttl * 60 < time(NULL);
111}