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