]>
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.
9 #include "sbr/m_gmprot.h"
10 #include "sbr/m_getfld.h"
11 #include "sbr/read_yes_or_no_if_tty.h"
12 #include "sbr/trimcpy.h"
13 #include "sbr/error.h"
19 #include "h/mhparse.h"
20 #include "h/mhcachesbr.h"
23 #include "sbr/lock_file.h"
24 #include "sbr/m_mktemp.h"
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(CACHE
, caches
);
34 struct swit
*cache_policy
= caches
;
39 int rcachesw
= CACHE_ASK
;
40 int wcachesw
= CACHE_ASK
;
43 * Location of public and private cache. These must
44 * be set before these routines are called.
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 (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
)
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
);