Completely reworked the path convertion functions
[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)
219                                         == OK) {
220                                 if (access(buffer, R_OK) != NOTOK) {
221 got_private:
222                                         if (writing)
223                                                 *writing = 1;
224 got_it:
225                                         status = OK;
226                                         break;
227                                 }
228                         }
229                         if (cache_public && find_cache_aux(writing ? 1 : 0,
230                                         cache_public, id, buffer, buflen)
231                                         == OK) {
232                                 if (writing || access(buffer, R_OK) != NOTOK) {
233                                         if (writing)
234                                                 *writing = 0;
235                                         goto got_it;
236                                 }
237                         }
238                         break;
239
240                 case CACHE_PRIVATE:
241                         if (cache_private && find_cache_aux(writing ? 2 : 0,
242                                         cache_private, id, buffer, buflen)
243                                         == OK) {
244                                 if (writing || access(buffer, R_OK) != NOTOK)
245                                         goto got_private;
246                         }
247                         break;
248
249         }
250
251         if (status == OK && policy == CACHE_ASK) {
252                 int len, buflen;
253                 char *bp, query[BUFSIZ];
254
255                 if (xpid) {
256                         if (xpid < 0)
257                                 xpid = -xpid;
258                         pidcheck(pidwait(xpid, NOTOK));
259                         xpid = 0;
260                 }
261
262                 /* Get buffer ready to go */
263                 bp = query;
264                 buflen = sizeof(query);
265
266                 /* Now, construct query */
267                 if (writing) {
268                         snprintf(bp, buflen, "Make cached, publically-accessible copy");
269                 } else {
270                         struct stat st;
271
272                         snprintf(bp, buflen, "Use cached copy");
273                         len = strlen(bp);
274                         bp += len;
275                         buflen -= len;
276
277                         if (ct->c_partno) {
278                                 snprintf(bp, buflen, " of content %s",
279                                                 ct->c_partno);
280                                 len = strlen(bp);
281                                 bp += len;
282                                 buflen -= len;
283                         }
284
285                         stat(buffer, &st);
286                         snprintf(bp, buflen, " (size %lu octets)",
287                                 (unsigned long) st.st_size);
288                 }
289                 len = strlen(bp);
290                 bp += len;
291                 buflen -= len;
292
293                 snprintf(bp, buflen, "\n    in file %s? ", buffer);
294
295                 /* Now, check answer */
296                 if (!getanswer(query))
297                         status = NOTOK;
298         }
299
300         if (status == OK && writing) {
301                 if (*writing && strchr(buffer, '/'))
302                         make_intermediates(buffer);
303                 unlink(buffer);
304         }
305
306         free(id);
307         return status;
308 }
309
310
311 static int
312 find_cache_aux(int writing, char *directory, char *id, char *buffer,
313                 int buflen)
314 {
315         int mask, usemap;
316         char mapfile[BUFSIZ], mapname[BUFSIZ];
317         FILE *fp;
318         static int partno, pid;
319         static time_t clock = 0;
320
321 #ifdef BSD42
322         usemap = strchr(id, '/') ? 1 : 0;
323 #else
324         usemap = 1;
325 #endif
326
327         if (debugsw)
328                 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
329                                 directory, usemap);
330
331         snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
332         if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
333                 goto done_map;
334
335         if (!writing) {
336                 if (usemap)
337                         return NOTOK;
338
339 use_raw:
340                 snprintf(buffer, buflen, "%s/%s", directory, id);
341                 return OK;
342         }
343
344         if (!usemap && access(mapfile, W_OK) == NOTOK)
345                 goto use_raw;
346
347         if (clock != 0) {
348                 time_t now;
349
350                 time(&now);
351                 if (now > clock)
352                         clock = 0;
353         } else {
354                 pid = getpid();
355         }
356
357         if (clock == 0) {
358                 time(&clock);
359                 partno = 0;
360         } else {
361                 if (partno > 0xff) {
362                         clock++;
363                         partno = 0;
364                 }
365         }
366
367         snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
368                         (unsigned int) (clock & 0xffffffff),
369                         (unsigned int) (pid & 0xffff),
370                         (unsigned int) (partno++ & 0xff));
371
372         if (debugsw)
373                 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
374
375         make_intermediates(mapfile);
376         mask = umask(writing == 2 ? 0077 : 0);
377         if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
378                 int fd;
379
380                 if ((fd = creat(mapfile, 0666)) != NOTOK) {
381                         close(fd);
382                         fp = lkfopen(mapfile, "a");
383                 }
384         }
385         umask(mask);
386         if (!fp)
387                 return NOTOK;
388         fprintf(fp, "%s: %s\n", mapname, id);
389         lkfclose(fp, mapfile);
390
391 done_map:
392         if (*mapname == '/')
393                 strncpy(buffer, mapname, buflen);
394         else
395                 snprintf(buffer, buflen, "%s/%s", directory, mapname);
396         if (debugsw)
397                 fprintf(stderr, "use %s\n", buffer);
398
399         return OK;
400 }
401
402
403 static int
404 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
405 {
406         int state;
407         char buf[BUFSIZ], name[NAMESZ];
408         FILE *fp;
409
410         if (!(fp = lkfopen(mapfile, "r")))
411                 return NOTOK;
412
413         for (state = FLD;;) {
414                 int result;
415                 char *cp, *dp;
416
417                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
418                         case FLD:
419                         case FLDPLUS:
420                         case FLDEOF:
421                                 strncpy(mapname, name, namelen);
422                                 if (state != FLDPLUS)
423                                         cp = buf;
424                                 else {
425                                         cp = getcpy(buf);
426                                         while (state == FLDPLUS) {
427                                                 state = m_getfld(state, name, buf, sizeof(buf), fp);
428                                                 cp = add(buf, cp);
429                                         }
430                                 }
431                                 dp = trimcpy(cp);
432                                 if (cp != buf)
433                                         free(cp);
434                                 if (debugsw)
435                                         fprintf(stderr, "compare %s to %s <- %s\n", id, dp, mapname);
436                                 result = strcmp(id, dp);
437                                 free(dp);
438                                 if (result == 0) {
439                                         lkfclose(fp, mapfile);
440                                         return OK;
441                                 }
442                                 if (state != FLDEOF)
443                                         continue;
444                                 /* else fall... */
445
446                         case BODY:
447                         case BODYEOF:
448                         case FILEEOF:
449                         default:
450                                 break;
451                 }
452                 break;
453         }
454
455         lkfclose(fp, mapfile);
456         return NOTOK;
457 }