]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
1 /* mhcachesbr.c -- routines to manipulate the MIME content cache
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
15 #include "h/mhparse.h"
16 #include "h/mhcachesbr.h"
19 #include "sbr/lock_file.h"
20 #include "sbr/m_mktemp.h"
22 #ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(CACHE
, caches
);
30 struct swit
*cache_policy
= caches
;
35 int rcachesw
= CACHE_ASK
;
36 int wcachesw
= CACHE_ASK
;
39 * Location of public and private cache. These must
40 * be set before these routines are called.
48 static void cache_content (CT
);
49 static int find_cache_aux (int, char *, char *, char *, int);
50 static int find_cache_aux2 (char *, char *, char *, int);
54 * Top level entry point to cache content
55 * from a group of messages
59 cache_all_messages (CT
*cts
)
63 for (ctp
= cts
; *ctp
; ctp
++) {
65 if (type_ok (ct
, 1)) {
72 (*ct
->c_ceclosefnx
) (ct
);
80 * Entry point to cache content from external sources.
87 char *file
, cachefile
[BUFSIZ
];
88 CE ce
= &ct
->c_cefile
;
91 inform("no %s: field in %s", ID_FIELD
, ct
->c_file
);
96 inform("unable to decode %s", ct
->c_file
);
100 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
101 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
103 inform("unable to cache %s's contents", ct
->c_file
);
106 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
108 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
113 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
117 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
120 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
123 if ((fp
= fopen (cachefile
, "w"))) {
126 FILE *gp
= ce
->ce_fp
;
128 fseek (gp
, 0L, SEEK_SET
);
130 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
132 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
133 advise ("cache_content", "fwrite");
138 admonish (ce
->ce_file
, "error reading");
139 (void) m_unlink (cachefile
);
142 admonish (cachefile
, "error writing");
143 (void) m_unlink (cachefile
);
148 content_error (cachefile
, ct
, "unable to fopen for writing");
153 fprintf (stderr
, "in place caching...\n");
156 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
157 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
163 find_cache (CT ct
, int policy
, int *writing
, char *id
,
164 char *buffer
, int buflen
)
173 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
174 policy
, writing
? "writing" : "reading", id
);
185 && find_cache_aux (0, cache_private
, id
,
186 buffer
, buflen
) == OK
) {
187 if (access (buffer
, R_OK
) != NOTOK
) {
197 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
198 buffer
, buflen
) == OK
) {
199 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
209 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
210 buffer
, buflen
) == OK
) {
211 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
218 if (status
== OK
&& policy
== CACHE_ASK
) {
220 char *bp
, query
[BUFSIZ
];
222 /* Get buffer ready to go */
224 buflen
= sizeof(query
);
226 /* Now, construct query */
228 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
232 snprintf (bp
, buflen
, "Use cached copy");
238 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
245 snprintf (bp
, buflen
, " (size %lu octets)",
246 (unsigned long) st
.st_size
);
252 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
254 /* Now, check answer */
255 if (!read_yes_or_no_if_tty (query
))
259 if (status
== OK
&& writing
) {
260 if (*writing
&& strchr(buffer
, '/'))
261 make_intermediates (buffer
);
262 (void) m_unlink (buffer
);
271 find_cache_aux (int writing
, char *directory
, char *id
,
272 char *buffer
, int buflen
)
276 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
278 int failed_to_lock
= 0;
279 static int partno
, pid
;
280 static time_t clock
= 0;
285 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
287 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
288 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
296 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
300 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
323 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
324 (unsigned int) (clock
& 0xffffffff),
325 (unsigned int) (pid
& 0xffff),
326 (unsigned int) (partno
++ & 0xff));
329 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
331 make_intermediates (mapfile
);
332 mask
= umask (writing
== 2 ? 0077 : 0);
333 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
336 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
338 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
339 if (failed_to_lock
) {
340 adios (mapfile
, "failed to lock");
347 fprintf (fp
, "%s: %s\n", mapname
, id
);
348 lkfclosedata (fp
, mapfile
);
352 strncpy (buffer
, mapname
, buflen
);
354 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
356 fprintf (stderr
, "use %s\n", buffer
);
363 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
366 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
368 m_getfld_state_t gstate
;
369 int failed_to_lock
= 0;
371 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
374 gstate
= m_getfld_state_init(fp
);
378 int bufsz
= sizeof buf
;
380 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
383 strncpy (mapname
, name
, namelen
);
384 if (state
!= FLDPLUS
)
387 cp
= mh_xstrdup(buf
);
388 while (state
== FLDPLUS
) {
390 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
398 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
400 result
= strcmp (id
, dp
);
403 lkfclosedata (fp
, mapfile
);
415 m_getfld_state_destroy (&gstate
);
417 lkfclosedata (fp
, mapfile
);