]> diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / mhcachesbr.c
1 /* mhcachesbr.c -- routines to manipulate the MIME content cache
2 *
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.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/md5.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
16 #include <h/utils.h>
17 #include "mhmisc.h"
18 #include "sbr/lock_file.h"
19 #include "sbr/m_mktemp.h"
20
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
23 #endif
24 #include <time.h>
25
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(CACHE, caches);
28 #undef X
29 struct swit *cache_policy = caches;
30
31 extern int debugsw;
32
33 /* cache policies */
34 int rcachesw = CACHE_ASK;
35 int wcachesw = CACHE_ASK;
36
37 /*
38 * Location of public and private cache. These must
39 * be set before these routines are called.
40 */
41 char *cache_public;
42 char *cache_private;
43
44 /*
45 * static prototypes
46 */
47 static void cache_content (CT);
48 static int find_cache_aux (int, char *, char *, char *, int);
49 static int find_cache_aux2 (char *, char *, char *, int);
50
51
52 /*
53 * Top level entry point to cache content
54 * from a group of messages
55 */
56
57 void
58 cache_all_messages (CT *cts)
59 {
60 CT ct, *ctp;
61
62 for (ctp = cts; *ctp; ctp++) {
63 ct = *ctp;
64 if (type_ok (ct, 1)) {
65 cache_content (ct);
66 if (ct->c_fp) {
67 fclose (ct->c_fp);
68 ct->c_fp = NULL;
69 }
70 if (ct->c_ceclosefnx)
71 (*ct->c_ceclosefnx) (ct);
72 }
73 }
74 flush_errors ();
75 }
76
77
78 /*
79 * Entry point to cache content from external sources.
80 */
81
82 static void
83 cache_content (CT ct)
84 {
85 int cachetype;
86 char *file, cachefile[BUFSIZ];
87 CE ce = &ct->c_cefile;
88
89 if (!ct->c_id) {
90 inform("no %s: field in %s", ID_FIELD, ct->c_file);
91 return;
92 }
93
94 if (!ce) {
95 inform("unable to decode %s", ct->c_file);
96 return;
97 }
98
99 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
100 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
101 == NOTOK) {
102 inform("unable to cache %s's contents", ct->c_file);
103 return;
104 }
105 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
106 fflush (stdout);
107 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
108 cachefile);
109 }
110
111 if (ce->ce_file) {
112 int mask = umask (cachetype ? ~m_gmprot () : 0222);
113 FILE *fp;
114
115 if (debugsw)
116 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
117
118 file = NULL;
119 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
120 goto reset_umask;
121
122 if ((fp = fopen (cachefile, "w"))) {
123 int cc;
124 char buffer[BUFSIZ];
125 FILE *gp = ce->ce_fp;
126
127 fseek (gp, 0L, SEEK_SET);
128
129 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
130 > 0)
131 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
132 advise ("cache_content", "fwrite");
133 }
134 fflush (fp);
135
136 if (ferror (gp)) {
137 admonish (ce->ce_file, "error reading");
138 (void) m_unlink (cachefile);
139 } else {
140 if (ferror (fp)) {
141 admonish (cachefile, "error writing");
142 (void) m_unlink (cachefile);
143 }
144 }
145 fclose (fp);
146 } else
147 content_error (cachefile, ct, "unable to fopen for writing");
148 reset_umask:
149 umask (mask);
150 } else {
151 if (debugsw)
152 fprintf (stderr, "in place caching...\n");
153
154 file = cachefile;
155 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
156 chmod (cachefile, cachetype ? m_gmprot () : 0444);
157 }
158 }
159
160
161 int
162 find_cache (CT ct, int policy, int *writing, char *id,
163 char *buffer, int buflen)
164 {
165 int status = NOTOK;
166
167 if (id == NULL)
168 return NOTOK;
169 id = trimcpy (id);
170
171 if (debugsw)
172 fprintf (stderr, "find_cache %s(%d) %s %s\n", caches[policy].sw,
173 policy, writing ? "writing" : "reading", id);
174
175 switch (policy) {
176 case CACHE_NEVER:
177 default:
178 break;
179
180 case CACHE_ASK:
181 case CACHE_PUBLIC:
182 if (cache_private
183 && !writing
184 && find_cache_aux (0, cache_private, id,
185 buffer, buflen) == OK) {
186 if (access (buffer, R_OK) != NOTOK) {
187 got_private:
188 if (writing)
189 *writing = 1;
190 got_it:
191 status = OK;
192 break;
193 }
194 }
195 if (cache_public
196 && find_cache_aux (writing ? 1 : 0, cache_public, id,
197 buffer, buflen) == OK) {
198 if (writing || access (buffer, R_OK) != NOTOK) {
199 if (writing)
200 *writing = 0;
201 goto got_it;
202 }
203 }
204 break;
205
206 case CACHE_PRIVATE:
207 if (cache_private
208 && find_cache_aux (writing ? 2 : 0, cache_private, id,
209 buffer, buflen) == OK) {
210 if (writing || access (buffer, R_OK) != NOTOK)
211 goto got_private;
212 }
213 break;
214
215 }
216
217 if (status == OK && policy == CACHE_ASK) {
218 int len, buflen;
219 char *bp, query[BUFSIZ];
220
221 /* Get buffer ready to go */
222 bp = query;
223 buflen = sizeof(query);
224
225 /* Now, construct query */
226 if (writing) {
227 snprintf (bp, buflen, "Make cached, publicly-accessible copy");
228 } else {
229 struct stat st;
230
231 snprintf (bp, buflen, "Use cached copy");
232 len = strlen (bp);
233 bp += len;
234 buflen -= len;
235
236 if (ct->c_partno) {
237 snprintf (bp, buflen, " of content %s", ct->c_partno);
238 len = strlen (bp);
239 bp += len;
240 buflen -= len;
241 }
242
243 stat (buffer, &st);
244 snprintf (bp, buflen, " (size %lu octets)",
245 (unsigned long) st.st_size);
246 }
247 len = strlen (bp);
248 bp += len;
249 buflen -= len;
250
251 snprintf (bp, buflen, "\n in file %s? ", buffer);
252
253 /* Now, check answer */
254 if (!read_yes_or_no_if_tty (query))
255 status = NOTOK;
256 }
257
258 if (status == OK && writing) {
259 if (*writing && strchr(buffer, '/'))
260 make_intermediates (buffer);
261 (void) m_unlink (buffer);
262 }
263
264 free (id);
265 return status;
266 }
267
268
269 static int
270 find_cache_aux (int writing, char *directory, char *id,
271 char *buffer, int buflen)
272 {
273 int mask;
274 bool usemap;
275 char mapfile[BUFSIZ], mapname[BUFSIZ];
276 FILE *fp;
277 int failed_to_lock = 0;
278 static int partno, pid;
279 static time_t clock = 0;
280
281 usemap = true;
282
283 if (debugsw)
284 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
285
286 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
287 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
288 goto done_map;
289
290 if (!writing) {
291 if (usemap)
292 return NOTOK;
293
294 use_raw:
295 snprintf (buffer, buflen, "%s/%s", directory, id);
296 return OK;
297 }
298
299 if (!usemap && access (mapfile, W_OK) == NOTOK)
300 goto use_raw;
301
302 if (clock != 0) {
303 time_t now;
304
305 time (&now);
306 if (now > clock)
307 clock = 0;
308 } else {
309 pid = getpid ();
310 }
311
312 if (clock == 0) {
313 time (&clock);
314 partno = 0;
315 } else {
316 if (partno > 0xff) {
317 clock++;
318 partno = 0;
319 }
320 }
321
322 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
323 (unsigned int) (clock & 0xffffffff),
324 (unsigned int) (pid & 0xffff),
325 (unsigned int) (partno++ & 0xff));
326
327 if (debugsw)
328 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
329
330 make_intermediates (mapfile);
331 mask = umask (writing == 2 ? 0077 : 0);
332 if (!(fp = lkfopendata (mapfile, "a", &failed_to_lock)) && errno == ENOENT) {
333 int fd;
334
335 if ((fd = creat (mapfile, 0666)) != NOTOK) {
336 close (fd);
337 fp = lkfopendata (mapfile, "a", &failed_to_lock);
338 if (failed_to_lock) {
339 adios (mapfile, "failed to lock");
340 }
341 }
342 }
343 umask (mask);
344 if (!fp)
345 return NOTOK;
346 fprintf (fp, "%s: %s\n", mapname, id);
347 lkfclosedata (fp, mapfile);
348
349 done_map:
350 if (*mapname == '/')
351 strncpy (buffer, mapname, buflen);
352 else
353 snprintf (buffer, buflen, "%s/%s", directory, mapname);
354 if (debugsw)
355 fprintf (stderr, "use %s\n", buffer);
356
357 return OK;
358 }
359
360
361 static int
362 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
363 {
364 int state;
365 char buf[NMH_BUFSIZ], name[NAMESZ];
366 FILE *fp;
367 m_getfld_state_t gstate;
368 int failed_to_lock = 0;
369
370 if (!(fp = lkfopendata (mapfile, "r", &failed_to_lock)))
371 return NOTOK;
372
373 gstate = m_getfld_state_init(fp);
374 for (;;) {
375 int result;
376 char *cp, *dp;
377 int bufsz = sizeof buf;
378
379 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
380 case FLD:
381 case FLDPLUS:
382 strncpy (mapname, name, namelen);
383 if (state != FLDPLUS)
384 cp = buf;
385 else {
386 cp = mh_xstrdup(buf);
387 while (state == FLDPLUS) {
388 bufsz = sizeof buf;
389 state = m_getfld2(&gstate, name, buf, &bufsz);
390 cp = add (buf, cp);
391 }
392 }
393 dp = trimcpy (cp);
394 if (cp != buf)
395 free (cp);
396 if (debugsw)
397 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
398 mapname);
399 result = strcmp (id, dp);
400 free (dp);
401 if (result == 0) {
402 lkfclosedata (fp, mapfile);
403 return OK;
404 }
405 continue;
406
407 case BODY:
408 case FILEEOF:
409 default:
410 break;
411 }
412 break;
413 }
414 m_getfld_state_destroy (&gstate);
415
416 lkfclosedata (fp, mapfile);
417 return NOTOK;
418 }