Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
23 #include "telescope.h"
24 #include "mcache.h"
25 #include "parser.h"
26 #include "utils.h"
28 static struct timeval tv = { 5 * 60, 0 };
29 static struct event timerev;
31 static struct ohash h;
32 static size_t npages;
33 static size_t tot;
35 struct mcache_entry {
36 time_t ts;
37 parserfn parser;
38 int trust;
39 char *buf;
40 size_t buflen;
41 char url[];
42 };
44 static void
45 mcache_free_entry(const char *url)
46 {
47 struct mcache_entry *e;
48 unsigned int slot;
50 slot = ohash_qlookup(&h, url);
51 if ((e = ohash_remove(&h, slot)) == NULL)
52 return;
54 npages--;
55 tot -= e->buflen;
57 free(e->buf);
58 free(e);
59 }
61 static void
62 clean_old_entries(int fd, short ev, void *data)
63 {
64 struct mcache_entry *e;
65 unsigned int i;
66 time_t treshold;
68 /* delete pages older than an hour */
69 treshold = time(NULL) - 60 * 60;
71 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
72 if (e->ts < treshold)
73 mcache_free_entry(e->url);
75 evtimer_add(&timerev, &tv);
76 }
78 void
79 mcache_init(void)
80 {
81 struct ohash_info info = {
82 .key_offset = offsetof(struct mcache_entry, url),
83 .calloc = hash_calloc,
84 .free = hash_free,
85 .alloc = hash_alloc,
86 };
88 ohash_init(&h, 5, &info);
90 evtimer_set(&timerev, clean_old_entries, NULL);
91 }
93 int
94 mcache_tab(struct tab *tab)
95 {
96 struct mcache_entry *e;
97 unsigned int slot;
98 size_t l, len;
99 const char *url;
100 FILE *fp;
102 url = tab->hist_cur->h;
103 l = strlen(url);
104 len = sizeof(*e) + l + 1;
106 if ((e = calloc(1, len)) == NULL)
107 return -1;
108 e->ts = time(NULL);
109 e->parser = tab->buffer.page.init;
110 e->trust = tab->trust;
111 memcpy(e->url, url, l);
113 if ((fp = open_memstream(&e->buf, &e->buflen)) == NULL)
114 goto err;
116 if (!parser_serialize(tab, fp))
117 goto err;
119 fclose(fp);
121 /* free any previously cached copies of this page */
122 mcache_free_entry(url);
124 slot = ohash_qlookup(&h, url);
125 ohash_insert(&h, slot, e);
127 npages++;
128 tot += e->buflen;
130 if (!evtimer_pending(&timerev, NULL))
131 evtimer_add(&timerev, &tv);
133 return 0;
135 err:
136 if (fp != NULL)
137 fclose(fp);
138 if (e->buf != NULL)
139 free(e->buf);
140 free(e);
141 return -1;
144 int
145 mcache_lookup(const char *url, struct tab *tab)
147 struct mcache_entry *e;
148 unsigned int slot;
150 slot = ohash_qlookup(&h, url);
151 if ((e = ohash_find(&h, slot)) == NULL)
152 return 0;
154 parser_init(tab, e->parser);
155 if (!parser_parse(tab, e->buf, e->buflen))
156 goto err;
157 if (!parser_free(tab))
158 goto err;
160 tab->trust = e->trust;
161 return 1;
163 err:
164 parser_free(tab);
165 erase_buffer(&tab->buffer);
166 return 0;
169 void
170 mcache_info(size_t *r_npages, size_t *r_tot)
172 *r_npages = npages;
173 *r_tot = tot;