1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
#include "cache.h"
#include "cgit.h"
#include "html.h"
#include "shared.h"
#ifdef HAVE_LINUX_SENDFILE
#include <sys/sendfile.h>
#endif
#define CACHE_BUFSIZE (1024 * 4)
#define SLOT_NAME_LEN 8
#define FNV_OFFSET 0x811c9dc5
#define FNV_PRIME 0x01000193
__attribute__((format (printf,1,2)))
static void log_error(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
struct cache_slot {
const char *key;
size_t keylen;
int ttl;
cache_fill_fn fn;
int cache_fd;
int lock_fd;
int saved_stdout;
const char *path;
const char *lock_path;
int key_matches;
int abandoned;
size_t max_bytes;
int oversized;
struct stat st;
int buflen;
char buf[CACHE_BUFSIZE];
};
static int open_slot(struct cache_slot *slot)
{
char *nul;
ssize_t keylen = -1;
slot->cache_fd = open(slot->path, O_RDONLY);
if (slot->cache_fd == -1)
return errno;
if (fstat(slot->cache_fd, &slot->st))
return errno;
slot->buflen = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
if (slot->buflen < 0)
return errno;
nul = memchr(slot->buf, 0, slot->buflen);
if (nul)
keylen = nul - slot->buf;
if (slot->key)
slot->key_matches =
keylen >= 0 &&
(size_t)keylen == slot->keylen &&
!memcmp(slot->key, slot->buf, keylen + 1);
return 0;
}
static int key_fits_slot(const char *key)
{
return strlen(key) + 1 <= CACHE_BUFSIZE;
}
static int close_slot(struct cache_slot *slot)
{
int err = 0;
if (slot->cache_fd > 0) {
if (close(slot->cache_fd))
err = errno;
else
slot->cache_fd = -1;
}
return err;
}
static int print_slot(struct cache_slot *slot)
{
off_t off;
#ifdef HAVE_LINUX_SENDFILE
off_t size;
#endif
off = slot->keylen + 1;
#ifdef HAVE_LINUX_SENDFILE
size = slot->st.st_size;
do {
ssize_t ret;
ret = sendfile(STDOUT_FILENO, slot->cache_fd, &off, size - off);
if (ret < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
if (errno == EINVAL || errno == ENOSYS)
break;
return errno;
}
if (off == size)
return 0;
} while (1);
#endif
if (lseek(slot->cache_fd, off, SEEK_SET) != off)
return errno;
do {
ssize_t ret;
ret = xread(slot->cache_fd, slot->buf, sizeof(slot->buf));
if (ret < 0)
return errno;
if (ret == 0)
return 0;
if (write_in_full(STDOUT_FILENO, slot->buf, ret) < 0)
return errno;
} while (1);
}
static int serve_slot(struct cache_slot *slot)
{
int err;
err = print_slot(slot);
if (err)
log_error("[cgit] Unable to send slot %s: %s (%d)\n", slot->path, strerror(err), err);
return err;
}
static int is_expired(struct cache_slot *slot)
{
if (slot->ttl < 0)
return 0;
return slot->st.st_mtime + slot->ttl * SECONDS_PER_MINUTE < time(NULL);
}
static int is_modified(struct cache_slot *slot)
{
struct stat current;
if (stat(slot->path, ¤t))
return 1;
return (
current.st_ino != slot->st.st_ino ||
current.st_mtime != slot->st.st_mtime ||
current.st_size != slot->st.st_size
);
}
static int close_lock(struct cache_slot *slot)
{
int err = 0;
if (slot->lock_fd > 0) {
if (close(slot->lock_fd))
err = errno;
else
slot->lock_fd = -1;
}
return err;
}
static int lock_slot(struct cache_slot *slot)
{
struct flock lock = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 0,
};
struct stat held, named;
slot->lock_fd = open(slot->lock_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (slot->lock_fd == -1)
return errno;
if (fcntl(slot->lock_fd, F_SETLK, &lock) < 0) {
int saved_errno = errno;
close(slot->lock_fd);
slot->lock_fd = -1;
return saved_errno;
}
if (
fstat(slot->lock_fd, &held) ||
stat(slot->lock_path, &named) ||
held.st_ino != named.st_ino ||
held.st_dev != named.st_dev
) {
close(slot->lock_fd);
slot->lock_fd = -1;
return EAGAIN;
}
if (ftruncate(slot->lock_fd, 0) < 0)
return errno;
if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0)
return errno;
return 0;
}
static int unlock_slot(struct cache_slot *slot, int replace_old_slot)
{
int err;
if (replace_old_slot)
err = rename(slot->lock_path, slot->path);
else
err = unlink(slot->lock_path);
if (slot->saved_stdout >= 0) {
dup2(slot->saved_stdout, STDOUT_FILENO);
close(slot->saved_stdout);
slot->saved_stdout = -1;
}
if (err)
return errno;
return 0;
}
static struct cache_slot *slot_being_filled;
static void replay_lock_file(struct cache_slot *slot)
{
off_t off = slot->keylen + 1;
ssize_t got;
if (lseek(slot->lock_fd, off, SEEK_SET) != off)
return;
while ((got = xread(slot->lock_fd, slot->buf, sizeof(slot->buf))) > 0) {
if (write_in_full(STDOUT_FILENO, slot->buf, got) < 0)
return;
}
}
int cache_abandon_fill(void)
{
struct cache_slot *slot = slot_being_filled;
if (!slot)
return 0;
slot_being_filled = NULL;
slot->abandoned = 1;
fflush(stdout);
if (slot->saved_stdout >= 0) {
dup2(slot->saved_stdout, STDOUT_FILENO);
close(slot->saved_stdout);
slot->saved_stdout = -1;
}
replay_lock_file(slot);
unlink(slot->lock_path);
return 1;
}
static void publish_slot(struct cache_slot *slot)
{
int err = unlock_slot(slot, 1);
if (err)
log_error("[cgit] Unable to publish cache slot %s: %s (%d)\n", slot->path,
strerror(err), err);
}
static int fill_slot(struct cache_slot *slot)
{
slot->saved_stdout = dup(STDOUT_FILENO);
if (slot->saved_stdout == -1)
return errno;
fcntl(slot->saved_stdout, F_SETFD, FD_CLOEXEC);
if (dup2(slot->lock_fd, STDOUT_FILENO) == -1)
return errno;
slot_being_filled = slot;
slot->fn();
slot_being_filled = NULL;
html_flush();
if (fflush(stdout))
return errno;
if (slot->abandoned)
return 0;
if (fstat(slot->lock_fd, &slot->st))
return errno;
slot->oversized = slot->max_bytes && (size_t)slot->st.st_size > slot->max_bytes;
return 0;
}
static void refresh_slot(struct cache_slot *slot)
{
if (lock_slot(slot))
return;
if (is_modified(slot) || fill_slot(slot)) {
unlock_slot(slot, 0);
close_lock(slot);
} else if (slot->abandoned) {
close_lock(slot);
} else {
close_slot(slot);
if (slot->oversized) {
unlink(slot->path);
unlock_slot(slot, 0);
} else {
publish_slot(slot);
}
slot->cache_fd = slot->lock_fd;
}
}
static int process_slot(struct cache_slot *slot)
{
int err;
err = open_slot(slot);
if (!err && slot->key_matches) {
if (is_expired(slot))
refresh_slot(slot);
if (slot->abandoned) {
close_slot(slot);
return 0;
}
err = serve_slot(slot);
close_slot(slot);
return err;
}
if (!err)
log_error("[cgit] Cache slot %s holds a different key, consider a larger cache-size\n",
slot->path);
close_slot(slot);
if ((err = lock_slot(slot)) != 0) {
log_error("[cgit] Unable to lock slot %s: %s (%d)\n", slot->lock_path, strerror(err), err);
slot->fn();
return 0;
}
if ((err = fill_slot(slot)) != 0) {
log_error("[cgit] Unable to fill slot %s: %s (%d)\n", slot->lock_path, strerror(err), err);
unlock_slot(slot, 0);
close_lock(slot);
if (!slot->abandoned)
slot->fn();
return 0;
}
if (slot->abandoned) {
close_lock(slot);
return 0;
}
slot->cache_fd = slot->lock_fd;
if (slot->oversized)
unlock_slot(slot, 0);
else
publish_slot(slot);
err = serve_slot(slot);
close_slot(slot);
return err;
}
static const char *format_time(const char *format, time_t when)
{
static char buf[64];
struct tm tm;
if (!when)
return "-";
gmtime_r(&when, &tm);
strftime(buf, sizeof(buf) - 1, format, &tm);
return buf;
}
unsigned long cache_hash_str(const char *str)
{
unsigned long h = FNV_OFFSET;
unsigned char *s = (unsigned char *)str;
if (!s)
return h;
while (*s) {
h *= FNV_PRIME;
h ^= *s++;
}
return h;
}
int cache_process(int size, const char *path, const char *key, int ttl, size_t max_bytes,
cache_fill_fn fn)
{
unsigned long hash;
int i;
struct strbuf slot_path = STRBUF_INIT;
struct strbuf lock_path = STRBUF_INIT;
struct cache_slot slot;
int result;
if (size <= 0 || ttl == 0) {
fn();
return 0;
}
if (!path) {
log_error("[cgit] Cache path not specified, caching is disabled\n");
fn();
return 0;
}
if (!key)
key = "";
if (!key_fits_slot(key)) {
log_error("[cgit] Cache key too long for a slot, caching is disabled for this request\n");
fn();
return 0;
}
hash = cache_hash_str(key) % size;
strbuf_addstr(&slot_path, path);
strbuf_ensure_end(&slot_path, '/');
for (i = 0; i < SLOT_NAME_LEN; i++) {
strbuf_addf(&slot_path, "%x", (unsigned char)(hash & 0xf));
hash >>= 4;
}
strbuf_addbuf(&lock_path, &slot_path);
strbuf_addstr(&lock_path, ".lock");
slot.fn = fn;
slot.ttl = ttl;
slot.saved_stdout = -1;
slot.abandoned = 0;
slot.max_bytes = max_bytes;
slot.oversized = 0;
slot.path = slot_path.buf;
slot.lock_path = lock_path.buf;
slot.key = key;
slot.keylen = strlen(key);
result = process_slot(&slot);
strbuf_release(&slot_path);
strbuf_release(&lock_path);
return result;
}
int cache_ls(const char *path)
{
DIR *dir;
struct dirent *ent;
int err = 0;
struct cache_slot slot = { 0 };
struct strbuf slot_path = STRBUF_INIT;
size_t prefixlen;
char *nul;
int keylen;
if (!path) {
log_error("[cgit] Cache path not specified\n");
return -1;
}
dir = opendir(path);
if (!dir) {
err = errno;
log_error("[cgit] Unable to open %s: %s (%d)\n", path, strerror(err), err);
return err;
}
strbuf_addstr(&slot_path, path);
strbuf_ensure_end(&slot_path, '/');
prefixlen = slot_path.len;
while ((ent = readdir(dir))) {
if (strlen(ent->d_name) != SLOT_NAME_LEN)
continue;
strbuf_setlen(&slot_path, prefixlen);
strbuf_addstr(&slot_path, ent->d_name);
slot.path = slot_path.buf;
if ((err = open_slot(&slot)) != 0) {
log_error("[cgit] Unable to open %s: %s (%d)\n", slot_path.buf, strerror(err), err);
close_slot(&slot);
continue;
}
nul = memchr(slot.buf, 0, slot.buflen);
keylen = nul ? (int)(nul - slot.buf) : slot.buflen;
htmlf(
"%s %s %10"PRIuMAX" %.*s\n", slot_path.buf,
format_time("%Y-%m-%d %H:%M:%S", slot.st.st_mtime),
(uintmax_t)slot.st.st_size, keylen, slot.buf
);
close_slot(&slot);
}
closedir(dir);
strbuf_release(&slot_path);
return 0;
}
|