blob: 801e63fd9d40c1525de5c18ca5adf61b5adbd220 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cache.c: cache management
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli7640d902006-12-10 22:41:14 +01004 *
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
Sebastian Andrzej Siewiord3581b52014-01-18 21:24:58 +010016#ifdef HAVE_LINUX_SENDFILE
17#include <sys/sendfile.h>
18#endif
Lars Hjemli25105d72006-12-10 22:31:36 +010019#include "cgit.h"
Lars Hjemliee4056b2008-03-27 09:22:13 +010020#include "cache.h"
John Keepingf32a2da2013-05-18 18:46:39 +010021#include "html.h"
Lars Hjemli25105d72006-12-10 22:31:36 +010022
Lars Hjemli939d32f2008-04-28 11:32:42 +020023#define CACHE_BUFSIZE (1024 * 4)
Lars Hjemli25105d72006-12-10 22:31:36 +010024
Lars Hjemli939d32f2008-04-28 11:32:42 +020025struct cache_slot {
26 const char *key;
27 int keylen;
28 int ttl;
29 cache_fill_fn fn;
Lars Hjemli939d32f2008-04-28 11:32:42 +020030 int cache_fd;
31 int lock_fd;
32 const char *cache_name;
33 const char *lock_name;
34 int match;
35 struct stat cache_st;
Lars Hjemli939d32f2008-04-28 11:32:42 +020036 int bufsize;
37 char buf[CACHE_BUFSIZE];
38};
39
40/* Open an existing cache slot and fill the cache buffer with
41 * (part of) the content of the cache file. Return 0 on success
42 * and errno otherwise.
43 */
44static int open_slot(struct cache_slot *slot)
Lars Hjemli2c2047f2007-01-12 00:24:35 +010045{
Lars Hjemli939d32f2008-04-28 11:32:42 +020046 char *bufz;
47 int bufkeylen = -1;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010048
Lars Hjemli939d32f2008-04-28 11:32:42 +020049 slot->cache_fd = open(slot->cache_name, O_RDONLY);
50 if (slot->cache_fd == -1)
51 return errno;
Lars Hjemli72fa5c62007-05-18 03:54:15 +020052
Lars Hjemli939d32f2008-04-28 11:32:42 +020053 if (fstat(slot->cache_fd, &slot->cache_st))
54 return errno;
55
Lars Hjemlicdc6b2f2008-05-18 23:26:05 +020056 slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
Lars Hjemli6102bcf2008-05-18 23:10:05 +020057 if (slot->bufsize < 0)
Lars Hjemli939d32f2008-04-28 11:32:42 +020058 return errno;
59
60 bufz = memchr(slot->buf, 0, slot->bufsize);
61 if (bufz)
62 bufkeylen = bufz - slot->buf;
63
64 slot->match = bufkeylen == slot->keylen &&
65 !memcmp(slot->key, slot->buf, bufkeylen + 1);
66
67 return 0;
Lars Hjemli2c2047f2007-01-12 00:24:35 +010068}
69
Lars Hjemli939d32f2008-04-28 11:32:42 +020070/* Close the active cache slot */
Lars Hjemlid4028112008-05-18 23:16:50 +020071static int close_slot(struct cache_slot *slot)
Lars Hjemlif5069d82006-12-11 09:57:58 +010072{
Lars Hjemlid4028112008-05-18 23:16:50 +020073 int err = 0;
Lars Hjemli939d32f2008-04-28 11:32:42 +020074 if (slot->cache_fd > 0) {
Lars Hjemlid4028112008-05-18 23:16:50 +020075 if (close(slot->cache_fd))
76 err = errno;
77 else
78 slot->cache_fd = -1;
Lars Hjemli25105d72006-12-10 22:31:36 +010079 }
Lars Hjemlid4028112008-05-18 23:16:50 +020080 return err;
Lars Hjemli25105d72006-12-10 22:31:36 +010081}
82
Lars Hjemli939d32f2008-04-28 11:32:42 +020083/* Print the content of the active cache slot (but skip the key). */
84static int print_slot(struct cache_slot *slot)
Lars Hjemli25105d72006-12-10 22:31:36 +010085{
Sebastian Andrzej Siewiord3581b52014-01-18 21:24:58 +010086#ifdef HAVE_LINUX_SENDFILE
87 off_t start_off;
88 int ret;
89
90 start_off = slot->keylen + 1;
91
92 do {
93 ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off,
94 slot->cache_st.st_size - start_off);
95 if (ret < 0) {
96 if (errno == EAGAIN || errno == EINTR)
97 continue;
98 return errno;
99 }
100 return 0;
101 } while (1);
102#else
Lars Hjemlidd7c1722008-05-20 17:56:47 +0200103 ssize_t i, j;
Lars Hjemli25105d72006-12-10 22:31:36 +0100104
Lars Hjemli939d32f2008-04-28 11:32:42 +0200105 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
106 if (i != slot->keylen + 1)
107 return errno;
108
Lars Hjemlidd7c1722008-05-20 17:56:47 +0200109 do {
110 i = j = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
111 if (i > 0)
112 j = xwrite(STDOUT_FILENO, slot->buf, i);
113 } while (i > 0 && j == i);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200114
Lars Hjemlidd7c1722008-05-20 17:56:47 +0200115 if (i < 0 || j != i)
Lars Hjemli939d32f2008-04-28 11:32:42 +0200116 return errno;
117 else
Lars Hjemli7c849d92006-12-16 13:55:58 +0100118 return 0;
Sebastian Andrzej Siewiord3581b52014-01-18 21:24:58 +0100119#endif
Lars Hjemli25105d72006-12-10 22:31:36 +0100120}
121
Lars Hjemli939d32f2008-04-28 11:32:42 +0200122/* Check if the slot has expired */
123static int is_expired(struct cache_slot *slot)
124{
125 if (slot->ttl < 0)
126 return 0;
127 else
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100128 return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200129}
130
131/* Check if the slot has been modified since we opened it.
132 * NB: If stat() fails, we pretend the file is modified.
133 */
134static int is_modified(struct cache_slot *slot)
Lars Hjemli318d1062006-12-11 12:10:12 +0100135{
136 struct stat st;
137
Lars Hjemli939d32f2008-04-28 11:32:42 +0200138 if (stat(slot->cache_name, &st))
139 return 1;
140 return (st.st_ino != slot->cache_st.st_ino ||
141 st.st_mtime != slot->cache_st.st_mtime ||
142 st.st_size != slot->cache_st.st_size);
143}
144
145/* Close an open lockfile */
Lars Hjemlid4028112008-05-18 23:16:50 +0200146static int close_lock(struct cache_slot *slot)
Lars Hjemli939d32f2008-04-28 11:32:42 +0200147{
Lars Hjemlid4028112008-05-18 23:16:50 +0200148 int err = 0;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200149 if (slot->lock_fd > 0) {
Lars Hjemlid4028112008-05-18 23:16:50 +0200150 if (close(slot->lock_fd))
151 err = errno;
152 else
153 slot->lock_fd = -1;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200154 }
Lars Hjemlid4028112008-05-18 23:16:50 +0200155 return err;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200156}
157
158/* Create a lockfile used to store the generated content for a cache
159 * slot, and write the slot key + \0 into it.
160 * Returns 0 on success and errno otherwise.
161 */
162static int lock_slot(struct cache_slot *slot)
163{
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100164 slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT | O_EXCL,
165 S_IRUSR | S_IWUSR);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200166 if (slot->lock_fd == -1)
167 return errno;
Lars Hjemlicdc6b2f2008-05-18 23:26:05 +0200168 if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0)
Lars Hjemlid4028112008-05-18 23:16:50 +0200169 return errno;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200170 return 0;
171}
172
173/* Release the current lockfile. If `replace_old_slot` is set the
174 * lockfile replaces the old cache slot, otherwise the lockfile is
175 * just deleted.
176 */
177static int unlock_slot(struct cache_slot *slot, int replace_old_slot)
178{
179 int err;
180
181 if (replace_old_slot)
182 err = rename(slot->lock_name, slot->cache_name);
Lars Hjemli318d1062006-12-11 12:10:12 +0100183 else
Lars Hjemli939d32f2008-04-28 11:32:42 +0200184 err = unlink(slot->lock_name);
Lars Hjemlid4028112008-05-18 23:16:50 +0200185
186 if (err)
187 return errno;
188
189 return 0;
Lars Hjemli318d1062006-12-11 12:10:12 +0100190}
191
Lars Hjemli939d32f2008-04-28 11:32:42 +0200192/* Generate the content for the current cache slot by redirecting
193 * stdout to the lock-fd and invoking the callback function
194 */
195static int fill_slot(struct cache_slot *slot)
Lars Hjemli25105d72006-12-10 22:31:36 +0100196{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200197 int tmp;
Lars Hjemli25105d72006-12-10 22:31:36 +0100198
Lars Hjemli939d32f2008-04-28 11:32:42 +0200199 /* Preserve stdout */
200 tmp = dup(STDOUT_FILENO);
201 if (tmp == -1)
202 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100203
Lars Hjemli939d32f2008-04-28 11:32:42 +0200204 /* Redirect stdout to lockfile */
205 if (dup2(slot->lock_fd, STDOUT_FILENO) == -1)
206 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100207
Lars Hjemli939d32f2008-04-28 11:32:42 +0200208 /* Generate cache content */
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100209 slot->fn();
Lars Hjemli318d1062006-12-11 12:10:12 +0100210
Sebastian Andrzej Siewiord3581b52014-01-18 21:24:58 +0100211 /* update stat info */
212 if (fstat(slot->lock_fd, &slot->cache_st))
213 return errno;
214
Lars Hjemli939d32f2008-04-28 11:32:42 +0200215 /* Restore stdout */
216 if (dup2(tmp, STDOUT_FILENO) == -1)
217 return errno;
Lars Hjemli318d1062006-12-11 12:10:12 +0100218
Lars Hjemli939d32f2008-04-28 11:32:42 +0200219 /* Close the temporary filedescriptor */
Lars Hjemlid4028112008-05-18 23:16:50 +0200220 if (close(tmp))
221 return errno;
222
Lars Hjemli939d32f2008-04-28 11:32:42 +0200223 return 0;
Lars Hjemli25105d72006-12-10 22:31:36 +0100224}
225
Lars Hjemli939d32f2008-04-28 11:32:42 +0200226/* Crude implementation of 32-bit FNV-1 hash algorithm,
227 * see http://www.isthe.com/chongo/tech/comp/fnv/ for details
228 * about the magic numbers.
229 */
230#define FNV_OFFSET 0x811c9dc5
231#define FNV_PRIME 0x01000193
232
233unsigned long hash_str(const char *str)
Lars Hjemli25105d72006-12-10 22:31:36 +0100234{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200235 unsigned long h = FNV_OFFSET;
236 unsigned char *s = (unsigned char *)str;
237
238 if (!s)
239 return h;
240
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500241 while (*s) {
Lars Hjemli939d32f2008-04-28 11:32:42 +0200242 h *= FNV_PRIME;
243 h ^= *s++;
244 }
245 return h;
Lars Hjemli25105d72006-12-10 22:31:36 +0100246}
247
Lars Hjemli939d32f2008-04-28 11:32:42 +0200248static int process_slot(struct cache_slot *slot)
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100249{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200250 int err;
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100251
Lars Hjemli939d32f2008-04-28 11:32:42 +0200252 err = open_slot(slot);
253 if (!err && slot->match) {
254 if (is_expired(slot)) {
255 if (!lock_slot(slot)) {
256 /* If the cachefile has been replaced between
257 * `open_slot` and `lock_slot`, we'll just
258 * serve the stale content from the original
259 * cachefile. This way we avoid pruning the
260 * newly generated slot. The same code-path
261 * is chosen if fill_slot() fails for some
262 * reason.
263 *
264 * TODO? check if the new slot contains the
265 * same key as the old one, since we would
266 * prefer to serve the newest content.
267 * This will require us to open yet another
268 * file-descriptor and read and compare the
269 * key from the new file, so for now we're
270 * lazy and just ignore the new file.
271 */
272 if (is_modified(slot) || fill_slot(slot)) {
273 unlock_slot(slot, 0);
274 close_lock(slot);
275 } else {
276 close_slot(slot);
277 unlock_slot(slot, 1);
278 slot->cache_fd = slot->lock_fd;
279 }
280 }
281 }
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200282 if ((err = print_slot(slot)) != 0) {
283 cache_log("[cgit] error printing cache %s: %s (%d)\n",
284 slot->cache_name,
285 strerror(err),
286 err);
287 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200288 close_slot(slot);
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200289 return err;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200290 }
291
292 /* If the cache slot does not exist (or its key doesn't match the
293 * current key), lets try to create a new cache slot for this
294 * request. If this fails (for whatever reason), lets just generate
295 * the content without caching it and fool the caller to belive
296 * everything worked out (but print a warning on stdout).
297 */
298
299 close_slot(slot);
300 if ((err = lock_slot(slot)) != 0) {
301 cache_log("[cgit] Unable to lock slot %s: %s (%d)\n",
302 slot->lock_name, strerror(err), err);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100303 slot->fn();
Lars Hjemli939d32f2008-04-28 11:32:42 +0200304 return 0;
305 }
306
307 if ((err = fill_slot(slot)) != 0) {
308 cache_log("[cgit] Unable to fill slot %s: %s (%d)\n",
309 slot->lock_name, strerror(err), err);
310 unlock_slot(slot, 0);
311 close_lock(slot);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100312 slot->fn();
Lars Hjemli939d32f2008-04-28 11:32:42 +0200313 return 0;
314 }
315 // We've got a valid cache slot in the lock file, which
316 // is about to replace the old cache slot. But if we
317 // release the lockfile and then try to open the new cache
318 // slot, we might get a race condition with a concurrent
319 // writer for the same cache slot (with a different key).
320 // Lets avoid such a race by just printing the content of
321 // the lock file.
322 slot->cache_fd = slot->lock_fd;
323 unlock_slot(slot, 1);
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200324 if ((err = print_slot(slot)) != 0) {
325 cache_log("[cgit] error printing cache %s: %s (%d)\n",
326 slot->cache_name,
327 strerror(err),
328 err);
329 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200330 close_slot(slot);
331 return err;
Lars Hjemli25105d72006-12-10 22:31:36 +0100332}
Lars Hjemli939d32f2008-04-28 11:32:42 +0200333
334/* Print cached content to stdout, generate the content if necessary. */
335int cache_process(int size, const char *path, const char *key, int ttl,
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100336 cache_fill_fn fn)
Lars Hjemli939d32f2008-04-28 11:32:42 +0200337{
338 unsigned long hash;
John Keepingfb3655d2013-04-06 10:28:57 +0100339 int i;
340 struct strbuf filename = STRBUF_INIT;
341 struct strbuf lockname = STRBUF_INIT;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200342 struct cache_slot slot;
John Keepingf75900b2013-05-18 18:28:14 +0100343 int result;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200344
345 /* If the cache is disabled, just generate the content */
Lukas Fleischer6ceba452014-02-20 20:59:22 +0100346 if (size <= 0 || ttl == 0) {
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100347 fn();
Lars Hjemli939d32f2008-04-28 11:32:42 +0200348 return 0;
349 }
350
351 /* Verify input, calculate filenames */
352 if (!path) {
353 cache_log("[cgit] Cache path not specified, caching is disabled\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100354 fn();
Lars Hjemli939d32f2008-04-28 11:32:42 +0200355 return 0;
356 }
Lars Hjemli939d32f2008-04-28 11:32:42 +0200357 if (!key)
358 key = "";
359 hash = hash_str(key) % size;
John Keepingfb3655d2013-04-06 10:28:57 +0100360 strbuf_addstr(&filename, path);
361 strbuf_ensure_end(&filename, '/');
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500362 for (i = 0; i < 8; i++) {
John Keepingfb3655d2013-04-06 10:28:57 +0100363 strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf));
Lars Hjemli939d32f2008-04-28 11:32:42 +0200364 hash >>= 4;
365 }
John Keepingfb3655d2013-04-06 10:28:57 +0100366 strbuf_addbuf(&lockname, &filename);
367 strbuf_addstr(&lockname, ".lock");
Lars Hjemli939d32f2008-04-28 11:32:42 +0200368 slot.fn = fn;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200369 slot.ttl = ttl;
John Keepingf75900b2013-05-18 18:28:14 +0100370 slot.cache_name = filename.buf;
371 slot.lock_name = lockname.buf;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200372 slot.key = key;
373 slot.keylen = strlen(key);
John Keepingf75900b2013-05-18 18:28:14 +0100374 result = process_slot(&slot);
375
376 strbuf_release(&filename);
377 strbuf_release(&lockname);
378 return result;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200379}
380
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200381/* Return a strftime formatted date/time
382 * NB: the result from this function is to shared memory
383 */
Lukas Fleischerbafab422013-03-04 08:52:33 +0100384static char *sprintftime(const char *format, time_t time)
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200385{
386 static char buf[64];
387 struct tm *tm;
388
389 if (!time)
390 return NULL;
391 tm = gmtime(&time);
392 strftime(buf, sizeof(buf)-1, format, tm);
393 return buf;
394}
395
396int cache_ls(const char *path)
397{
398 DIR *dir;
399 struct dirent *ent;
400 int err = 0;
John Keeping382ecf12014-01-12 16:49:40 +0000401 struct cache_slot slot = { 0 };
John Keepingfb3655d2013-04-06 10:28:57 +0100402 struct strbuf fullname = STRBUF_INIT;
403 size_t prefixlen;
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200404
405 if (!path) {
406 cache_log("[cgit] cache path not specified\n");
407 return -1;
408 }
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200409 dir = opendir(path);
410 if (!dir) {
411 err = errno;
412 cache_log("[cgit] unable to open path %s: %s (%d)\n",
413 path, strerror(err), err);
414 return err;
415 }
John Keepingfb3655d2013-04-06 10:28:57 +0100416 strbuf_addstr(&fullname, path);
417 strbuf_ensure_end(&fullname, '/');
418 prefixlen = fullname.len;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500419 while ((ent = readdir(dir)) != NULL) {
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200420 if (strlen(ent->d_name) != 8)
421 continue;
John Keepingfb3655d2013-04-06 10:28:57 +0100422 strbuf_setlen(&fullname, prefixlen);
423 strbuf_addstr(&fullname, ent->d_name);
John Keepingf75900b2013-05-18 18:28:14 +0100424 slot.cache_name = fullname.buf;
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200425 if ((err = open_slot(&slot)) != 0) {
426 cache_log("[cgit] unable to open path %s: %s (%d)\n",
John Keepingfb3655d2013-04-06 10:28:57 +0100427 fullname.buf, strerror(err), err);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200428 continue;
429 }
John Keepingf32a2da2013-05-18 18:46:39 +0100430 htmlf("%s %s %10"PRIuMAX" %s\n",
431 fullname.buf,
432 sprintftime("%Y-%m-%d %H:%M:%S",
433 slot.cache_st.st_mtime),
434 (uintmax_t)slot.cache_st.st_size,
435 slot.buf);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200436 close_slot(&slot);
437 }
438 closedir(dir);
John Keepingf75900b2013-05-18 18:28:14 +0100439 strbuf_release(&fullname);
Lars Hjemli9000bbf2008-04-28 12:10:13 +0200440 return 0;
441}
442
Lars Hjemli939d32f2008-04-28 11:32:42 +0200443/* Print a message to stdout */
444void cache_log(const char *format, ...)
445{
446 va_list args;
447 va_start(args, format);
448 vfprintf(stderr, format, args);
449 va_end(args);
450}
451