]>
diplodocus.org Git - nmh/blob - uip/mhcachesbr.c
3 * mhcachesbr.c -- routines to manipulate the MIME content cache
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
23 #include <h/mhcachesbr.h>
25 #ifdef TIME_WITH_SYS_TIME
26 # include <sys/time.h>
29 # ifdef TM_IN_SYS_TIME
30 # include <sys/time.h>
36 #ifdef HAVE_SYS_WAIT_H
37 # include <sys/wait.h>
44 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
47 int rcachesw
= CACHE_ASK
;
48 int wcachesw
= CACHE_ASK
;
51 * Location of public and private cache. These must
52 * be set before these routines are called.
58 /* mhparse.c (OR) mhbuildsbr.c */
62 int part_ok (CT
, int);
63 int type_ok (CT
, int);
64 int make_intermediates (char *);
65 void content_error (char *, CT
, char *, ...);
66 void flush_errors (void);
71 void cache_all_messages (CT
*);
72 int find_cache (CT
, int, int *, char *, char *, int);
77 static void cache_content (CT
);
78 static int find_cache_aux (int, char *, char *, char *, int);
79 static int find_cache_aux2 (char *, char *, char *, int);
83 * Top level entry point to cache content
84 * from a group of messages
88 cache_all_messages (CT
*cts
)
92 for (ctp
= cts
; *ctp
; ctp
++) {
94 if (type_ok (ct
, 1)) {
100 if (ct
->c_ceclosefnx
)
101 (*ct
->c_ceclosefnx
) (ct
);
109 * Entry point to cache content from external sources.
113 cache_content (CT ct
)
116 char *file
, cachefile
[BUFSIZ
];
117 CE ce
= ct
->c_cefile
;
120 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
125 advise (NULL
, "unable to decode %s", ct
->c_file
);
129 /* THIS NEEDS TO BE FIXED */
131 if (ct
->c_ceopenfnx
== openMail
) {
132 advise (NULL
, "a radish may no know Greek, but I do...");
137 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
138 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
140 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
143 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
145 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
150 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
154 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
157 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
160 if ((fp
= fopen (cachefile
, "w"))) {
163 FILE *gp
= ce
->ce_fp
;
165 fseek (gp
, 0L, SEEK_SET
);
167 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
169 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
173 admonish (ce
->ce_file
, "error reading");
177 admonish (cachefile
, "error writing");
183 content_error (cachefile
, ct
, "unable to fopen for writing");
188 fprintf (stderr
, "in place caching...\n");
191 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
192 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
198 find_cache (CT ct
, int policy
, int *writing
, char *id
,
199 char *buffer
, int buflen
)
208 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
209 policy
, writing
? "writing" : "reading", id
);
220 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
221 buffer
, buflen
) == OK
) {
222 if (access (buffer
, R_OK
) != NOTOK
) {
232 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
233 buffer
, buflen
) == OK
) {
234 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
244 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
245 buffer
, buflen
) == OK
) {
246 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
253 if (status
== OK
&& policy
== CACHE_ASK
) {
255 char *bp
, query
[BUFSIZ
];
260 pidcheck (pidwait (xpid
, NOTOK
));
264 /* Get buffer ready to go */
266 buflen
= sizeof(query
);
268 /* Now, construct query */
270 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
274 snprintf (bp
, buflen
, "Use cached copy");
280 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
287 snprintf (bp
, buflen
, " (size %lu octets)",
288 (unsigned long) st
.st_size
);
294 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
296 /* Now, check answer */
297 if (!getanswer (query
))
301 if (status
== OK
&& writing
) {
302 if (*writing
&& strchr(buffer
, '/'))
303 make_intermediates (buffer
);
313 find_cache_aux (int writing
, char *directory
, char *id
,
314 char *buffer
, int buflen
)
317 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
319 static int partno
, pid
;
320 static time_t clock
= 0;
323 usemap
= strchr (id
, '/') ? 1 : 0;
329 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
331 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
332 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
340 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
344 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
367 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
368 (unsigned int) (clock
& 0xffffffff),
369 (unsigned int) (pid
& 0xffff),
370 (unsigned int) (partno
++ & 0xff));
373 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
375 make_intermediates (mapfile
);
376 mask
= umask (writing
== 2 ? 0077 : 0);
377 if (!(fp
= lkfopen (mapfile
, "a")) && errno
== ENOENT
) {
380 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
382 fp
= lkfopen (mapfile
, "a");
388 fprintf (fp
, "%s: %s\n", mapname
, id
);
389 lkfclose (fp
, mapfile
);
393 strncpy (buffer
, mapname
, buflen
);
395 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
397 fprintf (stderr
, "use %s\n", buffer
);
404 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
407 char buf
[BUFSIZ
], name
[NAMESZ
];
410 if (!(fp
= lkfopen (mapfile
, "r")))
413 for (state
= FLD
;;) {
417 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
421 strncpy (mapname
, name
, namelen
);
422 if (state
!= FLDPLUS
)
425 cp
= add (buf
, NULL
);
426 while (state
== FLDPLUS
) {
427 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
435 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
437 result
= strcmp (id
, dp
);
440 lkfclose (fp
, mapfile
);
456 lkfclose (fp
, mapfile
);