]> gitweb.ps.run Git - ps-cgit/blob - cache.c
cgit.c: remove useless null check
[ps-cgit] / cache.c
1 /* cache.c: cache management
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
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  *
14  */
15
16 #include "cgit.h"
17 #include "cache.h"
18 #include "html.h"
19 #ifdef HAVE_LINUX_SENDFILE
20 #include <sys/sendfile.h>
21 #endif
22
23 #define CACHE_BUFSIZE (1024 * 4)
24
25 struct cache_slot {
26         const char *key;
27         int keylen;
28         int ttl;
29         cache_fill_fn fn;
30         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;
36         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  */
44 static int open_slot(struct cache_slot *slot)
45 {
46         char *bufz;
47         int bufkeylen = -1;
48
49         slot->cache_fd = open(slot->cache_name, O_RDONLY);
50         if (slot->cache_fd == -1)
51                 return errno;
52
53         if (fstat(slot->cache_fd, &slot->cache_st))
54                 return errno;
55
56         slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
57         if (slot->bufsize < 0)
58                 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;
68 }
69
70 /* Close the active cache slot */
71 static int close_slot(struct cache_slot *slot)
72 {
73         int err = 0;
74         if (slot->cache_fd > 0) {
75                 if (close(slot->cache_fd))
76                         err = errno;
77                 else
78                         slot->cache_fd = -1;
79         }
80         return err;
81 }
82
83 /* Print the content of the active cache slot (but skip the key). */
84 static int print_slot(struct cache_slot *slot)
85 {
86 #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
103         ssize_t i, j;
104
105         i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
106         if (i != slot->keylen + 1)
107                 return errno;
108
109         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);
114
115         if (i < 0 || j != i)
116                 return errno;
117         else
118                 return 0;
119 #endif
120 }
121
122 /* Check if the slot has expired */
123 static int is_expired(struct cache_slot *slot)
124 {
125         if (slot->ttl < 0)
126                 return 0;
127         else
128                 return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL);
129 }
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  */
134 static int is_modified(struct cache_slot *slot)
135 {
136         struct stat st;
137
138         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 */
146 static int close_lock(struct cache_slot *slot)
147 {
148         int err = 0;
149         if (slot->lock_fd > 0) {
150                 if (close(slot->lock_fd))
151                         err = errno;
152                 else
153                         slot->lock_fd = -1;
154         }
155         return err;
156 }
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  */
162 static int lock_slot(struct cache_slot *slot)
163 {
164         struct flock lock = {
165                 .l_type = F_WRLCK,
166                 .l_whence = SEEK_SET,
167                 .l_start = 0,
168                 .l_len = 0,
169         };
170
171         slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT,
172                              S_IRUSR | S_IWUSR);
173         if (slot->lock_fd == -1)
174                 return errno;
175         if (fcntl(slot->lock_fd, F_SETLK, &lock) < 0) {
176                 int saved_errno = errno;
177                 close(slot->lock_fd);
178                 slot->lock_fd = -1;
179                 return saved_errno;
180         }
181         if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0)
182                 return errno;
183         return 0;
184 }
185
186 /* Release the current lockfile. If `replace_old_slot` is set the
187  * lockfile replaces the old cache slot, otherwise the lockfile is
188  * just deleted.
189  */
190 static int unlock_slot(struct cache_slot *slot, int replace_old_slot)
191 {
192         int err;
193
194         if (replace_old_slot)
195                 err = rename(slot->lock_name, slot->cache_name);
196         else
197                 err = unlink(slot->lock_name);
198
199         if (err)
200                 return errno;
201
202         return 0;
203 }
204
205 /* Generate the content for the current cache slot by redirecting
206  * stdout to the lock-fd and invoking the callback function
207  */
208 static int fill_slot(struct cache_slot *slot)
209 {
210         int tmp;
211
212         /* Preserve stdout */
213         tmp = dup(STDOUT_FILENO);
214         if (tmp == -1)
215                 return errno;
216
217         /* Redirect stdout to lockfile */
218         if (dup2(slot->lock_fd, STDOUT_FILENO) == -1)
219                 return errno;
220
221         /* Generate cache content */
222         slot->fn();
223
224         /* update stat info */
225         if (fstat(slot->lock_fd, &slot->cache_st))
226                 return errno;
227
228         /* Restore stdout */
229         if (dup2(tmp, STDOUT_FILENO) == -1)
230                 return errno;
231
232         /* Close the temporary filedescriptor */
233         if (close(tmp))
234                 return errno;
235
236         return 0;
237 }
238
239 /* Crude implementation of 32-bit FNV-1 hash algorithm,
240  * see http://www.isthe.com/chongo/tech/comp/fnv/ for details
241  * about the magic numbers.
242  */
243 #define FNV_OFFSET 0x811c9dc5
244 #define FNV_PRIME  0x01000193
245
246 unsigned long hash_str(const char *str)
247 {
248         unsigned long h = FNV_OFFSET;
249         unsigned char *s = (unsigned char *)str;
250
251         if (!s)
252                 return h;
253
254         while (*s) {
255                 h *= FNV_PRIME;
256                 h ^= *s++;
257         }
258         return h;
259 }
260
261 static int process_slot(struct cache_slot *slot)
262 {
263         int err;
264
265         err = open_slot(slot);
266         if (!err && slot->match) {
267                 if (is_expired(slot)) {
268                         if (!lock_slot(slot)) {
269                                 /* If the cachefile has been replaced between
270                                  * `open_slot` and `lock_slot`, we'll just
271                                  * serve the stale content from the original
272                                  * cachefile. This way we avoid pruning the
273                                  * newly generated slot. The same code-path
274                                  * is chosen if fill_slot() fails for some
275                                  * reason.
276                                  *
277                                  * TODO? check if the new slot contains the
278                                  * same key as the old one, since we would
279                                  * prefer to serve the newest content.
280                                  * This will require us to open yet another
281                                  * file-descriptor and read and compare the
282                                  * key from the new file, so for now we're
283                                  * lazy and just ignore the new file.
284                                  */
285                                 if (is_modified(slot) || fill_slot(slot)) {
286                                         unlock_slot(slot, 0);
287                                         close_lock(slot);
288                                 } else {
289                                         close_slot(slot);
290                                         unlock_slot(slot, 1);
291                                         slot->cache_fd = slot->lock_fd;
292                                 }
293                         }
294                 }
295                 if ((err = print_slot(slot)) != 0) {
296                         cache_log("[cgit] error printing cache %s: %s (%d)\n",
297                                   slot->cache_name,
298                                   strerror(err),
299                                   err);
300                 }
301                 close_slot(slot);
302                 return err;
303         }
304
305         /* If the cache slot does not exist (or its key doesn't match the
306          * current key), lets try to create a new cache slot for this
307          * request. If this fails (for whatever reason), lets just generate
308          * the content without caching it and fool the caller to belive
309          * everything worked out (but print a warning on stdout).
310          */
311
312         close_slot(slot);
313         if ((err = lock_slot(slot)) != 0) {
314                 cache_log("[cgit] Unable to lock slot %s: %s (%d)\n",
315                           slot->lock_name, strerror(err), err);
316                 slot->fn();
317                 return 0;
318         }
319
320         if ((err = fill_slot(slot)) != 0) {
321                 cache_log("[cgit] Unable to fill slot %s: %s (%d)\n",
322                           slot->lock_name, strerror(err), err);
323                 unlock_slot(slot, 0);
324                 close_lock(slot);
325                 slot->fn();
326                 return 0;
327         }
328         // We've got a valid cache slot in the lock file, which
329         // is about to replace the old cache slot. But if we
330         // release the lockfile and then try to open the new cache
331         // slot, we might get a race condition with a concurrent
332         // writer for the same cache slot (with a different key).
333         // Lets avoid such a race by just printing the content of
334         // the lock file.
335         slot->cache_fd = slot->lock_fd;
336         unlock_slot(slot, 1);
337         if ((err = print_slot(slot)) != 0) {
338                 cache_log("[cgit] error printing cache %s: %s (%d)\n",
339                           slot->cache_name,
340                           strerror(err),
341                           err);
342         }
343         close_slot(slot);
344         return err;
345 }
346
347 /* Print cached content to stdout, generate the content if necessary. */
348 int cache_process(int size, const char *path, const char *key, int ttl,
349                   cache_fill_fn fn)
350 {
351         unsigned long hash;
352         int i;
353         struct strbuf filename = STRBUF_INIT;
354         struct strbuf lockname = STRBUF_INIT;
355         struct cache_slot slot;
356         int result;
357
358         /* If the cache is disabled, just generate the content */
359         if (size <= 0 || ttl == 0) {
360                 fn();
361                 return 0;
362         }
363
364         /* Verify input, calculate filenames */
365         if (!path) {
366                 cache_log("[cgit] Cache path not specified, caching is disabled\n");
367                 fn();
368                 return 0;
369         }
370         if (!key)
371                 key = "";
372         hash = hash_str(key) % size;
373         strbuf_addstr(&filename, path);
374         strbuf_ensure_end(&filename, '/');
375         for (i = 0; i < 8; i++) {
376                 strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf));
377                 hash >>= 4;
378         }
379         strbuf_addbuf(&lockname, &filename);
380         strbuf_addstr(&lockname, ".lock");
381         slot.fn = fn;
382         slot.ttl = ttl;
383         slot.cache_name = filename.buf;
384         slot.lock_name = lockname.buf;
385         slot.key = key;
386         slot.keylen = strlen(key);
387         result = process_slot(&slot);
388
389         strbuf_release(&filename);
390         strbuf_release(&lockname);
391         return result;
392 }
393
394 /* Return a strftime formatted date/time
395  * NB: the result from this function is to shared memory
396  */
397 static char *sprintftime(const char *format, time_t time)
398 {
399         static char buf[64];
400         struct tm *tm;
401
402         if (!time)
403                 return NULL;
404         tm = gmtime(&time);
405         strftime(buf, sizeof(buf)-1, format, tm);
406         return buf;
407 }
408
409 int cache_ls(const char *path)
410 {
411         DIR *dir;
412         struct dirent *ent;
413         int err = 0;
414         struct cache_slot slot = { NULL };
415         struct strbuf fullname = STRBUF_INIT;
416         size_t prefixlen;
417
418         if (!path) {
419                 cache_log("[cgit] cache path not specified\n");
420                 return -1;
421         }
422         dir = opendir(path);
423         if (!dir) {
424                 err = errno;
425                 cache_log("[cgit] unable to open path %s: %s (%d)\n",
426                           path, strerror(err), err);
427                 return err;
428         }
429         strbuf_addstr(&fullname, path);
430         strbuf_ensure_end(&fullname, '/');
431         prefixlen = fullname.len;
432         while ((ent = readdir(dir)) != NULL) {
433                 if (strlen(ent->d_name) != 8)
434                         continue;
435                 strbuf_setlen(&fullname, prefixlen);
436                 strbuf_addstr(&fullname, ent->d_name);
437                 slot.cache_name = fullname.buf;
438                 if ((err = open_slot(&slot)) != 0) {
439                         cache_log("[cgit] unable to open path %s: %s (%d)\n",
440                                   fullname.buf, strerror(err), err);
441                         continue;
442                 }
443                 htmlf("%s %s %10"PRIuMAX" %s\n",
444                       fullname.buf,
445                       sprintftime("%Y-%m-%d %H:%M:%S",
446                                   slot.cache_st.st_mtime),
447                       (uintmax_t)slot.cache_st.st_size,
448                       slot.buf);
449                 close_slot(&slot);
450         }
451         closedir(dir);
452         strbuf_release(&fullname);
453         return 0;
454 }
455
456 /* Print a message to stdout */
457 void cache_log(const char *format, ...)
458 {
459         va_list args;
460         va_start(args, format);
461         vfprintf(stderr, format, args);
462         va_end(args);
463 }
464