Lars Hjemli | 7640d90 | 2006-12-10 22:41:14 +0100 | [diff] [blame] | 1 | /* 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 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" |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 19 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 20 | #define CACHE_BUFSIZE (1024 * 4) |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 21 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 22 | struct 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 | */ |
| 43 | static int open_slot(struct cache_slot *slot) |
Lars Hjemli | 2c2047f | 2007-01-12 00:24:35 +0100 | [diff] [blame] | 44 | { |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 45 | char *bufz; |
| 46 | int bufkeylen = -1; |
Lars Hjemli | 2c2047f | 2007-01-12 00:24:35 +0100 | [diff] [blame] | 47 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 48 | slot->cache_fd = open(slot->cache_name, O_RDONLY); |
| 49 | if (slot->cache_fd == -1) |
| 50 | return errno; |
Lars Hjemli | 72fa5c6 | 2007-05-18 03:54:15 +0200 | [diff] [blame] | 51 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 52 | if (fstat(slot->cache_fd, &slot->cache_st)) |
| 53 | return errno; |
| 54 | |
Lars Hjemli | cdc6b2f | 2008-05-18 23:26:05 +0200 | [diff] [blame] | 55 | slot->bufsize = xread(slot->cache_fd, slot->buf, sizeof(slot->buf)); |
Lars Hjemli | 6102bcf | 2008-05-18 23:10:05 +0200 | [diff] [blame] | 56 | if (slot->bufsize < 0) |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 57 | 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 Hjemli | 2c2047f | 2007-01-12 00:24:35 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 69 | /* Close the active cache slot */ |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 70 | static int close_slot(struct cache_slot *slot) |
Lars Hjemli | f5069d8 | 2006-12-11 09:57:58 +0100 | [diff] [blame] | 71 | { |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 72 | int err = 0; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 73 | if (slot->cache_fd > 0) { |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 74 | if (close(slot->cache_fd)) |
| 75 | err = errno; |
| 76 | else |
| 77 | slot->cache_fd = -1; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 78 | } |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 79 | return err; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 82 | /* Print the content of the active cache slot (but skip the key). */ |
| 83 | static int print_slot(struct cache_slot *slot) |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 84 | { |
Lars Hjemli | dd7c172 | 2008-05-20 17:56:47 +0200 | [diff] [blame] | 85 | ssize_t i, j; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 86 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 87 | i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET); |
| 88 | if (i != slot->keylen + 1) |
| 89 | return errno; |
| 90 | |
Lars Hjemli | dd7c172 | 2008-05-20 17:56:47 +0200 | [diff] [blame] | 91 | 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 Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 96 | |
Lars Hjemli | dd7c172 | 2008-05-20 17:56:47 +0200 | [diff] [blame] | 97 | if (i < 0 || j != i) |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 98 | return errno; |
| 99 | else |
Lars Hjemli | 7c849d9 | 2006-12-16 13:55:58 +0100 | [diff] [blame] | 100 | return 0; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 103 | /* Check if the slot has expired */ |
| 104 | static int is_expired(struct cache_slot *slot) |
| 105 | { |
| 106 | if (slot->ttl < 0) |
| 107 | return 0; |
| 108 | else |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 109 | return slot->cache_st.st_mtime + slot->ttl * 60 < time(NULL); |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 110 | } |
| 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 | */ |
| 115 | static int is_modified(struct cache_slot *slot) |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 116 | { |
| 117 | struct stat st; |
| 118 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 119 | 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 Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 127 | static int close_lock(struct cache_slot *slot) |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 128 | { |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 129 | int err = 0; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 130 | if (slot->lock_fd > 0) { |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 131 | if (close(slot->lock_fd)) |
| 132 | err = errno; |
| 133 | else |
| 134 | slot->lock_fd = -1; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 135 | } |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 136 | return err; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 137 | } |
| 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 | */ |
| 143 | static int lock_slot(struct cache_slot *slot) |
| 144 | { |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 145 | slot->lock_fd = open(slot->lock_name, O_RDWR | O_CREAT | O_EXCL, |
| 146 | S_IRUSR | S_IWUSR); |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 147 | if (slot->lock_fd == -1) |
| 148 | return errno; |
Lars Hjemli | cdc6b2f | 2008-05-18 23:26:05 +0200 | [diff] [blame] | 149 | if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0) |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 150 | return errno; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 151 | 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 | */ |
| 158 | static 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 Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 164 | else |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 165 | err = unlink(slot->lock_name); |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 166 | |
| 167 | if (err) |
| 168 | return errno; |
| 169 | |
| 170 | return 0; |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 173 | /* Generate the content for the current cache slot by redirecting |
| 174 | * stdout to the lock-fd and invoking the callback function |
| 175 | */ |
| 176 | static int fill_slot(struct cache_slot *slot) |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 177 | { |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 178 | int tmp; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 179 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 180 | /* Preserve stdout */ |
| 181 | tmp = dup(STDOUT_FILENO); |
| 182 | if (tmp == -1) |
| 183 | return errno; |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 184 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 185 | /* Redirect stdout to lockfile */ |
| 186 | if (dup2(slot->lock_fd, STDOUT_FILENO) == -1) |
| 187 | return errno; |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 188 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 189 | /* Generate cache content */ |
| 190 | slot->fn(slot->cbdata); |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 191 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 192 | /* Restore stdout */ |
| 193 | if (dup2(tmp, STDOUT_FILENO) == -1) |
| 194 | return errno; |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame] | 195 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 196 | /* Close the temporary filedescriptor */ |
Lars Hjemli | d402811 | 2008-05-18 23:16:50 +0200 | [diff] [blame] | 197 | if (close(tmp)) |
| 198 | return errno; |
| 199 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 200 | return 0; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 203 | /* 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 | |
| 210 | unsigned long hash_str(const char *str) |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 211 | { |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 212 | unsigned long h = FNV_OFFSET; |
| 213 | unsigned char *s = (unsigned char *)str; |
| 214 | |
| 215 | if (!s) |
| 216 | return h; |
| 217 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 218 | while (*s) { |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 219 | h *= FNV_PRIME; |
| 220 | h ^= *s++; |
| 221 | } |
| 222 | return h; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 225 | static int process_slot(struct cache_slot *slot) |
Lars Hjemli | fbaf117 | 2006-12-11 22:53:50 +0100 | [diff] [blame] | 226 | { |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 227 | int err; |
Lars Hjemli | fbaf117 | 2006-12-11 22:53:50 +0100 | [diff] [blame] | 228 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 229 | 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 Hjemli | af2e756 | 2008-05-18 23:59:11 +0200 | [diff] [blame] | 259 | 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 Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 265 | close_slot(slot); |
Lars Hjemli | af2e756 | 2008-05-18 23:59:11 +0200 | [diff] [blame] | 266 | return err; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 267 | } |
| 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 Hjemli | af2e756 | 2008-05-18 23:59:11 +0200 | [diff] [blame] | 301 | 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 Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 307 | close_slot(slot); |
| 308 | return err; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 309 | } |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 310 | |
| 311 | /* Print cached content to stdout, generate the content if necessary. */ |
| 312 | int 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 Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 316 | int i; |
| 317 | struct strbuf filename = STRBUF_INIT; |
| 318 | struct strbuf lockname = STRBUF_INIT; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 319 | struct cache_slot slot; |
John Keeping | f75900b | 2013-05-18 18:28:14 +0100 | [diff] [blame] | 320 | int result; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 321 | |
| 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 Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 334 | if (!key) |
| 335 | key = ""; |
| 336 | hash = hash_str(key) % size; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 337 | strbuf_addstr(&filename, path); |
| 338 | strbuf_ensure_end(&filename, '/'); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 339 | for (i = 0; i < 8; i++) { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 340 | strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf)); |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 341 | hash >>= 4; |
| 342 | } |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 343 | strbuf_addbuf(&lockname, &filename); |
| 344 | strbuf_addstr(&lockname, ".lock"); |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 345 | slot.fn = fn; |
| 346 | slot.cbdata = cbdata; |
| 347 | slot.ttl = ttl; |
John Keeping | f75900b | 2013-05-18 18:28:14 +0100 | [diff] [blame] | 348 | slot.cache_name = filename.buf; |
| 349 | slot.lock_name = lockname.buf; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 350 | slot.key = key; |
| 351 | slot.keylen = strlen(key); |
John Keeping | f75900b | 2013-05-18 18:28:14 +0100 | [diff] [blame] | 352 | result = process_slot(&slot); |
| 353 | |
| 354 | strbuf_release(&filename); |
| 355 | strbuf_release(&lockname); |
| 356 | return result; |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 359 | /* Return a strftime formatted date/time |
| 360 | * NB: the result from this function is to shared memory |
| 361 | */ |
Lukas Fleischer | bafab42 | 2013-03-04 08:52:33 +0100 | [diff] [blame] | 362 | static char *sprintftime(const char *format, time_t time) |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 363 | { |
| 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 | |
| 374 | int cache_ls(const char *path) |
| 375 | { |
| 376 | DIR *dir; |
| 377 | struct dirent *ent; |
| 378 | int err = 0; |
| 379 | struct cache_slot slot; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 380 | struct strbuf fullname = STRBUF_INIT; |
| 381 | size_t prefixlen; |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 382 | |
| 383 | if (!path) { |
| 384 | cache_log("[cgit] cache path not specified\n"); |
| 385 | return -1; |
| 386 | } |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 387 | 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 Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 394 | strbuf_addstr(&fullname, path); |
| 395 | strbuf_ensure_end(&fullname, '/'); |
| 396 | prefixlen = fullname.len; |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 397 | while ((ent = readdir(dir)) != NULL) { |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 398 | if (strlen(ent->d_name) != 8) |
| 399 | continue; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 400 | strbuf_setlen(&fullname, prefixlen); |
| 401 | strbuf_addstr(&fullname, ent->d_name); |
John Keeping | f75900b | 2013-05-18 18:28:14 +0100 | [diff] [blame] | 402 | slot.cache_name = fullname.buf; |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 403 | if ((err = open_slot(&slot)) != 0) { |
| 404 | cache_log("[cgit] unable to open path %s: %s (%d)\n", |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 405 | fullname.buf, strerror(err), err); |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 406 | continue; |
| 407 | } |
John Keeping | f32a2da | 2013-05-18 18:46:39 +0100 | [diff] [blame^] | 408 | 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 Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 414 | close_slot(&slot); |
| 415 | } |
| 416 | closedir(dir); |
John Keeping | f75900b | 2013-05-18 18:28:14 +0100 | [diff] [blame] | 417 | strbuf_release(&fullname); |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
Lars Hjemli | 939d32f | 2008-04-28 11:32:42 +0200 | [diff] [blame] | 421 | /* Print a message to stdout */ |
| 422 | void 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 | |