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 <sys/time.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <time.h>
26 #include "ev.h"
27 #include "hist.h"
28 #include "mcache.h"
29 #include "parser.h"
30 #include "telescope.h"
31 #include "utils.h"
32 #include "xwrapper.h"
34 static struct timeval tv = { 5 * 60, 0 };
35 static unsigned int timeout;
37 static struct ohash h;
38 static size_t npages;
39 static size_t tot;
41 struct mcache_entry {
42 time_t ts;
43 const struct parser *parser;
44 int trust;
45 char *buf;
46 size_t buflen;
47 char url[];
48 };
50 static void
51 mcache_free_entry(const char *url)
52 {
53 struct mcache_entry *e;
54 unsigned int slot;
56 slot = ohash_qlookup(&h, url);
57 if ((e = ohash_remove(&h, slot)) == NULL)
58 return;
60 npages--;
61 tot -= e->buflen;
63 free(e->buf);
64 free(e);
65 }
67 static void
68 clean_old_entries(int fd, int ev, void *data)
69 {
70 struct mcache_entry *e;
71 unsigned int i;
72 time_t threshold;
74 /* delete pages older than an hour */
75 threshold = time(NULL) - 60 * 60;
77 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
78 if (e->ts < threshold)
79 mcache_free_entry(e->url);
81 timeout = ev_timer(&tv, clean_old_entries, NULL);
82 }
84 void
85 mcache_init(void)
86 {
87 struct ohash_info info = {
88 .key_offset = offsetof(struct mcache_entry, url),
89 .calloc = hash_calloc,
90 .free = hash_free,
91 .alloc = hash_alloc,
92 };
94 ohash_init(&h, 5, &info);
95 }
97 int
98 mcache_tab(struct tab *tab)
99 {
100 struct mcache_entry *e;
101 unsigned int slot;
102 size_t l, len;
103 const char *url;
104 FILE *fp;
106 url = hist_cur(tab->hist);
107 l = strlen(url);
108 len = sizeof(*e) + l + 1;
110 e = xcalloc(1, len);
111 e->ts = time(NULL);
112 e->parser = tab->buffer.parser;
113 e->trust = tab->trust;
114 memcpy(e->url, url, l);
116 if ((fp = open_memstream(&e->buf, &e->buflen)) == NULL)
117 goto err;
119 if (!parser_serialize(&tab->buffer, fp))
120 goto err;
122 fclose(fp);
124 /* free any previously cached copies of this page */
125 mcache_free_entry(url);
127 slot = ohash_qlookup(&h, url);
128 ohash_insert(&h, slot, e);
130 npages++;
131 tot += e->buflen;
133 if (!ev_timer_pending(timeout))
134 timeout = ev_timer(&tv, clean_old_entries, NULL);
136 return 0;
138 err:
139 if (fp != NULL)
140 fclose(fp);
141 if (e->buf != NULL)
142 free(e->buf);
143 free(e);
144 return -1;
147 int
148 mcache_lookup(const char *url, struct tab *tab)
150 struct mcache_entry *e;
151 unsigned int slot;
153 slot = ohash_qlookup(&h, url);
154 if ((e = ohash_find(&h, slot)) == NULL)
155 return 0;
157 parser_init(&tab->buffer, e->parser);
158 if (!parser_parse(&tab->buffer, e->buf, e->buflen))
159 goto err;
160 if (!parser_free(tab))
161 goto err;
163 tab->trust = e->trust;
164 return 1;
166 err:
167 parser_free(tab);
168 erase_buffer(&tab->buffer);
169 return 0;
172 void
173 mcache_info(size_t *r_npages, size_t *r_tot)
175 *r_npages = npages;
176 *r_tot = tot;