]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
3 * mhcachesbr.c -- routines to manipulate the MIME content cache
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.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
28 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
31 int rcachesw
= CACHE_ASK
;
32 int wcachesw
= CACHE_ASK
;
35 * Location of public and private cache. These must
36 * be set before these routines are called.
43 int part_ok (CT
, int);
44 int type_ok (CT
, int);
45 void content_error (char *, CT
, char *, ...);
46 void flush_errors (void);
51 void cache_all_messages (CT
*);
52 int find_cache (CT
, int, int *, char *, char *, int);
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);
63 * Top level entry point to cache content
64 * from a group of messages
68 cache_all_messages (CT
*cts
)
72 for (ctp
= cts
; *ctp
; ctp
++) {
74 if (type_ok (ct
, 1)) {
81 (*ct
->c_ceclosefnx
) (ct
);
89 * Entry point to cache content from external sources.
96 char *file
, cachefile
[BUFSIZ
];
97 CE ce
= &ct
->c_cefile
;
100 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
105 advise (NULL
, "unable to decode %s", ct
->c_file
);
109 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
110 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
112 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
115 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
117 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
122 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
126 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
129 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
132 if ((fp
= fopen (cachefile
, "w"))) {
135 FILE *gp
= ce
->ce_fp
;
137 fseek (gp
, 0L, SEEK_SET
);
139 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
141 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
145 admonish (ce
->ce_file
, "error reading");
149 admonish (cachefile
, "error writing");
155 content_error (cachefile
, ct
, "unable to fopen for writing");
160 fprintf (stderr
, "in place caching...\n");
163 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
164 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
170 find_cache (CT ct
, int policy
, int *writing
, char *id
,
171 char *buffer
, int buflen
)
180 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
181 policy
, writing
? "writing" : "reading", id
);
192 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
193 buffer
, buflen
) == OK
) {
194 if (access (buffer
, R_OK
) != NOTOK
) {
204 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
205 buffer
, buflen
) == OK
) {
206 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
216 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
217 buffer
, buflen
) == OK
) {
218 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
225 if (status
== OK
&& policy
== CACHE_ASK
) {
227 char *bp
, query
[BUFSIZ
];
232 pidcheck (pidwait (xpid
, NOTOK
));
236 /* Get buffer ready to go */
238 buflen
= sizeof(query
);
240 /* Now, construct query */
242 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
246 snprintf (bp
, buflen
, "Use cached copy");
252 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
259 snprintf (bp
, buflen
, " (size %lu octets)",
260 (unsigned long) st
.st_size
);
266 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
268 /* Now, check answer */
269 if (!getanswer (query
))
273 if (status
== OK
&& writing
) {
274 if (*writing
&& strchr(buffer
, '/'))
275 make_intermediates (buffer
);
285 find_cache_aux (int writing
, char *directory
, char *id
,
286 char *buffer
, int buflen
)
289 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
291 static int partno
, pid
;
292 static time_t clock
= 0;
297 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
299 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
300 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
308 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
312 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
335 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
336 (unsigned int) (clock
& 0xffffffff),
337 (unsigned int) (pid
& 0xffff),
338 (unsigned int) (partno
++ & 0xff));
341 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
343 make_intermediates (mapfile
);
344 mask
= umask (writing
== 2 ? 0077 : 0);
345 if (!(fp
= lkfopendata (mapfile
, "a")) && errno
== ENOENT
) {
348 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
350 fp
= lkfopendata (mapfile
, "a");
356 fprintf (fp
, "%s: %s\n", mapname
, id
);
357 lkfclosedata (fp
, mapfile
);
361 strncpy (buffer
, mapname
, buflen
);
363 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
365 fprintf (stderr
, "use %s\n", buffer
);
372 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
375 char buf
[BUFSIZ
], name
[NAMESZ
];
377 m_getfld_state_t gstate
= 0;
379 if (!(fp
= lkfopendata (mapfile
, "r")))
385 int bufsz
= sizeof buf
;
387 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
390 strncpy (mapname
, name
, namelen
);
391 if (state
!= FLDPLUS
)
394 cp
= add (buf
, NULL
);
395 while (state
== FLDPLUS
) {
397 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
405 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
407 result
= strcmp (id
, dp
);
410 lkfclosedata (fp
, mapfile
);
422 m_getfld_state_destroy (&gstate
);
424 lkfclosedata (fp
, mapfile
);