]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
1 /* mhcachesbr.c -- routines to manipulate the MIME content cache
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.
14 #include <h/mhparse.h>
15 #include <h/mhcachesbr.h>
18 #include "sbr/lock_file.h"
19 #include "sbr/m_mktemp.h"
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(CACHE
, caches
);
29 struct swit
*cache_policy
= caches
;
34 int rcachesw
= CACHE_ASK
;
35 int wcachesw
= CACHE_ASK
;
38 * Location of public and private cache. These must
39 * be set before these routines are called.
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);
53 * Top level entry point to cache content
54 * from a group of messages
58 cache_all_messages (CT
*cts
)
62 for (ctp
= cts
; *ctp
; ctp
++) {
64 if (type_ok (ct
, 1)) {
71 (*ct
->c_ceclosefnx
) (ct
);
79 * Entry point to cache content from external sources.
86 char *file
, cachefile
[BUFSIZ
];
87 CE ce
= &ct
->c_cefile
;
90 inform("no %s: field in %s", ID_FIELD
, ct
->c_file
);
95 inform("unable to decode %s", ct
->c_file
);
99 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
100 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
102 inform("unable to cache %s's contents", ct
->c_file
);
105 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
107 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
112 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
116 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
119 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
122 if ((fp
= fopen (cachefile
, "w"))) {
125 FILE *gp
= ce
->ce_fp
;
127 fseek (gp
, 0L, SEEK_SET
);
129 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
131 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
132 advise ("cache_content", "fwrite");
137 admonish (ce
->ce_file
, "error reading");
138 (void) m_unlink (cachefile
);
141 admonish (cachefile
, "error writing");
142 (void) m_unlink (cachefile
);
147 content_error (cachefile
, ct
, "unable to fopen for writing");
152 fprintf (stderr
, "in place caching...\n");
155 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
156 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
162 find_cache (CT ct
, int policy
, int *writing
, char *id
,
163 char *buffer
, int buflen
)
172 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
173 policy
, writing
? "writing" : "reading", id
);
184 && find_cache_aux (0, cache_private
, id
,
185 buffer
, buflen
) == OK
) {
186 if (access (buffer
, R_OK
) != NOTOK
) {
196 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
197 buffer
, buflen
) == OK
) {
198 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
208 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
209 buffer
, buflen
) == OK
) {
210 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
217 if (status
== OK
&& policy
== CACHE_ASK
) {
219 char *bp
, query
[BUFSIZ
];
221 /* Get buffer ready to go */
223 buflen
= sizeof(query
);
225 /* Now, construct query */
227 snprintf (bp
, buflen
, "Make cached, publicly-accessible copy");
231 snprintf (bp
, buflen
, "Use cached copy");
237 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
244 snprintf (bp
, buflen
, " (size %lu octets)",
245 (unsigned long) st
.st_size
);
251 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
253 /* Now, check answer */
254 if (!read_yes_or_no_if_tty (query
))
258 if (status
== OK
&& writing
) {
259 if (*writing
&& strchr(buffer
, '/'))
260 make_intermediates (buffer
);
261 (void) m_unlink (buffer
);
270 find_cache_aux (int writing
, char *directory
, char *id
,
271 char *buffer
, int buflen
)
274 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
276 int failed_to_lock
= 0;
277 static int partno
, pid
;
278 static time_t clock
= 0;
283 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
285 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
286 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
294 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
298 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
321 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
322 (unsigned int) (clock
& 0xffffffff),
323 (unsigned int) (pid
& 0xffff),
324 (unsigned int) (partno
++ & 0xff));
327 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
329 make_intermediates (mapfile
);
330 mask
= umask (writing
== 2 ? 0077 : 0);
331 if (!(fp
= lkfopendata (mapfile
, "a", &failed_to_lock
)) && errno
== ENOENT
) {
334 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
336 fp
= lkfopendata (mapfile
, "a", &failed_to_lock
);
337 if (failed_to_lock
) {
338 adios (mapfile
, "failed to lock");
345 fprintf (fp
, "%s: %s\n", mapname
, id
);
346 lkfclosedata (fp
, mapfile
);
350 strncpy (buffer
, mapname
, buflen
);
352 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
354 fprintf (stderr
, "use %s\n", buffer
);
361 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
364 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
366 m_getfld_state_t gstate
;
367 int failed_to_lock
= 0;
369 if (!(fp
= lkfopendata (mapfile
, "r", &failed_to_lock
)))
372 gstate
= m_getfld_state_init(fp
);
376 int bufsz
= sizeof buf
;
378 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
381 strncpy (mapname
, name
, namelen
);
382 if (state
!= FLDPLUS
)
385 cp
= mh_xstrdup(buf
);
386 while (state
== FLDPLUS
) {
388 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
396 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
398 result
= strcmp (id
, dp
);
401 lkfclosedata (fp
, mapfile
);
413 m_getfld_state_destroy (&gstate
);
415 lkfclosedata (fp
, mapfile
);