|
diff --git a/cache.c b/cache.c index 1ff1251..8df7c26 100644 --- a/ cache.c+++ b/ cache.c |
|
@@ -1,76 +1,92 @@ |
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 | |
11 | const int NOLOCK = -1; |
11 | const int NOLOCK = -1; |
12 | |
12 | |
| |
13 | char *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 | |
13 | int cache_exist(struct cacheitem *item) |
29 | int cache_exist(struct cacheitem *item) |
14 | { |
30 | { |
15 | if (stat(item->name, &item->st)) { |
31 | if (stat(item->name, &item->st)) { |
16 | item->st.st_mtime = 0; |
32 | item->st.st_mtime = 0; |
17 | return 0; |
33 | return 0; |
18 | } |
34 | } |
19 | return 1; |
35 | return 1; |
20 | } |
36 | } |
21 | |
37 | |
22 | int cache_create_dirs() |
38 | int cache_create_dirs() |
23 | { |
39 | { |
24 | char *path; |
40 | char *path; |
25 | |
41 | |
26 | path = fmt("%s", cgit_cache_root); |
42 | path = fmt("%s", cgit_cache_root); |
27 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
43 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
28 | return 0; |
44 | return 0; |
29 | |
45 | |
30 | if (!cgit_query_repo) |
46 | if (!cgit_query_repo) |
31 | return 0; |
47 | return 0; |
32 | |
48 | |
33 | path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); |
49 | path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); |
34 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
50 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
35 | return 0; |
51 | return 0; |
36 | |
52 | |
37 | if (cgit_query_page) { |
53 | if (cgit_query_page) { |
38 | path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, |
54 | path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, |
39 | cgit_query_page); |
55 | cgit_query_page); |
40 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
56 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
41 | return 0; |
57 | return 0; |
42 | } |
58 | } |
43 | return 1; |
59 | return 1; |
44 | } |
60 | } |
45 | |
61 | |
46 | int cache_refill_overdue(const char *lockfile) |
62 | int cache_refill_overdue(const char *lockfile) |
47 | { |
63 | { |
48 | struct stat st; |
64 | struct stat st; |
49 | |
65 | |
50 | if (stat(lockfile, &st)) |
66 | if (stat(lockfile, &st)) |
51 | return 0; |
67 | return 0; |
52 | else |
68 | else |
53 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); |
69 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); |
54 | } |
70 | } |
55 | |
71 | |
56 | int cache_lock(struct cacheitem *item) |
72 | int cache_lock(struct cacheitem *item) |
57 | { |
73 | { |
58 | int i = 0; |
74 | int i = 0; |
59 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
75 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
60 | |
76 | |
61 | top: |
77 | top: |
62 | if (++i > cgit_max_lock_attempts) |
78 | if (++i > cgit_max_lock_attempts) |
63 | die("cache_lock: unable to lock %s: %s", |
79 | die("cache_lock: unable to lock %s: %s", |
64 | item->name, strerror(errno)); |
80 | item->name, strerror(errno)); |
65 | |
81 | |
66 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
82 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
67 | |
83 | |
68 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
84 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
69 | goto top; |
85 | goto top; |
70 | |
86 | |
71 | if (item->fd == NOLOCK && errno == EEXIST && |
87 | if (item->fd == NOLOCK && errno == EEXIST && |
72 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
88 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
73 | goto top; |
89 | goto top; |
74 | |
90 | |
75 | free(lockfile); |
91 | free(lockfile); |
76 | return (item->fd > 0); |
92 | return (item->fd > 0); |
|