3 * mhcachesbr.c -- routines to manipulate the MIME content cache
10 #include <h/signals.h>
15 #include <zotnet/mts/mts.h>
16 #include <zotnet/tws/tws.h>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
29 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
32 int rcachesw = CACHE_ASK;
33 int wcachesw = CACHE_ASK;
36 * Location of public and private cache. These must
37 * be set before these routines are called.
43 /* mhparse.c (OR) mhbuildsbr.c */
47 int part_ok (CT, int);
48 int type_ok (CT, int);
49 int make_intermediates (char *);
50 void content_error (char *, CT, char *, ...);
51 void flush_errors (void);
56 void cache_all_messages (CT *);
57 int find_cache (CT, int, int *, char *, char *, int);
62 static void cache_content (CT);
63 static int find_cache_aux (int, char *, char *, char *, int);
64 static int find_cache_aux2 (char *, char *, char *, int);
68 * Top level entry point to cache content
69 * from a group of messages
73 cache_all_messages (CT *cts)
77 for (ctp = cts; *ctp; ctp++) {
79 if (type_ok (ct, 1)) {
86 (*ct->c_ceclosefnx) (ct);
94 * Entry point to cache content from external sources.
101 char *file, cachefile[BUFSIZ];
102 CE ce = ct->c_cefile;
105 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
110 advise (NULL, "unable to decode %s", ct->c_file);
114 /* THIS NEEDS TO BE FIXED */
116 if (ct->c_ceopenfnx == openMail) {
117 advise (NULL, "a radish may no know Greek, but I do...");
122 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
123 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
125 advise (NULL, "unable to cache %s's contents", ct->c_file);
128 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
130 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
135 int mask = umask (cachetype ? ~m_gmprot () : 0222);
139 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
142 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
145 if ((fp = fopen (cachefile, "w"))) {
148 FILE *gp = ce->ce_fp;
150 fseek (gp, 0L, SEEK_SET);
152 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
154 fwrite (buffer, sizeof(*buffer), cc, fp);
158 admonish (ce->ce_file, "error reading");
162 admonish (cachefile, "error writing");
168 content_error (cachefile, ct, "unable to fopen for writing");
173 fprintf (stderr, "in place caching...\n");
176 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
177 chmod (cachefile, cachetype ? m_gmprot () : 0444);
183 find_cache (CT ct, int policy, int *writing, char *id,
184 char *buffer, int buflen)
193 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
194 policy, writing ? "writing" : "reading", id);
205 && find_cache_aux (writing ? 2 : 0, cache_private, id,
206 buffer, buflen) == OK) {
207 if (access (buffer, R_OK) != NOTOK) {
217 && find_cache_aux (writing ? 1 : 0, cache_public, id,
218 buffer, buflen) == OK) {
219 if (writing || access (buffer, R_OK) != NOTOK) {
229 && find_cache_aux (writing ? 2 : 0, cache_private, id,
230 buffer, buflen) == OK) {
231 if (writing || access (buffer, R_OK) != NOTOK)
238 if (status == OK && policy == CACHE_ASK) {
240 char *bp, query[BUFSIZ];
245 pidcheck (pidwait (xpid, NOTOK));
249 /* Get buffer ready to go */
251 buflen = sizeof(query);
253 /* Now, construct query */
255 snprintf (bp, buflen, "Make cached, publically-accessible copy");
259 snprintf (bp, buflen, "Use cached copy");
265 snprintf (bp, buflen, " of content %s", ct->c_partno);
272 snprintf (bp, buflen, " (size %lu octets)",
273 (unsigned long) st.st_size);
279 snprintf (bp, buflen, "\n in file %s? ", buffer);
281 /* Now, check answer */
282 if (!getanswer (query))
286 if (status == OK && writing) {
287 if (*writing && strchr(buffer, '/'))
288 make_intermediates (buffer);
298 find_cache_aux (int writing, char *directory, char *id,
299 char *buffer, int buflen)
302 char mapfile[BUFSIZ], mapname[BUFSIZ];
304 static int partno, pid;
305 static time_t clock = 0;
308 usemap = strchr (id, '/') ? 1 : 0;
314 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
316 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
317 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
325 snprintf (buffer, buflen, "%s/%s", directory, id);
329 if (!usemap && access (mapfile, W_OK) == NOTOK)
352 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
353 (unsigned int) (clock & 0xffffffff),
354 (unsigned int) (pid & 0xffff),
355 (unsigned int) (partno++ & 0xff));
358 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
360 make_intermediates (mapfile);
361 mask = umask (writing == 2 ? 0077 : 0);
362 if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
365 if ((fd = creat (mapfile, 0666)) != NOTOK) {
367 fp = lkfopen (mapfile, "a");
373 fprintf (fp, "%s: %s\n", mapname, id);
374 lkfclose (fp, mapfile);
378 strncpy (buffer, mapname, buflen);
380 snprintf (buffer, buflen, "%s/%s", directory, mapname);
382 fprintf (stderr, "use %s\n", buffer);
389 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
392 char buf[BUFSIZ], name[NAMESZ];
395 if (!(fp = lkfopen (mapfile, "r")))
398 for (state = FLD;;) {
402 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
406 strncpy (mapname, name, namelen);
407 if (state != FLDPLUS)
410 cp = add (buf, NULL);
411 while (state == FLDPLUS) {
412 state = m_getfld (state, name, buf, sizeof(buf), fp);
420 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
422 result = strcmp (id, dp);
425 lkfclose (fp, mapfile);
441 lkfclose (fp, mapfile);