]>
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.
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
26 int rcachesw
= CACHE_ASK
;
27 int wcachesw
= CACHE_ASK
;
30 * Location of public and private cache. These must
31 * be set before these routines are called.
39 int type_ok (CT
, int);
40 void content_error (char *, CT
, char *, ...);
41 void flush_errors (void);
46 void cache_all_messages (CT
*);
47 int find_cache (CT
, int, int *, char *, char *, int);
52 static void cache_content (CT
);
53 static int find_cache_aux (int, char *, char *, char *, int);
54 static int find_cache_aux2 (char *, char *, char *, int);
58 * Top level entry point to cache content
59 * from a group of messages
63 cache_all_messages (CT
*cts
)
67 for (ctp
= cts
; *ctp
; ctp
++) {
69 if (type_ok (ct
, 1)) {
76 (*ct
->c_ceclosefnx
) (ct
);
84 * Entry point to cache content from external sources.
91 char *file
, cachefile
[BUFSIZ
];
92 CE ce
= &ct
->c_cefile
;
95 inform("no %s: field in %s", ID_FIELD
, ct
->c_file
);
100 inform("unable to decode %s", ct
->c_file
);
104 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
105 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
107 inform("unable to cache %s's contents", ct
->c_file
);
110 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
112 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
117 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
121 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
124 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
127 if ((fp
= fopen (cachefile
, "w"))) {
130 FILE *gp
= ce
->ce_fp
;
132 fseek (gp
, 0L, SEEK_SET
);
134 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
136 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
137 advise ("cache_content", "fwrite");
142 admonish (ce
->ce_file
, "error reading");
143 (void) m_unlink (cachefile
);
146 admonish (cachefile
, "error writing");
147 (void) m_unlink (cachefile
);
152 content_error (cachefile
, ct
, "unable to fopen for writing");
157 fprintf (stderr
, "in place caching...\n");
160 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
161 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
167 find_cache (CT ct
, int policy
, int *writing
, char *id
,
168 char *buffer
, int buflen
)
177 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
178 policy
, writing
? "writing" : "reading", id
);
189 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
190 buffer
, buflen
) == OK
) {
191 if (access (buffer
, R_OK
) != NOTOK
) {
201 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
202 buffer
, buflen
) == OK
) {
203 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
213 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
214 buffer
, buflen
) == OK
) {
215 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
222 if (status
== OK
&& policy
== CACHE_ASK
) {
224 char *bp
, query
[BUFSIZ
];
226 /* Get buffer ready to go */
228 buflen
= sizeof(query
);
230 /* Now, construct query */
232 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
236 snprintf (bp
, buflen
, "Use cached copy");
242 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
249 snprintf (bp
, buflen
, " (size %lu octets)",
250 (unsigned long) st
.st_size
);
256 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
258 /* Now, check answer */
259 if (!read_yes_or_no_if_tty (query
))
263 if (status
== OK
&& writing
) {
264 if (*writing
&& strchr(buffer
, '/'))
265 make_intermediates (buffer
);
266 (void) m_unlink (buffer
);
275 find_cache_aux (int writing
, char *directory
, char *id
,
276 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
[BUFSIZ
], name
[NAMESZ
];
371 m_getfld_state_t gstate
= 0;
372 int failed_to_lock
= 0;
374 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
380 int bufsz
= sizeof buf
;
382 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
385 strncpy (mapname
, name
, namelen
);
386 if (state
!= FLDPLUS
)
389 cp
= mh_xstrdup(buf
);
390 while (state
== FLDPLUS
) {
392 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
400 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
402 result
= strcmp (id
, dp
);
405 lkfclosedata (fp
, mapfile
);
417 m_getfld_state_destroy (&gstate
);
419 lkfclosedata (fp
, mapfile
);