]> diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[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, usemap;
274 char mapfile[BUFSIZ], mapname[BUFSIZ];
275 FILE *fp;
276 int failed_to_lock = 0;
277 static int partno, pid;
278 static time_t clock = 0;
279
280 usemap = 1;
281
282 if (debugsw)
283 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
284
285 snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
286 if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
287 goto done_map;
288
289 if (!writing) {
290 if (usemap)
291 return NOTOK;
292
293 use_raw:
294 snprintf (buffer, buflen, "%s/%s", directory, id);
295 return OK;
296 }
297
298 if (!usemap && access (mapfile, W_OK) == NOTOK)
299 goto use_raw;
300
301 if (clock != 0) {
302 time_t now;
303
304 time (&now);
305 if (now > clock)
306 clock = 0;
307 } else {
308 pid = getpid ();
309 }
310
311 if (clock == 0) {
312 time (&clock);
313 partno = 0;
314 } else {
315 if (partno > 0xff) {
316 clock++;
317 partno = 0;
318 }
319 }
320
321 snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
322 (unsigned int) (clock & 0xffffffff),
323 (unsigned int) (pid & 0xffff),
324 (unsigned int) (partno++ & 0xff));
325
326 if (debugsw)
327 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
328
329 make_intermediates (mapfile);
330 mask = umask (writing == 2 ? 0077 : 0);
331 if (!(fp = lkfopendata (mapfile, "a", &failed_to_lock)) && errno == ENOENT) {
332 int fd;
333
334 if ((fd = creat (mapfile, 0666)) != NOTOK) {
335 close (fd);
336 fp = lkfopendata (mapfile, "a", &failed_to_lock);
337 if (failed_to_lock) {
338 adios (mapfile, "failed to lock");
339 }
340 }
341 }
342 umask (mask);
343 if (!fp)
344 return NOTOK;
345 fprintf (fp, "%s: %s\n", mapname, id);
346 lkfclosedata (fp, mapfile);
347
348 done_map:
349 if (*mapname == '/')
350 strncpy (buffer, mapname, buflen);
351 else
352 snprintf (buffer, buflen, "%s/%s", directory, mapname);
353 if (debugsw)
354 fprintf (stderr, "use %s\n", buffer);
355
356 return OK;
357 }
358
359
360 static int
361 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
362 {
363 int state;
364 char buf[NMH_BUFSIZ], name[NAMESZ];
365 FILE *fp;
366 m_getfld_state_t gstate;
367 int failed_to_lock = 0;
368
369 if (!(fp = lkfopendata (mapfile, "r", &failed_to_lock)))
370 return NOTOK;
371
372 gstate = m_getfld_state_init(fp);
373 for (;;) {
374 int result;
375 char *cp, *dp;
376 int bufsz = sizeof buf;
377
378 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
379 case FLD:
380 case FLDPLUS:
381 strncpy (mapname, name, namelen);
382 if (state != FLDPLUS)
383 cp = buf;
384 else {
385 cp = mh_xstrdup(buf);
386 while (state == FLDPLUS) {
387 bufsz = sizeof buf;
388 state = m_getfld2(&gstate, name, buf, &bufsz);
389 cp = add (buf, cp);
390 }
391 }
392 dp = trimcpy (cp);
393 if (cp != buf)
394 free (cp);
395 if (debugsw)
396 fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
397 mapname);
398 result = strcmp (id, dp);
399 free (dp);
400 if (result == 0) {
401 lkfclosedata (fp, mapfile);
402 return OK;
403 }
404 continue;
405
406 case BODY:
407 case FILEEOF:
408 default:
409 break;
410 }
411 break;
412 }
413 m_getfld_state_destroy (&gstate);
414
415 lkfclosedata (fp, mapfile);
416 return NOTOK;
417 }