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