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 TIME_WITH_SYS_TIME
23 # include <sys/time.h>
26 # ifdef TM_IN_SYS_TIME
27 # include <sys/time.h>
35 extern pid_t xpid; /* mhshowsbr.c or mhbuild.c */
38 int rcachesw = CACHE_ASK;
39 int wcachesw = CACHE_ASK;
42 ** Location of public and private cache. These must
43 ** be set before these routines are called.
49 /* mhparse.c (OR) mhbuild.c */
55 int make_intermediates(char *);
56 void content_error(char *, CT, char *, ...);
57 void flush_errors(void);
62 void cache_all_messages(CT *);
63 int find_cache(CT, int, int *, char *, char *, int);
68 static void cache_content(CT);
69 static int find_cache_aux(int, char *, char *, char *, int);
70 static int find_cache_aux2(char *, char *, char *, int);
74 ** Top level entry point to cache content
75 ** from a group of messages
79 cache_all_messages(CT *cts)
83 for (ctp = cts; *ctp; ctp++) {
92 (*ct->c_ceclosefnx) (ct);
100 ** Entry point to cache content from external sources.
107 char *file, cachefile[BUFSIZ];
108 CE ce = ct->c_cefile;
111 advise(NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
116 advise(NULL, "unable to decode %s", ct->c_file);
120 /* THIS NEEDS TO BE FIXED */
122 if (ct->c_ceopenfnx == openMail) {
123 advise(NULL, "a radish may no know Greek, but I do...");
128 if (find_cache(NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
129 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
131 advise(NULL, "unable to cache %s's contents", ct->c_file);
134 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
136 fprintf(stderr, "caching message %s as file %s\n", ct->c_file,
141 int mask = umask(cachetype ? ~m_gmprot() : 0222);
145 fprintf(stderr, "caching by copying %s...\n",
149 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
152 if ((fp = fopen(cachefile, "w"))) {
155 FILE *gp = ce->ce_fp;
157 fseek(gp, 0L, SEEK_SET);
159 while ((cc = fread(buffer, sizeof(*buffer),
160 sizeof(buffer), gp)) > 0)
161 fwrite(buffer, sizeof(*buffer), cc, fp);
165 admonish(ce->ce_file, "error reading");
169 admonish(cachefile, "error writing");
175 content_error(cachefile, ct,
176 "unable to fopen for writing");
181 fprintf(stderr, "in place caching...\n");
184 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
185 chmod(cachefile, cachetype ? m_gmprot() : 0444);
191 find_cache(CT ct, int policy, int *writing, char *id, char *buffer, int buflen)
200 fprintf(stderr, "find_cache %s(%d) %s %s\n",
201 caches[policy].sw, policy,
202 writing ? "writing" : "reading", id);
211 if (cache_private && !writing &&
212 find_cache_aux(writing ? 2 : 0,
213 cache_private, id, buffer, buflen) == OK) {
214 if (access(buffer, R_OK) != NOTOK) {
223 if (cache_public && find_cache_aux(writing ? 1 : 0,
224 cache_public, id, buffer, buflen) == OK) {
225 if (writing || access(buffer, R_OK) != NOTOK) {
234 if (cache_private && find_cache_aux(writing ? 2 : 0,
235 cache_private, id, buffer, buflen) == OK) {
236 if (writing || access(buffer, R_OK) != NOTOK)
243 if (status == OK && policy == CACHE_ASK) {
245 char *bp, query[BUFSIZ];
250 pidcheck(pidwait(xpid, NOTOK));
254 /* Get buffer ready to go */
256 buflen = sizeof(query);
258 /* Now, construct query */
260 snprintf(bp, buflen, "Make cached, publically-accessible copy");
264 snprintf(bp, buflen, "Use cached copy");
270 snprintf(bp, buflen, " of content %s",
278 snprintf(bp, buflen, " (size %lu octets)",
279 (unsigned long) st.st_size);
285 snprintf(bp, buflen, "\n in file %s? ", buffer);
287 /* Now, check answer */
288 if (!getanswer(query))
292 if (status == OK && writing) {
293 if (*writing && strchr(buffer, '/'))
294 make_intermediates(buffer);
304 find_cache_aux(int writing, char *directory, char *id, char *buffer,
308 char mapfile[BUFSIZ], mapname[BUFSIZ];
310 static int partno, pid;
311 static time_t clock = 0;
314 usemap = strchr(id, '/') ? 1 : 0;
320 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
323 snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
324 if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
332 snprintf(buffer, buflen, "%s/%s", directory, id);
336 if (!usemap && access(mapfile, W_OK) == NOTOK)
359 snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
360 (unsigned int) (clock & 0xffffffff),
361 (unsigned int) (pid & 0xffff),
362 (unsigned int) (partno++ & 0xff));
365 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
367 make_intermediates(mapfile);
368 mask = umask(writing == 2 ? 0077 : 0);
369 if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
372 if ((fd = creat(mapfile, 0666)) != NOTOK) {
374 fp = lkfopen(mapfile, "a");
380 fprintf(fp, "%s: %s\n", mapname, id);
381 lkfclose(fp, mapfile);
385 strncpy(buffer, mapname, buflen);
387 snprintf(buffer, buflen, "%s/%s", directory, mapname);
389 fprintf(stderr, "use %s\n", buffer);
396 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
399 char buf[BUFSIZ], name[NAMESZ];
402 if (!(fp = lkfopen(mapfile, "r")))
405 for (state = FLD;;) {
409 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
413 strncpy(mapname, name, namelen);
414 if (state != FLDPLUS)
418 while (state == FLDPLUS) {
419 state = m_getfld(state, name, buf,
428 fprintf(stderr, "compare %s to %s <- %s\n",
430 result = strcmp(id, dp);
433 lkfclose(fp, mapfile);
449 lkfclose(fp, mapfile);