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