]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
30 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
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.
45 int part_ok (CT
, int);
46 int type_ok (CT
, int);
47 void content_error (char *, CT
, char *, ...);
48 void flush_errors (void);
53 void cache_all_messages (CT
*);
54 int find_cache (CT
, int, int *, char *, char *, int);
59 static void cache_content (CT
);
60 static int find_cache_aux (int, char *, char *, char *, int);
61 static int find_cache_aux2 (char *, char *, char *, int);
65 * Top level entry point to cache content
66 * from a group of messages
70 cache_all_messages (CT
*cts
)
74 for (ctp
= cts
; *ctp
; ctp
++) {
76 if (type_ok (ct
, 1)) {
83 (*ct
->c_ceclosefnx
) (ct
);
91 * Entry point to cache content from external sources.
98 char *file
, cachefile
[BUFSIZ
];
102 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
107 advise (NULL
, "unable to decode %s", ct
->c_file
);
111 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
112 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
114 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
117 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
119 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
124 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
128 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
131 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
134 if ((fp
= fopen (cachefile
, "w"))) {
137 FILE *gp
= ce
->ce_fp
;
139 fseek (gp
, 0L, SEEK_SET
);
141 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
143 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
147 admonish (ce
->ce_file
, "error reading");
151 admonish (cachefile
, "error writing");
157 content_error (cachefile
, ct
, "unable to fopen for writing");
162 fprintf (stderr
, "in place caching...\n");
165 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
166 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
172 find_cache (CT ct
, int policy
, int *writing
, char *id
,
173 char *buffer
, int buflen
)
182 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
183 policy
, writing
? "writing" : "reading", id
);
194 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
195 buffer
, buflen
) == OK
) {
196 if (access (buffer
, R_OK
) != NOTOK
) {
206 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
207 buffer
, buflen
) == OK
) {
208 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
218 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
219 buffer
, buflen
) == OK
) {
220 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
227 if (status
== OK
&& policy
== CACHE_ASK
) {
229 char *bp
, query
[BUFSIZ
];
234 pidcheck (pidwait (xpid
, NOTOK
));
238 /* Get buffer ready to go */
240 buflen
= sizeof(query
);
242 /* Now, construct query */
244 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
248 snprintf (bp
, buflen
, "Use cached copy");
254 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
261 snprintf (bp
, buflen
, " (size %lu octets)",
262 (unsigned long) st
.st_size
);
268 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
270 /* Now, check answer */
271 if (!getanswer (query
))
275 if (status
== OK
&& writing
) {
276 if (*writing
&& strchr(buffer
, '/'))
277 make_intermediates (buffer
);
287 find_cache_aux (int writing
, char *directory
, char *id
,
288 char *buffer
, int buflen
)
291 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
293 static int partno
, pid
;
294 static time_t clock
= 0;
299 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
301 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
302 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
310 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
314 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
337 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
338 (unsigned int) (clock
& 0xffffffff),
339 (unsigned int) (pid
& 0xffff),
340 (unsigned int) (partno
++ & 0xff));
343 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
345 make_intermediates (mapfile
);
346 mask
= umask (writing
== 2 ? 0077 : 0);
347 if (!(fp
= lkfopen (mapfile
, "a")) && errno
== ENOENT
) {
350 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
352 fp
= lkfopen (mapfile
, "a");
358 fprintf (fp
, "%s: %s\n", mapname
, id
);
359 lkfclose (fp
, mapfile
);
363 strncpy (buffer
, mapname
, buflen
);
365 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
367 fprintf (stderr
, "use %s\n", buffer
);
374 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
377 char buf
[BUFSIZ
], name
[NAMESZ
];
379 m_getfld_state_t gstate
= 0;
381 if (!(fp
= lkfopen (mapfile
, "r")))
387 int bufsz
= sizeof buf
;
389 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
392 strncpy (mapname
, name
, namelen
);
393 if (state
!= FLDPLUS
)
396 cp
= add (buf
, NULL
);
397 while (state
== FLDPLUS
) {
399 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
407 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
409 result
= strcmp (id
, dp
);
412 lkfclose (fp
, mapfile
);
424 m_getfld_state_destroy (&gstate
);
426 lkfclose (fp
, mapfile
);