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