-rw-r--r-- | cache.c | 427 | ||||
-rw-r--r-- | cache.h | 38 | ||||
-rw-r--r-- | cgit.c | 166 | ||||
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | cmd.c | 21 | ||||
-rwxr-xr-x | tests/setup.sh | 2 | ||||
-rwxr-xr-x | tests/t0020-validate-cache.sh | 67 |
7 files changed, 506 insertions, 216 deletions
@@ -1,120 +1,411 @@ | |||
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 | * | ||
8 | * | ||
9 | * The cache is just a directory structure where each file is a cache slot, | ||
10 | * and each filename is based on the hash of some key (e.g. the cgit url). | ||
11 | * Each file contains the full key followed by the cached content for that | ||
12 | * key. | ||
13 | * | ||
7 | */ | 14 | */ |
8 | 15 | ||
9 | #include "cgit.h" | 16 | #include "cgit.h" |
10 | #include "cache.h" | 17 | #include "cache.h" |
11 | 18 | ||
12 | const int NOLOCK = -1; | 19 | #define CACHE_BUFSIZE (1024 * 4) |
13 | 20 | ||
14 | char *cache_safe_filename(const char *unsafe) | 21 | struct cache_slot { |
22 | const char *key; | ||
23 | int keylen; | ||
24 | int ttl; | ||
25 | cache_fill_fn fn; | ||
26 | void *cbdata; | ||
27 | int cache_fd; | ||
28 | int lock_fd; | ||
29 | const char *cache_name; | ||
30 | const char *lock_name; | ||
31 | int match; | ||
32 | struct stat cache_st; | ||
33 | struct stat lock_st; | ||
34 | int bufsize; | ||
35 | char buf[CACHE_BUFSIZE]; | ||
36 | }; | ||
37 | |||
38 | /* Open an existing cache slot and fill the cache buffer with | ||
39 | * (part of) the content of the cache file. Return 0 on success | ||
40 | * and errno otherwise. | ||
41 | */ | ||
42 | static int open_slot(struct cache_slot *slot) | ||
15 | { | 43 | { |
16 | static char buf[4][PATH_MAX]; | 44 | char *bufz; |
17 | static int bufidx; | 45 | int bufkeylen = -1; |
18 | char *s; | 46 | |
19 | char c; | 47 | slot->cache_fd = open(slot->cache_name, O_RDONLY); |
20 | 48 | if (slot->cache_fd == -1) | |
21 | bufidx++; | 49 | return errno; |
22 | bufidx &= 3; | 50 | |
23 | s = buf[bufidx]; | 51 | if (fstat(slot->cache_fd, &slot->cache_st)) |
24 | 52 | return errno; | |
25 | while(unsafe && (c = *unsafe++) != 0) { | 53 | |
26 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 54 | slot->bufsize = read(slot->cache_fd, slot->buf, sizeof(slot->buf)); |
27 | c == '>' || c == '<' || c == '.') | 55 | if (slot->bufsize == 0) |
28 | c = '_'; | 56 | return errno; |
29 | *s++ = c; | 57 | |
30 | } | 58 | bufz = memchr(slot->buf, 0, slot->bufsize); |
31 | *s = '\0'; | 59 | if (bufz) |
32 | return buf[bufidx]; | 60 | bufkeylen = bufz - slot->buf; |
61 | |||
62 | slot->match = bufkeylen == slot->keylen && | ||
63 | !memcmp(slot->key, slot->buf, bufkeylen + 1); | ||
64 | |||
65 | return 0; | ||
33 | } | 66 | } |
34 | 67 | ||
35 | int cache_exist(struct cacheitem *item) | 68 | /* Close the active cache slot */ |
69 | static void close_slot(struct cache_slot *slot) | ||
36 | { | 70 | { |
37 | if (stat(item->name, &item->st)) { | 71 | if (slot->cache_fd > 0) { |
38 | item->st.st_mtime = 0; | 72 | close(slot->cache_fd); |
39 | return 0; | 73 | slot->cache_fd = -1; |
40 | } | 74 | } |
41 | return 1; | ||
42 | } | 75 | } |
43 | 76 | ||
44 | int cache_create_dirs() | 77 | /* Print the content of the active cache slot (but skip the key). */ |
78 | static int print_slot(struct cache_slot *slot) | ||
45 | { | 79 | { |
46 | char *path; | 80 | ssize_t i, j = 0; |
81 | |||
82 | i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET); | ||
83 | if (i != slot->keylen + 1) | ||
84 | return errno; | ||
85 | |||
86 | while((i=read(slot->cache_fd, slot->buf, sizeof(slot->buf))) > 0) | ||
87 | j = write(STDOUT_FILENO, slot->buf, i); | ||
47 | 88 | ||
48 | path = fmt("%s", ctx.cfg.cache_root); | 89 | if (j < 0) |
49 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 90 | return errno; |
91 | else | ||
50 | return 0; | 92 | return 0; |
93 | } | ||
51 | 94 | ||
52 | if (!ctx.repo) | 95 | /* Check if the slot has expired */ |
96 | static int is_expired(struct cache_slot *slot) | ||
97 | { | ||
98 | if (slot->ttl < 0) | ||
53 | return 0; | 99 | return 0; |
100 | else | ||
101 | return slot->cache_st.st_mtime + slot->ttl*60 < time(NULL); | ||
102 | } | ||
54 | 103 | ||
55 | path = fmt("%s/%s", ctx.cfg.cache_root, | 104 | /* Check if the slot has been modified since we opened it. |
56 | cache_safe_filename(ctx.repo->url)); | 105 | * NB: If stat() fails, we pretend the file is modified. |
106 | */ | ||
107 | static int is_modified(struct cache_slot *slot) | ||
108 | { | ||
109 | struct stat st; | ||
57 | 110 | ||
58 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 111 | if (stat(slot->cache_name, &st)) |
59 | return 0; | 112 | return 1; |
113 | return (st.st_ino != slot->cache_st.st_ino || | ||
114 | st.st_mtime != slot->cache_st.st_mtime || | ||
115 | st.st_size != slot->cache_st.st_size); | ||
116 | } | ||
60 | 117 | ||
61 | if (ctx.qry.page) { | 118 | /* Close an open lockfile */ |
62 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, | 119 | static void close_lock(struct cache_slot *slot) |
63 | cache_safe_filename(ctx.repo->url), | 120 | { |
64 | ctx.qry.page); | 121 | if (slot->lock_fd > 0) { |
65 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 122 | close(slot->lock_fd); |
66 | return 0; | 123 | slot->lock_fd = -1; |
67 | } | 124 | } |
68 | return 1; | ||
69 | } | 125 | } |
70 | 126 | ||
71 | int cache_refill_overdue(const char *lockfile) | 127 | /* Create a lockfile used to store the generated content for a cache |
128 | * slot, and write the slot key + \0 into it. | ||
129 | * Returns 0 on success and errno otherwise. | ||
130 | */ | ||
131 | static int lock_slot(struct cache_slot *slot) | ||
72 | { | 132 | { |
73 | struct stat st; | 133 | slot->lock_fd = open(slot->lock_name, O_RDWR|O_CREAT|O_EXCL, |
134 | S_IRUSR|S_IWUSR); | ||
135 | if (slot->lock_fd == -1) | ||
136 | return errno; | ||
137 | write(slot->lock_fd, slot->key, slot->keylen + 1); | ||
138 | return 0; | ||
139 | } | ||
74 | 140 | ||
75 | if (stat(lockfile, &st)) | 141 | /* Release the current lockfile. If `replace_old_slot` is set the |
76 | return 0; | 142 | * lockfile replaces the old cache slot, otherwise the lockfile is |
143 | * just deleted. | ||
144 | */ | ||
145 | static int unlock_slot(struct cache_slot *slot, int replace_old_slot) | ||
146 | { | ||
147 | int err; | ||
148 | |||
149 | if (replace_old_slot) | ||
150 | err = rename(slot->lock_name, slot->cache_name); | ||
77 | else | 151 | else |
78 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); | 152 | err = unlink(slot->lock_name); |
153 | return err; | ||
79 | } | 154 | } |
80 | 155 | ||
81 | int cache_lock(struct cacheitem *item) | 156 | /* Generate the content for the current cache slot by redirecting |
157 | * stdout to the lock-fd and invoking the callback function | ||
158 | */ | ||
159 | static int fill_slot(struct cache_slot *slot) | ||
82 | { | 160 | { |
83 | int i = 0; | 161 | int tmp; |
84 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | ||
85 | 162 | ||
86 | top: | 163 | /* Preserve stdout */ |
87 | if (++i > ctx.cfg.max_lock_attempts) | 164 | tmp = dup(STDOUT_FILENO); |
88 | die("cache_lock: unable to lock %s: %s", | 165 | if (tmp == -1) |
89 | item->name, strerror(errno)); | 166 | return errno; |
90 | 167 | ||
91 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 168 | /* Redirect stdout to lockfile */ |
169 | if (dup2(slot->lock_fd, STDOUT_FILENO) == -1) | ||
170 | return errno; | ||
92 | 171 | ||
93 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 172 | /* Generate cache content */ |
94 | goto top; | 173 | slot->fn(slot->cbdata); |
95 | 174 | ||
96 | if (item->fd == NOLOCK && errno == EEXIST && | 175 | /* Restore stdout */ |
97 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 176 | if (dup2(tmp, STDOUT_FILENO) == -1) |
98 | goto top; | 177 | return errno; |
99 | 178 | ||
100 | free(lockfile); | 179 | /* Close the temporary filedescriptor */ |
101 | return (item->fd > 0); | 180 | close(tmp); |
181 | return 0; | ||
102 | } | 182 | } |
103 | 183 | ||
104 | int cache_unlock(struct cacheitem *item) | 184 | /* Crude implementation of 32-bit FNV-1 hash algorithm, |
185 | * see http://www.isthe.com/chongo/tech/comp/fnv/ for details | ||
186 | * about the magic numbers. | ||
187 | */ | ||
188 | #define FNV_OFFSET 0x811c9dc5 | ||
189 | #define FNV_PRIME 0x01000193 | ||
190 | |||
191 | unsigned long hash_str(const char *str) | ||
105 | { | 192 | { |
106 | close(item->fd); | 193 | unsigned long h = FNV_OFFSET; |
107 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 194 | unsigned char *s = (unsigned char *)str; |
195 | |||
196 | if (!s) | ||
197 | return h; | ||
198 | |||
199 | while(*s) { | ||
200 | h *= FNV_PRIME; | ||
201 | h ^= *s++; | ||
202 | } | ||
203 | return h; | ||
108 | } | 204 | } |
109 | 205 | ||
110 | int cache_cancel_lock(struct cacheitem *item) | 206 | static int process_slot(struct cache_slot *slot) |
111 | { | 207 | { |
112 | return (unlink(fmt("%s.lock", item->name)) == 0); | 208 | int err; |
209 | |||
210 | err = open_slot(slot); | ||
211 | if (!err && slot->match) { | ||
212 | if (is_expired(slot)) { | ||
213 | if (!lock_slot(slot)) { | ||
214 | /* If the cachefile has been replaced between | ||
215 | * `open_slot` and `lock_slot`, we'll just | ||
216 | * serve the stale content from the original | ||
217 | * cachefile. This way we avoid pruning the | ||
218 | * newly generated slot. The same code-path | ||
219 | * is chosen if fill_slot() fails for some | ||
220 | * reason. | ||
221 | * | ||
222 | * TODO? check if the new slot contains the | ||
223 | * same key as the old one, since we would | ||
224 | * prefer to serve the newest content. | ||
225 | * This will require us to open yet another | ||
226 | * file-descriptor and read and compare the | ||
227 | * key from the new file, so for now we're | ||
228 | * lazy and just ignore the new file. | ||
229 | */ | ||
230 | if (is_modified(slot) || fill_slot(slot)) { | ||
231 | unlock_slot(slot, 0); | ||
232 | close_lock(slot); | ||
233 | } else { | ||
234 | close_slot(slot); | ||
235 | unlock_slot(slot, 1); | ||
236 | slot->cache_fd = slot->lock_fd; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | print_slot(slot); | ||
241 | close_slot(slot); | ||
242 | return 0; | ||
243 | } | ||
244 | |||
245 | /* If the cache slot does not exist (or its key doesn't match the | ||
246 | * current key), lets try to create a new cache slot for this | ||
247 | * request. If this fails (for whatever reason), lets just generate | ||
248 | * the content without caching it and fool the caller to belive | ||
249 | * everything worked out (but print a warning on stdout). | ||
250 | */ | ||
251 | |||
252 | close_slot(slot); | ||
253 | if ((err = lock_slot(slot)) != 0) { | ||
254 | cache_log("[cgit] Unable to lock slot %s: %s (%d)\n", | ||
255 | slot->lock_name, strerror(err), err); | ||
256 | slot->fn(slot->cbdata); | ||
257 | return 0; | ||
258 | } | ||
259 | |||
260 | if ((err = fill_slot(slot)) != 0) { | ||
261 | cache_log("[cgit] Unable to fill slot %s: %s (%d)\n", | ||
262 | slot->lock_name, strerror(err), err); | ||
263 | unlock_slot(slot, 0); | ||
264 | close_lock(slot); | ||
265 | slot->fn(slot->cbdata); | ||
266 | return 0; | ||
267 | } | ||
268 | // We've got a valid cache slot in the lock file, which | ||
269 | // is about to replace the old cache slot. But if we | ||
270 | // release the lockfile and then try to open the new cache | ||
271 | // slot, we might get a race condition with a concurrent | ||
272 | // writer for the same cache slot (with a different key). | ||
273 | // Lets avoid such a race by just printing the content of | ||
274 | // the lock file. | ||
275 | slot->cache_fd = slot->lock_fd; | ||
276 | unlock_slot(slot, 1); | ||
277 | err = print_slot(slot); | ||
278 | close_slot(slot); | ||
279 | return err; | ||
113 | } | 280 | } |
114 | 281 | ||
115 | int cache_expired(struct cacheitem *item) | 282 | /* Print cached content to stdout, generate the content if necessary. */ |
283 | int cache_process(int size, const char *path, const char *key, int ttl, | ||
284 | cache_fill_fn fn, void *cbdata) | ||
116 | { | 285 | { |
117 | if (item->ttl < 0) | 286 | unsigned long hash; |
287 | int len, i; | ||
288 | char filename[1024]; | ||
289 | char lockname[1024 + 5]; /* 5 = ".lock" */ | ||
290 | struct cache_slot slot; | ||
291 | |||
292 | /* If the cache is disabled, just generate the content */ | ||
293 | if (size <= 0) { | ||
294 | fn(cbdata); | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | /* Verify input, calculate filenames */ | ||
299 | if (!path) { | ||
300 | cache_log("[cgit] Cache path not specified, caching is disabled\n"); | ||
301 | fn(cbdata); | ||
118 | return 0; | 302 | return 0; |
119 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 303 | } |
304 | len = strlen(path); | ||
305 | if (len > sizeof(filename) - 10) { /* 10 = "/01234567\0" */ | ||
306 | cache_log("[cgit] Cache path too long, caching is disabled: %s\n", | ||
307 | path); | ||
308 | fn(cbdata); | ||
309 | return 0; | ||
310 | } | ||
311 | if (!key) | ||
312 | key = ""; | ||
313 | hash = hash_str(key) % size; | ||
314 | strcpy(filename, path); | ||
315 | if (filename[len - 1] != '/') | ||
316 | filename[len++] = '/'; | ||
317 | for(i = 0; i < 8; i++) { | ||
318 | sprintf(filename + len++, "%x", | ||
319 | (unsigned char)(hash & 0xf)); | ||
320 | hash >>= 4; | ||
321 | } | ||
322 | filename[len] = '\0'; | ||
323 | strcpy(lockname, filename); | ||
324 | strcpy(lockname + len, ".lock"); | ||
325 | slot.fn = fn; | ||
326 | slot.cbdata = cbdata; | ||
327 | slot.ttl = ttl; | ||
328 | slot.cache_name = filename; | ||
329 | slot.lock_name = lockname; | ||
330 | slot.key = key; | ||
331 | slot.keylen = strlen(key); | ||
332 | return process_slot(&slot); | ||
120 | } | 333 | } |
334 | |||
335 | /* Return a strftime formatted date/time | ||
336 | * NB: the result from this function is to shared memory | ||
337 | */ | ||
338 | char *sprintftime(const char *format, time_t time) | ||
339 | { | ||
340 | static char buf[64]; | ||
341 | struct tm *tm; | ||
342 | |||
343 | if (!time) | ||
344 | return NULL; | ||
345 | tm = gmtime(&time); | ||
346 | strftime(buf, sizeof(buf)-1, format, tm); | ||
347 | return buf; | ||
348 | } | ||
349 | |||
350 | int cache_ls(const char *path) | ||
351 | { | ||
352 | DIR *dir; | ||
353 | struct dirent *ent; | ||
354 | int err = 0; | ||
355 | struct cache_slot slot; | ||
356 | char fullname[1024]; | ||
357 | char *name; | ||
358 | |||
359 | if (!path) { | ||
360 | cache_log("[cgit] cache path not specified\n"); | ||
361 | return -1; | ||
362 | } | ||
363 | if (strlen(path) > 1024 - 10) { | ||
364 | cache_log("[cgit] cache path too long: %s\n", | ||
365 | path); | ||
366 | return -1; | ||
367 | } | ||
368 | dir = opendir(path); | ||
369 | if (!dir) { | ||
370 | err = errno; | ||
371 | cache_log("[cgit] unable to open path %s: %s (%d)\n", | ||
372 | path, strerror(err), err); | ||
373 | return err; | ||
374 | } | ||
375 | strcpy(fullname, path); | ||
376 | name = fullname + strlen(path); | ||
377 | if (*(name - 1) != '/') { | ||
378 | *name++ = '/'; | ||
379 | *name = '\0'; | ||
380 | } | ||
381 | slot.cache_name = fullname; | ||
382 | while((ent = readdir(dir)) != NULL) { | ||
383 | if (strlen(ent->d_name) != 8) | ||
384 | continue; | ||
385 | strcpy(name, ent->d_name); | ||
386 | if ((err = open_slot(&slot)) != 0) { | ||
387 | cache_log("[cgit] unable to open path %s: %s (%d)\n", | ||
388 | fullname, strerror(err), err); | ||
389 | continue; | ||
390 | } | ||
391 | printf("%s %s %10lld %s\n", | ||
392 | name, | ||
393 | sprintftime("%Y-%m-%d %H:%M:%S", | ||
394 | slot.cache_st.st_mtime), | ||
395 | slot.cache_st.st_size, | ||
396 | slot.buf); | ||
397 | close_slot(&slot); | ||
398 | } | ||
399 | closedir(dir); | ||
400 | return 0; | ||
401 | } | ||
402 | |||
403 | /* Print a message to stdout */ | ||
404 | void cache_log(const char *format, ...) | ||
405 | { | ||
406 | va_list args; | ||
407 | va_start(args, format); | ||
408 | vfprintf(stderr, format, args); | ||
409 | va_end(args); | ||
410 | } | ||
411 | |||
@@ -1,23 +1,35 @@ | |||
1 | /* | 1 | /* |
2 | * Since git has it's own cache.h which we include, | 2 | * Since git has it's own cache.h which we include, |
3 | * lets test on CGIT_CACHE_H to avoid confusion | 3 | * lets test on CGIT_CACHE_H to avoid confusion |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #ifndef CGIT_CACHE_H | 6 | #ifndef CGIT_CACHE_H |
7 | #define CGIT_CACHE_H | 7 | #define CGIT_CACHE_H |
8 | 8 | ||
9 | struct cacheitem { | 9 | typedef void (*cache_fill_fn)(void *cbdata); |
10 | char *name; | 10 | |
11 | struct stat st; | 11 | |
12 | int ttl; | 12 | /* Print cached content to stdout, generate the content if necessary. |
13 | int fd; | 13 | * |
14 | }; | 14 | * Parameters |
15 | 15 | * size max number of cache files | |
16 | extern char *cache_safe_filename(const char *unsafe); | 16 | * path directory used to store cache files |
17 | extern int cache_lock(struct cacheitem *item); | 17 | * key the key used to lookup cache files |
18 | extern int cache_unlock(struct cacheitem *item); | 18 | * ttl max cache time in seconds for this key |
19 | extern int cache_cancel_lock(struct cacheitem *item); | 19 | * fn content generator function for this key |
20 | extern int cache_exist(struct cacheitem *item); | 20 | * cbdata user-supplied data to the content generator function |
21 | extern int cache_expired(struct cacheitem *item); | 21 | * |
22 | * Return value | ||
23 | * 0 indicates success, everyting else is an error | ||
24 | */ | ||
25 | extern int cache_process(int size, const char *path, const char *key, int ttl, | ||
26 | cache_fill_fn fn, void *cbdata); | ||
27 | |||
28 | |||
29 | /* List info about all cache entries on stdout */ | ||
30 | extern int cache_ls(const char *path); | ||
31 | |||
32 | /* Print a message to stdout */ | ||
33 | extern void cache_log(const char *format, ...); | ||
22 | 34 | ||
23 | #endif /* CGIT_CACHE_H */ | 35 | #endif /* CGIT_CACHE_H */ |
@@ -1,475 +1,383 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
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 | #include "cache.h" | 10 | #include "cache.h" |
11 | #include "cmd.h" | 11 | #include "cmd.h" |
12 | #include "configfile.h" | 12 | #include "configfile.h" |
13 | #include "html.h" | 13 | #include "html.h" |
14 | #include "ui-shared.h" | 14 | #include "ui-shared.h" |
15 | 15 | ||
16 | const char *cgit_version = CGIT_VERSION; | 16 | const char *cgit_version = CGIT_VERSION; |
17 | 17 | ||
18 | void config_cb(const char *name, const char *value) | 18 | void config_cb(const char *name, const char *value) |
19 | { | 19 | { |
20 | if (!strcmp(name, "root-title")) | 20 | if (!strcmp(name, "root-title")) |
21 | ctx.cfg.root_title = xstrdup(value); | 21 | ctx.cfg.root_title = xstrdup(value); |
22 | else if (!strcmp(name, "root-desc")) | 22 | else if (!strcmp(name, "root-desc")) |
23 | ctx.cfg.root_desc = xstrdup(value); | 23 | ctx.cfg.root_desc = xstrdup(value); |
24 | else if (!strcmp(name, "root-readme")) | 24 | else if (!strcmp(name, "root-readme")) |
25 | ctx.cfg.root_readme = xstrdup(value); | 25 | ctx.cfg.root_readme = xstrdup(value); |
26 | else if (!strcmp(name, "css")) | 26 | else if (!strcmp(name, "css")) |
27 | ctx.cfg.css = xstrdup(value); | 27 | ctx.cfg.css = xstrdup(value); |
28 | else if (!strcmp(name, "logo")) | 28 | else if (!strcmp(name, "logo")) |
29 | ctx.cfg.logo = xstrdup(value); | 29 | ctx.cfg.logo = xstrdup(value); |
30 | else if (!strcmp(name, "index-header")) | 30 | else if (!strcmp(name, "index-header")) |
31 | ctx.cfg.index_header = xstrdup(value); | 31 | ctx.cfg.index_header = xstrdup(value); |
32 | else if (!strcmp(name, "index-info")) | 32 | else if (!strcmp(name, "index-info")) |
33 | ctx.cfg.index_info = xstrdup(value); | 33 | ctx.cfg.index_info = xstrdup(value); |
34 | else if (!strcmp(name, "logo-link")) | 34 | else if (!strcmp(name, "logo-link")) |
35 | ctx.cfg.logo_link = xstrdup(value); | 35 | ctx.cfg.logo_link = xstrdup(value); |
36 | else if (!strcmp(name, "module-link")) | 36 | else if (!strcmp(name, "module-link")) |
37 | ctx.cfg.module_link = xstrdup(value); | 37 | ctx.cfg.module_link = xstrdup(value); |
38 | else if (!strcmp(name, "virtual-root")) { | 38 | else if (!strcmp(name, "virtual-root")) { |
39 | ctx.cfg.virtual_root = trim_end(value, '/'); | 39 | ctx.cfg.virtual_root = trim_end(value, '/'); |
40 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 40 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
41 | ctx.cfg.virtual_root = ""; | 41 | ctx.cfg.virtual_root = ""; |
42 | } else if (!strcmp(name, "nocache")) | 42 | } else if (!strcmp(name, "nocache")) |
43 | ctx.cfg.nocache = atoi(value); | 43 | ctx.cfg.nocache = atoi(value); |
44 | else if (!strcmp(name, "snapshots")) | 44 | else if (!strcmp(name, "snapshots")) |
45 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 45 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
46 | else if (!strcmp(name, "enable-index-links")) | 46 | else if (!strcmp(name, "enable-index-links")) |
47 | ctx.cfg.enable_index_links = atoi(value); | 47 | ctx.cfg.enable_index_links = atoi(value); |
48 | else if (!strcmp(name, "enable-log-filecount")) | 48 | else if (!strcmp(name, "enable-log-filecount")) |
49 | ctx.cfg.enable_log_filecount = atoi(value); | 49 | ctx.cfg.enable_log_filecount = atoi(value); |
50 | else if (!strcmp(name, "enable-log-linecount")) | 50 | else if (!strcmp(name, "enable-log-linecount")) |
51 | ctx.cfg.enable_log_linecount = atoi(value); | 51 | ctx.cfg.enable_log_linecount = atoi(value); |
52 | else if (!strcmp(name, "cache-size")) | ||
53 | ctx.cfg.cache_size = atoi(value); | ||
52 | else if (!strcmp(name, "cache-root")) | 54 | else if (!strcmp(name, "cache-root")) |
53 | ctx.cfg.cache_root = xstrdup(value); | 55 | ctx.cfg.cache_root = xstrdup(value); |
54 | else if (!strcmp(name, "cache-root-ttl")) | 56 | else if (!strcmp(name, "cache-root-ttl")) |
55 | ctx.cfg.cache_root_ttl = atoi(value); | 57 | ctx.cfg.cache_root_ttl = atoi(value); |
56 | else if (!strcmp(name, "cache-repo-ttl")) | 58 | else if (!strcmp(name, "cache-repo-ttl")) |
57 | ctx.cfg.cache_repo_ttl = atoi(value); | 59 | ctx.cfg.cache_repo_ttl = atoi(value); |
58 | else if (!strcmp(name, "cache-static-ttl")) | 60 | else if (!strcmp(name, "cache-static-ttl")) |
59 | ctx.cfg.cache_static_ttl = atoi(value); | 61 | ctx.cfg.cache_static_ttl = atoi(value); |
60 | else if (!strcmp(name, "cache-dynamic-ttl")) | 62 | else if (!strcmp(name, "cache-dynamic-ttl")) |
61 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 63 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
62 | else if (!strcmp(name, "max-message-length")) | 64 | else if (!strcmp(name, "max-message-length")) |
63 | ctx.cfg.max_msg_len = atoi(value); | 65 | ctx.cfg.max_msg_len = atoi(value); |
64 | else if (!strcmp(name, "max-repodesc-length")) | 66 | else if (!strcmp(name, "max-repodesc-length")) |
65 | ctx.cfg.max_repodesc_len = atoi(value); | 67 | ctx.cfg.max_repodesc_len = atoi(value); |
66 | else if (!strcmp(name, "max-commit-count")) | 68 | else if (!strcmp(name, "max-commit-count")) |
67 | ctx.cfg.max_commit_count = atoi(value); | 69 | ctx.cfg.max_commit_count = atoi(value); |
68 | else if (!strcmp(name, "summary-log")) | 70 | else if (!strcmp(name, "summary-log")) |
69 | ctx.cfg.summary_log = atoi(value); | 71 | ctx.cfg.summary_log = atoi(value); |
70 | else if (!strcmp(name, "summary-branches")) | 72 | else if (!strcmp(name, "summary-branches")) |
71 | ctx.cfg.summary_branches = atoi(value); | 73 | ctx.cfg.summary_branches = atoi(value); |
72 | else if (!strcmp(name, "summary-tags")) | 74 | else if (!strcmp(name, "summary-tags")) |
73 | ctx.cfg.summary_tags = atoi(value); | 75 | ctx.cfg.summary_tags = atoi(value); |
74 | else if (!strcmp(name, "agefile")) | 76 | else if (!strcmp(name, "agefile")) |
75 | ctx.cfg.agefile = xstrdup(value); | 77 | ctx.cfg.agefile = xstrdup(value); |
76 | else if (!strcmp(name, "renamelimit")) | 78 | else if (!strcmp(name, "renamelimit")) |
77 | ctx.cfg.renamelimit = atoi(value); | 79 | ctx.cfg.renamelimit = atoi(value); |
78 | else if (!strcmp(name, "robots")) | 80 | else if (!strcmp(name, "robots")) |
79 | ctx.cfg.robots = xstrdup(value); | 81 | ctx.cfg.robots = xstrdup(value); |
80 | else if (!strcmp(name, "clone-prefix")) | 82 | else if (!strcmp(name, "clone-prefix")) |
81 | ctx.cfg.clone_prefix = xstrdup(value); | 83 | ctx.cfg.clone_prefix = xstrdup(value); |
82 | else if (!strcmp(name, "repo.group")) | 84 | else if (!strcmp(name, "repo.group")) |
83 | ctx.cfg.repo_group = xstrdup(value); | 85 | ctx.cfg.repo_group = xstrdup(value); |
84 | else if (!strcmp(name, "repo.url")) | 86 | else if (!strcmp(name, "repo.url")) |
85 | ctx.repo = cgit_add_repo(value); | 87 | ctx.repo = cgit_add_repo(value); |
86 | else if (!strcmp(name, "repo.name")) | 88 | else if (!strcmp(name, "repo.name")) |
87 | ctx.repo->name = xstrdup(value); | 89 | ctx.repo->name = xstrdup(value); |
88 | else if (ctx.repo && !strcmp(name, "repo.path")) | 90 | else if (ctx.repo && !strcmp(name, "repo.path")) |
89 | ctx.repo->path = trim_end(value, '/'); | 91 | ctx.repo->path = trim_end(value, '/'); |
90 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 92 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
91 | ctx.repo->clone_url = xstrdup(value); | 93 | ctx.repo->clone_url = xstrdup(value); |
92 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 94 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
93 | ctx.repo->desc = xstrdup(value); | 95 | ctx.repo->desc = xstrdup(value); |
94 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 96 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
95 | ctx.repo->owner = xstrdup(value); | 97 | ctx.repo->owner = xstrdup(value); |
96 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 98 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
97 | ctx.repo->defbranch = xstrdup(value); | 99 | ctx.repo->defbranch = xstrdup(value); |
98 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 100 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
99 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 101 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
100 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 102 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
101 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 103 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
102 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 104 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
103 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 105 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
104 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 106 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
105 | ctx.repo->module_link= xstrdup(value); | 107 | ctx.repo->module_link= xstrdup(value); |
106 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 108 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
107 | if (*value == '/') | 109 | if (*value == '/') |
108 | ctx.repo->readme = xstrdup(value); | 110 | ctx.repo->readme = xstrdup(value); |
109 | else | 111 | else |
110 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 112 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
111 | } else if (!strcmp(name, "include")) | 113 | } else if (!strcmp(name, "include")) |
112 | parse_configfile(value, config_cb); | 114 | parse_configfile(value, config_cb); |
113 | } | 115 | } |
114 | 116 | ||
115 | static void querystring_cb(const char *name, const char *value) | 117 | static void querystring_cb(const char *name, const char *value) |
116 | { | 118 | { |
117 | if (!strcmp(name,"r")) { | 119 | if (!strcmp(name,"r")) { |
118 | ctx.qry.repo = xstrdup(value); | 120 | ctx.qry.repo = xstrdup(value); |
119 | ctx.repo = cgit_get_repoinfo(value); | 121 | ctx.repo = cgit_get_repoinfo(value); |
120 | } else if (!strcmp(name, "p")) { | 122 | } else if (!strcmp(name, "p")) { |
121 | ctx.qry.page = xstrdup(value); | 123 | ctx.qry.page = xstrdup(value); |
122 | } else if (!strcmp(name, "url")) { | 124 | } else if (!strcmp(name, "url")) { |
123 | cgit_parse_url(value); | 125 | cgit_parse_url(value); |
124 | } else if (!strcmp(name, "qt")) { | 126 | } else if (!strcmp(name, "qt")) { |
125 | ctx.qry.grep = xstrdup(value); | 127 | ctx.qry.grep = xstrdup(value); |
126 | } else if (!strcmp(name, "q")) { | 128 | } else if (!strcmp(name, "q")) { |
127 | ctx.qry.search = xstrdup(value); | 129 | ctx.qry.search = xstrdup(value); |
128 | } else if (!strcmp(name, "h")) { | 130 | } else if (!strcmp(name, "h")) { |
129 | ctx.qry.head = xstrdup(value); | 131 | ctx.qry.head = xstrdup(value); |
130 | ctx.qry.has_symref = 1; | 132 | ctx.qry.has_symref = 1; |
131 | } else if (!strcmp(name, "id")) { | 133 | } else if (!strcmp(name, "id")) { |
132 | ctx.qry.sha1 = xstrdup(value); | 134 | ctx.qry.sha1 = xstrdup(value); |
133 | ctx.qry.has_sha1 = 1; | 135 | ctx.qry.has_sha1 = 1; |
134 | } else if (!strcmp(name, "id2")) { | 136 | } else if (!strcmp(name, "id2")) { |
135 | ctx.qry.sha2 = xstrdup(value); | 137 | ctx.qry.sha2 = xstrdup(value); |
136 | ctx.qry.has_sha1 = 1; | 138 | ctx.qry.has_sha1 = 1; |
137 | } else if (!strcmp(name, "ofs")) { | 139 | } else if (!strcmp(name, "ofs")) { |
138 | ctx.qry.ofs = atoi(value); | 140 | ctx.qry.ofs = atoi(value); |
139 | } else if (!strcmp(name, "path")) { | 141 | } else if (!strcmp(name, "path")) { |
140 | ctx.qry.path = trim_end(value, '/'); | 142 | ctx.qry.path = trim_end(value, '/'); |
141 | } else if (!strcmp(name, "name")) { | 143 | } else if (!strcmp(name, "name")) { |
142 | ctx.qry.name = xstrdup(value); | 144 | ctx.qry.name = xstrdup(value); |
143 | } | 145 | } |
144 | } | 146 | } |
145 | 147 | ||
146 | static void prepare_context(struct cgit_context *ctx) | 148 | static void prepare_context(struct cgit_context *ctx) |
147 | { | 149 | { |
148 | memset(ctx, 0, sizeof(ctx)); | 150 | memset(ctx, 0, sizeof(ctx)); |
149 | ctx->cfg.agefile = "info/web/last-modified"; | 151 | ctx->cfg.agefile = "info/web/last-modified"; |
152 | ctx->cfg.nocache = 0; | ||
153 | ctx->cfg.cache_size = 0; | ||
150 | ctx->cfg.cache_dynamic_ttl = 5; | 154 | ctx->cfg.cache_dynamic_ttl = 5; |
151 | ctx->cfg.cache_max_create_time = 5; | 155 | ctx->cfg.cache_max_create_time = 5; |
152 | ctx->cfg.cache_repo_ttl = 5; | 156 | ctx->cfg.cache_repo_ttl = 5; |
153 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 157 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
154 | ctx->cfg.cache_root_ttl = 5; | 158 | ctx->cfg.cache_root_ttl = 5; |
155 | ctx->cfg.cache_static_ttl = -1; | 159 | ctx->cfg.cache_static_ttl = -1; |
156 | ctx->cfg.css = "/cgit.css"; | 160 | ctx->cfg.css = "/cgit.css"; |
157 | ctx->cfg.logo = "/git-logo.png"; | 161 | ctx->cfg.logo = "/git-logo.png"; |
158 | ctx->cfg.max_commit_count = 50; | 162 | ctx->cfg.max_commit_count = 50; |
159 | ctx->cfg.max_lock_attempts = 5; | 163 | ctx->cfg.max_lock_attempts = 5; |
160 | ctx->cfg.max_msg_len = 60; | 164 | ctx->cfg.max_msg_len = 60; |
161 | ctx->cfg.max_repodesc_len = 60; | 165 | ctx->cfg.max_repodesc_len = 60; |
162 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 166 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
163 | ctx->cfg.renamelimit = -1; | 167 | ctx->cfg.renamelimit = -1; |
164 | ctx->cfg.robots = "index, nofollow"; | 168 | ctx->cfg.robots = "index, nofollow"; |
165 | ctx->cfg.root_title = "Git repository browser"; | 169 | ctx->cfg.root_title = "Git repository browser"; |
166 | ctx->cfg.root_desc = "a fast webinterface for the git dscm"; | 170 | ctx->cfg.root_desc = "a fast webinterface for the git dscm"; |
167 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 171 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
168 | ctx->page.mimetype = "text/html"; | 172 | ctx->page.mimetype = "text/html"; |
169 | ctx->page.charset = PAGE_ENCODING; | 173 | ctx->page.charset = PAGE_ENCODING; |
170 | ctx->page.filename = NULL; | 174 | ctx->page.filename = NULL; |
171 | } | 175 | ctx->page.modified = time(NULL); |
172 | 176 | ctx->page.expires = ctx->page.modified; | |
173 | static int cgit_prepare_cache(struct cacheitem *item) | ||
174 | { | ||
175 | if (!ctx.repo && ctx.qry.repo) { | ||
176 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | ||
177 | "Bad request"); | ||
178 | cgit_print_http_headers(&ctx); | ||
179 | cgit_print_docstart(&ctx); | ||
180 | cgit_print_pageheader(&ctx); | ||
181 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | ||
182 | cgit_print_docend(); | ||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | if (!ctx.repo) { | ||
187 | item->name = xstrdup(fmt("%s/index.%s.html", | ||
188 | ctx.cfg.cache_root, | ||
189 | cache_safe_filename(ctx.qry.raw))); | ||
190 | item->ttl = ctx.cfg.cache_root_ttl; | ||
191 | return 1; | ||
192 | } | ||
193 | |||
194 | if (!ctx.qry.page) { | ||
195 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | ||
196 | cache_safe_filename(ctx.repo->url), | ||
197 | cache_safe_filename(ctx.qry.raw))); | ||
198 | item->ttl = ctx.cfg.cache_repo_ttl; | ||
199 | } else { | ||
200 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | ||
201 | cache_safe_filename(ctx.repo->url), | ||
202 | ctx.qry.page, | ||
203 | cache_safe_filename(ctx.qry.raw))); | ||
204 | if (ctx.qry.has_symref) | ||
205 | item->ttl = ctx.cfg.cache_dynamic_ttl; | ||
206 | else if (ctx.qry.has_sha1) | ||
207 | item->ttl = ctx.cfg.cache_static_ttl; | ||
208 | else | ||
209 | item->ttl = ctx.cfg.cache_repo_ttl; | ||
210 | } | ||
211 | return 1; | ||
212 | } | 177 | } |
213 | 178 | ||
214 | struct refmatch { | 179 | struct refmatch { |
215 | char *req_ref; | 180 | char *req_ref; |
216 | char *first_ref; | 181 | char *first_ref; |
217 | int match; | 182 | int match; |
218 | }; | 183 | }; |
219 | 184 | ||
220 | int find_current_ref(const char *refname, const unsigned char *sha1, | 185 | int find_current_ref(const char *refname, const unsigned char *sha1, |
221 | int flags, void *cb_data) | 186 | int flags, void *cb_data) |
222 | { | 187 | { |
223 | struct refmatch *info; | 188 | struct refmatch *info; |
224 | 189 | ||
225 | info = (struct refmatch *)cb_data; | 190 | info = (struct refmatch *)cb_data; |
226 | if (!strcmp(refname, info->req_ref)) | 191 | if (!strcmp(refname, info->req_ref)) |
227 | info->match = 1; | 192 | info->match = 1; |
228 | if (!info->first_ref) | 193 | if (!info->first_ref) |
229 | info->first_ref = xstrdup(refname); | 194 | info->first_ref = xstrdup(refname); |
230 | return info->match; | 195 | return info->match; |
231 | } | 196 | } |
232 | 197 | ||
233 | char *find_default_branch(struct cgit_repo *repo) | 198 | char *find_default_branch(struct cgit_repo *repo) |
234 | { | 199 | { |
235 | struct refmatch info; | 200 | struct refmatch info; |
236 | 201 | ||
237 | info.req_ref = repo->defbranch; | 202 | info.req_ref = repo->defbranch; |
238 | info.first_ref = NULL; | 203 | info.first_ref = NULL; |
239 | info.match = 0; | 204 | info.match = 0; |
240 | for_each_branch_ref(find_current_ref, &info); | 205 | for_each_branch_ref(find_current_ref, &info); |
241 | if (info.match) | 206 | if (info.match) |
242 | return info.req_ref; | 207 | return info.req_ref; |
243 | else | 208 | else |
244 | return info.first_ref; | 209 | return info.first_ref; |
245 | } | 210 | } |
246 | 211 | ||
247 | static int prepare_repo_cmd(struct cgit_context *ctx) | 212 | static int prepare_repo_cmd(struct cgit_context *ctx) |
248 | { | 213 | { |
249 | char *tmp; | 214 | char *tmp; |
250 | unsigned char sha1[20]; | 215 | unsigned char sha1[20]; |
251 | int nongit = 0; | 216 | int nongit = 0; |
252 | 217 | ||
253 | setenv("GIT_DIR", ctx->repo->path, 1); | 218 | setenv("GIT_DIR", ctx->repo->path, 1); |
254 | setup_git_directory_gently(&nongit); | 219 | setup_git_directory_gently(&nongit); |
255 | if (nongit) { | 220 | if (nongit) { |
256 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 221 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
257 | "config error"); | 222 | "config error"); |
258 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 223 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
259 | ctx->repo = NULL; | 224 | ctx->repo = NULL; |
260 | cgit_print_http_headers(ctx); | 225 | cgit_print_http_headers(ctx); |
261 | cgit_print_docstart(ctx); | 226 | cgit_print_docstart(ctx); |
262 | cgit_print_pageheader(ctx); | 227 | cgit_print_pageheader(ctx); |
263 | cgit_print_error(tmp); | 228 | cgit_print_error(tmp); |
264 | cgit_print_docend(); | 229 | cgit_print_docend(); |
265 | return 1; | 230 | return 1; |
266 | } | 231 | } |
267 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 232 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
268 | 233 | ||
269 | if (!ctx->qry.head) { | 234 | if (!ctx->qry.head) { |
270 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 235 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
271 | ctx->repo->defbranch = ctx->qry.head; | 236 | ctx->repo->defbranch = ctx->qry.head; |
272 | } | 237 | } |
273 | 238 | ||
274 | if (!ctx->qry.head) { | 239 | if (!ctx->qry.head) { |
275 | cgit_print_http_headers(ctx); | 240 | cgit_print_http_headers(ctx); |
276 | cgit_print_docstart(ctx); | 241 | cgit_print_docstart(ctx); |
277 | cgit_print_pageheader(ctx); | 242 | cgit_print_pageheader(ctx); |
278 | cgit_print_error("Repository seems to be empty"); | 243 | cgit_print_error("Repository seems to be empty"); |
279 | cgit_print_docend(); | 244 | cgit_print_docend(); |
280 | return 1; | 245 | return 1; |
281 | } | 246 | } |
282 | 247 | ||
283 | if (get_sha1(ctx->qry.head, sha1)) { | 248 | if (get_sha1(ctx->qry.head, sha1)) { |
284 | tmp = xstrdup(ctx->qry.head); | 249 | tmp = xstrdup(ctx->qry.head); |
285 | ctx->qry.head = ctx->repo->defbranch; | 250 | ctx->qry.head = ctx->repo->defbranch; |
286 | cgit_print_http_headers(ctx); | 251 | cgit_print_http_headers(ctx); |
287 | cgit_print_docstart(ctx); | 252 | cgit_print_docstart(ctx); |
288 | cgit_print_pageheader(ctx); | 253 | cgit_print_pageheader(ctx); |
289 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 254 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
290 | cgit_print_docend(); | 255 | cgit_print_docend(); |
291 | return 1; | 256 | return 1; |
292 | } | 257 | } |
293 | return 0; | 258 | return 0; |
294 | } | 259 | } |
295 | 260 | ||
296 | static void process_request(struct cgit_context *ctx) | 261 | static void process_request(void *cbdata) |
297 | { | 262 | { |
263 | struct cgit_context *ctx = cbdata; | ||
298 | struct cgit_cmd *cmd; | 264 | struct cgit_cmd *cmd; |
299 | 265 | ||
300 | cmd = cgit_get_cmd(ctx); | 266 | cmd = cgit_get_cmd(ctx); |
301 | if (!cmd) { | 267 | if (!cmd) { |
302 | ctx->page.title = "cgit error"; | 268 | ctx->page.title = "cgit error"; |
303 | ctx->repo = NULL; | 269 | ctx->repo = NULL; |
304 | cgit_print_http_headers(ctx); | 270 | cgit_print_http_headers(ctx); |
305 | cgit_print_docstart(ctx); | 271 | cgit_print_docstart(ctx); |
306 | cgit_print_pageheader(ctx); | 272 | cgit_print_pageheader(ctx); |
307 | cgit_print_error("Invalid request"); | 273 | cgit_print_error("Invalid request"); |
308 | cgit_print_docend(); | 274 | cgit_print_docend(); |
309 | return; | 275 | return; |
310 | } | 276 | } |
311 | 277 | ||
312 | if (cmd->want_repo && !ctx->repo) { | 278 | if (cmd->want_repo && !ctx->repo) { |
313 | cgit_print_http_headers(ctx); | 279 | cgit_print_http_headers(ctx); |
314 | cgit_print_docstart(ctx); | 280 | cgit_print_docstart(ctx); |
315 | cgit_print_pageheader(ctx); | 281 | cgit_print_pageheader(ctx); |
316 | cgit_print_error(fmt("No repository selected")); | 282 | cgit_print_error(fmt("No repository selected")); |
317 | cgit_print_docend(); | 283 | cgit_print_docend(); |
318 | return; | 284 | return; |
319 | } | 285 | } |
320 | 286 | ||
321 | if (ctx->repo && prepare_repo_cmd(ctx)) | 287 | if (ctx->repo && prepare_repo_cmd(ctx)) |
322 | return; | 288 | return; |
323 | 289 | ||
324 | if (cmd->want_layout) { | 290 | if (cmd->want_layout) { |
325 | cgit_print_http_headers(ctx); | 291 | cgit_print_http_headers(ctx); |
326 | cgit_print_docstart(ctx); | 292 | cgit_print_docstart(ctx); |
327 | cgit_print_pageheader(ctx); | 293 | cgit_print_pageheader(ctx); |
328 | } | 294 | } |
329 | 295 | ||
330 | cmd->fn(ctx); | 296 | cmd->fn(ctx); |
331 | 297 | ||
332 | if (cmd->want_layout) | 298 | if (cmd->want_layout) |
333 | cgit_print_docend(); | 299 | cgit_print_docend(); |
334 | } | 300 | } |
335 | 301 | ||
336 | static long ttl_seconds(long ttl) | ||
337 | { | ||
338 | if (ttl<0) | ||
339 | return 60 * 60 * 24 * 365; | ||
340 | else | ||
341 | return ttl * 60; | ||
342 | } | ||
343 | |||
344 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | ||
345 | { | ||
346 | int stdout2; | ||
347 | |||
348 | if (use_cache) { | ||
349 | stdout2 = chk_positive(dup(STDOUT_FILENO), | ||
350 | "Preserving STDOUT"); | ||
351 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | ||
352 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | ||
353 | } | ||
354 | |||
355 | ctx.page.modified = time(NULL); | ||
356 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | ||
357 | process_request(&ctx); | ||
358 | |||
359 | if (use_cache) { | ||
360 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | ||
361 | chk_positive(dup2(stdout2, STDOUT_FILENO), | ||
362 | "Restoring original STDOUT"); | ||
363 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | ||
364 | } | ||
365 | } | ||
366 | |||
367 | static void cgit_check_cache(struct cacheitem *item) | ||
368 | { | ||
369 | int i = 0; | ||
370 | |||
371 | top: | ||
372 | if (++i > ctx.cfg.max_lock_attempts) { | ||
373 | die("cgit_refresh_cache: unable to lock %s: %s", | ||
374 | item->name, strerror(errno)); | ||
375 | } | ||
376 | if (!cache_exist(item)) { | ||
377 | if (!cache_lock(item)) { | ||
378 | sleep(1); | ||
379 | goto top; | ||
380 | } | ||
381 | if (!cache_exist(item)) { | ||
382 | cgit_fill_cache(item, 1); | ||
383 | cache_unlock(item); | ||
384 | } else { | ||
385 | cache_cancel_lock(item); | ||
386 | } | ||
387 | } else if (cache_expired(item) && cache_lock(item)) { | ||
388 | if (cache_expired(item)) { | ||
389 | cgit_fill_cache(item, 1); | ||
390 | cache_unlock(item); | ||
391 | } else { | ||
392 | cache_cancel_lock(item); | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | static void cgit_print_cache(struct cacheitem *item) | ||
398 | { | ||
399 | static char buf[4096]; | ||
400 | ssize_t i; | ||
401 | |||
402 | int fd = open(item->name, O_RDONLY); | ||
403 | if (fd<0) | ||
404 | die("Unable to open cached file %s", item->name); | ||
405 | |||
406 | while((i=read(fd, buf, sizeof(buf))) > 0) | ||
407 | write(STDOUT_FILENO, buf, i); | ||
408 | |||
409 | close(fd); | ||
410 | } | ||
411 | |||
412 | static void cgit_parse_args(int argc, const char **argv) | 302 | static void cgit_parse_args(int argc, const char **argv) |
413 | { | 303 | { |
414 | int i; | 304 | int i; |
415 | 305 | ||
416 | for (i = 1; i < argc; i++) { | 306 | for (i = 1; i < argc; i++) { |
417 | if (!strncmp(argv[i], "--cache=", 8)) { | 307 | if (!strncmp(argv[i], "--cache=", 8)) { |
418 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 308 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
419 | } | 309 | } |
420 | if (!strcmp(argv[i], "--nocache")) { | 310 | if (!strcmp(argv[i], "--nocache")) { |
421 | ctx.cfg.nocache = 1; | 311 | ctx.cfg.nocache = 1; |
422 | } | 312 | } |
423 | if (!strncmp(argv[i], "--query=", 8)) { | 313 | if (!strncmp(argv[i], "--query=", 8)) { |
424 | ctx.qry.raw = xstrdup(argv[i]+8); | 314 | ctx.qry.raw = xstrdup(argv[i]+8); |
425 | } | 315 | } |
426 | if (!strncmp(argv[i], "--repo=", 7)) { | 316 | if (!strncmp(argv[i], "--repo=", 7)) { |
427 | ctx.qry.repo = xstrdup(argv[i]+7); | 317 | ctx.qry.repo = xstrdup(argv[i]+7); |
428 | } | 318 | } |
429 | if (!strncmp(argv[i], "--page=", 7)) { | 319 | if (!strncmp(argv[i], "--page=", 7)) { |
430 | ctx.qry.page = xstrdup(argv[i]+7); | 320 | ctx.qry.page = xstrdup(argv[i]+7); |
431 | } | 321 | } |
432 | if (!strncmp(argv[i], "--head=", 7)) { | 322 | if (!strncmp(argv[i], "--head=", 7)) { |
433 | ctx.qry.head = xstrdup(argv[i]+7); | 323 | ctx.qry.head = xstrdup(argv[i]+7); |
434 | ctx.qry.has_symref = 1; | 324 | ctx.qry.has_symref = 1; |
435 | } | 325 | } |
436 | if (!strncmp(argv[i], "--sha1=", 7)) { | 326 | if (!strncmp(argv[i], "--sha1=", 7)) { |
437 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 327 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
438 | ctx.qry.has_sha1 = 1; | 328 | ctx.qry.has_sha1 = 1; |
439 | } | 329 | } |
440 | if (!strncmp(argv[i], "--ofs=", 6)) { | 330 | if (!strncmp(argv[i], "--ofs=", 6)) { |
441 | ctx.qry.ofs = atoi(argv[i]+6); | 331 | ctx.qry.ofs = atoi(argv[i]+6); |
442 | } | 332 | } |
443 | } | 333 | } |
444 | } | 334 | } |
445 | 335 | ||
336 | static int calc_ttl() | ||
337 | { | ||
338 | if (!ctx.repo) | ||
339 | return ctx.cfg.cache_root_ttl; | ||
340 | |||
341 | if (!ctx.qry.page) | ||
342 | return ctx.cfg.cache_repo_ttl; | ||
343 | |||
344 | if (ctx.qry.has_symref) | ||
345 | return ctx.cfg.cache_dynamic_ttl; | ||
346 | |||
347 | if (ctx.qry.has_sha1) | ||
348 | return ctx.cfg.cache_static_ttl; | ||
349 | |||
350 | return ctx.cfg.cache_repo_ttl; | ||
351 | } | ||
352 | |||
446 | int main(int argc, const char **argv) | 353 | int main(int argc, const char **argv) |
447 | { | 354 | { |
448 | struct cacheitem item; | ||
449 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 355 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
356 | int err, ttl; | ||
450 | 357 | ||
451 | prepare_context(&ctx); | 358 | prepare_context(&ctx); |
452 | item.st.st_mtime = time(NULL); | ||
453 | cgit_repolist.length = 0; | 359 | cgit_repolist.length = 0; |
454 | cgit_repolist.count = 0; | 360 | cgit_repolist.count = 0; |
455 | cgit_repolist.repos = NULL; | 361 | cgit_repolist.repos = NULL; |
456 | 362 | ||
457 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 363 | parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
458 | config_cb); | 364 | config_cb); |
459 | ctx.repo = NULL; | 365 | ctx.repo = NULL; |
460 | if (getenv("SCRIPT_NAME")) | 366 | if (getenv("SCRIPT_NAME")) |
461 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 367 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
462 | if (getenv("QUERY_STRING")) | 368 | if (getenv("QUERY_STRING")) |
463 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 369 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
464 | cgit_parse_args(argc, argv); | 370 | cgit_parse_args(argc, argv); |
465 | http_parse_querystring(ctx.qry.raw, querystring_cb); | 371 | http_parse_querystring(ctx.qry.raw, querystring_cb); |
466 | if (!cgit_prepare_cache(&item)) | 372 | |
467 | return 0; | 373 | ttl = calc_ttl(); |
468 | if (ctx.cfg.nocache) { | 374 | ctx.page.expires += ttl*60; |
469 | cgit_fill_cache(&item, 0); | 375 | if (ctx.cfg.nocache) |
470 | } else { | 376 | ctx.cfg.cache_size = 0; |
471 | cgit_check_cache(&item); | 377 | err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, |
472 | cgit_print_cache(&item); | 378 | ctx.qry.raw, ttl, process_request, &ctx); |
473 | } | 379 | if (err) |
474 | return 0; | 380 | cache_log("[cgit] error %d - %s\n", |
381 | err, strerror(err)); | ||
382 | return err; | ||
475 | } | 383 | } |
@@ -11,223 +11,224 @@ | |||
11 | #include <tag.h> | 11 | #include <tag.h> |
12 | #include <diff.h> | 12 | #include <diff.h> |
13 | #include <diffcore.h> | 13 | #include <diffcore.h> |
14 | #include <refs.h> | 14 | #include <refs.h> |
15 | #include <revision.h> | 15 | #include <revision.h> |
16 | #include <log-tree.h> | 16 | #include <log-tree.h> |
17 | #include <archive.h> | 17 | #include <archive.h> |
18 | #include <xdiff/xdiff.h> | 18 | #include <xdiff/xdiff.h> |
19 | #include <utf8.h> | 19 | #include <utf8.h> |
20 | 20 | ||
21 | 21 | ||
22 | /* | 22 | /* |
23 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
24 | */ | 24 | */ |
25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
26 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
27 | 27 | ||
28 | 28 | ||
29 | /* | 29 | /* |
30 | * Limits used for relative dates | 30 | * Limits used for relative dates |
31 | */ | 31 | */ |
32 | #define TM_MIN 60 | 32 | #define TM_MIN 60 |
33 | #define TM_HOUR (TM_MIN * 60) | 33 | #define TM_HOUR (TM_MIN * 60) |
34 | #define TM_DAY (TM_HOUR * 24) | 34 | #define TM_DAY (TM_HOUR * 24) |
35 | #define TM_WEEK (TM_DAY * 7) | 35 | #define TM_WEEK (TM_DAY * 7) |
36 | #define TM_YEAR (TM_DAY * 365) | 36 | #define TM_YEAR (TM_DAY * 365) |
37 | #define TM_MONTH (TM_YEAR / 12.0) | 37 | #define TM_MONTH (TM_YEAR / 12.0) |
38 | 38 | ||
39 | 39 | ||
40 | /* | 40 | /* |
41 | * Default encoding | 41 | * Default encoding |
42 | */ | 42 | */ |
43 | #define PAGE_ENCODING "UTF-8" | 43 | #define PAGE_ENCODING "UTF-8" |
44 | 44 | ||
45 | typedef void (*configfn)(const char *name, const char *value); | 45 | typedef void (*configfn)(const char *name, const char *value); |
46 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 46 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
47 | typedef void (*linediff_fn)(char *line, int len); | 47 | typedef void (*linediff_fn)(char *line, int len); |
48 | 48 | ||
49 | struct cgit_repo { | 49 | struct cgit_repo { |
50 | char *url; | 50 | char *url; |
51 | char *name; | 51 | char *name; |
52 | char *path; | 52 | char *path; |
53 | char *desc; | 53 | char *desc; |
54 | char *owner; | 54 | char *owner; |
55 | char *defbranch; | 55 | char *defbranch; |
56 | char *group; | 56 | char *group; |
57 | char *module_link; | 57 | char *module_link; |
58 | char *readme; | 58 | char *readme; |
59 | char *clone_url; | 59 | char *clone_url; |
60 | int snapshots; | 60 | int snapshots; |
61 | int enable_log_filecount; | 61 | int enable_log_filecount; |
62 | int enable_log_linecount; | 62 | int enable_log_linecount; |
63 | }; | 63 | }; |
64 | 64 | ||
65 | struct cgit_repolist { | 65 | struct cgit_repolist { |
66 | int length; | 66 | int length; |
67 | int count; | 67 | int count; |
68 | struct cgit_repo *repos; | 68 | struct cgit_repo *repos; |
69 | }; | 69 | }; |
70 | 70 | ||
71 | struct commitinfo { | 71 | struct commitinfo { |
72 | struct commit *commit; | 72 | struct commit *commit; |
73 | char *author; | 73 | char *author; |
74 | char *author_email; | 74 | char *author_email; |
75 | unsigned long author_date; | 75 | unsigned long author_date; |
76 | char *committer; | 76 | char *committer; |
77 | char *committer_email; | 77 | char *committer_email; |
78 | unsigned long committer_date; | 78 | unsigned long committer_date; |
79 | char *subject; | 79 | char *subject; |
80 | char *msg; | 80 | char *msg; |
81 | char *msg_encoding; | 81 | char *msg_encoding; |
82 | }; | 82 | }; |
83 | 83 | ||
84 | struct taginfo { | 84 | struct taginfo { |
85 | char *tagger; | 85 | char *tagger; |
86 | char *tagger_email; | 86 | char *tagger_email; |
87 | int tagger_date; | 87 | int tagger_date; |
88 | char *msg; | 88 | char *msg; |
89 | }; | 89 | }; |
90 | 90 | ||
91 | struct refinfo { | 91 | struct refinfo { |
92 | const char *refname; | 92 | const char *refname; |
93 | struct object *object; | 93 | struct object *object; |
94 | union { | 94 | union { |
95 | struct taginfo *tag; | 95 | struct taginfo *tag; |
96 | struct commitinfo *commit; | 96 | struct commitinfo *commit; |
97 | }; | 97 | }; |
98 | }; | 98 | }; |
99 | 99 | ||
100 | struct reflist { | 100 | struct reflist { |
101 | struct refinfo **refs; | 101 | struct refinfo **refs; |
102 | int alloc; | 102 | int alloc; |
103 | int count; | 103 | int count; |
104 | }; | 104 | }; |
105 | 105 | ||
106 | struct cgit_query { | 106 | struct cgit_query { |
107 | int has_symref; | 107 | int has_symref; |
108 | int has_sha1; | 108 | int has_sha1; |
109 | char *raw; | 109 | char *raw; |
110 | char *repo; | 110 | char *repo; |
111 | char *page; | 111 | char *page; |
112 | char *search; | 112 | char *search; |
113 | char *grep; | 113 | char *grep; |
114 | char *head; | 114 | char *head; |
115 | char *sha1; | 115 | char *sha1; |
116 | char *sha2; | 116 | char *sha2; |
117 | char *path; | 117 | char *path; |
118 | char *name; | 118 | char *name; |
119 | int ofs; | 119 | int ofs; |
120 | }; | 120 | }; |
121 | 121 | ||
122 | struct cgit_config { | 122 | struct cgit_config { |
123 | char *agefile; | 123 | char *agefile; |
124 | char *cache_root; | 124 | char *cache_root; |
125 | char *clone_prefix; | 125 | char *clone_prefix; |
126 | char *css; | 126 | char *css; |
127 | char *index_header; | 127 | char *index_header; |
128 | char *index_info; | 128 | char *index_info; |
129 | char *logo; | 129 | char *logo; |
130 | char *logo_link; | 130 | char *logo_link; |
131 | char *module_link; | 131 | char *module_link; |
132 | char *repo_group; | 132 | char *repo_group; |
133 | char *robots; | 133 | char *robots; |
134 | char *root_title; | 134 | char *root_title; |
135 | char *root_desc; | 135 | char *root_desc; |
136 | char *root_readme; | 136 | char *root_readme; |
137 | char *script_name; | 137 | char *script_name; |
138 | char *virtual_root; | 138 | char *virtual_root; |
139 | int cache_size; | ||
139 | int cache_dynamic_ttl; | 140 | int cache_dynamic_ttl; |
140 | int cache_max_create_time; | 141 | int cache_max_create_time; |
141 | int cache_repo_ttl; | 142 | int cache_repo_ttl; |
142 | int cache_root_ttl; | 143 | int cache_root_ttl; |
143 | int cache_static_ttl; | 144 | int cache_static_ttl; |
144 | int enable_index_links; | 145 | int enable_index_links; |
145 | int enable_log_filecount; | 146 | int enable_log_filecount; |
146 | int enable_log_linecount; | 147 | int enable_log_linecount; |
147 | int max_commit_count; | 148 | int max_commit_count; |
148 | int max_lock_attempts; | 149 | int max_lock_attempts; |
149 | int max_msg_len; | 150 | int max_msg_len; |
150 | int max_repodesc_len; | 151 | int max_repodesc_len; |
151 | int nocache; | 152 | int nocache; |
152 | int renamelimit; | 153 | int renamelimit; |
153 | int snapshots; | 154 | int snapshots; |
154 | int summary_branches; | 155 | int summary_branches; |
155 | int summary_log; | 156 | int summary_log; |
156 | int summary_tags; | 157 | int summary_tags; |
157 | }; | 158 | }; |
158 | 159 | ||
159 | struct cgit_page { | 160 | struct cgit_page { |
160 | time_t modified; | 161 | time_t modified; |
161 | time_t expires; | 162 | time_t expires; |
162 | char *mimetype; | 163 | char *mimetype; |
163 | char *charset; | 164 | char *charset; |
164 | char *filename; | 165 | char *filename; |
165 | char *title; | 166 | char *title; |
166 | }; | 167 | }; |
167 | 168 | ||
168 | struct cgit_context { | 169 | struct cgit_context { |
169 | struct cgit_query qry; | 170 | struct cgit_query qry; |
170 | struct cgit_config cfg; | 171 | struct cgit_config cfg; |
171 | struct cgit_repo *repo; | 172 | struct cgit_repo *repo; |
172 | struct cgit_page page; | 173 | struct cgit_page page; |
173 | }; | 174 | }; |
174 | 175 | ||
175 | struct cgit_snapshot_format { | 176 | struct cgit_snapshot_format { |
176 | const char *suffix; | 177 | const char *suffix; |
177 | const char *mimetype; | 178 | const char *mimetype; |
178 | write_archive_fn_t write_func; | 179 | write_archive_fn_t write_func; |
179 | int bit; | 180 | int bit; |
180 | }; | 181 | }; |
181 | 182 | ||
182 | extern const char *cgit_version; | 183 | extern const char *cgit_version; |
183 | 184 | ||
184 | extern struct cgit_repolist cgit_repolist; | 185 | extern struct cgit_repolist cgit_repolist; |
185 | extern struct cgit_context ctx; | 186 | extern struct cgit_context ctx; |
186 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; | 187 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; |
187 | 188 | ||
188 | extern struct cgit_repo *cgit_add_repo(const char *url); | 189 | extern struct cgit_repo *cgit_add_repo(const char *url); |
189 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); | 190 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
190 | extern void cgit_repo_config_cb(const char *name, const char *value); | 191 | extern void cgit_repo_config_cb(const char *name, const char *value); |
191 | 192 | ||
192 | extern int chk_zero(int result, char *msg); | 193 | extern int chk_zero(int result, char *msg); |
193 | extern int chk_positive(int result, char *msg); | 194 | extern int chk_positive(int result, char *msg); |
194 | extern int chk_non_negative(int result, char *msg); | 195 | extern int chk_non_negative(int result, char *msg); |
195 | 196 | ||
196 | extern char *trim_end(const char *str, char c); | 197 | extern char *trim_end(const char *str, char c); |
197 | extern char *strlpart(char *txt, int maxlen); | 198 | extern char *strlpart(char *txt, int maxlen); |
198 | extern char *strrpart(char *txt, int maxlen); | 199 | extern char *strrpart(char *txt, int maxlen); |
199 | 200 | ||
200 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 201 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
201 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 202 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
202 | int flags, void *cb_data); | 203 | int flags, void *cb_data); |
203 | 204 | ||
204 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 205 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
205 | 206 | ||
206 | extern int cgit_diff_files(const unsigned char *old_sha1, | 207 | extern int cgit_diff_files(const unsigned char *old_sha1, |
207 | const unsigned char *new_sha1, | 208 | const unsigned char *new_sha1, |
208 | linediff_fn fn); | 209 | linediff_fn fn); |
209 | 210 | ||
210 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 211 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
211 | const unsigned char *new_sha1, | 212 | const unsigned char *new_sha1, |
212 | filepair_fn fn, const char *prefix); | 213 | filepair_fn fn, const char *prefix); |
213 | 214 | ||
214 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 215 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
215 | 216 | ||
216 | extern char *fmt(const char *format,...); | 217 | extern char *fmt(const char *format,...); |
217 | 218 | ||
218 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 219 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
219 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 220 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
220 | extern void cgit_parse_url(const char *url); | 221 | extern void cgit_parse_url(const char *url); |
221 | 222 | ||
222 | extern const char *cgit_repobasename(const char *reponame); | 223 | extern const char *cgit_repobasename(const char *reponame); |
223 | 224 | ||
224 | extern int cgit_parse_snapshots_mask(const char *str); | 225 | extern int cgit_parse_snapshots_mask(const char *str); |
225 | 226 | ||
226 | /* libgit.a either links against or compiles its own implementation of | 227 | /* libgit.a either links against or compiles its own implementation of |
227 | * strcasestr(), and we'd like to reuse it. Simply re-declaring it | 228 | * strcasestr(), and we'd like to reuse it. Simply re-declaring it |
228 | * seems to do the trick. | 229 | * seems to do the trick. |
229 | */ | 230 | */ |
230 | extern char *strcasestr(const char *haystack, const char *needle); | 231 | extern char *strcasestr(const char *haystack, const char *needle); |
231 | 232 | ||
232 | 233 | ||
233 | #endif /* CGIT_H */ | 234 | #endif /* CGIT_H */ |
@@ -1,121 +1,132 @@ | |||
1 | /* cmd.c: the cgit command dispatcher | 1 | /* cmd.c: the cgit command dispatcher |
2 | * | 2 | * |
3 | * Copyright (C) 2008 Lars Hjemli | 3 | * Copyright (C) 2008 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 | #include "cmd.h" | 10 | #include "cmd.h" |
11 | #include "cache.h" | ||
12 | #include "ui-shared.h" | ||
11 | #include "ui-blob.h" | 13 | #include "ui-blob.h" |
12 | #include "ui-commit.h" | 14 | #include "ui-commit.h" |
13 | #include "ui-diff.h" | 15 | #include "ui-diff.h" |
14 | #include "ui-log.h" | 16 | #include "ui-log.h" |
15 | #include "ui-patch.h" | 17 | #include "ui-patch.h" |
16 | #include "ui-refs.h" | 18 | #include "ui-refs.h" |
17 | #include "ui-repolist.h" | 19 | #include "ui-repolist.h" |
18 | #include "ui-snapshot.h" | 20 | #include "ui-snapshot.h" |
19 | #include "ui-summary.h" | 21 | #include "ui-summary.h" |
20 | #include "ui-tag.h" | 22 | #include "ui-tag.h" |
21 | #include "ui-tree.h" | 23 | #include "ui-tree.h" |
22 | 24 | ||
23 | static void about_fn(struct cgit_context *ctx) | 25 | static void about_fn(struct cgit_context *ctx) |
24 | { | 26 | { |
25 | if (ctx->repo) | 27 | if (ctx->repo) |
26 | cgit_print_repo_readme(); | 28 | cgit_print_repo_readme(); |
27 | else | 29 | else |
28 | cgit_print_site_readme(); | 30 | cgit_print_site_readme(); |
29 | } | 31 | } |
30 | 32 | ||
31 | static void blob_fn(struct cgit_context *ctx) | 33 | static void blob_fn(struct cgit_context *ctx) |
32 | { | 34 | { |
33 | cgit_print_blob(ctx->qry.sha1, ctx->qry.path); | 35 | cgit_print_blob(ctx->qry.sha1, ctx->qry.path); |
34 | } | 36 | } |
35 | 37 | ||
36 | static void commit_fn(struct cgit_context *ctx) | 38 | static void commit_fn(struct cgit_context *ctx) |
37 | { | 39 | { |
38 | cgit_print_commit(ctx->qry.sha1); | 40 | cgit_print_commit(ctx->qry.sha1); |
39 | } | 41 | } |
40 | 42 | ||
41 | static void diff_fn(struct cgit_context *ctx) | 43 | static void diff_fn(struct cgit_context *ctx) |
42 | { | 44 | { |
43 | cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); | 45 | cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path); |
44 | } | 46 | } |
45 | 47 | ||
46 | static void repolist_fn(struct cgit_context *ctx) | ||
47 | { | ||
48 | cgit_print_repolist(); | ||
49 | } | ||
50 | |||
51 | static void log_fn(struct cgit_context *ctx) | 48 | static void log_fn(struct cgit_context *ctx) |
52 | { | 49 | { |
53 | cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, | 50 | cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count, |
54 | ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); | 51 | ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1); |
55 | } | 52 | } |
56 | 53 | ||
54 | static void ls_cache_fn(struct cgit_context *ctx) | ||
55 | { | ||
56 | ctx->page.mimetype = "text/plain"; | ||
57 | ctx->page.filename = "ls-cache.txt"; | ||
58 | cgit_print_http_headers(ctx); | ||
59 | cache_ls(ctx->cfg.cache_root); | ||
60 | } | ||
61 | |||
62 | static void repolist_fn(struct cgit_context *ctx) | ||
63 | { | ||
64 | cgit_print_repolist(); | ||
65 | } | ||
66 | |||
57 | static void patch_fn(struct cgit_context *ctx) | 67 | static void patch_fn(struct cgit_context *ctx) |
58 | { | 68 | { |
59 | cgit_print_patch(ctx->qry.sha1); | 69 | cgit_print_patch(ctx->qry.sha1); |
60 | } | 70 | } |
61 | 71 | ||
62 | static void refs_fn(struct cgit_context *ctx) | 72 | static void refs_fn(struct cgit_context *ctx) |
63 | { | 73 | { |
64 | cgit_print_refs(); | 74 | cgit_print_refs(); |
65 | } | 75 | } |
66 | 76 | ||
67 | static void snapshot_fn(struct cgit_context *ctx) | 77 | static void snapshot_fn(struct cgit_context *ctx) |
68 | { | 78 | { |
69 | cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, | 79 | cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, |
70 | cgit_repobasename(ctx->repo->url), ctx->qry.path, | 80 | cgit_repobasename(ctx->repo->url), ctx->qry.path, |
71 | ctx->repo->snapshots); | 81 | ctx->repo->snapshots); |
72 | } | 82 | } |
73 | 83 | ||
74 | static void summary_fn(struct cgit_context *ctx) | 84 | static void summary_fn(struct cgit_context *ctx) |
75 | { | 85 | { |
76 | cgit_print_summary(); | 86 | cgit_print_summary(); |
77 | } | 87 | } |
78 | 88 | ||
79 | static void tag_fn(struct cgit_context *ctx) | 89 | static void tag_fn(struct cgit_context *ctx) |
80 | { | 90 | { |
81 | cgit_print_tag(ctx->qry.sha1); | 91 | cgit_print_tag(ctx->qry.sha1); |
82 | } | 92 | } |
83 | 93 | ||
84 | static void tree_fn(struct cgit_context *ctx) | 94 | static void tree_fn(struct cgit_context *ctx) |
85 | { | 95 | { |
86 | cgit_print_tree(ctx->qry.sha1, ctx->qry.path); | 96 | cgit_print_tree(ctx->qry.sha1, ctx->qry.path); |
87 | } | 97 | } |
88 | 98 | ||
89 | #define def_cmd(name, want_repo, want_layout) \ | 99 | #define def_cmd(name, want_repo, want_layout) \ |
90 | {#name, name##_fn, want_repo, want_layout} | 100 | {#name, name##_fn, want_repo, want_layout} |
91 | 101 | ||
92 | struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) | 102 | struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx) |
93 | { | 103 | { |
94 | static struct cgit_cmd cmds[] = { | 104 | static struct cgit_cmd cmds[] = { |
95 | def_cmd(about, 0, 1), | 105 | def_cmd(about, 0, 1), |
96 | def_cmd(blob, 1, 0), | 106 | def_cmd(blob, 1, 0), |
97 | def_cmd(commit, 1, 1), | 107 | def_cmd(commit, 1, 1), |
98 | def_cmd(diff, 1, 1), | 108 | def_cmd(diff, 1, 1), |
99 | def_cmd(log, 1, 1), | 109 | def_cmd(log, 1, 1), |
110 | def_cmd(ls_cache, 0, 0), | ||
100 | def_cmd(patch, 1, 0), | 111 | def_cmd(patch, 1, 0), |
101 | def_cmd(refs, 1, 1), | 112 | def_cmd(refs, 1, 1), |
102 | def_cmd(repolist, 0, 0), | 113 | def_cmd(repolist, 0, 0), |
103 | def_cmd(snapshot, 1, 0), | 114 | def_cmd(snapshot, 1, 0), |
104 | def_cmd(summary, 1, 1), | 115 | def_cmd(summary, 1, 1), |
105 | def_cmd(tag, 1, 1), | 116 | def_cmd(tag, 1, 1), |
106 | def_cmd(tree, 1, 1), | 117 | def_cmd(tree, 1, 1), |
107 | }; | 118 | }; |
108 | int i; | 119 | int i; |
109 | 120 | ||
110 | if (ctx->qry.page == NULL) { | 121 | if (ctx->qry.page == NULL) { |
111 | if (ctx->repo) | 122 | if (ctx->repo) |
112 | ctx->qry.page = "summary"; | 123 | ctx->qry.page = "summary"; |
113 | else | 124 | else |
114 | ctx->qry.page = "repolist"; | 125 | ctx->qry.page = "repolist"; |
115 | } | 126 | } |
116 | 127 | ||
117 | for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) | 128 | for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) |
118 | if (!strcmp(ctx->qry.page, cmds[i].name)) | 129 | if (!strcmp(ctx->qry.page, cmds[i].name)) |
119 | return &cmds[i]; | 130 | return &cmds[i]; |
120 | return NULL; | 131 | return NULL; |
121 | } | 132 | } |
diff --git a/tests/setup.sh b/tests/setup.sh index 66bf406..e37306e 100755 --- a/tests/setup.sh +++ b/tests/setup.sh | |||
@@ -1,116 +1,116 @@ | |||
1 | # This file should be sourced by all test-scripts | 1 | # This file should be sourced by all test-scripts |
2 | # | 2 | # |
3 | # Main functions: | 3 | # Main functions: |
4 | # prepare_tests(description) - setup for testing, i.e. create repos+config | 4 | # prepare_tests(description) - setup for testing, i.e. create repos+config |
5 | # run_test(description, script) - run one test, i.e. eval script | 5 | # run_test(description, script) - run one test, i.e. eval script |
6 | # | 6 | # |
7 | # Helper functions | 7 | # Helper functions |
8 | # cgit_query(querystring) - call cgit with the specified querystring | 8 | # cgit_query(querystring) - call cgit with the specified querystring |
9 | # cgit_url(url) - call cgit with the specified virtual url | 9 | # cgit_url(url) - call cgit with the specified virtual url |
10 | # | 10 | # |
11 | # Example script: | 11 | # Example script: |
12 | # | 12 | # |
13 | # . setup.sh | 13 | # . setup.sh |
14 | # prepare_tests "html validation" | 14 | # prepare_tests "html validation" |
15 | # run_test 'repo index' 'cgit_url "/" | tidy -e' | 15 | # run_test 'repo index' 'cgit_url "/" | tidy -e' |
16 | # run_test 'repo summary' 'cgit_url "/foo" | tidy -e' | 16 | # run_test 'repo summary' 'cgit_url "/foo" | tidy -e' |
17 | 17 | ||
18 | 18 | ||
19 | mkrepo() { | 19 | mkrepo() { |
20 | name=$1 | 20 | name=$1 |
21 | count=$2 | 21 | count=$2 |
22 | dir=$PWD | 22 | dir=$PWD |
23 | test -d $name && return | 23 | test -d $name && return |
24 | printf "Creating testrepo %s\n" $name | 24 | printf "Creating testrepo %s\n" $name |
25 | mkdir -p $name | 25 | mkdir -p $name |
26 | cd $name | 26 | cd $name |
27 | git init | 27 | git init |
28 | for ((n=1; n<=count; n++)) | 28 | for ((n=1; n<=count; n++)) |
29 | do | 29 | do |
30 | echo $n >file-$n | 30 | echo $n >file-$n |
31 | git add file-$n | 31 | git add file-$n |
32 | git commit -m "commit $n" | 32 | git commit -m "commit $n" |
33 | done | 33 | done |
34 | cd $dir | 34 | cd $dir |
35 | } | 35 | } |
36 | 36 | ||
37 | setup_repos() | 37 | setup_repos() |
38 | { | 38 | { |
39 | rm -rf trash/cache | 39 | rm -rf trash/cache |
40 | mkdir -p trash/cache | 40 | mkdir -p trash/cache |
41 | mkrepo trash/repos/foo 5 >/dev/null | 41 | mkrepo trash/repos/foo 5 >/dev/null |
42 | mkrepo trash/repos/bar 50 >/dev/null | 42 | mkrepo trash/repos/bar 50 >/dev/null |
43 | cat >trash/cgitrc <<EOF | 43 | cat >trash/cgitrc <<EOF |
44 | virtual-root=/ | 44 | virtual-root=/ |
45 | cache-root=$PWD/trash/cache | 45 | cache-root=$PWD/trash/cache |
46 | 46 | ||
47 | nocache=0 | 47 | cache-size=1021 |
48 | snapshots=tar.gz tar.bz zip | 48 | snapshots=tar.gz tar.bz zip |
49 | enable-log-filecount=1 | 49 | enable-log-filecount=1 |
50 | enable-log-linecount=1 | 50 | enable-log-linecount=1 |
51 | summary-log=5 | 51 | summary-log=5 |
52 | summary-branches=5 | 52 | summary-branches=5 |
53 | summary-tags=5 | 53 | summary-tags=5 |
54 | 54 | ||
55 | repo.url=foo | 55 | repo.url=foo |
56 | repo.path=$PWD/trash/repos/foo/.git | 56 | repo.path=$PWD/trash/repos/foo/.git |
57 | # Do not specify a description for this repo, as it then will be assigned | 57 | # Do not specify a description for this repo, as it then will be assigned |
58 | # the constant value "[no description]" (which actually used to cause a | 58 | # the constant value "[no description]" (which actually used to cause a |
59 | # segfault). | 59 | # segfault). |
60 | 60 | ||
61 | repo.url=bar | 61 | repo.url=bar |
62 | repo.path=$PWD/trash/repos/bar/.git | 62 | repo.path=$PWD/trash/repos/bar/.git |
63 | repo.desc=the bar repo | 63 | repo.desc=the bar repo |
64 | EOF | 64 | EOF |
65 | } | 65 | } |
66 | 66 | ||
67 | prepare_tests() | 67 | prepare_tests() |
68 | { | 68 | { |
69 | setup_repos | 69 | setup_repos |
70 | rm -f test-output.log 2>/dev/null | 70 | rm -f test-output.log 2>/dev/null |
71 | test_count=0 | 71 | test_count=0 |
72 | test_failed=0 | 72 | test_failed=0 |
73 | echo "[$0]" "$@" >test-output.log | 73 | echo "[$0]" "$@" >test-output.log |
74 | echo "$@" "($0)" | 74 | echo "$@" "($0)" |
75 | } | 75 | } |
76 | 76 | ||
77 | tests_done() | 77 | tests_done() |
78 | { | 78 | { |
79 | printf "\n" | 79 | printf "\n" |
80 | if test $test_failed -gt 0 | 80 | if test $test_failed -gt 0 |
81 | then | 81 | then |
82 | printf "test: *** %s failure(s), logfile=%s\n" \ | 82 | printf "test: *** %s failure(s), logfile=%s\n" \ |
83 | $test_failed "$(pwd)/test-output.log" | 83 | $test_failed "$(pwd)/test-output.log" |
84 | false | 84 | false |
85 | fi | 85 | fi |
86 | } | 86 | } |
87 | 87 | ||
88 | run_test() | 88 | run_test() |
89 | { | 89 | { |
90 | desc=$1 | 90 | desc=$1 |
91 | script=$2 | 91 | script=$2 |
92 | ((test_count++)) | 92 | ((test_count++)) |
93 | printf "\ntest %d: name='%s'\n" $test_count "$desc" >>test-output.log | 93 | printf "\ntest %d: name='%s'\n" $test_count "$desc" >>test-output.log |
94 | printf "test %d: eval='%s'\n" $test_count "$2" >>test-output.log | 94 | printf "test %d: eval='%s'\n" $test_count "$2" >>test-output.log |
95 | eval "$2" >>test-output.log 2>>test-output.log | 95 | eval "$2" >>test-output.log 2>>test-output.log |
96 | res=$? | 96 | res=$? |
97 | printf "test %d: exitcode=%d\n" $test_count $res >>test-output.log | 97 | printf "test %d: exitcode=%d\n" $test_count $res >>test-output.log |
98 | if test $res = 0 | 98 | if test $res = 0 |
99 | then | 99 | then |
100 | printf " %2d) %-60s [ok]\n" $test_count "$desc" | 100 | printf " %2d) %-60s [ok]\n" $test_count "$desc" |
101 | else | 101 | else |
102 | ((test_failed++)) | 102 | ((test_failed++)) |
103 | printf " %2d) %-60s [failed]\n" $test_count "$desc" | 103 | printf " %2d) %-60s [failed]\n" $test_count "$desc" |
104 | fi | 104 | fi |
105 | } | 105 | } |
106 | 106 | ||
107 | cgit_query() | 107 | cgit_query() |
108 | { | 108 | { |
109 | CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="$1" "$PWD/../cgit" | 109 | CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="$1" "$PWD/../cgit" |
110 | } | 110 | } |
111 | 111 | ||
112 | cgit_url() | 112 | cgit_url() |
113 | { | 113 | { |
114 | CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="url=$1" "$PWD/../cgit" | 114 | CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="url=$1" "$PWD/../cgit" |
115 | } | 115 | } |
116 | 116 | ||
diff --git a/tests/t0020-validate-cache.sh b/tests/t0020-validate-cache.sh new file mode 100755 index 0000000..53ec2eb --- a/dev/null +++ b/tests/t0020-validate-cache.sh | |||
@@ -0,0 +1,67 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | . ./setup.sh | ||
4 | |||
5 | prepare_tests 'Validate cache' | ||
6 | |||
7 | run_test 'verify cache-size=0' ' | ||
8 | |||
9 | rm -f trash/cache/* && | ||
10 | sed -i -e "s/cache-size=1021$/cache-size=0/" trash/cgitrc && | ||
11 | cgit_url "" && | ||
12 | cgit_url "foo" && | ||
13 | cgit_url "foo/refs" && | ||
14 | cgit_url "foo/tree" && | ||
15 | cgit_url "foo/log" && | ||
16 | cgit_url "foo/diff" && | ||
17 | cgit_url "foo/patch" && | ||
18 | cgit_url "bar" && | ||
19 | cgit_url "bar/refs" && | ||
20 | cgit_url "bar/tree" && | ||
21 | cgit_url "bar/log" && | ||
22 | cgit_url "bar/diff" && | ||
23 | cgit_url "bar/patch" && | ||
24 | test 0 -eq $(ls trash/cache | wc -l) | ||
25 | ' | ||
26 | |||
27 | run_test 'verify cache-size=1' ' | ||
28 | |||
29 | rm -f trash/cache/* && | ||
30 | sed -i -e "s/cache-size=0$/cache-size=1/" trash/cgitrc && | ||
31 | cgit_url "" && | ||
32 | cgit_url "foo" && | ||
33 | cgit_url "foo/refs" && | ||
34 | cgit_url "foo/tree" && | ||
35 | cgit_url "foo/log" && | ||
36 | cgit_url "foo/diff" && | ||
37 | cgit_url "foo/patch" && | ||
38 | cgit_url "bar" && | ||
39 | cgit_url "bar/refs" && | ||
40 | cgit_url "bar/tree" && | ||
41 | cgit_url "bar/log" && | ||
42 | cgit_url "bar/diff" && | ||
43 | cgit_url "bar/patch" && | ||
44 | test 1 -eq $(ls trash/cache | wc -l) | ||
45 | ' | ||
46 | |||
47 | run_test 'verify cache-size=1021' ' | ||
48 | |||
49 | rm -f trash/cache/* && | ||
50 | sed -i -e "s/cache-size=1$/cache-size=1021/" trash/cgitrc && | ||
51 | cgit_url "" && | ||
52 | cgit_url "foo" && | ||
53 | cgit_url "foo/refs" && | ||
54 | cgit_url "foo/tree" && | ||
55 | cgit_url "foo/log" && | ||
56 | cgit_url "foo/diff" && | ||
57 | cgit_url "foo/patch" && | ||
58 | cgit_url "bar" && | ||
59 | cgit_url "bar/refs" && | ||
60 | cgit_url "bar/tree" && | ||
61 | cgit_url "bar/log" && | ||
62 | cgit_url "bar/diff" && | ||
63 | cgit_url "bar/patch" && | ||
64 | test 13 -eq $(ls trash/cache | wc -l) | ||
65 | ' | ||
66 | |||
67 | tests_done | ||