]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
3 * mhcachesbr.c -- routines to manipulate the MIME content cache
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
28 int rcachesw
= CACHE_ASK
;
29 int wcachesw
= CACHE_ASK
;
32 * Location of public and private cache. These must
33 * be set before these routines are called.
41 int type_ok (CT
, int);
42 void content_error (char *, CT
, char *, ...);
43 void flush_errors (void);
48 void cache_all_messages (CT
*);
49 int find_cache (CT
, int, int *, char *, char *, int);
54 static void cache_content (CT
);
55 static int find_cache_aux (int, char *, char *, char *, int);
56 static int find_cache_aux2 (char *, char *, char *, int);
60 * Top level entry point to cache content
61 * from a group of messages
65 cache_all_messages (CT
*cts
)
69 for (ctp
= cts
; *ctp
; ctp
++) {
71 if (type_ok (ct
, 1)) {
78 (*ct
->c_ceclosefnx
) (ct
);
86 * Entry point to cache content from external sources.
93 char *file
, cachefile
[BUFSIZ
];
94 CE ce
= &ct
->c_cefile
;
97 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
102 advise (NULL
, "unable to decode %s", ct
->c_file
);
106 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
107 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
109 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
112 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
114 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
119 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
123 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
126 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
129 if ((fp
= fopen (cachefile
, "w"))) {
132 FILE *gp
= ce
->ce_fp
;
134 fseek (gp
, 0L, SEEK_SET
);
136 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
138 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
139 advise ("cache_content", "fwrite");
144 admonish (ce
->ce_file
, "error reading");
145 (void) m_unlink (cachefile
);
148 admonish (cachefile
, "error writing");
149 (void) m_unlink (cachefile
);
154 content_error (cachefile
, ct
, "unable to fopen for writing");
159 fprintf (stderr
, "in place caching...\n");
162 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
163 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
169 find_cache (CT ct
, int policy
, int *writing
, char *id
,
170 char *buffer
, int buflen
)
179 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
180 policy
, writing
? "writing" : "reading", id
);
191 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
192 buffer
, buflen
) == OK
) {
193 if (access (buffer
, R_OK
) != NOTOK
) {
203 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
204 buffer
, buflen
) == OK
) {
205 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
215 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
216 buffer
, buflen
) == OK
) {
217 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
224 if (status
== OK
&& policy
== CACHE_ASK
) {
226 char *bp
, query
[BUFSIZ
];
228 /* Get buffer ready to go */
230 buflen
= sizeof(query
);
232 /* Now, construct query */
234 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
238 snprintf (bp
, buflen
, "Use cached copy");
244 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
251 snprintf (bp
, buflen
, " (size %lu octets)",
252 (unsigned long) st
.st_size
);
258 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
260 /* Now, check answer */
261 if (!read_yes_or_no_if_tty (query
))
265 if (status
== OK
&& writing
) {
266 if (*writing
&& strchr(buffer
, '/'))
267 make_intermediates (buffer
);
268 (void) m_unlink (buffer
);
277 find_cache_aux (int writing
, char *directory
, char *id
,
278 char *buffer
, int buflen
)
281 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
283 int failed_to_lock
= 0;
284 static int partno
, pid
;
285 static time_t clock
= 0;
290 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
292 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
293 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
301 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
305 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
328 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
329 (unsigned int) (clock
& 0xffffffff),
330 (unsigned int) (pid
& 0xffff),
331 (unsigned int) (partno
++ & 0xff));
334 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
336 make_intermediates (mapfile
);
337 mask
= umask (writing
== 2 ? 0077 : 0);
338 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
341 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
343 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
344 if (failed_to_lock
) {
345 adios (mapfile
, "failed to lock");
352 fprintf (fp
, "%s: %s\n", mapname
, id
);
353 lkfclosedata (fp
, mapfile
);
357 strncpy (buffer
, mapname
, buflen
);
359 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
361 fprintf (stderr
, "use %s\n", buffer
);
368 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
371 char buf
[BUFSIZ
], name
[NAMESZ
];
373 m_getfld_state_t gstate
= 0;
374 int failed_to_lock
= 0;
376 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
382 int bufsz
= sizeof buf
;
384 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
387 strncpy (mapname
, name
, namelen
);
388 if (state
!= FLDPLUS
)
391 cp
= mh_xstrdup(buf
);
392 while (state
== FLDPLUS
) {
394 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
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
);