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