author | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:07:13 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:10:50 (UTC) |
commit | b228d4ff82a65fdcd4a7364759fe36a0bdda5978 (patch) (unidiff) | |
tree | 33b8cc2ff48113f8d7ad3ba88c7ea19a7cac570a /cache.c | |
parent | d14d77fe95c3b6224b40df9b101dded0deea913c (diff) | |
download | cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.zip cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.tar.gz cgit-b228d4ff82a65fdcd4a7364759fe36a0bdda5978.tar.bz2 |
Add all config variables into struct cgit_context
This removes another big set of global variables, and introduces the
cgit_prepare_context() function which populates a context-variable with
compile-time default values.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cache.c | 10 |
1 files changed, 5 insertions, 5 deletions
@@ -1,119 +1,119 @@ | |||
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) | 13 | char *cache_safe_filename(const char *unsafe) |
14 | { | 14 | { |
15 | static char buf[4][PATH_MAX]; | 15 | static char buf[4][PATH_MAX]; |
16 | static int bufidx; | 16 | static int bufidx; |
17 | char *s; | 17 | char *s; |
18 | char c; | 18 | char c; |
19 | 19 | ||
20 | bufidx++; | 20 | bufidx++; |
21 | bufidx &= 3; | 21 | bufidx &= 3; |
22 | s = buf[bufidx]; | 22 | s = buf[bufidx]; |
23 | 23 | ||
24 | while(unsafe && (c = *unsafe++) != 0) { | 24 | while(unsafe && (c = *unsafe++) != 0) { |
25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
26 | c == '>' || c == '<' || c == '.') | 26 | c == '>' || c == '<' || c == '.') |
27 | c = '_'; | 27 | c = '_'; |
28 | *s++ = c; | 28 | *s++ = c; |
29 | } | 29 | } |
30 | *s = '\0'; | 30 | *s = '\0'; |
31 | return buf[bufidx]; | 31 | return buf[bufidx]; |
32 | } | 32 | } |
33 | 33 | ||
34 | int cache_exist(struct cacheitem *item) | 34 | int cache_exist(struct cacheitem *item) |
35 | { | 35 | { |
36 | if (stat(item->name, &item->st)) { | 36 | if (stat(item->name, &item->st)) { |
37 | item->st.st_mtime = 0; | 37 | item->st.st_mtime = 0; |
38 | return 0; | 38 | return 0; |
39 | } | 39 | } |
40 | return 1; | 40 | return 1; |
41 | } | 41 | } |
42 | 42 | ||
43 | int cache_create_dirs() | 43 | int cache_create_dirs() |
44 | { | 44 | { |
45 | char *path; | 45 | char *path; |
46 | 46 | ||
47 | path = fmt("%s", cgit_cache_root); | 47 | path = fmt("%s", ctx.cfg.cache_root); |
48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
49 | return 0; | 49 | return 0; |
50 | 50 | ||
51 | if (!cgit_repo) | 51 | if (!cgit_repo) |
52 | return 0; | 52 | return 0; |
53 | 53 | ||
54 | path = fmt("%s/%s", cgit_cache_root, | 54 | path = fmt("%s/%s", ctx.cfg.cache_root, |
55 | cache_safe_filename(cgit_repo->url)); | 55 | cache_safe_filename(cgit_repo->url)); |
56 | 56 | ||
57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
58 | return 0; | 58 | return 0; |
59 | 59 | ||
60 | if (ctx.qry.page) { | 60 | if (ctx.qry.page) { |
61 | path = fmt("%s/%s/%s", cgit_cache_root, | 61 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
62 | cache_safe_filename(cgit_repo->url), | 62 | cache_safe_filename(cgit_repo->url), |
63 | ctx.qry.page); | 63 | ctx.qry.page); |
64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
65 | return 0; | 65 | return 0; |
66 | } | 66 | } |
67 | return 1; | 67 | return 1; |
68 | } | 68 | } |
69 | 69 | ||
70 | int cache_refill_overdue(const char *lockfile) | 70 | int cache_refill_overdue(const char *lockfile) |
71 | { | 71 | { |
72 | struct stat st; | 72 | struct stat st; |
73 | 73 | ||
74 | if (stat(lockfile, &st)) | 74 | if (stat(lockfile, &st)) |
75 | return 0; | 75 | return 0; |
76 | else | 76 | else |
77 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); | 77 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
78 | } | 78 | } |
79 | 79 | ||
80 | int cache_lock(struct cacheitem *item) | 80 | int cache_lock(struct cacheitem *item) |
81 | { | 81 | { |
82 | int i = 0; | 82 | int i = 0; |
83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
84 | 84 | ||
85 | top: | 85 | top: |
86 | if (++i > cgit_max_lock_attempts) | 86 | if (++i > ctx.cfg.max_lock_attempts) |
87 | die("cache_lock: unable to lock %s: %s", | 87 | die("cache_lock: unable to lock %s: %s", |
88 | item->name, strerror(errno)); | 88 | item->name, strerror(errno)); |
89 | 89 | ||
90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
91 | 91 | ||
92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
93 | goto top; | 93 | goto top; |
94 | 94 | ||
95 | if (item->fd == NOLOCK && errno == EEXIST && | 95 | if (item->fd == NOLOCK && errno == EEXIST && |
96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
97 | goto top; | 97 | goto top; |
98 | 98 | ||
99 | free(lockfile); | 99 | free(lockfile); |
100 | return (item->fd > 0); | 100 | return (item->fd > 0); |
101 | } | 101 | } |
102 | 102 | ||
103 | int cache_unlock(struct cacheitem *item) | 103 | int cache_unlock(struct cacheitem *item) |
104 | { | 104 | { |
105 | close(item->fd); | 105 | close(item->fd); |
106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
107 | } | 107 | } |
108 | 108 | ||
109 | int cache_cancel_lock(struct cacheitem *item) | 109 | int cache_cancel_lock(struct cacheitem *item) |
110 | { | 110 | { |
111 | return (unlink(fmt("%s.lock", item->name)) == 0); | 111 | return (unlink(fmt("%s.lock", item->name)) == 0); |
112 | } | 112 | } |
113 | 113 | ||
114 | int cache_expired(struct cacheitem *item) | 114 | int cache_expired(struct cacheitem *item) |
115 | { | 115 | { |
116 | if (item->ttl < 0) | 116 | if (item->ttl < 0) |
117 | return 0; | 117 | return 0; |
118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
119 | } | 119 | } |