]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
30 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
33 int rcachesw
= CACHE_ASK
;
34 int wcachesw
= CACHE_ASK
;
37 * Location of public and private cache. These must
38 * be set before these routines are called.
45 int part_ok (CT
, int);
46 int type_ok (CT
, int);
47 void content_error (char *, CT
, char *, ...);
48 void flush_errors (void);
53 void cache_all_messages (CT
*);
54 int find_cache (CT
, int, int *, char *, char *, int);
59 static void cache_content (CT
);
60 static int find_cache_aux (int, char *, char *, char *, int);
61 static int find_cache_aux2 (char *, char *, char *, int);
65 * Top level entry point to cache content
66 * from a group of messages
70 cache_all_messages (CT
*cts
)
74 for (ctp
= cts
; *ctp
; ctp
++) {
76 if (type_ok (ct
, 1)) {
83 (*ct
->c_ceclosefnx
) (ct
);
91 * Entry point to cache content from external sources.
98 char *file
, cachefile
[BUFSIZ
];
102 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
107 advise (NULL
, "unable to decode %s", ct
->c_file
);
111 /* THIS NEEDS TO BE FIXED */
113 if (ct
->c_ceopenfnx
== openMail
) {
114 advise (NULL
, "a radish may no know Greek, but I do...");
119 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
120 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
122 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
125 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
127 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
132 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
136 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
139 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
142 if ((fp
= fopen (cachefile
, "w"))) {
145 FILE *gp
= ce
->ce_fp
;
147 fseek (gp
, 0L, SEEK_SET
);
149 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
151 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
155 admonish (ce
->ce_file
, "error reading");
159 admonish (cachefile
, "error writing");
165 content_error (cachefile
, ct
, "unable to fopen for writing");
170 fprintf (stderr
, "in place caching...\n");
173 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
174 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
180 find_cache (CT ct
, int policy
, int *writing
, char *id
,
181 char *buffer
, int buflen
)
190 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
191 policy
, writing
? "writing" : "reading", id
);
202 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
203 buffer
, buflen
) == OK
) {
204 if (access (buffer
, R_OK
) != NOTOK
) {
214 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
215 buffer
, buflen
) == OK
) {
216 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
226 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
227 buffer
, buflen
) == OK
) {
228 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
235 if (status
== OK
&& policy
== CACHE_ASK
) {
237 char *bp
, query
[BUFSIZ
];
242 pidcheck (pidwait (xpid
, NOTOK
));
246 /* Get buffer ready to go */
248 buflen
= sizeof(query
);
250 /* Now, construct query */
252 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
256 snprintf (bp
, buflen
, "Use cached copy");
262 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
269 snprintf (bp
, buflen
, " (size %lu octets)",
270 (unsigned long) st
.st_size
);
276 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
278 /* Now, check answer */
279 if (!getanswer (query
))
283 if (status
== OK
&& writing
) {
284 if (*writing
&& strchr(buffer
, '/'))
285 make_intermediates (buffer
);
295 find_cache_aux (int writing
, char *directory
, char *id
,
296 char *buffer
, int buflen
)
299 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
301 static int partno
, pid
;
302 static time_t clock
= 0;
307 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
309 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
310 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
318 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
322 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
345 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
346 (unsigned int) (clock
& 0xffffffff),
347 (unsigned int) (pid
& 0xffff),
348 (unsigned int) (partno
++ & 0xff));
351 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
353 make_intermediates (mapfile
);
354 mask
= umask (writing
== 2 ? 0077 : 0);
355 if (!(fp
= lkfopen (mapfile
, "a")) && errno
== ENOENT
) {
358 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
360 fp
= lkfopen (mapfile
, "a");
366 fprintf (fp
, "%s: %s\n", mapname
, id
);
367 lkfclose (fp
, mapfile
);
371 strncpy (buffer
, mapname
, buflen
);
373 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
375 fprintf (stderr
, "use %s\n", buffer
);
382 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
385 char buf
[BUFSIZ
], name
[NAMESZ
];
388 if (!(fp
= lkfopen (mapfile
, "r")))
391 for (state
= FLD
;;) {
395 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
399 strncpy (mapname
, name
, namelen
);
400 if (state
!= FLDPLUS
)
403 cp
= add (buf
, NULL
);
404 while (state
== FLDPLUS
) {
405 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
413 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
415 result
= strcmp (id
, dp
);
418 lkfclose (fp
, mapfile
);
434 lkfclose (fp
, mapfile
);