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