3 * mhcachesbr.c -- routines to manipulate the MIME content cache
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
23 #include <h/mhcachesbr.h>
25 #ifdef TIME_WITH_SYS_TIME
26 # include <sys/time.h>
29 # ifdef TM_IN_SYS_TIME
30 # include <sys/time.h>
36 #ifdef HAVE_SYS_WAIT_H
37 # include <sys/wait.h>
42 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
45 int rcachesw = CACHE_ASK;
46 int wcachesw = CACHE_ASK;
49 * Location of public and private cache. These must
50 * be set before these routines are called.
56 /* mhparse.c (OR) mhbuildsbr.c */
60 int part_ok (CT, int);
61 int type_ok (CT, int);
62 int make_intermediates (char *);
63 void content_error (char *, CT, char *, ...);
64 void flush_errors (void);
69 void cache_all_messages (CT *);
70 int find_cache (CT, int, int *, char *, char *, int);
75 static void cache_content (CT);
76 static int find_cache_aux (int, char *, char *, char *, int);
77 static int find_cache_aux2 (char *, char *, char *, int);
81 * Top level entry point to cache content
82 * from a group of messages
86 cache_all_messages (CT *cts)
90 for (ctp = cts; *ctp; ctp++) {
92 if (type_ok (ct, 1)) {
99 (*ct->c_ceclosefnx) (ct);
107 * Entry point to cache content from external sources.
111 cache_content (CT ct)
114 char *file, cachefile[BUFSIZ];
115 CE ce = ct->c_cefile;
118 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
123 advise (NULL, "unable to decode %s", ct->c_file);
127 /* THIS NEEDS TO BE FIXED */
129 if (ct->c_ceopenfnx == openMail) {
130 advise (NULL, "a radish may no know Greek, but I do...");
135 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
136 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
138 advise (NULL, "unable to cache %s's contents", ct->c_file);
141 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
143 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
148 int mask = umask (cachetype ? ~m_gmprot () : 0222);
152 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
155 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
158 if ((fp = fopen (cachefile, "w"))) {
161 FILE *gp = ce->ce_fp;
163 fseek (gp, 0L, SEEK_SET);
165 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
167 fwrite (buffer, sizeof(*buffer), cc, fp);
171 admonish (ce->ce_file, "error reading");
175 admonish (cachefile, "error writing");
181 content_error (cachefile, ct, "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", caches[policy].sw,
207 policy, writing ? "writing" : "reading", id);
218 && find_cache_aux (writing ? 2 : 0, cache_private, id,
219 buffer, buflen) == OK) {
220 if (access (buffer, R_OK) != NOTOK) {
230 && find_cache_aux (writing ? 1 : 0, cache_public, id,
231 buffer, buflen) == OK) {
232 if (writing || access (buffer, R_OK) != NOTOK) {
242 && find_cache_aux (writing ? 2 : 0, cache_private, id,
243 buffer, buflen) == OK) {
244 if (writing || access (buffer, R_OK) != NOTOK)
251 if (status == OK && policy == CACHE_ASK) {
253 char *bp, query[BUFSIZ];
258 pidcheck (pidwait (xpid, NOTOK));
262 /* Get buffer ready to go */
264 buflen = sizeof(query);
266 /* Now, construct query */
268 snprintf (bp, buflen, "Make cached, publically-accessible copy");
272 snprintf (bp, buflen, "Use cached copy");
278 snprintf (bp, buflen, " of content %s", ct->c_partno);
285 snprintf (bp, buflen, " (size %lu octets)",
286 (unsigned long) st.st_size);
292 snprintf (bp, buflen, "\n in file %s? ", buffer);
294 /* Now, check answer */
295 if (!getanswer (query))
299 if (status == OK && writing) {
300 if (*writing && strchr(buffer, '/'))
301 make_intermediates (buffer);
311 find_cache_aux (int writing, char *directory, char *id,
312 char *buffer, int buflen)
315 char mapfile[BUFSIZ], mapname[BUFSIZ];
317 static int partno, pid;
318 static time_t clock = 0;
321 usemap = strchr (id, '/') ? 1 : 0;
327 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
329 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
330 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
338 snprintf (buffer, buflen, "%s/%s", directory, id);
342 if (!usemap && access (mapfile, W_OK) == NOTOK)
365 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
366 (unsigned int) (clock & 0xffffffff),
367 (unsigned int) (pid & 0xffff),
368 (unsigned int) (partno++ & 0xff));
371 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
373 make_intermediates (mapfile);
374 mask = umask (writing == 2 ? 0077 : 0);
375 if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
378 if ((fd = creat (mapfile, 0666)) != NOTOK) {
380 fp = lkfopen (mapfile, "a");
386 fprintf (fp, "%s: %s\n", mapname, id);
387 lkfclose (fp, mapfile);
391 strncpy (buffer, mapname, buflen);
393 snprintf (buffer, buflen, "%s/%s", directory, mapname);
395 fprintf (stderr, "use %s\n", buffer);
402 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
405 char buf[BUFSIZ], name[NAMESZ];
408 if (!(fp = lkfopen (mapfile, "r")))
411 for (state = FLD;;) {
415 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
419 strncpy (mapname, name, namelen);
420 if (state != FLDPLUS)
423 cp = add (buf, NULL);
424 while (state == FLDPLUS) {
425 state = m_getfld (state, name, buf, sizeof(buf), fp);
433 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
435 result = strcmp (id, dp);
438 lkfclose (fp, mapfile);
454 lkfclose (fp, mapfile);