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