]>
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>
17 #include "../sbr/lock_file.h"
18 #include "../sbr/m_mktemp.h"
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(CACHE
, caches
);
28 struct swit
*cache_policy
= caches
;
33 int rcachesw
= CACHE_ASK
;
34 int wcachesw
= CACHE_ASK
;
37 * Location of public and private cache. These must
38 * be set before these routines are called.
46 int type_ok (CT
, int);
47 void content_error (char *, CT
, char *, ...);
48 void flush_errors (void);
53 static void cache_content (CT
);
54 static int find_cache_aux (int, char *, char *, char *, int);
55 static int find_cache_aux2 (char *, char *, char *, int);
59 * Top level entry point to cache content
60 * from a group of messages
64 cache_all_messages (CT
*cts
)
68 for (ctp
= cts
; *ctp
; ctp
++) {
70 if (type_ok (ct
, 1)) {
77 (*ct
->c_ceclosefnx
) (ct
);
85 * Entry point to cache content from external sources.
92 char *file
, cachefile
[BUFSIZ
];
93 CE ce
= &ct
->c_cefile
;
96 inform("no %s: field in %s", ID_FIELD
, ct
->c_file
);
101 inform("unable to decode %s", ct
->c_file
);
105 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
106 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
108 inform("unable to cache %s's contents", ct
->c_file
);
111 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
113 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
118 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
122 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
125 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
128 if ((fp
= fopen (cachefile
, "w"))) {
131 FILE *gp
= ce
->ce_fp
;
133 fseek (gp
, 0L, SEEK_SET
);
135 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
137 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
138 advise ("cache_content", "fwrite");
143 admonish (ce
->ce_file
, "error reading");
144 (void) m_unlink (cachefile
);
147 admonish (cachefile
, "error writing");
148 (void) m_unlink (cachefile
);
153 content_error (cachefile
, ct
, "unable to fopen for writing");
158 fprintf (stderr
, "in place caching...\n");
161 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
162 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
168 find_cache (CT ct
, int policy
, int *writing
, char *id
,
169 char *buffer
, int buflen
)
178 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
179 policy
, writing
? "writing" : "reading", id
);
190 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
191 buffer
, buflen
) == OK
) {
192 if (access (buffer
, R_OK
) != NOTOK
) {
202 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
203 buffer
, buflen
) == OK
) {
204 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
214 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
215 buffer
, buflen
) == OK
) {
216 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
223 if (status
== OK
&& policy
== CACHE_ASK
) {
225 char *bp
, query
[BUFSIZ
];
227 /* Get buffer ready to go */
229 buflen
= sizeof(query
);
231 /* Now, construct query */
233 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
237 snprintf (bp
, buflen
, "Use cached copy");
243 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
250 snprintf (bp
, buflen
, " (size %lu octets)",
251 (unsigned long) st
.st_size
);
257 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
259 /* Now, check answer */
260 if (!read_yes_or_no_if_tty (query
))
264 if (status
== OK
&& writing
) {
265 if (*writing
&& strchr(buffer
, '/'))
266 make_intermediates (buffer
);
267 (void) m_unlink (buffer
);
276 find_cache_aux (int writing
, char *directory
, char *id
,
277 char *buffer
, int buflen
)
280 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
282 int failed_to_lock
= 0;
283 static int partno
, pid
;
284 static time_t clock
= 0;
289 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
291 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
292 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
300 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
304 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
327 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
328 (unsigned int) (clock
& 0xffffffff),
329 (unsigned int) (pid
& 0xffff),
330 (unsigned int) (partno
++ & 0xff));
333 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
335 make_intermediates (mapfile
);
336 mask
= umask (writing
== 2 ? 0077 : 0);
337 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
340 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
342 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
343 if (failed_to_lock
) {
344 adios (mapfile
, "failed to lock");
351 fprintf (fp
, "%s: %s\n", mapname
, id
);
352 lkfclosedata (fp
, mapfile
);
356 strncpy (buffer
, mapname
, buflen
);
358 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
360 fprintf (stderr
, "use %s\n", buffer
);
367 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
370 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
372 m_getfld_state_t gstate
;
373 int failed_to_lock
= 0;
375 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
378 gstate
= m_getfld_state_init(fp
);
382 int bufsz
= sizeof buf
;
384 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
387 strncpy (mapname
, name
, namelen
);
388 if (state
!= FLDPLUS
)
391 cp
= mh_xstrdup(buf
);
392 while (state
== FLDPLUS
) {
394 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
402 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
404 result
= strcmp (id
, dp
);
407 lkfclosedata (fp
, mapfile
);
419 m_getfld_state_destroy (&gstate
);
421 lkfclosedata (fp
, mapfile
);