Rearranged whitespace (and comments) in all the code!
[mmh] / uip / mhcachesbr.c
1 /*
2  * mhcachesbr.c -- routines to manipulate the MIME content cache
3  *
4  * This code is Copyright (c) 2002, by the authors of nmh.  See the
5  * COPYRIGHT file in the root directory of the nmh distribution for
6  * complete copyright information.
7  */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <h/md5.h>
13 #include <errno.h>
14 #include <setjmp.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 TIME_WITH_SYS_TIME
24 # include <sys/time.h>
25 # include <time.h>
26 #else
27 # ifdef TM_IN_SYS_TIME
28 #  include <sys/time.h>
29 # else
30 #  include <time.h>
31 # endif
32 #endif
33
34 #ifdef HAVE_SYS_WAIT_H
35 # include <sys/wait.h>
36 #endif
37
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),
164                                 sizeof(buffer), gp)) > 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 && !writing
215                                 && find_cache_aux (writing ? 2 : 0,
216                                 cache_private, id, buffer, buflen) == OK) {
217                                 if (access (buffer, R_OK) != NOTOK) {
218 got_private:
219                                         if (writing)
220                                                 *writing = 1;
221 got_it:
222                                         status = OK;
223                                         break;
224                                 }
225                         }
226                         if (cache_public && find_cache_aux (writing ? 1 : 0,
227                                 cache_public, id, buffer, buflen) == OK) {
228                                 if (writing || access (buffer, R_OK) != NOTOK) {
229                                         if (writing)
230                                                 *writing = 0;
231                                         goto got_it;
232                                 }
233                         }
234                         break;
235
236                 case CACHE_PRIVATE:
237                         if (cache_private && find_cache_aux (writing ? 2 : 0,
238                                 cache_private, id, 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 }