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>
33 #ifdef HAVE_SYS_WAIT_H
34 # include <sys/wait.h>
39 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
42 int rcachesw = CACHE_ASK;
43 int wcachesw = CACHE_ASK;
46 ** Location of public and private cache. These must
47 ** be set before these routines are called.
53 /* mhparse.c (OR) mhbuildsbr.c */
59 int make_intermediates(char *);
60 void content_error(char *, CT, char *, ...);
61 void flush_errors(void);
66 void cache_all_messages(CT *);
67 int find_cache(CT, int, int *, char *, char *, int);
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);
78 ** Top level entry point to cache content
79 ** from a group of messages
83 cache_all_messages(CT *cts)
87 for (ctp = cts; *ctp; ctp++) {
96 (*ct->c_ceclosefnx) (ct);
104 ** Entry point to cache content from external sources.
111 char *file, cachefile[BUFSIZ];
112 CE ce = ct->c_cefile;
115 advise(NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
120 advise(NULL, "unable to decode %s", ct->c_file);
124 /* THIS NEEDS TO BE FIXED */
126 if (ct->c_ceopenfnx == openMail) {
127 advise(NULL, "a radish may no know Greek, but I do...");
132 if (find_cache(NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
133 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
135 advise(NULL, "unable to cache %s's contents", ct->c_file);
138 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
140 fprintf(stderr, "caching message %s as file %s\n", ct->c_file,
145 int mask = umask(cachetype ? ~m_gmprot() : 0222);
149 fprintf(stderr, "caching by copying %s...\n",
153 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
156 if ((fp = fopen(cachefile, "w"))) {
159 FILE *gp = ce->ce_fp;
161 fseek(gp, 0L, SEEK_SET);
163 while ((cc = fread(buffer, sizeof(*buffer),
164 sizeof(buffer), gp)) > 0)
165 fwrite(buffer, sizeof(*buffer), cc, fp);
169 admonish(ce->ce_file, "error reading");
173 admonish(cachefile, "error writing");
179 content_error(cachefile, ct,
180 "unable to fopen for writing");
185 fprintf(stderr, "in place caching...\n");
188 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
189 chmod(cachefile, cachetype ? m_gmprot() : 0444);
195 find_cache(CT ct, int policy, int *writing, char *id, char *buffer, int buflen)
204 fprintf(stderr, "find_cache %s(%d) %s %s\n",
205 caches[policy].sw, policy,
206 writing ? "writing" : "reading", id);
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) {
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) {
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)
247 if (status == OK && policy == CACHE_ASK) {
249 char *bp, query[BUFSIZ];
254 pidcheck(pidwait(xpid, NOTOK));
258 /* Get buffer ready to go */
260 buflen = sizeof(query);
262 /* Now, construct query */
264 snprintf(bp, buflen, "Make cached, publically-accessible copy");
268 snprintf(bp, buflen, "Use cached copy");
274 snprintf(bp, buflen, " of content %s",
282 snprintf(bp, buflen, " (size %lu octets)",
283 (unsigned long) st.st_size);
289 snprintf(bp, buflen, "\n in file %s? ", buffer);
291 /* Now, check answer */
292 if (!getanswer(query))
296 if (status == OK && writing) {
297 if (*writing && strchr(buffer, '/'))
298 make_intermediates(buffer);
308 find_cache_aux(int writing, char *directory, char *id, char *buffer,
312 char mapfile[BUFSIZ], mapname[BUFSIZ];
314 static int partno, pid;
315 static time_t clock = 0;
318 usemap = strchr(id, '/') ? 1 : 0;
324 fprintf(stderr, "find_cache_aux %s usemap=%d\n",
327 snprintf(mapfile, sizeof(mapfile), "%s/cache.map", directory);
328 if (find_cache_aux2(mapfile, id, mapname, sizeof(mapname)) == OK)
336 snprintf(buffer, buflen, "%s/%s", directory, id);
340 if (!usemap && access(mapfile, W_OK) == NOTOK)
363 snprintf(mapname, sizeof(mapname), "%08x%04x%02x",
364 (unsigned int) (clock & 0xffffffff),
365 (unsigned int) (pid & 0xffff),
366 (unsigned int) (partno++ & 0xff));
369 fprintf(stderr, "creating mapping %s->%s\n", mapname, id);
371 make_intermediates(mapfile);
372 mask = umask(writing == 2 ? 0077 : 0);
373 if (!(fp = lkfopen(mapfile, "a")) && errno == ENOENT) {
376 if ((fd = creat(mapfile, 0666)) != NOTOK) {
378 fp = lkfopen(mapfile, "a");
384 fprintf(fp, "%s: %s\n", mapname, id);
385 lkfclose(fp, mapfile);
389 strncpy(buffer, mapname, buflen);
391 snprintf(buffer, buflen, "%s/%s", directory, mapname);
393 fprintf(stderr, "use %s\n", buffer);
400 find_cache_aux2(char *mapfile, char *id, char *mapname, int namelen)
403 char buf[BUFSIZ], name[NAMESZ];
406 if (!(fp = lkfopen(mapfile, "r")))
409 for (state = FLD;;) {
413 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
417 strncpy(mapname, name, namelen);
418 if (state != FLDPLUS)
422 while (state == FLDPLUS) {
423 state = m_getfld(state, name, buf,
432 fprintf(stderr, "compare %s to %s <- %s\n",
434 result = strcmp(id, dp);
437 lkfclose(fp, mapfile);
453 lkfclose(fp, mapfile);