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 TIME_WITH_SYS_TIME
25 # include <sys/time.h>
28 # ifdef TM_IN_SYS_TIME
29 # include <sys/time.h>
35 #ifdef HAVE_SYS_WAIT_H
36 # include <sys/wait.h>
41 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
44 int rcachesw = CACHE_ASK;
45 int wcachesw = CACHE_ASK;
48 * Location of public and private cache. These must
49 * be set before these routines are called.
55 /* mhparse.c (OR) mhbuildsbr.c */
59 int part_ok (CT, int);
60 int type_ok (CT, int);
61 int make_intermediates (char *);
62 void content_error (char *, CT, char *, ...);
63 void flush_errors (void);
68 void cache_all_messages (CT *);
69 int find_cache (CT, int, int *, char *, char *, int);
74 static void cache_content (CT);
75 static int find_cache_aux (int, char *, char *, char *, int);
76 static int find_cache_aux2 (char *, char *, char *, int);
80 * Top level entry point to cache content
81 * from a group of messages
85 cache_all_messages (CT *cts)
89 for (ctp = cts; *ctp; ctp++) {
91 if (type_ok (ct, 1)) {
98 (*ct->c_ceclosefnx) (ct);
106 * Entry point to cache content from external sources.
110 cache_content (CT ct)
113 char *file, cachefile[BUFSIZ];
114 CE ce = ct->c_cefile;
117 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
122 advise (NULL, "unable to decode %s", ct->c_file);
126 /* THIS NEEDS TO BE FIXED */
128 if (ct->c_ceopenfnx == openMail) {
129 advise (NULL, "a radish may no know Greek, but I do...");
134 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
135 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
137 advise (NULL, "unable to cache %s's contents", ct->c_file);
140 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
142 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
147 int mask = umask (cachetype ? ~m_gmprot () : 0222);
151 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
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), sizeof(buffer), gp))
166 fwrite (buffer, sizeof(*buffer), cc, fp);
170 admonish (ce->ce_file, "error reading");
174 admonish (cachefile, "error writing");
180 content_error (cachefile, ct, "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,
196 char *buffer, int buflen)
205 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
206 policy, writing ? "writing" : "reading", id);
217 && find_cache_aux (writing ? 2 : 0, cache_private, id,
218 buffer, buflen) == OK) {
219 if (access (buffer, R_OK) != NOTOK) {
229 && find_cache_aux (writing ? 1 : 0, cache_public, id,
230 buffer, buflen) == OK) {
231 if (writing || access (buffer, R_OK) != NOTOK) {
241 && find_cache_aux (writing ? 2 : 0, cache_private, id,
242 buffer, buflen) == OK) {
243 if (writing || access (buffer, R_OK) != NOTOK)
250 if (status == OK && policy == CACHE_ASK) {
252 char *bp, query[BUFSIZ];
257 pidcheck (pidwait (xpid, NOTOK));
261 /* Get buffer ready to go */
263 buflen = sizeof(query);
265 /* Now, construct query */
267 snprintf (bp, buflen, "Make cached, publically-accessible copy");
271 snprintf (bp, buflen, "Use cached copy");
277 snprintf (bp, buflen, " of content %s", ct->c_partno);
284 snprintf (bp, buflen, " (size %lu octets)",
285 (unsigned long) st.st_size);
291 snprintf (bp, buflen, "\n in file %s? ", buffer);
293 /* Now, check answer */
294 if (!getanswer (query))
298 if (status == OK && writing) {
299 if (*writing && strchr(buffer, '/'))
300 make_intermediates (buffer);
310 find_cache_aux (int writing, char *directory, char *id,
311 char *buffer, int buflen)
314 char mapfile[BUFSIZ], mapname[BUFSIZ];
316 static int partno, pid;
317 static time_t clock = 0;
320 usemap = strchr (id, '/') ? 1 : 0;
326 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
328 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
329 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
337 snprintf (buffer, buflen, "%s/%s", directory, id);
341 if (!usemap && access (mapfile, W_OK) == NOTOK)
364 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
365 (unsigned int) (clock & 0xffffffff),
366 (unsigned int) (pid & 0xffff),
367 (unsigned int) (partno++ & 0xff));
370 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
372 make_intermediates (mapfile);
373 mask = umask (writing == 2 ? 0077 : 0);
374 if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
377 if ((fd = creat (mapfile, 0666)) != NOTOK) {
379 fp = lkfopen (mapfile, "a");
385 fprintf (fp, "%s: %s\n", mapname, id);
386 lkfclose (fp, mapfile);
390 strncpy (buffer, mapname, buflen);
392 snprintf (buffer, buflen, "%s/%s", directory, mapname);
394 fprintf (stderr, "use %s\n", buffer);
401 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
404 char buf[BUFSIZ], name[NAMESZ];
407 if (!(fp = lkfopen (mapfile, "r")))
410 for (state = FLD;;) {
414 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
418 strncpy (mapname, name, namelen);
419 if (state != FLDPLUS)
422 cp = add (buf, NULL);
423 while (state == FLDPLUS) {
424 state = m_getfld (state, name, buf, sizeof(buf), fp);
432 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
434 result = strcmp (id, dp);
437 lkfclose (fp, mapfile);
453 lkfclose (fp, mapfile);