summaryrefslogtreecommitdiffabout
path: root/cache.c
Unidiff
Diffstat (limited to 'cache.c') (more/less context) (show whitespace changes)
-rw-r--r--cache.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..372e38d 100644
--- a/cache.c
+++ b/cache.c
@@ -3,64 +3,72 @@
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[4][PATH_MAX];
16 char *s = buf; 16 static int bufidx;
17 char *s;
17 char c; 18 char c;
18 19
20 bufidx++;
21 bufidx &= 3;
22 s = buf[bufidx];
23
19 while(unsafe && (c = *unsafe++) != 0) { 24 while(unsafe && (c = *unsafe++) != 0) {
20 if (c == '/' || c == ' ' || c == '&' || c == '|' || 25 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
21 c == '>' || c == '<' || c == '.') 26 c == '>' || c == '<' || c == '.')
22 c = '_'; 27 c = '_';
23 *s++ = c; 28 *s++ = c;
24 } 29 }
25 *s = '\0'; 30 *s = '\0';
26 return buf; 31 return buf[bufidx];
27} 32}
28 33
29int cache_exist(struct cacheitem *item) 34int cache_exist(struct cacheitem *item)
30{ 35{
31 if (stat(item->name, &item->st)) { 36 if (stat(item->name, &item->st)) {
32 item->st.st_mtime = 0; 37 item->st.st_mtime = 0;
33 return 0; 38 return 0;
34 } 39 }
35 return 1; 40 return 1;
36} 41}
37 42
38int cache_create_dirs() 43int cache_create_dirs()
39{ 44{
40 char *path; 45 char *path;
41 46
42 path = fmt("%s", cgit_cache_root); 47 path = fmt("%s", cgit_cache_root);
43 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 48 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44 return 0; 49 return 0;
45 50
46 if (!cgit_query_repo) 51 if (!cgit_repo)
47 return 0; 52 return 0;
48 53
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 54 path = fmt("%s/%s", cgit_cache_root,
55 cache_safe_filename(cgit_repo->url));
56
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 57 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
51 return 0; 58 return 0;
52 59
53 if (cgit_query_page) { 60 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 61 path = fmt("%s/%s/%s", cgit_cache_root,
62 cache_safe_filename(cgit_repo->url),
55 cgit_query_page); 63 cgit_query_page);
56 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 64 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
57 return 0; 65 return 0;
58 } 66 }
59 return 1; 67 return 1;
60} 68}
61 69
62int cache_refill_overdue(const char *lockfile) 70int cache_refill_overdue(const char *lockfile)
63{ 71{
64 struct stat st; 72 struct stat st;
65 73
66 if (stat(lockfile, &st)) 74 if (stat(lockfile, &st))