Remove OS-specific defines. As part of it, remove scary longjmp() inside
[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     usemap = 1;
316
317     if (debugsw)
318         fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
319
320     snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
321     if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
322         goto done_map;
323
324     if (!writing) {
325         if (usemap)
326             return NOTOK;
327
328 use_raw:
329         snprintf (buffer, buflen, "%s/%s", directory, id);
330         return OK;
331     }
332
333     if (!usemap && access (mapfile, W_OK) == NOTOK)
334         goto use_raw;
335
336     if (clock != 0) {
337         time_t now;
338         
339         time (&now);
340         if (now > clock)
341             clock = 0;
342     } else {
343         pid = getpid ();
344     }
345
346     if (clock == 0) {
347         time (&clock);
348         partno = 0;
349     } else {
350         if (partno > 0xff) {
351             clock++;
352             partno = 0;
353         }
354     }
355
356     snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
357                 (unsigned int) (clock & 0xffffffff),
358                 (unsigned int) (pid & 0xffff),
359                 (unsigned int) (partno++ & 0xff));
360
361     if (debugsw)
362         fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
363
364     make_intermediates (mapfile);
365     mask = umask (writing == 2 ? 0077 : 0);
366     if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
367         int fd;
368
369         if ((fd = creat (mapfile, 0666)) != NOTOK) {
370             close (fd);
371             fp = lkfopen (mapfile, "a");
372         }
373     }
374     umask (mask);
375     if (!fp)
376         return NOTOK;
377     fprintf (fp, "%s: %s\n", mapname, id);
378     lkfclose (fp, mapfile);
379
380 done_map:
381     if (*mapname == '/')
382         strncpy (buffer, mapname, buflen);
383     else
384         snprintf (buffer, buflen, "%s/%s", directory, mapname);
385     if (debugsw)
386         fprintf (stderr, "use %s\n", buffer);
387
388     return OK;
389 }
390
391
392 static int
393 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
394 {
395     int state;
396     char buf[BUFSIZ], name[NAMESZ];
397     FILE *fp;
398
399     if (!(fp = lkfopen (mapfile, "r")))
400         return NOTOK;
401
402     for (state = FLD;;) {
403         int result;
404         char *cp, *dp;
405
406         switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
407             case FLD:
408             case FLDPLUS:
409             case FLDEOF:
410                 strncpy (mapname, name, namelen);
411                 if (state != FLDPLUS)
412                     cp = buf;
413                 else {
414                     cp = add (buf, NULL);
415                     while (state == FLDPLUS) {
416                         state = m_getfld (state, name, buf, sizeof(buf), fp);
417                         cp = add (buf, cp);
418                     }
419                 }
420                 dp = trimcpy (cp);
421                 if (cp != buf)
422                     free (cp);
423                 if (debugsw)
424                     fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
425                              mapname);
426                 result = strcmp (id, dp);
427                 free (dp);
428                 if (result == 0) {
429                     lkfclose (fp, mapfile);
430                     return OK;
431                 }
432                 if (state != FLDEOF)
433                     continue;
434                 /* else fall... */
435
436             case BODY:
437             case BODYEOF:
438             case FILEEOF:
439             default:
440                 break;
441         }
442         break;
443     }
444
445     lkfclose (fp, mapfile);
446     return NOTOK;
447 }