]> diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
498efe224399efa5395cbb8ab67ba0f1a3232092
[nmh] / uip / mhcachesbr.c
1 /* mhcachesbr.c -- routines to manipulate the MIME content cache
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/m_gmprot.h"
10 #include "sbr/m_getfld.h"
11 #include "sbr/read_yes_or_no_if_tty.h"
12 #include "sbr/trimcpy.h"
13 #include "sbr/error.h"
14 #include <fcntl.h>
15 #include "h/mts.h"
16 #include "h/tws.h"
17 #include "h/mime.h"
18 #include "h/mhparse.h"
19 #include "h/mhcachesbr.h"
20 #include "h/utils.h"
21 #include "mhmisc.h"
22 #include "sbr/lock_file.h"
23 #include "sbr/m_mktemp.h"
24
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 #endif
28 #include <time.h>
29
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(CACHE, caches);
32 #undef X
33 struct swit *cache_policy = caches;
34
35 extern int debugsw;
36
37 /* cache policies */
38 int rcachesw = CACHE_ASK;
39 int wcachesw = CACHE_ASK;
40
41 /*
42 * Location of public and private cache. These must
43 * be set before these routines are called.
44 */
45 char *cache_public;
46 char *cache_private;
47
48 /*
49 * static prototypes
50 */
51 static void cache_content (CT);
52 static int find_cache_aux (int, char *, char *, char *, int);
53 static int find_cache_aux2 (char *, char *, char *, int);
54
55
56 /*
57 * Top level entry point to cache content
58 * from a group of messages
59 */
60
61 void
62 cache_all_messages (CT *cts)
63 {
64 CT ct, *ctp;
65
66 for (ctp = cts; *ctp; ctp++) {
67 ct = *ctp;
68 if (type_ok (ct, 1)) {
69 cache_content (ct);
70 if (ct->c_fp) {
71 fclose (ct->c_fp);
72 ct->c_fp = NULL;
73 }
74 if (ct->c_ceclosefnx)
75 (*ct->c_ceclosefnx) (ct);
76 }
77 }
78 flush_errors ();
79 }
80
81
82 /*
83 * Entry point to cache content from external sources.
84 */
85
86 static void
87 cache_content (CT ct)
88 {
89 int cachetype;
90 char *file, cachefile[BUFSIZ];
91 CE ce = &ct->c_cefile;
92
93 if (!ct->c_id) {
94 inform("no %s: field in %s", ID_FIELD, ct->c_file);
95 return;
96 }
97
98 if (!ce) {
99 inform("unable to decode %s", ct->c_file);
100 return;
101 }
102
103 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
104 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
105 == NOTOK) {
106 inform("unable to cache %s's contents", ct->c_file);
107 return;
108 }
109 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
110 fflush (stdout);
111 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
112 cachefile);
113 }
114
115 if (ce->ce_file) {
116 int mask = umask (cachetype ? ~m_gmprot () : 0222);
117 FILE *fp;
118
119 if (debugsw)
120 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
121
122 file = NULL;
123 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
124 goto reset_umask;
125
126 if ((fp = fopen (cachefile, "w"))) {
127 int cc;
128 char buffer[BUFSIZ];
129 FILE *gp = ce->ce_fp;
130
131 fseek (gp, 0L, SEEK_SET);
132
133 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
134 > 0)
135 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
136 advise ("cache_content", "fwrite");
137 }
138 fflush (fp);
139
140 if (ferror (gp)) {
141 admonish (ce->ce_file, "error reading");
142 (void) m_unlink (cachefile);
143 } else {
144 if (ferror (fp)) {
145 admonish (cachefile, "error writing");
146 (void) m_unlink (cachefile);
147 }
148 }
149 fclose (fp);
150 } else
151 content_error (cachefile, ct, "unable to fopen for writing");
152 reset_umask:
153 umask (mask);
154 } else {
155 if (debugsw)
156 fprintf (stderr, "in place caching...\n");
157
158 file = cachefile;
159 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
160 chmod (cachefile, cachetype ? m_gmprot () : 0444);
161 }
162 }
163
164
165 int
166 find_cache (CT ct, int policy, int *writing, char *id,
167 char *buffer, int buflen)
168 {
169 int status = NOTOK;
170
171 if (id == NULL)
172 return NOTOK;
173 id = trimcpy (id);
174
175 if (debugsw)
176 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
177 policy, writing ? "writing" : "reading", id);
178
179 switch (policy) {
180 case CACHE_NEVER:
181 default:
182 break;
183
184 case CACHE_ASK:
185 case CACHE_PUBLIC:
186 if (cache_private
187 && !writing
188 && find_cache_aux (0, cache_private, id,
189 buffer, buflen) == OK) {
190 if (access (buffer, R_OK) != NOTOK) {
191 got_private:
192 if (writing)
193 *writing = 1;
194 got_it:
195 status = OK;
196 break;
197 }
198 }
199 if (cache_public
200 && find_cache_aux (writing ? 1 : 0, cache_public, id,
201 buffer, buflen) == OK) {
202 if (writing || access (buffer, R_OK) != NOTOK) {
203 if (writing)
204 *writing = 0;
205 goto got_it;
206 }
207 }
208 break;
209
210 case CACHE_PRIVATE:
211 if (cache_private
212 && find_cache_aux (writing ? 2 : 0, cache_private, id,
213 buffer, buflen) == OK) {
214 if (writing || access (buffer, R_OK) != NOTOK)
215 goto got_private;
216 }
217 break;
218
219 }
220
221 if (status == OK && policy == CACHE_ASK) {
222 int len, buflen;
223 char *bp, query[BUFSIZ];
224
225 /* Get buffer ready to go */
226 bp = query;
227 buflen = sizeof(query);
228
229 /* Now, construct query */
230 if (writing) {
231 snprintf (bp, buflen, "Make cached, publicly-accessible copy");
232 } else {
233 struct stat st;
234
235 snprintf (bp, buflen, "Use cached copy");
236 len = strlen (bp);
237 bp += len;
238 buflen -= len;
239
240 if (ct->c_partno) {
241 snprintf (bp, buflen, " of content %s", ct->c_partno);
242 len = strlen (bp);
243 bp += len;
244 buflen -= len;
245 }
246
247 stat (buffer, &st);
248 snprintf (bp, buflen, " (size %lu octets)",
249 (unsigned long) st.st_size);
250 }
251 len = strlen (bp);
252 bp += len;
253 buflen -= len;
254
255 snprintf (bp, buflen, "\n in file %s? ", buffer);
256
257 /* Now, check answer */
258 if (!read_yes_or_no_if_tty (query))
259 status = NOTOK;
260 }
261
262 if (status == OK && writing) {
263 if (*writing && strchr(buffer, '/'))
264 make_intermediates (buffer);
265 (void) m_unlink (buffer);
266 }
267
268 free (id);
269 return status;
270 }
271
272
273 static int
274 find_cache_aux (int writing, char *directory, char *id,
275 char *buffer, int buflen)
276 {
277 int mask;
278 bool usemap;
279 char mapfile[BUFSIZ], mapname[BUFSIZ];
280 FILE *fp;
281 int failed_to_lock = 0;
282 static int partno, pid;
283 static time_t clock = 0;
284
285 usemap = true;
286
287 if (debugsw)
288 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
289
290 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
291 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
292 goto done_map;
293
294 if (!writing) {
295 if (usemap)
296 return NOTOK;
297
298 use_raw:
299 snprintf (buffer, buflen, "%s/%s", directory, id);
300 return OK;
301 }
302
303 if (!usemap && access (mapfile, W_OK) == NOTOK)
304 goto use_raw;
305
306 if (clock != 0) {
307 time_t now;
308
309 time (&now);
310 if (now > clock)
311 clock = 0;
312 } else {
313 pid = getpid ();
314 }
315
316 if (clock == 0) {
317 time (&clock);
318 partno = 0;
319 } else {
320 if (partno > 0xff) {
321 clock++;
322 partno = 0;
323 }
324 }
325
326 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
327 (unsigned int) (clock & 0xffffffff),
328 (unsigned int) (pid & 0xffff),
329 (unsigned int) (partno++ & 0xff));
330
331 if (debugsw)
332 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
333
334 make_intermediates (mapfile);
335 mask = umask (writing == 2 ? 0077 : 0);
336 if (!(fp = lkfopendata (mapfile, "a", &failed_to_lock)) && errno == ENOENT) {
337 int fd;
338
339 if ((fd = creat (mapfile, 0666)) != NOTOK) {
340 close (fd);
341 fp = lkfopendata (mapfile, "a", &failed_to_lock);
342 if (failed_to_lock) {
343 adios (mapfile, "failed to lock");
344 }
345 }
346 }
347 umask (mask);
348 if (!fp)
349 return NOTOK;
350 fprintf (fp, "%s: %s\n", mapname, id);
351 lkfclosedata (fp, mapfile);
352
353 done_map:
354 if (*mapname == '/')
355 strncpy (buffer, mapname, buflen);
356 else
357 snprintf (buffer, buflen, "%s/%s", directory, mapname);
358 if (debugsw)
359 fprintf (stderr, "use %s\n", buffer);
360
361 return OK;
362 }
363
364
365 static int
366 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
367 {
368 int state;
369 char buf[NMH_BUFSIZ], name[NAMESZ];
370 FILE *fp;
371 m_getfld_state_t gstate;
372 int failed_to_lock = 0;
373
374 if (!(fp = lkfopendata (mapfile, "r", &failed_to_lock)))
375 return NOTOK;
376
377 gstate = m_getfld_state_init(fp);
378 for (;;) {
379 int result;
380 char *cp, *dp;
381 int bufsz = sizeof buf;
382
383 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
384 case FLD:
385 case FLDPLUS:
386 strncpy (mapname, name, namelen);
387 if (state != FLDPLUS)
388 cp = buf;
389 else {
390 cp = mh_xstrdup(buf);
391 while (state == FLDPLUS) {
392 bufsz = sizeof buf;
393 state = m_getfld2(&gstate, name, buf, &bufsz);
394 cp = add (buf, cp);
395 }
396 }
397 dp = trimcpy (cp);
398 if (cp != buf)
399 free (cp);
400 if (debugsw)
401 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
402 mapname);
403 result = strcmp (id, dp);
404 free (dp);
405 if (result == 0) {
406 lkfclosedata (fp, mapfile);
407 return OK;
408 }
409 continue;
410
411 case BODY:
412 case FILEEOF:
413 default:
414 break;
415 }
416 break;
417 }
418 m_getfld_state_destroy (&gstate);
419
420 lkfclosedata (fp, mapfile);
421 return NOTOK;
422 }