summaryrefslogtreecommitdiffabout
path: root/cache.c
Unidiff
Diffstat (limited to 'cache.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..7cdea9b 100644
--- a/cache.c
+++ b/cache.c
@@ -1,111 +1,114 @@
1/* cache.c: cache management 1/* cache.c: cache management
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11const int NOLOCK = -1; 11const int NOLOCK = -1;
12 12
13char *cache_safe_filename(const char *unsafe) 13char *cache_safe_filename(const char *unsafe)
14{ 14{
15 static char buf[PATH_MAX]; 15 static char buf[PATH_MAX];
16 char *s = buf; 16 char *s = buf;
17 char c; 17 char c;
18 18
19 while(unsafe && (c = *unsafe++) != 0) { 19 while(unsafe && (c = *unsafe++) != 0) {
20 if (c == '/' || c == ' ' || c == '&' || c == '|' || 20 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
21 c == '>' || c == '<' || c == '.') 21 c == '>' || c == '<' || c == '.')
22 c = '_'; 22 c = '_';
23 *s++ = c; 23 *s++ = c;
24 } 24 }
25 *s = '\0'; 25 *s = '\0';
26 return buf; 26 return buf;
27} 27}
28 28
29int cache_exist(struct cacheitem *item) 29int cache_exist(struct cacheitem *item)
30{ 30{
31 if (stat(item->name, &item->st)) { 31 if (stat(item->name, &item->st)) {
32 item->st.st_mtime = 0; 32 item->st.st_mtime = 0;
33 return 0; 33 return 0;
34 } 34 }
35 return 1; 35 return 1;
36} 36}
37 37
38int cache_create_dirs() 38int cache_create_dirs()
39{ 39{
40 char *path; 40 char *path;
41 41
42 path = fmt("%s", cgit_cache_root); 42 path = fmt("%s", cgit_cache_root);
43 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 43 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44 return 0; 44 return 0;
45 45
46 if (!cgit_query_repo) 46 if (!cgit_repo)
47 return 0; 47 return 0;
48 48
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 49 path = fmt("%s/%s", cgit_cache_root,
50 cache_safe_filename(cgit_repo->url));
51
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 52 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
51 return 0; 53 return 0;
52 54
53 if (cgit_query_page) { 55 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 56 path = fmt("%s/%s/%s", cgit_cache_root,
57 cache_safe_filename(cgit_repo->url),
55 cgit_query_page); 58 cgit_query_page);
56 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 59 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
57 return 0; 60 return 0;
58 } 61 }
59 return 1; 62 return 1;
60} 63}
61 64
62int cache_refill_overdue(const char *lockfile) 65int cache_refill_overdue(const char *lockfile)
63{ 66{
64 struct stat st; 67 struct stat st;
65 68
66 if (stat(lockfile, &st)) 69 if (stat(lockfile, &st))
67 return 0; 70 return 0;
68 else 71 else
69 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 72 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
70} 73}
71 74
72int cache_lock(struct cacheitem *item) 75int cache_lock(struct cacheitem *item)
73{ 76{
74 int i = 0; 77 int i = 0;
75 char *lockfile = xstrdup(fmt("%s.lock", item->name)); 78 char *lockfile = xstrdup(fmt("%s.lock", item->name));
76 79
77 top: 80 top:
78 if (++i > cgit_max_lock_attempts) 81 if (++i > cgit_max_lock_attempts)
79 die("cache_lock: unable to lock %s: %s", 82 die("cache_lock: unable to lock %s: %s",
80 item->name, strerror(errno)); 83 item->name, strerror(errno));
81 84
82 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 85 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
83 86
84 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) 87 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
85 goto top; 88 goto top;
86 89
87 if (item->fd == NOLOCK && errno == EEXIST && 90 if (item->fd == NOLOCK && errno == EEXIST &&
88 cache_refill_overdue(lockfile) && !unlink(lockfile)) 91 cache_refill_overdue(lockfile) && !unlink(lockfile))
89 goto top; 92 goto top;
90 93
91 free(lockfile); 94 free(lockfile);
92 return (item->fd > 0); 95 return (item->fd > 0);
93} 96}
94 97
95int cache_unlock(struct cacheitem *item) 98int cache_unlock(struct cacheitem *item)
96{ 99{
97 close(item->fd); 100 close(item->fd);
98 return (rename(fmt("%s.lock", item->name), item->name) == 0); 101 return (rename(fmt("%s.lock", item->name), item->name) == 0);
99} 102}
100 103
101int cache_cancel_lock(struct cacheitem *item) 104int cache_cancel_lock(struct cacheitem *item)
102{ 105{
103 return (unlink(fmt("%s.lock", item->name)) == 0); 106 return (unlink(fmt("%s.lock", item->name)) == 0);
104} 107}
105 108
106int cache_expired(struct cacheitem *item) 109int cache_expired(struct cacheitem *item)
107{ 110{
108 if (item->ttl < 0) 111 if (item->ttl < 0)
109 return 0; 112 return 0;
110 return item->st.st_mtime + item->ttl * 60 < time(NULL); 113 return item->st.st_mtime + item->ttl * 60 < time(NULL);
111} 114}