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