Relayouted all switch statements: case aligns with switch.
[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",
151                                         ce->ce_file);
152
153                 file = NULL;
154                 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
155                         goto reset_umask;
156
157                 if ((fp = fopen(cachefile, "w"))) {
158                         int cc;
159                         char buffer[BUFSIZ];
160                         FILE *gp = ce->ce_fp;
161
162                         fseek(gp, 0L, SEEK_SET);
163
164                         while ((cc = fread(buffer, sizeof(*buffer),
165                                 sizeof(buffer), gp)) > 0)
166                                 fwrite(buffer, sizeof(*buffer), cc, fp);
167                         fflush(fp);
168
169                         if (ferror(gp)) {
170                                 admonish(ce->ce_file, "error reading");
171                                 unlink(cachefile);
172                         } else {
173                                 if (ferror(fp)) {
174                                         admonish(cachefile, "error writing");
175                                         unlink(cachefile);
176                                 }
177                         }
178                         fclose(fp);
179                 } else
180                         content_error(cachefile, ct,
181                                         "unable to fopen for writing");
182 reset_umask:
183                 umask(mask);
184         } else {
185                 if (debugsw)
186                         fprintf(stderr, "in place caching...\n");
187
188                 file = cachefile;
189                 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
190                         chmod(cachefile, cachetype ? m_gmprot() : 0444);
191         }
192 }
193
194
195 int
196 find_cache(CT ct, int policy, int *writing, char *id, char *buffer, int buflen)
197 {
198         int status = NOTOK;
199
200         if (id == NULL)
201                 return NOTOK;
202         id = trimcpy(id);
203
204         if (debugsw)
205                 fprintf(stderr, "find_cache %s(%d) %s %s\n",
206                                 caches[policy].sw, policy,
207                                 writing ? "writing" : "reading", id);
208
209         switch (policy) {
210         case CACHE_NEVER:
211         default:
212                 break;
213
214         case CACHE_ASK:
215         case CACHE_PUBLIC:
216                 if (cache_private && !writing &&
217                                 find_cache_aux(writing ? 2 : 0,
218                                 cache_private, id, buffer, buflen) == OK) {
219                         if (access(buffer, R_OK) != NOTOK) {
220 got_private:
221                                 if (writing)
222                                         *writing = 1;
223 got_it:
224                                 status = OK;
225                                 break;
226                         }
227                 }
228                 if (cache_public && find_cache_aux(writing ? 1 : 0,
229                                 cache_public, id, buffer, buflen) == OK) {
230                         if (writing || access(buffer, R_OK) != NOTOK) {
231                                 if (writing)
232                                         *writing = 0;
233                                 goto got_it;
234                         }
235                 }
236                 break;
237
238         case CACHE_PRIVATE:
239                 if (cache_private && find_cache_aux(writing ? 2 : 0,
240                                 cache_private, id, buffer, buflen) == OK) {
241                         if (writing || access(buffer, R_OK) != NOTOK)
242                                 goto got_private;
243                 }
244                 break;
245
246         }
247
248         if (status == OK && policy == CACHE_ASK) {
249                 int len, buflen;
250                 char *bp, query[BUFSIZ];
251
252                 if (xpid) {
253                         if (xpid < 0)
254                                 xpid = -xpid;
255                         pidcheck(pidwait(xpid, NOTOK));
256                         xpid = 0;
257                 }
258
259                 /* Get buffer ready to go */
260                 bp = query;
261                 buflen = sizeof(query);
262
263                 /* Now, construct query */
264                 if (writing) {
265                         snprintf(bp, buflen, "Make cached, publically-accessible copy");
266                 } else {
267                         struct stat st;
268
269                         snprintf(bp, buflen, "Use cached copy");
270                         len = strlen(bp);
271                         bp += len;
272                         buflen -= len;
273
274                         if (ct->c_partno) {
275                                 snprintf(bp, buflen, " of content %s",
276                                                 ct->c_partno);
277                                 len = strlen(bp);
278                                 bp += len;
279                                 buflen -= len;
280                         }
281
282                         stat(buffer, &st);
283                         snprintf(bp, buflen, " (size %lu octets)",
284                                 (unsigned long) st.st_size);
285                 }
286                 len = strlen(bp);
287                 bp += len;
288                 buflen -= len;
289
290                 snprintf(bp, buflen, "\n    in file %s? ", buffer);
291
292                 /* Now, check answer */
293                 if (!getanswer(query))
294                         status = NOTOK;
295         }
296
297         if (status == OK && writing) {
298                 if (*writing && strchr(buffer, '/'))
299                         make_intermediates(buffer);
300                 unlink(buffer);
301         }
302
303         free(id);
304         return status;
305 }
306
307
308 static int
309 find_cache_aux(int writing, char *directory, char *id, char *buffer,
310                 int buflen)
311 {
312         int mask, usemap;
313         char mapfile[BUFSIZ], mapname[BUFSIZ];
314         FILE *fp;
315         static int partno, pid;
316         static time_t clock = 0;
317
318 #ifdef BSD42
319         usemap = strchr(id, '/') ? 1 : 0;
320 #else
321         usemap = 1;
322 #endif
323
324         if (debugsw)
325                 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
326                                 directory, usemap);
327
328         snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
329         if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
330                 goto done_map;
331
332         if (!writing) {
333                 if (usemap)
334                         return NOTOK;
335
336 use_raw:
337                 snprintf(buffer, buflen, "%s/%s", directory, id);
338                 return OK;
339         }
340
341         if (!usemap && access(mapfile, W_OK) == NOTOK)
342                 goto use_raw;
343
344         if (clock != 0) {
345                 time_t now;
346
347                 time(&now);
348                 if (now > clock)
349                         clock = 0;
350         } else {
351                 pid = getpid();
352         }
353
354         if (clock == 0) {
355                 time(&clock);
356                 partno = 0;
357         } else {
358                 if (partno > 0xff) {
359                         clock++;
360                         partno = 0;
361                 }
362         }
363
364         snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
365                         (unsigned int) (clock & 0xffffffff),
366                         (unsigned int) (pid & 0xffff),
367                         (unsigned int) (partno++ & 0xff));
368
369         if (debugsw)
370                 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
371
372         make_intermediates(mapfile);
373         mask = umask(writing == 2 ? 0077 : 0);
374         if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
375                 int fd;
376
377                 if ((fd = creat(mapfile, 0666)) != NOTOK) {
378                         close(fd);
379                         fp = lkfopen(mapfile, "a");
380                 }
381         }
382         umask(mask);
383         if (!fp)
384                 return NOTOK;
385         fprintf(fp, "%s: %s\n", mapname, id);
386         lkfclose(fp, mapfile);
387
388 done_map:
389         if (*mapname == '/')
390                 strncpy(buffer, mapname, buflen);
391         else
392                 snprintf(buffer, buflen, "%s/%s", directory, mapname);
393         if (debugsw)
394                 fprintf(stderr, "use %s\n", buffer);
395
396         return OK;
397 }
398
399
400 static int
401 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
402 {
403         int state;
404         char buf[BUFSIZ], name[NAMESZ];
405         FILE *fp;
406
407         if (!(fp = lkfopen(mapfile, "r")))
408                 return NOTOK;
409
410         for (state = FLD;;) {
411                 int result;
412                 char *cp, *dp;
413
414                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
415                 case FLD:
416                 case FLDPLUS:
417                 case FLDEOF:
418                         strncpy(mapname, name, namelen);
419                         if (state != FLDPLUS)
420                                 cp = buf;
421                         else {
422                                 cp = getcpy(buf);
423                                 while (state == FLDPLUS) {
424                                         state = m_getfld(state, name, buf,
425                                                         sizeof(buf), fp);
426                                         cp = add(buf, cp);
427                                 }
428                         }
429                         dp = trimcpy(cp);
430                         if (cp != buf)
431                                 free(cp);
432                         if (debugsw)
433                                 fprintf(stderr, "compare %s to %s <- %s\n",
434                                                 id, dp, mapname);
435                         result = strcmp(id, dp);
436                         free(dp);
437                         if (result == 0) {
438                                 lkfclose(fp, mapfile);
439                                 return OK;
440                         }
441                         if (state != FLDEOF)
442                                 continue;
443                         /* else fall... */
444
445                 case BODY:
446                 case BODYEOF:
447                 case FILEEOF:
448                 default:
449                         break;
450                 }
451                 break;
452         }
453
454         lkfclose(fp, mapfile);
455         return NOTOK;
456 }