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