-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.c
675 lines (577 loc) · 18 KB
/
cache.c
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#include "pch.h"
#include "cache.h"
#include "paths.h"
#include "state.h"
#include "tui.h"
#define DIR_PERMS (S_IRWXU | S_IRWXG)
static struct cache s_cache;
#if CACHE_USE_DISK
enum
cache_meta_info_id
{
CACHE_META_URI = 0,
CACHE_META_SIZE,
CACHE_META_MIME,
CACHE_META_TIMESTAMP,
CACHE_META_CHECKSUM,
CACHE_META_COUNT
};
static int
cache_create_path(const char *p) { return mkdir(p, DIR_PERMS); }
static int
cache_create_gem(void)
{
if (access(path_get(PATH_ID_CACHE_GEMINI), F_OK) == 0) return 0;
return cache_create_path(path_get(PATH_ID_CACHE_GEMINI));
}
static int
cache_create_ph(void)
{
if (access(path_get(PATH_ID_CACHE_GOPHER), F_OK) == 0) return 0;
return cache_create_path(path_get(PATH_ID_CACHE_GOPHER));
}
static const enum path_id CACHE_PATH_IDS[PROTOCOL_COUNT] =
{
[PROTOCOL_UNKNOWN] = PATH_ID_UNKNOWN,
[PROTOCOL_GEMINI] = PATH_ID_CACHE_GEMINI,
[PROTOCOL_GOPHER] = PATH_ID_CACHE_GOPHER,
[PROTOCOL_FINGER] = PATH_ID_UNKNOWN,
[PROTOCOL_FILE] = PATH_ID_UNKNOWN,
[PROTOCOL_MAILTO] = PATH_ID_UNKNOWN,
};
/* Generate filepath for on-disk cache item */
static void
cache_gen_filepath(
const struct uri *restrict const uri,
char *restrict path,
size_t path_size,
bool with_prefix)
{
size_t len;
if (with_prefix)
{
len = snprintf(path, path_size,
"%s/%s%s",
path_get(CACHE_PATH_IDS[uri->protocol]),
uri->hostname,
uri->path);
}
else
{
len = snprintf(path, path_size,
"%s%s",
uri->hostname,
uri->path);
}
// If the resource is part of a directory, we need to manually add a
// filename
if (path[len - 1] == '/')
{
strncat(path, "index", path_size - len);
}
else
{
char *canon_path;
char path_on_disk[FILENAME_MAX];
if (!with_prefix)
{
// Generate the path with the prefix in it
snprintf(path_on_disk, sizeof(path_on_disk),
"%s/%s",
path_get(CACHE_PATH_IDS[uri->protocol]),
path);
canon_path = path_on_disk;
}
else
{
// Already got prefix in the path
canon_path = path;
}
// Check if this resource name is already used by a directory; if so
// then this needs to become an 'index' file.
static struct stat path_stat;
if (stat(canon_path, &path_stat) < 0)
{
// Path doesn't exist; all fine
return;
}
if (path_stat.st_mode & S_IFDIR)
{
// Make the path an index file
strncat(path, "/index", path_size - len);
}
}
}
/*
* Attempts to convert a on-disk cache file to a directory which can store
* other cache files.
*
* This is useful in cases where we cache an item and wrongly assume it's not
* the index of a directory.
* e.g. applied situation:
* + We visit gemini.circumlunar.space/docs, and hence create a cache file
* called 'docs' (technically doesn't happen here as the site redirects to
* 'docs/', but we assume the server doesn't perform the redirect.
* + Then we visit gemini.circumlunar.space/docs/faq.gmi, and are now unable
* to cache the page faq.gmi because the on-disk 'docs' is not a directory
* + We attempt to remedy the issue by calling this function, which will
* rename the 'docs' file to 'index' under a newly-created 'docs/' directory
* The function returns false if it fails to make the directory, and true on
* success or if no action needed to be taken
*/
static bool
cache_file_to_dir(const char *fpath)
{
// Check if the given path indeed exists on disk as a file
static struct stat fpath_stat;
if (stat(fpath, &fpath_stat) < 0)
{
// No such file on disk
return false;
}
if (fpath_stat.st_mode & S_IFDIR)
{
// Path is already a directory; we don't need to do anything
return true;
}
// Generate a path to temporarily move the file
char path_index[FILENAME_MAX], path_tmp[FILENAME_MAX];
strncpy(path_tmp, path_get(PATH_ID_CACHE_TMP), FILENAME_MAX);
if (mkstemp(path_tmp) == -1) return false;
// Rename file to a temporary file name
if (rename(fpath, path_tmp) != 0) return false;
// Make a directory where the file was
if (mkdir(fpath, DIR_PERMS) != 0) return false;
// Move the file to the new directory
snprintf(path_index, sizeof(path_index), "%s/index", fpath);
if (rename(path_tmp, path_index) != 0) return false;
return true;
}
#endif // CACHE_USE_DISK
static struct cached_item *cache_get_next_item(void);
int
cache_init(void)
{
// Allocate in-memory cache
s_cache.capacity = CACHE_ITEM_CAPACITY_INITIAL;
s_cache.items = malloc(
sizeof(struct cached_item) * s_cache.capacity);
s_cache.count = 0;
s_cache.total_size = 0;
// If cache directory on disk doesn't exist, create it
#if CACHE_USE_DISK
if (access(path_get(PATH_ID_CACHE_ROOT), F_OK) != 0 &&
// Create with RW for owner and group
mkdir(path_get(PATH_ID_CACHE_ROOT), DIR_PERMS) != 0)
{
tui_status_begin();
tui_printf("cache: failed to make cache directory %s",
path_get(PATH_ID_CACHE_ROOT));
tui_status_end();
return -1;
}
if (cache_create_gem() != 0) return -1;
if (cache_create_ph() != 0) return -1;
#endif // CACHE_USE_DISK
return 0;
}
void
cache_deinit(void)
{
int i;
#if CACHE_USE_DISK
/*
* Flush whatever cache is left in memory to the disk
*/
FILE *fp, *fp_meta, *fp_meta_tmp;
char path[FILENAME_MAX];
/* Open the temporary metadata file for writing */
if (!(fp_meta_tmp = fopen(path_get(PATH_ID_CACHE_META_TMP), "w")))
goto abort_flush;
tui_status_say("Flushing cache to disk ...");
/*
* Iterate over cache items, write them to the disk and add to the metadata
* state
*/
for (i = 0; i < s_cache.count; ++i)
{
const struct cached_item *const item = &s_cache.items[i];
if (*item->uri.query)
{
// Skip if the item has a query--we don't cache these pages
continue;
}
/* Make all the directories as needed */
// So here, 'path' represents the entire path on-disk, 'path_rel' is
// the path from the start of the URI entry itself, which we iterate
// over to find directories to create.
snprintf(path, sizeof(path),
"%s/", path_get(CACHE_PATH_IDS[item->uri.protocol]));
char *path_rel = path + strlen(path);
cache_gen_filepath(&item->uri,
path_rel, sizeof(path) - (path_rel - path), false);
for (char *x = path_rel; *x; ++x)
{
if (*x != '/') continue;
// Found a slash; put a null-terminator so we can easily work this
// directory.
*x = '\0';
// Check if the directory exists
if (access(path, F_OK) != 0)
{
// Directory doesn't exist; so create it
if (mkdir(path, DIR_PERMS) != 0)
{
// Give up
goto fail;
}
}
else
{
// The path exists, now ensure it is indeed a *directory* and
// not an already-stored file
if (!cache_file_to_dir(path))
{
// Give up if converting it to a directory failed
goto fail;
}
}
// Fix the string so we can move to next directory
*x = '/';
}
if (!(fp = fopen(path, "w"))) continue;
// Write file to disk
fwrite(item->data, item->data_size, 1, fp);
fclose(fp);
// Append to the temporary meta file
fprintf(fp_meta_tmp,
"%.*s"
"\t%lu"
"\t%s"
"\t%lu"
"\t",
(int)item->uristr_len,
item->uristr,
item->data_size,
item->mime.str,
item->timestamp);
for (unsigned h = 0; h < item->hash_len; ++h)
{
fprintf(fp_meta_tmp, "%02x", item->hash[h]);
}
fprintf(fp_meta_tmp, "\n");
fail:
continue;
}
// Now open the real meta file for reading, and read any URIs that haven't
// already been written to the temporary one
bool did_read_meta;
if (access(path_get(PATH_ID_CACHE_META), F_OK) != 0 ||
!(fp_meta = fopen(path_get(PATH_ID_CACHE_META), "r")))
{
did_read_meta = false;
goto no_meta_file;
}
did_read_meta = true;
size_t tmp;
ssize_t len;
char *line;
for (line = NULL; (len = getline(&line, &tmp, fp_meta)) != -1;)
{
if (len < 1) continue;
size_t uri_len = min(strcspn(line, "\t\n"), len - 1);
// Check if this URI already has been written
for (i = 0; i < s_cache.count; ++i)
{
const struct cached_item *const item = &s_cache.items[i];
if (uri_len == item->uristr_len &&
strncmp(item->uristr, line, uri_len) == 0)
{
// Don't add this URI as we already have it
goto skip;
}
}
// Add the line to the temporary meta file
(void)!fwrite(line, len, 1, fp_meta_tmp);
skip:;
}
if (line) free(line);
fclose(fp_meta);
no_meta_file:
fclose(fp_meta_tmp);
/* Now replace the original metadata file with the temporary one */
if (did_read_meta)
{
remove(path_get(PATH_ID_CACHE_META_BAK));
// Make backup of meta.dir
if (rename(
path_get(PATH_ID_CACHE_META),
path_get(PATH_ID_CACHE_META_BAK)) != 0)
{
goto abort_flush;
}
}
// Make temp file the main one
if (rename(
path_get(PATH_ID_CACHE_META_TMP),
path_get(PATH_ID_CACHE_META)) != 0)
{
goto abort_flush;
}
abort_flush:
#endif // CACHE_USE_DISK
/* Free everything */
for (i = 0; i < s_cache.count; ++i)
{
const struct cached_item *const item = &s_cache.items[i];
free(item->data);
}
// Deallocate
free(s_cache.items);
}
/* Find a URI in the cache */
bool
cache_find(
const struct uri *restrict const uri,
struct cached_item **restrict const o)
{
g_recv->b_alt = NULL;
// If the link is a gopher item with no query then there's nothing to read
// from cache (as the link is a prompt)
if (uri->protocol == PROTOCOL_GOPHER &&
uri->gopher_item == GOPHER_ITEM_SEARCH &&
!*uri->query) return false;
// See if we have the URI cached in memory already
for (int i = 0; i < s_cache.count; ++i)
{
struct cached_item *item = &s_cache.items[i];
if (uri_cmp_notrailing(&item->uri, uri) != 0) continue;
// Page is cached; set it as the alternative recv buffer (instead of
// memcpy'ing directly into the recv buffer)
g_recv->size = item->data_size;
g_recv->b_alt = item->data;
g_recv->mime = item->mime;
*o = item;
return true;
}
// URIs with queries are not cached on disk
if (*uri->query) goto fail;
#if CACHE_USE_DISK
// Search the on-disk cache
static char path[FILENAME_MAX];
FILE *fp;
// Check if the file exists on the disk
cache_gen_filepath(uri, path, sizeof(path), true);
if (access(path, F_OK) != 0)
{
return false;
}
// Generate URI string
char uri_string[URI_STRING_MAX];
size_t uri_string_len = uri_str(
uri,
uri_string,
sizeof(uri_string),
URI_FLAGS_NO_PORT_BIT |
URI_FLAGS_NO_TRAILING_SLASH_BIT |
URI_FLAGS_NO_GOPHER_ITEM_BIT |
URI_FLAGS_NO_QUERY_BIT);
/* Read the metadata file */
if (!(fp = fopen(path_get(PATH_ID_CACHE_META), "r")))
{
return false;
}
// Item which will be added to memory pretty soon
struct cached_item item;
item.data_size = 0;
item.session.last_sel = -1;
item.session.last_scroll = 0;
tui_status_say("Checking disk cache ...");
// Iterate over metadata file's lines
size_t tmp;
ssize_t n_bytes;
bool read = false;
char *line;
for (line = NULL; (n_bytes = getline(&line, &tmp, fp)) != -1;)
{
if (n_bytes < 1) continue;
size_t uri_len = min(strcspn(line, "\t\n"), n_bytes - 1);
// Check if URIs match
if (uri_len != uri_string_len ||
strncmp(uri_string, line, uri_string_len) != 0) continue;
enum cache_meta_info_id meta_id = CACHE_META_URI + 1;
// Copy URI
item.uri = uri_parse(uri_string, uri_len);
item.uristr_len = uri_len;
strncpy(item.uristr, uri_string, uri_len);
// Parse file metadata
char *m_last = line + uri_len;
for (char *m = m_last + 1;;
++m)
{
if (*m && *m != '\t' && *m != '\n') continue;
switch(meta_id)
{
case CACHE_META_SIZE:
item.data_size = strtoul(m_last, NULL, 10);
break;
case CACHE_META_MIME:
mime_parse(&item.mime, m_last, m - m_last);
break;
case CACHE_META_TIMESTAMP:
item.timestamp = strtoul(m_last, NULL, 10);
break;
case CACHE_META_CHECKSUM:
item.hash_len = 0;
for (char *x = m_last;
x < m;
x += 2)
{
// I swear why can't there just be a strtol that accepts
// max bytes or something...
char *term = x + 2;
char term_old;
if (term < m)
{
term_old = *term;
*term = '\0';
}
item.hash[item.hash_len++] = strtol(x, NULL, 16);
if (term < m) *term = term_old;
}
break;
default: break;
}
// End of line
if (!*m || m >= line + n_bytes) break;
m_last = m + 1;
if (meta_id + 1 >= CACHE_META_COUNT) break;
++meta_id;
}
read = true;
break;
}
if (line) free(line);
fclose(fp);
if (!read) goto fail;
/* Read the file content from the disk */
fp = fopen(path, "r");
item.data = malloc(item.data_size);
// Read file
bool success = fread(item.data, item.data_size, 1, fp) > 0;
fclose(fp);
if (!success) goto fail;
// Copy to the actual pager buffer
g_recv->size = item.data_size;
g_recv->b_alt = item.data;
g_recv->mime = item.mime;
struct cached_item *item_real = cache_get_next_item();
if (!item_real)
{
free(item.data);
return false;
}
*item_real = item;
*o = item_real;
return true;
#endif // CACHE_USE_DISK
fail:
g_recv->size = 0;
return false;
}
/* Push current page to cache */
struct cached_item *
cache_push_current(void)
{
struct cached_item *item = NULL;
if (
// Don't cache internal pages
g_state.uri.protocol == PROTOCOL_INTERNAL) return NULL;
// Check if the URI is in the cache already; so we can update it
for (int i = 0; i < s_cache.count; ++i)
{
struct cached_item *m = &s_cache.items[i];
if (uri_cmp_notrailing(&m->uri, &g_state.uri) != 0) continue;
item = m;
s_cache.total_size -= m->data_size;
free(item->data);
break;
}
if (!item) { item = cache_get_next_item(); }
if (!item)
{
tui_status_begin();
tui_say("cache: max size of ");
tui_print_size(CACHE_IN_MEM_MAX_SIZE);
tui_say(" exceeded.");
tui_status_end();
return NULL;
}
item->uri = g_state.uri;
item->timestamp = time(NULL);
item->mime = g_recv->mime;
item->data_size = g_recv->size;
item->data = malloc(item->data_size);
item->session.last_sel = -1;
item->session.last_scroll = 0;
memcpy(item->data, g_recv->b, item->data_size);
#if CACHE_USE_DISK
// Disk-cache only stuff. Not applied to items which have queries as we
// don't store them on-disk
if (!*item->uri.query)
{
// Write URI string
item->uristr_len = uri_str(
&item->uri,
item->uristr,
sizeof(item->uristr),
URI_FLAGS_NO_PORT_BIT |
URI_FLAGS_NO_TRAILING_SLASH_BIT |
URI_FLAGS_NO_GOPHER_ITEM_BIT |
URI_FLAGS_NO_QUERY_BIT);
// Generate a SHA256 hash of the content. The algorithm used shouldn't
// matter too much, as it's literally only used to detect changes in
// file content.
const EVP_MD *md = EVP_sha256();
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, md, NULL);
EVP_DigestUpdate(ctx, item->data, item->data_size);
EVP_DigestFinal_ex(ctx, item->hash, &item->hash_len);
EVP_MD_CTX_free(ctx);
}
#endif // CACHE_USE_DISK
s_cache.total_size += item->data_size;
return item;
}
static struct cached_item *
cache_get_next_item(void)
{
struct cached_item *item = NULL;
if (s_cache.total_size + g_recv->size > CACHE_IN_MEM_MAX_SIZE &&
s_cache.count)
{
// Replace oldest cached item
time_t oldest = s_cache.items[0].timestamp;
for (int i = 0; i < s_cache.count; ++i)
{
if (s_cache.items[i].timestamp < oldest)
{
item = &s_cache.items[i];
oldest = item->timestamp;
}
}
if (item)
{
free(item->data);
s_cache.total_size -= item->data_size;
}
return item;
}
// Add a new item
if (s_cache.count + 1 >= s_cache.capacity)
{
// TODO: reallocate
return NULL;
}
return &s_cache.items[s_cache.count++];
}