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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
27 # ifdef TM_IN_SYS_TIME
28 # include <sys/time.h>
34 #ifdef HAVE_SYS_WAIT_H
35 # include <sys/wait.h>
40 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
43 int rcachesw = CACHE_ASK;
44 int wcachesw = CACHE_ASK;
47 ** Location of public and private cache. These must
48 ** be set before these routines are called.
54 /* mhparse.c (OR) mhbuildsbr.c */
58 int part_ok (CT, int);
59 int type_ok (CT, int);
60 int make_intermediates (char *);
61 void content_error (char *, CT, char *, ...);
62 void flush_errors (void);
67 void cache_all_messages (CT *);
68 int find_cache (CT, int, int *, char *, char *, int);
73 static void cache_content (CT);
74 static int find_cache_aux (int, char *, char *, char *, int);
75 static int find_cache_aux2 (char *, char *, char *, int);
79 ** Top level entry point to cache content
80 ** from a group of messages
84 cache_all_messages (CT *cts)
88 for (ctp = cts; *ctp; ctp++) {
90 if (type_ok (ct, 1)) {
97 (*ct->c_ceclosefnx) (ct);
105 ** Entry point to cache content from external sources.
109 cache_content (CT ct)
112 char *file, cachefile[BUFSIZ];
113 CE ce = ct->c_cefile;
116 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
121 advise (NULL, "unable to decode %s", ct->c_file);
125 /* THIS NEEDS TO BE FIXED */
127 if (ct->c_ceopenfnx == openMail) {
128 advise (NULL, "a radish may no know Greek, but I do...");
133 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
134 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
136 advise (NULL, "unable to cache %s's contents", ct->c_file);
139 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
141 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
146 int mask = umask (cachetype ? ~m_gmprot () : 0222);
150 fprintf (stderr, "caching by copying %s...\n",
154 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
157 if ((fp = fopen (cachefile, "w"))) {
160 FILE *gp = ce->ce_fp;
162 fseek (gp, 0L, SEEK_SET);
164 while ((cc = fread (buffer, sizeof(*buffer),
165 sizeof(buffer), gp)) > 0)
166 fwrite (buffer, sizeof(*buffer), cc, fp);
170 admonish (ce->ce_file, "error reading");
174 admonish (cachefile, "error writing");
180 content_error (cachefile, ct,
181 "unable to fopen for writing");
186 fprintf (stderr, "in place caching...\n");
189 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
190 chmod (cachefile, cachetype ? m_gmprot () : 0444);
196 find_cache (CT ct, int policy, int *writing, char *id,
197 char *buffer, int buflen)
206 fprintf (stderr, "find_cache %s(%d) %s %s\n",
207 caches[policy].sw, policy,
208 writing ? "writing" : "reading", id);
217 if (cache_private && !writing
218 && find_cache_aux (writing ? 2 : 0,
219 cache_private, id, buffer, buflen) == OK) {
220 if (access (buffer, R_OK) != NOTOK) {
229 if (cache_public && find_cache_aux (writing ? 1 : 0,
230 cache_public, id, buffer, buflen) == OK) {
231 if (writing || access (buffer, R_OK) != NOTOK) {
240 if (cache_private && find_cache_aux (writing ? 2 : 0,
241 cache_private, id, buffer, buflen) == OK) {
242 if (writing || access (buffer, R_OK) != NOTOK)
249 if (status == OK && policy == CACHE_ASK) {
251 char *bp, query[BUFSIZ];
256 pidcheck (pidwait (xpid, NOTOK));
260 /* Get buffer ready to go */
262 buflen = sizeof(query);
264 /* Now, construct query */
266 snprintf (bp, buflen, "Make cached, publically-accessible copy");
270 snprintf (bp, buflen, "Use cached copy");
276 snprintf (bp, buflen, " of content %s", ct->c_partno);
283 snprintf (bp, buflen, " (size %lu octets)",
284 (unsigned long) st.st_size);
290 snprintf (bp, buflen, "\n in file %s? ", buffer);
292 /* Now, check answer */
293 if (!getanswer (query))
297 if (status == OK && writing) {
298 if (*writing && strchr(buffer, '/'))
299 make_intermediates (buffer);
309 find_cache_aux (int writing, char *directory, char *id,
310 char *buffer, int buflen)
313 char mapfile[BUFSIZ], mapname[BUFSIZ];
315 static int partno, pid;
316 static time_t clock = 0;
319 usemap = strchr (id, '/') ? 1 : 0;
325 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
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)
421 cp = add (buf, NULL);
422 while (state == FLDPLUS) {
423 state = m_getfld (state, name, buf, sizeof(buf), fp);
431 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
433 result = strcmp (id, dp);
436 lkfclose (fp, mapfile);
452 lkfclose (fp, mapfile);