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