]>
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>
20 #include <h/mhparse.h>
21 #include <h/mhcachesbr.h>
24 #ifdef TIME_WITH_SYS_TIME
25 # include <sys/time.h>
28 # ifdef TM_IN_SYS_TIME
29 # include <sys/time.h>
37 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
40 int rcachesw
= CACHE_ASK
;
41 int wcachesw
= CACHE_ASK
;
44 * Location of public and private cache. These must
45 * be set before these routines are called.
51 /* mhparse.c (OR) mhbuildsbr.c */
55 int part_ok (CT
, int);
56 int type_ok (CT
, int);
57 int make_intermediates (char *);
58 void content_error (char *, CT
, char *, ...);
59 void flush_errors (void);
64 void cache_all_messages (CT
*);
65 int find_cache (CT
, int, int *, char *, char *, int);
70 static void cache_content (CT
);
71 static int find_cache_aux (int, char *, char *, char *, int);
72 static int find_cache_aux2 (char *, char *, char *, int);
76 * Top level entry point to cache content
77 * from a group of messages
81 cache_all_messages (CT
*cts
)
85 for (ctp
= cts
; *ctp
; ctp
++) {
87 if (type_ok (ct
, 1)) {
94 (*ct
->c_ceclosefnx
) (ct
);
102 * Entry point to cache content from external sources.
106 cache_content (CT ct
)
109 char *file
, cachefile
[BUFSIZ
];
110 CE ce
= ct
->c_cefile
;
113 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
118 advise (NULL
, "unable to decode %s", ct
->c_file
);
122 /* THIS NEEDS TO BE FIXED */
124 if (ct
->c_ceopenfnx
== openMail
) {
125 advise (NULL
, "a radish may no know Greek, but I do...");
130 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
131 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
133 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
136 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
138 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
143 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
147 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
150 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
153 if ((fp
= fopen (cachefile
, "w"))) {
156 FILE *gp
= ce
->ce_fp
;
158 fseek (gp
, 0L, SEEK_SET
);
160 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
162 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
166 admonish (ce
->ce_file
, "error reading");
170 admonish (cachefile
, "error writing");
176 content_error (cachefile
, ct
, "unable to fopen for writing");
181 fprintf (stderr
, "in place caching...\n");
184 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
185 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
191 find_cache (CT ct
, int policy
, int *writing
, char *id
,
192 char *buffer
, int buflen
)
201 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
202 policy
, writing
? "writing" : "reading", id
);
213 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
214 buffer
, buflen
) == OK
) {
215 if (access (buffer
, R_OK
) != NOTOK
) {
225 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
226 buffer
, buflen
) == OK
) {
227 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
237 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
238 buffer
, buflen
) == OK
) {
239 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
246 if (status
== OK
&& policy
== CACHE_ASK
) {
248 char *bp
, query
[BUFSIZ
];
253 pidcheck (pidwait (xpid
, NOTOK
));
257 /* Get buffer ready to go */
259 buflen
= sizeof(query
);
261 /* Now, construct query */
263 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
267 snprintf (bp
, buflen
, "Use cached copy");
273 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
280 snprintf (bp
, buflen
, " (size %lu octets)",
281 (unsigned long) st
.st_size
);
287 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
289 /* Now, check answer */
290 if (!getanswer (query
))
294 if (status
== OK
&& writing
) {
295 if (*writing
&& strchr(buffer
, '/'))
296 make_intermediates (buffer
);
306 find_cache_aux (int writing
, char *directory
, char *id
,
307 char *buffer
, int buflen
)
310 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
312 static int partno
, pid
;
313 static time_t clock
= 0;
316 usemap
= strchr (id
, '/') ? 1 : 0;
322 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
324 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
325 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
333 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
337 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
360 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
361 (unsigned int) (clock
& 0xffffffff),
362 (unsigned int) (pid
& 0xffff),
363 (unsigned int) (partno
++ & 0xff));
366 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
368 make_intermediates (mapfile
);
369 mask
= umask (writing
== 2 ? 0077 : 0);
370 if (!(fp
= lkfopen (mapfile
, "a")) && errno
== ENOENT
) {
373 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
375 fp
= lkfopen (mapfile
, "a");
381 fprintf (fp
, "%s: %s\n", mapname
, id
);
382 lkfclose (fp
, mapfile
);
386 strncpy (buffer
, mapname
, buflen
);
388 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
390 fprintf (stderr
, "use %s\n", buffer
);
397 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
400 char buf
[BUFSIZ
], name
[NAMESZ
];
403 if (!(fp
= lkfopen (mapfile
, "r")))
406 for (state
= FLD
;;) {
410 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
414 strncpy (mapname
, name
, namelen
);
415 if (state
!= FLDPLUS
)
418 cp
= add (buf
, NULL
);
419 while (state
== FLDPLUS
) {
420 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
428 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
430 result
= strcmp (id
, dp
);
433 lkfclose (fp
, mapfile
);
449 lkfclose (fp
, mapfile
);