]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
1cdd6fdbc2f36392c74fc6fce1ccda01139ac7ca
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.
9 #include "sbr/m_getfld.h"
10 #include "sbr/read_yes_or_no_if_tty.h"
11 #include "sbr/trimcpy.h"
12 #include "sbr/error.h"
18 #include "h/mhparse.h"
19 #include "h/mhcachesbr.h"
22 #include "sbr/lock_file.h"
23 #include "sbr/m_mktemp.h"
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(CACHE
, caches
);
33 struct swit
*cache_policy
= caches
;
38 int rcachesw
= CACHE_ASK
;
39 int wcachesw
= CACHE_ASK
;
42 * Location of public and private cache. These must
43 * be set before these routines are called.
51 static void cache_content (CT
);
52 static int find_cache_aux (int, char *, char *, char *, int);
53 static int find_cache_aux2 (char *, char *, char *, int);
57 * Top level entry point to cache content
58 * from a group of messages
62 cache_all_messages (CT
*cts
)
66 for (ctp
= cts
; *ctp
; ctp
++) {
68 if (type_ok (ct
, 1)) {
75 (*ct
->c_ceclosefnx
) (ct
);
83 * Entry point to cache content from external sources.
90 char *file
, cachefile
[BUFSIZ
];
91 CE ce
= &ct
->c_cefile
;
94 inform("no %s: field in %s", ID_FIELD
, ct
->c_file
);
99 inform("unable to decode %s", ct
->c_file
);
103 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
104 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
106 inform("unable to cache %s's contents", ct
->c_file
);
109 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
111 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
116 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
120 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
123 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
126 if ((fp
= fopen (cachefile
, "w"))) {
129 FILE *gp
= ce
->ce_fp
;
131 fseek (gp
, 0L, SEEK_SET
);
133 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
135 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
136 advise ("cache_content", "fwrite");
141 admonish (ce
->ce_file
, "error reading");
142 (void) m_unlink (cachefile
);
145 admonish (cachefile
, "error writing");
146 (void) m_unlink (cachefile
);
151 content_error (cachefile
, ct
, "unable to fopen for writing");
156 fprintf (stderr
, "in place caching...\n");
159 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
160 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
166 find_cache (CT ct
, int policy
, int *writing
, char *id
,
167 char *buffer
, int buflen
)
176 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
177 policy
, writing
? "writing" : "reading", id
);
188 && find_cache_aux (0, cache_private
, id
,
189 buffer
, buflen
) == OK
) {
190 if (access (buffer
, R_OK
) != NOTOK
) {
200 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
201 buffer
, buflen
) == OK
) {
202 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
212 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
213 buffer
, buflen
) == OK
) {
214 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
221 if (status
== OK
&& policy
== CACHE_ASK
) {
223 char *bp
, query
[BUFSIZ
];
225 /* Get buffer ready to go */
227 buflen
= sizeof(query
);
229 /* Now, construct query */
231 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
235 snprintf (bp
, buflen
, "Use cached copy");
241 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
248 snprintf (bp
, buflen
, " (size %lu octets)",
249 (unsigned long) st
.st_size
);
255 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
257 /* Now, check answer */
258 if (!read_yes_or_no_if_tty (query
))
262 if (status
== OK
&& writing
) {
263 if (*writing
&& strchr(buffer
, '/'))
264 make_intermediates (buffer
);
265 (void) m_unlink (buffer
);
274 find_cache_aux (int writing
, char *directory
, char *id
,
275 char *buffer
, int buflen
)
279 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
281 int failed_to_lock
= 0;
282 static int partno
, pid
;
283 static time_t clock
= 0;
288 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
290 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
291 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
299 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
303 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
326 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
327 (unsigned int) (clock
& 0xffffffff),
328 (unsigned int) (pid
& 0xffff),
329 (unsigned int) (partno
++ & 0xff));
332 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
334 make_intermediates (mapfile
);
335 mask
= umask (writing
== 2 ? 0077 : 0);
336 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
339 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
341 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
342 if (failed_to_lock
) {
343 adios (mapfile
, "failed to lock");
350 fprintf (fp
, "%s: %s\n", mapname
, id
);
351 lkfclosedata (fp
, mapfile
);
355 strncpy (buffer
, mapname
, buflen
);
357 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
359 fprintf (stderr
, "use %s\n", buffer
);
366 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
369 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
371 m_getfld_state_t gstate
;
372 int failed_to_lock
= 0;
374 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
377 gstate
= m_getfld_state_init(fp
);
381 int bufsz
= sizeof buf
;
383 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
386 strncpy (mapname
, name
, namelen
);
387 if (state
!= FLDPLUS
)
390 cp
= mh_xstrdup(buf
);
391 while (state
== FLDPLUS
) {
393 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
401 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
403 result
= strcmp (id
, dp
);
406 lkfclosedata (fp
, mapfile
);
418 m_getfld_state_destroy (&gstate
);
420 lkfclosedata (fp
, mapfile
);