]>
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.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
29 int rcachesw
= CACHE_ASK
;
30 int wcachesw
= CACHE_ASK
;
33 * Location of public and private cache. These must
34 * be set before these routines are called.
42 int type_ok (CT
, int);
43 void content_error (char *, CT
, char *, ...);
44 void flush_errors (void);
49 void cache_all_messages (CT
*);
50 int find_cache (CT
, int, int *, char *, char *, int);
55 static void cache_content (CT
);
56 static int find_cache_aux (int, char *, char *, char *, int);
57 static int find_cache_aux2 (char *, char *, char *, int);
61 * Top level entry point to cache content
62 * from a group of messages
66 cache_all_messages (CT
*cts
)
70 for (ctp
= cts
; *ctp
; ctp
++) {
72 if (type_ok (ct
, 1)) {
79 (*ct
->c_ceclosefnx
) (ct
);
87 * Entry point to cache content from external sources.
94 char *file
, cachefile
[BUFSIZ
];
95 CE ce
= &ct
->c_cefile
;
98 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
103 advise (NULL
, "unable to decode %s", ct
->c_file
);
107 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
108 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
110 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
113 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
115 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
120 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
124 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
127 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
130 if ((fp
= fopen (cachefile
, "w"))) {
133 FILE *gp
= ce
->ce_fp
;
135 fseek (gp
, 0L, SEEK_SET
);
137 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
139 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
140 advise ("cache_content", "fwrite");
145 admonish (ce
->ce_file
, "error reading");
146 (void) m_unlink (cachefile
);
149 admonish (cachefile
, "error writing");
150 (void) m_unlink (cachefile
);
155 content_error (cachefile
, ct
, "unable to fopen for writing");
160 fprintf (stderr
, "in place caching...\n");
163 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
164 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
170 find_cache (CT ct
, int policy
, int *writing
, char *id
,
171 char *buffer
, int buflen
)
180 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
181 policy
, writing
? "writing" : "reading", id
);
192 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
193 buffer
, buflen
) == OK
) {
194 if (access (buffer
, R_OK
) != NOTOK
) {
204 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
205 buffer
, buflen
) == OK
) {
206 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
216 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
217 buffer
, buflen
) == OK
) {
218 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
225 if (status
== OK
&& policy
== CACHE_ASK
) {
227 char *bp
, query
[BUFSIZ
];
229 /* Get buffer ready to go */
231 buflen
= sizeof(query
);
233 /* Now, construct query */
235 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
239 snprintf (bp
, buflen
, "Use cached copy");
245 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
252 snprintf (bp
, buflen
, " (size %lu octets)",
253 (unsigned long) st
.st_size
);
259 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
261 /* Now, check answer */
262 if (!getanswer (query
))
266 if (status
== OK
&& writing
) {
267 if (*writing
&& strchr(buffer
, '/'))
268 make_intermediates (buffer
);
269 (void) m_unlink (buffer
);
278 find_cache_aux (int writing
, char *directory
, char *id
,
279 char *buffer
, int buflen
)
282 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
284 int failed_to_lock
= 0;
285 static int partno
, pid
;
286 static time_t clock
= 0;
291 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
293 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
294 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
302 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
306 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
329 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
330 (unsigned int) (clock
& 0xffffffff),
331 (unsigned int) (pid
& 0xffff),
332 (unsigned int) (partno
++ & 0xff));
335 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
337 make_intermediates (mapfile
);
338 mask
= umask (writing
== 2 ? 0077 : 0);
339 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
342 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
344 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
345 if (failed_to_lock
) {
346 adios (mapfile
, "failed to lock");
353 fprintf (fp
, "%s: %s\n", mapname
, id
);
354 lkfclosedata (fp
, mapfile
);
358 strncpy (buffer
, mapname
, buflen
);
360 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
362 fprintf (stderr
, "use %s\n", buffer
);
369 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
372 char buf
[BUFSIZ
], name
[NAMESZ
];
374 m_getfld_state_t gstate
= 0;
375 int failed_to_lock
= 0;
377 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
383 int bufsz
= sizeof buf
;
385 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
388 strncpy (mapname
, name
, namelen
);
389 if (state
!= FLDPLUS
)
392 cp
= add (buf
, NULL
);
393 while (state
== FLDPLUS
) {
395 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
403 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
405 result
= strcmp (id
, dp
);
408 lkfclosedata (fp
, mapfile
);
420 m_getfld_state_destroy (&gstate
);
422 lkfclosedata (fp
, mapfile
);