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