blob: aa870e3654125cf6017d5f7d6d0f21da8a549a0e [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cache.c: cache management
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
Lars Hjemli939d32f2008-04-28 11:32:42 +02007 *
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 *
Lars Hjemli7640d902006-12-10 22:41:14 +010014 */
15
Lars Hjemli25105d72006-12-10 22:31:36 +010016#include "cgit.h"
Lars Hjemliee4056b2008-03-27 09:22:13 +010017#include "cache.h"
John Keepingf32a2da2013-05-18 18:46:39 +010018#include "html.h"
Lars Hjemli25105d72006-12-10 22:31:36 +010019
Lars Hjemli939d32f2008-04-28 11:32:42 +020020#define CACHE_BUFSIZE (1024 * 4)
Lars Hjemli25105d72006-12-10 22:31:36 +010021
Lars Hjemli939d32f2008-04-28 11:32:42 +020022struct cache_slot {
23 const char *key;
24 int keylen;
25 int ttl;
26 cache_fill_fn fn;
27 void *cbdata;
28 int cache_fd;
29 int lock_fd;
30 const char *cache_name;
31 const char *lock_name;
32 int match;
33 struct stat cache_st;
34 struct stat lock_st;
35 int bufsize;
36 char buf[CACHE_BUFSIZE];
37};
38
39/* Open an existing cache slot and fill the cache buffer with
40 * (part of) the content of the cache file. Return 0 on success
41 * and errno otherwise.
42 */
43static int open_slot(struct cache_slot *slot)
Lars Hjemli2c2047f2007-01-12 00:24:35 +010044{
Lars Hjemli939d32f2008-04-28 11:32:42 +020045 char *bufz;
46 int bufkeylen = -1;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010047
Lars Hjemli939d32f2008-04-28 11:32:42 +020048 slot->cache_fd = open(slot->cache_name, O_RDONLY);
49 if (slot->cache_fd == -1)
50 return errno;
Lars Hjemli72fa5c62007-05-18 03:54:15 +020051
Lars Hjemli939d32f2008-04-28 11:32:42 +020052 if (fstat(slot->cache_fd, &slot->cache_st))
53 return errno;
54
Lars Hjemlicdc6b2f2008-05-18 23:26:05 +020055 slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
Lars Hjemli6102bcf2008-05-18 23:10:05 +020056 if (slot->bufsize < 0)
Lars Hjemli939d32f2008-04-28 11:32:42 +020057 return errno;
58
59 bufz = memchr(slot->buf, 0, slot->bufsize);
60 if (bufz)
61 bufkeylen = bufz - slot->buf;
62
63 slot->match = bufkeylen == slot->keylen &&
64 !memcmp(slot->key, slot->buf, bufkeylen + 1);
65
66 return 0;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010067}
68
Lars Hjemli939d32f2008-04-28 11:32:42 +020069/* Close the active cache slot */
Lars Hjemlid4028112008-05-18 23:16:50 +020070static int close_slot(struct cache_slot *slot)
Lars Hjemlif5069d82006-12-11 09:57:58 +010071{
Lars Hjemlid4028112008-05-18 23:16:50 +020072 int err = 0;
Lars Hjemli939d32f2008-04-28 11:32:42 +020073 if (slot->cache_fd > 0) {
Lars Hjemlid4028112008-05-18 23:16:50 +020074 if (close(slot->cache_fd))
75 err = errno;
76 else
77 slot->cache_fd = -1;
Lars Hjemli25105d72006-12-10 22:31:36 +010078 }
Lars Hjemlid4028112008-05-18 23:16:50 +020079 return err;
Lars Hjemli25105d72006-12-10 22:31:36 +010080}
81
Lars Hjemli939d32f2008-04-28 11:32:42 +020082/* Print the content of the active cache slot (but skip the key). */
83static int print_slot(struct cache_slot *slot)
Lars Hjemli25105d72006-12-10 22:31:36 +010084{
Lars Hjemlidd7c1722008-05-20 17:56:47 +020085 ssize_t i, j;
Lars Hjemli25105d72006-12-10 22:31:36 +010086
Lars Hjemli939d32f2008-04-28 11:32:42 +020087 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
88 if (i != slot->keylen + 1)
89 return errno;
90
Lars Hjemlidd7c1722008-05-20 17:56:47 +020091 do {
92 i = j = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
93 if (i > 0)
94 j = xwrite(STDOUT_FILENO, slot->buf, i);
95 } while (i > 0 && j == i);
Lars Hjemli939d32f2008-04-28 11:32:42 +020096
Lars Hjemlidd7c1722008-05-20 17:56:47 +020097 if (i < 0 || j != i)
Lars Hjemli939d32f2008-04-28 11:32:42 +020098 return errno;
99 else
Lars Hjemli7c849d92006-12-16 13:55:58 +0100100 return 0;
Lars Hjemli25105d72006-12-10 22:31:36 +0100101}
102
Lars Hjemli939d32f2008-04-28 11:32:42 +0200103/* Check if the slot has expired */
104static int is_expired(struct cache_slot *slot)
105{
106 if (slot->ttl < 0)
107 return 0;
108 else
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100109 return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200110}
111
112/* Check if the slot has been modified since we opened it.
113 * NB: If stat() fails, we pretend the file is modified.
114 */
115static int is_modified(struct cache_slot *slot)
Lars Hjemli318d1062006-12-11 12:10:12 +0100116{
117 struct stat st;
118
Lars Hjemli939d32f2008-04-28 11:32:42 +0200119 if (stat(slot->cache_name, &st))
120 return 1;
121 return (st.st_ino != slot->cache_st.st_ino ||
122 st.st_mtime != slot->cache_st.st_mtime ||
123 st.st_size != slot->cache_st.st_size);
124}
125
126/* Close an open lockfile */
Lars Hjemlid4028112008-05-18 23:16:50 +0200127static int close_lock(struct cache_slot *slot)
Lars Hjemli939d32f2008-04-28 11:32:42 +0200128{
Lars Hjemlid4028112008-05-18 23:16:50 +0200129 int err = 0;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200130 if (slot->lock_fd > 0) {
Lars Hjemlid4028112008-05-18 23:16:50 +0200131 if (close(slot->lock_fd))
132 err = errno;
133 else
134 slot->lock_fd = -1;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200135 }
Lars Hjemlid4028112008-05-18 23:16:50 +0200136 return err;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200137}
138
139/* Create a lockfile used to store the generated content for a cache
140 * slot, and write the slot key + \0 into it.
141 * Returns 0 on success and errno otherwise.
142 */
143static int lock_slot(struct cache_slot *slot)
144{
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100145 slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT | O_EXCL,
146 S_IRUSR | S_IWUSR);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200147 if (slot->lock_fd == -1)
148 return errno;
Lars Hjemlicdc6b2f2008-05-18 23:26:05 +0200149 if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0)
Lars Hjemlid4028112008-05-18 23:16:50 +0200150 return errno;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200151 return 0;
152}
153
154/* Release the current lockfile. If `replace_old_slot` is set the
155 * lockfile replaces the old cache slot, otherwise the lockfile is
156 * just deleted.
157 */
158static int unlock_slot(struct cache_slot *slot, int replace_old_slot)
159{
160 int err;
161
162 if (replace_old_slot)
163 err = rename(slot->lock_name, slot->cache_name);
Lars Hjemli318d1062006-12-11 12:10:12 +0100164 else
Lars Hjemli939d32f2008-04-28 11:32:42 +0200165 err = unlink(slot->lock_name);
Lars Hjemlid4028112008-05-18 23:16:50 +0200166
167 if (err)
168 return errno;
169
170 return 0;
Lars Hjemli318d1062006-12-11 12:10:12 +0100171}
172
Lars Hjemli939d32f2008-04-28 11:32:42 +0200173/* Generate the content for the current cache slot by redirecting
174 * stdout to the lock-fd and invoking the callback function
175 */
176static int fill_slot(struct cache_slot *slot)
Lars Hjemli25105d72006-12-10 22:31:36 +0100177{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200178 int tmp;
Lars Hjemli25105d72006-12-10 22:31:36 +0100179
Lars Hjemli939d32f2008-04-28 11:32:42 +0200180 /* Preserve stdout */
181 tmp = dup(STDOUT_FILENO);
182 if (tmp == -1)
183 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100184
Lars Hjemli939d32f2008-04-28 11:32:42 +0200185 /* Redirect stdout to lockfile */
186 if (dup2(slot->lock_fd, STDOUT_FILENO) == -1)
187 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100188
Lars Hjemli939d32f2008-04-28 11:32:42 +0200189 /* Generate cache content */
190 slot->fn(slot->cbdata);
Lars Hjemli318d1062006-12-11 12:10:12 +0100191
Lars Hjemli939d32f2008-04-28 11:32:42 +0200192 /* Restore stdout */
193 if (dup2(tmp, STDOUT_FILENO) == -1)
194 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100195
Lars Hjemli939d32f2008-04-28 11:32:42 +0200196 /* Close the temporary filedescriptor */
Lars Hjemlid4028112008-05-18 23:16:50 +0200197 if (close(tmp))
198 return errno;
199
Lars Hjemli939d32f2008-04-28 11:32:42 +0200200 return 0;
Lars Hjemli25105d72006-12-10 22:31:36 +0100201}
202
Lars Hjemli939d32f2008-04-28 11:32:42 +0200203/* Crude implementation of 32-bit FNV-1 hash algorithm,
204 * see http://www.isthe.com/chongo/tech/comp/fnv/ for details
205 * about the magic numbers.
206 */
207#define FNV_OFFSET 0x811c9dc5
208#define FNV_PRIME 0x01000193
209
210unsigned long hash_str(const char *str)
Lars Hjemli25105d72006-12-10 22:31:36 +0100211{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200212 unsigned long h = FNV_OFFSET;
213 unsigned char *s = (unsigned char *)str;
214
215 if (!s)
216 return h;
217
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500218 while (*s) {
Lars Hjemli939d32f2008-04-28 11:32:42 +0200219 h *= FNV_PRIME;
220 h ^= *s++;
221 }
222 return h;
Lars Hjemli25105d72006-12-10 22:31:36 +0100223}
224
Lars Hjemli939d32f2008-04-28 11:32:42 +0200225static int process_slot(struct cache_slot *slot)
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100226{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200227 int err;
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100228
Lars Hjemli939d32f2008-04-28 11:32:42 +0200229 err = open_slot(slot);
230 if (!err && slot->match) {
231 if (is_expired(slot)) {
232 if (!lock_slot(slot)) {
233 /* If the cachefile has been replaced between
234 * `open_slot` and `lock_slot`, we'll just
235 * serve the stale content from the original
236 * cachefile. This way we avoid pruning the
237 * newly generated slot. The same code-path
238 * is chosen if fill_slot() fails for some
239 * reason.
240 *
241 * TODO? check if the new slot contains the
242 * same key as the old one, since we would
243 * prefer to serve the newest content.
244 * This will require us to open yet another
245 * file-descriptor and read and compare the
246 * key from the new file, so for now we're
247 * lazy and just ignore the new file.
248 */
249 if (is_modified(slot) || fill_slot(slot)) {
250 unlock_slot(slot, 0);
251 close_lock(slot);
252 } else {
253 close_slot(slot);
254 unlock_slot(slot, 1);
255 slot->cache_fd = slot->lock_fd;
256 }
257 }
258 }
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200259 if ((err = print_slot(slot)) != 0) {
260 cache_log("[cgit] error printing cache %s: %s (%d)\n",
261 slot->cache_name,
262 strerror(err),
263 err);
264 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200265 close_slot(slot);
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200266 return err;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200267 }
268
269 /* If the cache slot does not exist (or its key doesn't match the
270 * current key), lets try to create a new cache slot for this
271 * request. If this fails (for whatever reason), lets just generate
272 * the content without caching it and fool the caller to belive
273 * everything worked out (but print a warning on stdout).
274 */
275
276 close_slot(slot);
277 if ((err = lock_slot(slot)) != 0) {
278 cache_log("[cgit] Unable to lock slot %s: %s (%d)\n",
279 slot->lock_name, strerror(err), err);
280 slot->fn(slot->cbdata);
281 return 0;
282 }
283
284 if ((err = fill_slot(slot)) != 0) {
285 cache_log("[cgit] Unable to fill slot %s: %s (%d)\n",
286 slot->lock_name, strerror(err), err);
287 unlock_slot(slot, 0);
288 close_lock(slot);
289 slot->fn(slot->cbdata);
290 return 0;
291 }
292 // We've got a valid cache slot in the lock file, which
293 // is about to replace the old cache slot. But if we
294 // release the lockfile and then try to open the new cache
295 // slot, we might get a race condition with a concurrent
296 // writer for the same cache slot (with a different key).
297 // Lets avoid such a race by just printing the content of
298 // the lock file.
299 slot->cache_fd = slot->lock_fd;
300 unlock_slot(slot, 1);
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200301 if ((err = print_slot(slot)) != 0) {
302 cache_log("[cgit] error printing cache %s: %s (%d)\n",
303 slot->cache_name,
304 strerror(err),
305 err);
306 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200307 close_slot(slot);
308 return err;
Lars Hjemli25105d72006-12-10 22:31:36 +0100309}
Lars Hjemli939d32f2008-04-28 11:32:42 +0200310
311/* Print cached content to stdout, generate the content if necessary. */
312int cache_process(int size, const char *path, const char *key, int ttl,
313 cache_fill_fn fn, void *cbdata)
314{
315 unsigned long hash;
John Keepingfb3655d2013-04-06 10:28:57 +0100316 int i;
317 struct strbuf filename = STRBUF_INIT;
318 struct strbuf lockname = STRBUF_INIT;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200319 struct cache_slot slot;
John Keepingf75900b2013-05-18 18:28:14 +0100320 int result;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200321
322 /* If the cache is disabled, just generate the content */
323 if (size <= 0) {
324 fn(cbdata);
325 return 0;
326 }
327
328 /* Verify input, calculate filenames */
329 if (!path) {
330 cache_log("[cgit] Cache path not specified, caching is disabled\n");
331 fn(cbdata);
332 return 0;
333 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200334 if (!key)
335 key = "";
336 hash = hash_str(key) % size;
John Keepingfb3655d2013-04-06 10:28:57 +0100337 strbuf_addstr(&filename, path);
338 strbuf_ensure_end(&filename, '/');
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500339 for (i = 0; i < 8; i++) {
John Keepingfb3655d2013-04-06 10:28:57 +0100340 strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf));
Lars Hjemli939d32f2008-04-28 11:32:42 +0200341 hash >>= 4;
342 }
John Keepingfb3655d2013-04-06 10:28:57 +0100343 strbuf_addbuf(&lockname, &filename);
344 strbuf_addstr(&lockname, ".lock");
Lars Hjemli939d32f2008-04-28 11:32:42 +0200345 slot.fn = fn;
346 slot.cbdata = cbdata;
347 slot.ttl = ttl;
John Keepingf75900b2013-05-18 18:28:14 +0100348 slot.cache_name = filename.buf;
349 slot.lock_name = lockname.buf;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200350 slot.key = key;
351 slot.keylen = strlen(key);
John Keepingf75900b2013-05-18 18:28:14 +0100352 result = process_slot(&slot);
353
354 strbuf_release(&filename);
355 strbuf_release(&lockname);
356 return result;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200357}
358
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200359/* Return a strftime formatted date/time
360 * NB: the result from this function is to shared memory
361 */
Lukas Fleischerbafab422013-03-04 08:52:33 +0100362static char *sprintftime(const char *format, time_t time)
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200363{
364 static char buf[64];
365 struct tm *tm;
366
367 if (!time)
368 return NULL;
369 tm = gmtime(&time);
370 strftime(buf, sizeof(buf)-1, format, tm);
371 return buf;
372}
373
374int cache_ls(const char *path)
375{
376 DIR *dir;
377 struct dirent *ent;
378 int err = 0;
379 struct cache_slot slot;
John Keepingfb3655d2013-04-06 10:28:57 +0100380 struct strbuf fullname = STRBUF_INIT;
381 size_t prefixlen;
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200382
383 if (!path) {
384 cache_log("[cgit] cache path not specified\n");
385 return -1;
386 }
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200387 dir = opendir(path);
388 if (!dir) {
389 err = errno;
390 cache_log("[cgit] unable to open path %s: %s (%d)\n",
391 path, strerror(err), err);
392 return err;
393 }
John Keepingfb3655d2013-04-06 10:28:57 +0100394 strbuf_addstr(&fullname, path);
395 strbuf_ensure_end(&fullname, '/');
396 prefixlen = fullname.len;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500397 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200398 if (strlen(ent->d_name) != 8)
399 continue;
John Keepingfb3655d2013-04-06 10:28:57 +0100400 strbuf_setlen(&fullname, prefixlen);
401 strbuf_addstr(&fullname, ent->d_name);
John Keepingf75900b2013-05-18 18:28:14 +0100402 slot.cache_name = fullname.buf;
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200403 if ((err = open_slot(&slot)) != 0) {
404 cache_log("[cgit] unable to open path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100405 fullname.buf, strerror(err), err);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200406 continue;
407 }
John Keepingf32a2da2013-05-18 18:46:39 +0100408 htmlf("%s %s %10"PRIuMAX" %s\n",
409 fullname.buf,
410 sprintftime("%Y-%m-%d %H:%M:%S",
411 slot.cache_st.st_mtime),
412 (uintmax_t)slot.cache_st.st_size,
413 slot.buf);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200414 close_slot(&slot);
415 }
416 closedir(dir);
John Keepingf75900b2013-05-18 18:28:14 +0100417 strbuf_release(&fullname);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200418 return 0;
419}
420
Lars Hjemli939d32f2008-04-28 11:32:42 +0200421/* Print a message to stdout */
422void cache_log(const char *format, ...)
423{
424 va_list args;
425 va_start(args, format);
426 vfprintf(stderr, format, args);
427 va_end(args);
428}
429