]> diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
Removed temporary probes added in commit
[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 extern pid_t xpid; /* mhshowsbr.c or mhbuildsbr.c */
29
30 /* cache policies */
31 int rcachesw = CACHE_ASK;
32 int wcachesw = CACHE_ASK;
33
34 /*
35 * Location of public and private cache. These must
36 * be set before these routines are called.
37 */
38 char *cache_public;
39 char *cache_private;
40
41
42 /* mhmisc.c */
43 int part_ok (CT, int);
44 int type_ok (CT, int);
45 void content_error (char *, CT, char *, ...);
46 void flush_errors (void);
47
48 /*
49 * prototypes
50 */
51 void cache_all_messages (CT *);
52 int find_cache (CT, int, int *, char *, char *, int);
53
54 /*
55 * static prototypes
56 */
57 static void cache_content (CT);
58 static int find_cache_aux (int, char *, char *, char *, int);
59 static int find_cache_aux2 (char *, char *, char *, int);
60
61
62 /*
63 * Top level entry point to cache content
64 * from a group of messages
65 */
66
67 void
68 cache_all_messages (CT *cts)
69 {
70 CT ct, *ctp;
71
72 for (ctp = cts; *ctp; ctp++) {
73 ct = *ctp;
74 if (type_ok (ct, 1)) {
75 cache_content (ct);
76 if (ct->c_fp) {
77 fclose (ct->c_fp);
78 ct->c_fp = NULL;
79 }
80 if (ct->c_ceclosefnx)
81 (*ct->c_ceclosefnx) (ct);
82 }
83 }
84 flush_errors ();
85 }
86
87
88 /*
89 * Entry point to cache content from external sources.
90 */
91
92 static void
93 cache_content (CT ct)
94 {
95 int cachetype;
96 char *file, cachefile[BUFSIZ];
97 CE ce = &ct->c_cefile;
98
99 if (!ct->c_id) {
100 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
101 return;
102 }
103
104 if (!ce) {
105 advise (NULL, "unable to decode %s", ct->c_file);
106 return;
107 }
108
109 if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
110 &cachetype, ct->c_id, cachefile, sizeof(cachefile))
111 == NOTOK) {
112 advise (NULL, "unable to cache %s's contents", ct->c_file);
113 return;
114 }
115 if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
116 fflush (stdout);
117 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
118 cachefile);
119 }
120
121 if (ce->ce_file) {
122 int mask = umask (cachetype ? ~m_gmprot () : 0222);
123 FILE *fp;
124
125 if (debugsw)
126 fprintf (stderr, "caching by copying %s...\n", ce->ce_file);
127
128 file = NULL;
129 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
130 goto reset_umask;
131
132 if ((fp = fopen (cachefile, "w"))) {
133 int cc;
134 char buffer[BUFSIZ];
135 FILE *gp = ce->ce_fp;
136
137 fseek (gp, 0L, SEEK_SET);
138
139 while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp))
140 > 0)
141 fwrite (buffer, sizeof(*buffer), cc, fp);
142 fflush (fp);
143
144 if (ferror (gp)) {
145 admonish (ce->ce_file, "error reading");
146 unlink (cachefile);
147 } else {
148 if (ferror (fp)) {
149 admonish (cachefile, "error writing");
150 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 if (xpid) {
230 if (xpid < 0)
231 xpid = -xpid;
232 pidcheck (pidwait (xpid, NOTOK));
233 xpid = 0;
234 }
235
236 /* Get buffer ready to go */
237 bp = query;
238 buflen = sizeof(query);
239
240 /* Now, construct query */
241 if (writing) {
242 snprintf (bp, buflen, "Make cached, publically-accessible copy");
243 } else {
244 struct stat st;
245
246 snprintf (bp, buflen, "Use cached copy");
247 len = strlen (bp);
248 bp += len;
249 buflen -= len;
250
251 if (ct->c_partno) {
252 snprintf (bp, buflen, " of content %s", ct->c_partno);
253 len = strlen (bp);
254 bp += len;
255 buflen -= len;
256 }
257
258 stat (buffer, &st);
259 snprintf (bp, buflen, " (size %lu octets)",
260 (unsigned long) st.st_size);
261 }
262 len = strlen (bp);
263 bp += len;
264 buflen -= len;
265
266 snprintf (bp, buflen, "\n in file %s? ", buffer);
267
268 /* Now, check answer */
269 if (!getanswer (query))
270 status = NOTOK;
271 }
272
273 if (status == OK && writing) {
274 if (*writing && strchr(buffer, '/'))
275 make_intermediates (buffer);
276 unlink (buffer);
277 }
278
279 free (id);
280 return status;
281 }
282
283
284 static int
285 find_cache_aux (int writing, char *directory, char *id,
286 char *buffer, int buflen)
287 {
288 int mask, usemap;
289 char mapfile[BUFSIZ], mapname[BUFSIZ];
290 FILE *fp;
291 static int partno, pid;
292 static time_t clock = 0;
293
294 usemap = 1;
295
296 if (debugsw)
297 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
298
299 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
300 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
301 goto done_map;
302
303 if (!writing) {
304 if (usemap)
305 return NOTOK;
306
307 use_raw:
308 snprintf (buffer, buflen, "%s/%s", directory, id);
309 return OK;
310 }
311
312 if (!usemap && access (mapfile, W_OK) == NOTOK)
313 goto use_raw;
314
315 if (clock != 0) {
316 time_t now;
317
318 time (&now);
319 if (now > clock)
320 clock = 0;
321 } else {
322 pid = getpid ();
323 }
324
325 if (clock == 0) {
326 time (&clock);
327 partno = 0;
328 } else {
329 if (partno > 0xff) {
330 clock++;
331 partno = 0;
332 }
333 }
334
335 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
336 (unsigned int) (clock & 0xffffffff),
337 (unsigned int) (pid & 0xffff),
338 (unsigned int) (partno++ & 0xff));
339
340 if (debugsw)
341 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
342
343 make_intermediates (mapfile);
344 mask = umask (writing == 2 ? 0077 : 0);
345 if (!(fp = lkfopendata (mapfile, "a")) && errno == ENOENT) {
346 int fd;
347
348 if ((fd = creat (mapfile, 0666)) != NOTOK) {
349 close (fd);
350 fp = lkfopendata (mapfile, "a");
351 }
352 }
353 umask (mask);
354 if (!fp)
355 return NOTOK;
356 fprintf (fp, "%s: %s\n", mapname, id);
357 lkfclosedata (fp, mapfile);
358
359 done_map:
360 if (*mapname == '/')
361 strncpy (buffer, mapname, buflen);
362 else
363 snprintf (buffer, buflen, "%s/%s", directory, mapname);
364 if (debugsw)
365 fprintf (stderr, "use %s\n", buffer);
366
367 return OK;
368 }
369
370
371 static int
372 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
373 {
374 int state;
375 char buf[BUFSIZ], name[NAMESZ];
376 FILE *fp;
377 m_getfld_state_t gstate = 0;
378
379 if (!(fp = lkfopendata (mapfile, "r")))
380 return NOTOK;
381
382 for (;;) {
383 int result;
384 char *cp, *dp;
385 int bufsz = sizeof buf;
386
387 switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
388 case FLD:
389 case FLDPLUS:
390 strncpy (mapname, name, namelen);
391 if (state != FLDPLUS)
392 cp = buf;
393 else {
394 cp = add (buf, NULL);
395 while (state == FLDPLUS) {
396 bufsz = sizeof buf;
397 state = m_getfld (&gstate, name, buf, &bufsz, fp);
398 cp = add (buf, cp);
399 }
400 }
401 dp = trimcpy (cp);
402 if (cp != buf)
403 free (cp);
404 if (debugsw)
405 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
406 mapname);
407 result = strcmp (id, dp);
408 free (dp);
409 if (result == 0) {
410 lkfclosedata (fp, mapfile);
411 return OK;
412 }
413 continue;
414
415 case BODY:
416 case FILEEOF:
417 default:
418 break;
419 }
420 break;
421 }
422 m_getfld_state_destroy (&gstate);
423
424 lkfclosedata (fp, mapfile);
425 return NOTOK;
426 }