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