3 * mhcachesbr.c -- routines to manipulate the MIME content cache
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
20 #include <h/mhparse.h>
21 #include <h/mhcachesbr.h>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
31 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
34 int rcachesw = CACHE_ASK;
35 int wcachesw = CACHE_ASK;
38 * Location of public and private cache. These must
39 * be set before these routines are called.
45 /* mhparse.c (OR) mhbuildsbr.c */
49 int part_ok (CT, int);
50 int type_ok (CT, int);
51 int make_intermediates (char *);
52 void content_error (char *, CT, char *, ...);
53 void flush_errors (void);
58 void cache_all_messages (CT *);
59 int find_cache (CT, int, int *, char *, char *, int);
64 static void cache_content (CT);
65 static int find_cache_aux (int, char *, char *, char *, int);
66 static int find_cache_aux2 (char *, char *, char *, int);
70 * Top level entry point to cache content
71 * from a group of messages
75 cache_all_messages (CT *cts)
79 for (ctp = cts; *ctp; ctp++) {
81 if (type_ok (ct, 1)) {
88 (*ct->c_ceclosefnx) (ct);
96 * Entry point to cache content from external sources.
100 cache_content (CT ct)
103 char *file, cachefile[BUFSIZ];
104 CE ce = ct->c_cefile;
107 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
112 advise (NULL, "unable to decode %s", ct->c_file);
116 /* THIS NEEDS TO BE FIXED */
118 if (ct->c_ceopenfnx == openMail) {
119 advise (NULL, "a radish may no know Greek, but I do...");
124 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
125 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
127 advise (NULL, "unable to cache %s's contents", ct->c_file);
130 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
132 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
137 int mask = umask (cachetype ? ~m_gmprot () : 0222);
141 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
144 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
147 if ((fp = fopen (cachefile, "w"))) {
150 FILE *gp = ce->ce_fp;
152 fseek (gp, 0L, SEEK_SET);
154 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
156 fwrite (buffer, sizeof(*buffer), cc, fp);
160 admonish (ce->ce_file, "error reading");
164 admonish (cachefile, "error writing");
170 content_error (cachefile, ct, "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,
186 char *buffer, int buflen)
195 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
196 policy, writing ? "writing" : "reading", id);
207 && find_cache_aux (writing ? 2 : 0, cache_private, id,
208 buffer, buflen) == OK) {
209 if (access (buffer, R_OK) != NOTOK) {
219 && find_cache_aux (writing ? 1 : 0, cache_public, id,
220 buffer, buflen) == OK) {
221 if (writing || access (buffer, R_OK) != NOTOK) {
231 && find_cache_aux (writing ? 2 : 0, cache_private, id,
232 buffer, buflen) == OK) {
233 if (writing || access (buffer, R_OK) != NOTOK)
240 if (status == OK && policy == CACHE_ASK) {
242 char *bp, query[BUFSIZ];
247 pidcheck (pidwait (xpid, NOTOK));
251 /* Get buffer ready to go */
253 buflen = sizeof(query);
255 /* Now, construct query */
257 snprintf (bp, buflen, "Make cached, publically-accessible copy");
261 snprintf (bp, buflen, "Use cached copy");
267 snprintf (bp, buflen, " of content %s", ct->c_partno);
274 snprintf (bp, buflen, " (size %lu octets)",
275 (unsigned long) st.st_size);
281 snprintf (bp, buflen, "\n in file %s? ", buffer);
283 /* Now, check answer */
284 if (!getanswer (query))
288 if (status == OK && writing) {
289 if (*writing && strchr(buffer, '/'))
290 make_intermediates (buffer);
300 find_cache_aux (int writing, char *directory, char *id,
301 char *buffer, int buflen)
304 char mapfile[BUFSIZ], mapname[BUFSIZ];
306 static int partno, pid;
307 static time_t clock = 0;
312 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
314 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
315 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
323 snprintf (buffer, buflen, "%s/%s", directory, id);
327 if (!usemap && access (mapfile, W_OK) == NOTOK)
350 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
351 (unsigned int) (clock & 0xffffffff),
352 (unsigned int) (pid & 0xffff),
353 (unsigned int) (partno++ & 0xff));
356 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
358 make_intermediates (mapfile);
359 mask = umask (writing == 2 ? 0077 : 0);
360 if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
363 if ((fd = creat (mapfile, 0666)) != NOTOK) {
365 fp = lkfopen (mapfile, "a");
371 fprintf (fp, "%s: %s\n", mapname, id);
372 lkfclose (fp, mapfile);
376 strncpy (buffer, mapname, buflen);
378 snprintf (buffer, buflen, "%s/%s", directory, mapname);
380 fprintf (stderr, "use %s\n", buffer);
387 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
390 char buf[BUFSIZ], name[NAMESZ];
393 if (!(fp = lkfopen (mapfile, "r")))
396 for (state = FLD;;) {
400 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
404 strncpy (mapname, name, namelen);
405 if (state != FLDPLUS)
408 cp = add (buf, NULL);
409 while (state == FLDPLUS) {
410 state = m_getfld (state, name, buf, sizeof(buf), fp);
418 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
420 result = strcmp (id, dp);
423 lkfclose (fp, mapfile);
439 lkfclose (fp, mapfile);