2 ** mhcachesbr.c -- routines to manipulate the MIME content cache
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.
11 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
28 extern pid_t xpid; /* mhshowsbr.c or mhbuild.c */
31 int rcachesw = CACHE_ASK;
32 int wcachesw = CACHE_ASK;
35 ** Location of public and private cache. These must
36 ** be set before these routines are called.
42 /* mhparse.c (OR) mhbuild.c */
48 int make_intermediates(char *);
49 void content_error(char *, CT, char *, ...);
50 void flush_errors(void);
55 void cache_all_messages(CT *);
56 int find_cache(CT, int, int *, char *, char *, int);
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);
67 ** Top level entry point to cache content
68 ** from a group of messages
72 cache_all_messages(CT *cts)
76 for (ctp = cts; *ctp; ctp++) {
85 (*ct->c_ceclosefnx) (ct);
93 ** Entry point to cache content from external sources.
100 char *file, cachefile[BUFSIZ];
101 CE ce = ct->c_cefile;
104 advise(NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
109 advise(NULL, "unable to decode %s", ct->c_file);
113 /* THIS NEEDS TO BE FIXED */
115 if (ct->c_ceopenfnx == openMail) {
116 advise(NULL, "a radish may no know Greek, but I do...");
121 if (find_cache(NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
122 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
124 advise(NULL, "unable to cache %s's contents", ct->c_file);
127 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
129 fprintf(stderr, "caching message %s as file %s\n", ct->c_file,
134 int mask = umask(cachetype ? ~m_gmprot() : 0222);
138 fprintf(stderr, "caching by copying %s...\n",
142 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
145 if ((fp = fopen(cachefile, "w"))) {
148 FILE *gp = ce->ce_fp;
150 fseek(gp, 0L, SEEK_SET);
152 while ((cc = fread(buffer, sizeof(*buffer),
153 sizeof(buffer), gp)) > 0)
154 fwrite(buffer, sizeof(*buffer), cc, fp);
158 admonish(ce->ce_file, "error reading");
162 admonish(cachefile, "error writing");
168 content_error(cachefile, ct,
169 "unable to fopen for writing");
174 fprintf(stderr, "in place caching...\n");
177 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
178 chmod(cachefile, cachetype ? m_gmprot() : 0444);
184 find_cache(CT ct, int policy, int *writing, char *id, char *buffer, int buflen)
193 fprintf(stderr, "find_cache %s(%d) %s %s\n",
194 caches[policy].sw, policy,
195 writing ? "writing" : "reading", id);
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) {
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) {
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)
236 if (status == OK && policy == CACHE_ASK) {
238 char *bp, query[BUFSIZ];
243 pidcheck(pidwait(xpid, NOTOK));
247 /* Get buffer ready to go */
249 buflen = sizeof(query);
251 /* Now, construct query */
253 snprintf(bp, buflen, "Make cached, publically-accessible copy");
257 snprintf(bp, buflen, "Use cached copy");
263 snprintf(bp, buflen, " of content %s",
271 snprintf(bp, buflen, " (size %lu octets)",
272 (unsigned long) st.st_size);
278 snprintf(bp, buflen, "\n in file %s? ", buffer);
280 /* Now, check answer */
281 if (!getanswer(query))
285 if (status == OK && writing) {
286 if (*writing && strchr(buffer, '/'))
287 make_intermediates(buffer);
297 find_cache_aux(int writing, char *directory, char *id, char *buffer,
301 char mapfile[BUFSIZ], mapname[BUFSIZ];
303 static int partno, pid;
304 static time_t clock = 0;
308 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
311 snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
312 if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
320 snprintf(buffer, buflen, "%s/%s", directory, id);
324 if (!usemap && access(mapfile, W_OK) == NOTOK)
347 snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
348 (unsigned int) (clock & 0xffffffff),
349 (unsigned int) (pid & 0xffff),
350 (unsigned int) (partno++ & 0xff));
353 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
355 make_intermediates(mapfile);
356 mask = umask(writing == 2 ? 0077 : 0);
357 if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
360 if ((fd = creat(mapfile, 0666)) != NOTOK) {
362 fp = lkfopen(mapfile, "a");
368 fprintf(fp, "%s: %s\n", mapname, id);
369 lkfclose(fp, mapfile);
373 strncpy(buffer, mapname, buflen);
375 snprintf(buffer, buflen, "%s/%s", directory, mapname);
377 fprintf(stderr, "use %s\n", buffer);
384 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
387 char buf[BUFSIZ], name[NAMESZ];
390 if (!(fp = lkfopen(mapfile, "r")))
393 for (state = FLD;;) {
397 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
401 strncpy(mapname, name, namelen);
402 if (state != FLDPLUS)
406 while (state == FLDPLUS) {
407 state = m_getfld(state, name, buf,
416 fprintf(stderr, "compare %s to %s <- %s\n",
418 result = strcmp(id, dp);
421 lkfclose(fp, mapfile);
437 lkfclose(fp, mapfile);