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