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