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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
30 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
33 int rcachesw = CACHE_ASK;
34 int wcachesw = CACHE_ASK;
37 * Location of public and private cache. These must
38 * be set before these routines are called.
44 /* mhparse.c (OR) mhbuildsbr.c */
48 int part_ok (CT, int);
49 int type_ok (CT, int);
50 int make_intermediates (char *);
51 void content_error (char *, CT, char *, ...);
52 void flush_errors (void);
57 void cache_all_messages (CT *);
58 int find_cache (CT, int, int *, char *, char *, int);
63 static void cache_content (CT);
64 static int find_cache_aux (int, char *, char *, char *, int);
65 static int find_cache_aux2 (char *, char *, char *, int);
69 * Top level entry point to cache content
70 * from a group of messages
74 cache_all_messages (CT *cts)
78 for (ctp = cts; *ctp; ctp++) {
80 if (type_ok (ct, 1)) {
87 (*ct->c_ceclosefnx) (ct);
95 * Entry point to cache content from external sources.
102 char *file, cachefile[BUFSIZ];
103 CE ce = ct->c_cefile;
106 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
111 advise (NULL, "unable to decode %s", ct->c_file);
115 /* THIS NEEDS TO BE FIXED */
117 if (ct->c_ceopenfnx == openMail) {
118 advise (NULL, "a radish may no know Greek, but I do...");
123 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
124 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
126 advise (NULL, "unable to cache %s's contents", ct->c_file);
129 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
131 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
136 int mask = umask (cachetype ? ~m_gmprot () : 0222);
140 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
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), sizeof(buffer), gp))
155 fwrite (buffer, sizeof(*buffer), cc, fp);
159 admonish (ce->ce_file, "error reading");
163 admonish (cachefile, "error writing");
169 content_error (cachefile, ct, "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,
185 char *buffer, int buflen)
194 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
195 policy, writing ? "writing" : "reading", id);
206 && find_cache_aux (writing ? 2 : 0, cache_private, id,
207 buffer, buflen) == OK) {
208 if (access (buffer, R_OK) != NOTOK) {
218 && find_cache_aux (writing ? 1 : 0, cache_public, id,
219 buffer, buflen) == OK) {
220 if (writing || access (buffer, R_OK) != NOTOK) {
230 && find_cache_aux (writing ? 2 : 0, cache_private, id,
231 buffer, buflen) == OK) {
232 if (writing || access (buffer, R_OK) != NOTOK)
239 if (status == OK && policy == CACHE_ASK) {
241 char *bp, query[BUFSIZ];
246 pidcheck (pidwait (xpid, NOTOK));
250 /* Get buffer ready to go */
252 buflen = sizeof(query);
254 /* Now, construct query */
256 snprintf (bp, buflen, "Make cached, publically-accessible copy");
260 snprintf (bp, buflen, "Use cached copy");
266 snprintf (bp, buflen, " of content %s", ct->c_partno);
273 snprintf (bp, buflen, " (size %lu octets)",
274 (unsigned long) st.st_size);
280 snprintf (bp, buflen, "\n in file %s? ", buffer);
282 /* Now, check answer */
283 if (!getanswer (query))
287 if (status == OK && writing) {
288 if (*writing && strchr(buffer, '/'))
289 make_intermediates (buffer);
299 find_cache_aux (int writing, char *directory, char *id,
300 char *buffer, int buflen)
303 char mapfile[BUFSIZ], mapname[BUFSIZ];
305 static int partno, pid;
306 static time_t clock = 0;
311 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
313 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
314 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
322 snprintf (buffer, buflen, "%s/%s", directory, id);
326 if (!usemap && access (mapfile, W_OK) == NOTOK)
349 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
350 (unsigned int) (clock & 0xffffffff),
351 (unsigned int) (pid & 0xffff),
352 (unsigned int) (partno++ & 0xff));
355 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
357 make_intermediates (mapfile);
358 mask = umask (writing == 2 ? 0077 : 0);
359 if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
362 if ((fd = creat (mapfile, 0666)) != NOTOK) {
364 fp = lkfopen (mapfile, "a");
370 fprintf (fp, "%s: %s\n", mapname, id);
371 lkfclose (fp, mapfile);
375 strncpy (buffer, mapname, buflen);
377 snprintf (buffer, buflen, "%s/%s", directory, mapname);
379 fprintf (stderr, "use %s\n", buffer);
386 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
389 char buf[BUFSIZ], name[NAMESZ];
392 if (!(fp = lkfopen (mapfile, "r")))
395 for (state = FLD;;) {
399 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
403 strncpy (mapname, name, namelen);
404 if (state != FLDPLUS)
407 cp = add (buf, NULL);
408 while (state == FLDPLUS) {
409 state = m_getfld (state, name, buf, sizeof(buf), fp);
417 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
419 result = strcmp (id, dp);
422 lkfclose (fp, mapfile);
438 lkfclose (fp, mapfile);