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