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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
29 extern pid_t xpid; /* mhshowsbr.c or mhbuild.c */
32 int rcachesw = CACHE_ASK;
33 int wcachesw = CACHE_ASK;
36 ** Location of public and private cache. These must
37 ** be set before these routines are called.
43 /* mhparse.c (OR) mhbuild.c */
49 int make_intermediates(char *);
50 void content_error(char *, CT, char *, ...);
51 void flush_errors(void);
56 void cache_all_messages(CT *);
57 int find_cache(CT, int, int *, char *, char *, int);
62 static void cache_content(CT);
63 static int find_cache_aux(int, char *, char *, char *, int);
64 static int find_cache_aux2(char *, char *, char *, int);
68 ** Top level entry point to cache content
69 ** from a group of messages
73 cache_all_messages(CT *cts)
77 for (ctp = cts; *ctp; ctp++) {
86 (*ct->c_ceclosefnx) (ct);
94 ** Entry point to cache content from external sources.
101 char *file, cachefile[BUFSIZ];
102 CE ce = ct->c_cefile;
105 advise(NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
110 advise(NULL, "unable to decode %s", ct->c_file);
114 /* THIS NEEDS TO BE FIXED */
116 if (ct->c_ceopenfnx == openMail) {
117 advise(NULL, "a radish may no know Greek, but I do...");
122 if (find_cache(NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
123 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
125 advise(NULL, "unable to cache %s's contents", ct->c_file);
128 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
130 fprintf(stderr, "caching message %s as file %s\n", ct->c_file,
135 int mask = umask(cachetype ? ~m_gmprot() : 0222);
139 fprintf(stderr, "caching by copying %s...\n",
143 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
146 if ((fp = fopen(cachefile, "w"))) {
149 FILE *gp = ce->ce_fp;
151 fseek(gp, 0L, SEEK_SET);
153 while ((cc = fread(buffer, sizeof(*buffer),
154 sizeof(buffer), gp)) > 0)
155 fwrite(buffer, sizeof(*buffer), cc, fp);
159 admonish(ce->ce_file, "error reading");
163 admonish(cachefile, "error writing");
169 content_error(cachefile, ct,
170 "unable to fopen for writing");
175 fprintf(stderr, "in place caching...\n");
178 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
179 chmod(cachefile, cachetype ? m_gmprot() : 0444);
185 find_cache(CT ct, int policy, int *writing, char *id, char *buffer, int buflen)
194 fprintf(stderr, "find_cache %s(%d) %s %s\n",
195 caches[policy].sw, policy,
196 writing ? "writing" : "reading", id);
205 if (cache_private && !writing &&
206 find_cache_aux(writing ? 2 : 0,
207 cache_private, id, buffer, buflen) == OK) {
208 if (access(buffer, R_OK) != NOTOK) {
217 if (cache_public && find_cache_aux(writing ? 1 : 0,
218 cache_public, id, buffer, buflen) == OK) {
219 if (writing || access(buffer, R_OK) != NOTOK) {
228 if (cache_private && find_cache_aux(writing ? 2 : 0,
229 cache_private, id, buffer, buflen) == OK) {
230 if (writing || access(buffer, R_OK) != NOTOK)
237 if (status == OK && policy == CACHE_ASK) {
239 char *bp, query[BUFSIZ];
244 pidcheck(pidwait(xpid, NOTOK));
248 /* Get buffer ready to go */
250 buflen = sizeof(query);
252 /* Now, construct query */
254 snprintf(bp, buflen, "Make cached, publically-accessible copy");
258 snprintf(bp, buflen, "Use cached copy");
264 snprintf(bp, buflen, " of content %s",
272 snprintf(bp, buflen, " (size %lu octets)",
273 (unsigned long) st.st_size);
279 snprintf(bp, buflen, "\n in file %s? ", buffer);
281 /* Now, check answer */
282 if (!getanswer(query))
286 if (status == OK && writing) {
287 if (*writing && strchr(buffer, '/'))
288 make_intermediates(buffer);
298 find_cache_aux(int writing, char *directory, char *id, char *buffer,
302 char mapfile[BUFSIZ], mapname[BUFSIZ];
304 static int partno, pid;
305 static time_t clock = 0;
309 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
312 snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
313 if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
321 snprintf(buffer, buflen, "%s/%s", directory, id);
325 if (!usemap && access(mapfile, W_OK) == NOTOK)
348 snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
349 (unsigned int) (clock & 0xffffffff),
350 (unsigned int) (pid & 0xffff),
351 (unsigned int) (partno++ & 0xff));
354 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
356 make_intermediates(mapfile);
357 mask = umask(writing == 2 ? 0077 : 0);
358 if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
361 if ((fd = creat(mapfile, 0666)) != NOTOK) {
363 fp = lkfopen(mapfile, "a");
369 fprintf(fp, "%s: %s\n", mapname, id);
370 lkfclose(fp, mapfile);
374 strncpy(buffer, mapname, buflen);
376 snprintf(buffer, buflen, "%s/%s", directory, mapname);
378 fprintf(stderr, "use %s\n", buffer);
385 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
388 char buf[BUFSIZ], name[NAMESZ];
391 if (!(fp = lkfopen(mapfile, "r")))
394 for (state = FLD;;) {
398 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
402 strncpy(mapname, name, namelen);
403 if (state != FLDPLUS)
407 while (state == FLDPLUS) {
408 state = m_getfld(state, name, buf,
417 fprintf(stderr, "compare %s to %s <- %s\n",
419 result = strcmp(id, dp);
422 lkfclose(fp, mapfile);
438 lkfclose(fp, mapfile);