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