]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
36b05b9b7ce6bb3c1eabc6ffe9b340a3b5c95080
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>
18 #include <h/mhparse.h>
19 #include <h/mhcachesbr.h>
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.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.
44 int part_ok (CT
, int);
45 int type_ok (CT
, int);
46 void content_error (char *, CT
, char *, ...);
47 void flush_errors (void);
52 void cache_all_messages (CT
*);
53 int find_cache (CT
, int, int *, char *, char *, int);
58 static void cache_content (CT
);
59 static int find_cache_aux (int, char *, char *, char *, int);
60 static int find_cache_aux2 (char *, char *, char *, int);
64 * Top level entry point to cache content
65 * from a group of messages
69 cache_all_messages (CT
*cts
)
73 for (ctp
= cts
; *ctp
; ctp
++) {
75 if (type_ok (ct
, 1)) {
82 (*ct
->c_ceclosefnx
) (ct
);
90 * Entry point to cache content from external sources.
97 char *file
, cachefile
[BUFSIZ
];
98 CE ce
= &ct
->c_cefile
;
101 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
106 advise (NULL
, "unable to decode %s", ct
->c_file
);
110 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
111 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
113 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
116 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
118 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
123 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
127 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
130 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
133 if ((fp
= fopen (cachefile
, "w"))) {
136 FILE *gp
= ce
->ce_fp
;
138 fseek (gp
, 0L, SEEK_SET
);
140 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
142 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
146 admonish (ce
->ce_file
, "error reading");
150 admonish (cachefile
, "error writing");
156 content_error (cachefile
, ct
, "unable to fopen for writing");
161 fprintf (stderr
, "in place caching...\n");
164 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
165 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
171 find_cache (CT ct
, int policy
, int *writing
, char *id
,
172 char *buffer
, int buflen
)
181 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
182 policy
, writing
? "writing" : "reading", id
);
193 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
194 buffer
, buflen
) == OK
) {
195 if (access (buffer
, R_OK
) != NOTOK
) {
205 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
206 buffer
, buflen
) == OK
) {
207 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
217 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
218 buffer
, buflen
) == OK
) {
219 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
226 if (status
== OK
&& policy
== CACHE_ASK
) {
228 char *bp
, query
[BUFSIZ
];
233 pidcheck (pidwait (xpid
, NOTOK
));
237 /* Get buffer ready to go */
239 buflen
= sizeof(query
);
241 /* Now, construct query */
243 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
247 snprintf (bp
, buflen
, "Use cached copy");
253 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
260 snprintf (bp
, buflen
, " (size %lu octets)",
261 (unsigned long) st
.st_size
);
267 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
269 /* Now, check answer */
270 if (!getanswer (query
))
274 if (status
== OK
&& writing
) {
275 if (*writing
&& strchr(buffer
, '/'))
276 make_intermediates (buffer
);
286 find_cache_aux (int writing
, char *directory
, char *id
,
287 char *buffer
, int buflen
)
290 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
292 static int partno
, pid
;
293 static time_t clock
= 0;
298 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
300 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
301 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
309 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
313 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
336 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
337 (unsigned int) (clock
& 0xffffffff),
338 (unsigned int) (pid
& 0xffff),
339 (unsigned int) (partno
++ & 0xff));
342 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
344 make_intermediates (mapfile
);
345 mask
= umask (writing
== 2 ? 0077 : 0);
346 if (!(fp
= lkfopendata (mapfile
, "a")) && errno
== ENOENT
) {
349 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
351 fp
= lkfopendata (mapfile
, "a");
357 fprintf (fp
, "%s: %s\n", mapname
, id
);
358 lkfclosedata (fp
, mapfile
);
362 strncpy (buffer
, mapname
, buflen
);
364 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
366 fprintf (stderr
, "use %s\n", buffer
);
373 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
376 char buf
[BUFSIZ
], name
[NAMESZ
];
378 m_getfld_state_t gstate
= 0;
380 if (!(fp
= lkfopendata (mapfile
, "r")))
386 int bufsz
= sizeof buf
;
388 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
391 strncpy (mapname
, name
, namelen
);
392 if (state
!= FLDPLUS
)
395 cp
= add (buf
, NULL
);
396 while (state
== FLDPLUS
) {
398 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
406 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
408 result
= strcmp (id
, dp
);
411 lkfclosedata (fp
, mapfile
);
423 m_getfld_state_destroy (&gstate
);
425 lkfclosedata (fp
, mapfile
);