]>
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>
26 #ifdef TIME_WITH_SYS_TIME
27 # include <sys/time.h>
30 # ifdef TM_IN_SYS_TIME
31 # include <sys/time.h>
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
43 extern pid_t xpid
; /* mhshowsbr.c or mhbuildsbr.c */
46 int rcachesw
= CACHE_ASK
;
47 int wcachesw
= CACHE_ASK
;
50 * Location of public and private cache. These must
51 * be set before these routines are called.
57 /* mhparse.c (OR) mhbuildsbr.c */
61 int part_ok (CT
, int);
62 int type_ok (CT
, int);
63 int make_intermediates (char *);
64 void content_error (char *, CT
, char *, ...);
65 void flush_errors (void);
70 void cache_all_messages (CT
*);
71 int find_cache (CT
, int, int *, char *, char *, int);
76 static void cache_content (CT
);
77 static int find_cache_aux (int, char *, char *, char *, int);
78 static int find_cache_aux2 (char *, char *, char *, int);
82 * Top level entry point to cache content
83 * from a group of messages
87 cache_all_messages (CT
*cts
)
91 for (ctp
= cts
; *ctp
; ctp
++) {
93 if (type_ok (ct
, 1)) {
100 (*ct
->c_ceclosefnx
) (ct
);
108 * Entry point to cache content from external sources.
112 cache_content (CT ct
)
115 char *file
, cachefile
[BUFSIZ
];
116 CE ce
= ct
->c_cefile
;
119 advise (NULL
, "no %s: field in %s", ID_FIELD
, ct
->c_file
);
124 advise (NULL
, "unable to decode %s", ct
->c_file
);
128 /* THIS NEEDS TO BE FIXED */
130 if (ct
->c_ceopenfnx
== openMail
) {
131 advise (NULL
, "a radish may no know Greek, but I do...");
136 if (find_cache (NULL
, wcachesw
!= CACHE_NEVER
? wcachesw
: CACHE_ASK
,
137 &cachetype
, ct
->c_id
, cachefile
, sizeof(cachefile
))
139 advise (NULL
, "unable to cache %s's contents", ct
->c_file
);
142 if (wcachesw
!= CACHE_NEVER
&& wcachesw
!= CACHE_ASK
) {
144 fprintf (stderr
, "caching message %s as file %s\n", ct
->c_file
,
149 int mask
= umask (cachetype
? ~m_gmprot () : 0222);
153 fprintf (stderr
, "caching by copying %s...\n", ce
->ce_file
);
156 if ((*ct
->c_ceopenfnx
) (ct
, &file
) == NOTOK
)
159 if ((fp
= fopen (cachefile
, "w"))) {
162 FILE *gp
= ce
->ce_fp
;
164 fseek (gp
, 0L, SEEK_SET
);
166 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), gp
))
168 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
172 admonish (ce
->ce_file
, "error reading");
176 admonish (cachefile
, "error writing");
182 content_error (cachefile
, ct
, "unable to fopen for writing");
187 fprintf (stderr
, "in place caching...\n");
190 if ((*ct
->c_ceopenfnx
) (ct
, &file
) != NOTOK
)
191 chmod (cachefile
, cachetype
? m_gmprot () : 0444);
197 find_cache (CT ct
, int policy
, int *writing
, char *id
,
198 char *buffer
, int buflen
)
207 fprintf (stderr
, "find_cache %s(%d) %s %s\n", caches
[policy
].sw
,
208 policy
, writing
? "writing" : "reading", id
);
219 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
220 buffer
, buflen
) == OK
) {
221 if (access (buffer
, R_OK
) != NOTOK
) {
231 && find_cache_aux (writing
? 1 : 0, cache_public
, id
,
232 buffer
, buflen
) == OK
) {
233 if (writing
|| access (buffer
, R_OK
) != NOTOK
) {
243 && find_cache_aux (writing
? 2 : 0, cache_private
, id
,
244 buffer
, buflen
) == OK
) {
245 if (writing
|| access (buffer
, R_OK
) != NOTOK
)
252 if (status
== OK
&& policy
== CACHE_ASK
) {
254 char *bp
, query
[BUFSIZ
];
259 pidcheck (pidwait (xpid
, NOTOK
));
263 /* Get buffer ready to go */
265 buflen
= sizeof(query
);
267 /* Now, construct query */
269 snprintf (bp
, buflen
, "Make cached, publically-accessible copy");
273 snprintf (bp
, buflen
, "Use cached copy");
279 snprintf (bp
, buflen
, " of content %s", ct
->c_partno
);
286 snprintf (bp
, buflen
, " (size %lu octets)",
287 (unsigned long) st
.st_size
);
293 snprintf (bp
, buflen
, "\n in file %s? ", buffer
);
295 /* Now, check answer */
296 if (!getanswer (query
))
300 if (status
== OK
&& writing
) {
301 if (*writing
&& strchr(buffer
, '/'))
302 make_intermediates (buffer
);
312 find_cache_aux (int writing
, char *directory
, char *id
,
313 char *buffer
, int buflen
)
316 char mapfile
[BUFSIZ
], mapname
[BUFSIZ
];
318 static int partno
, pid
;
319 static time_t clock
= 0;
322 usemap
= strchr (id
, '/') ? 1 : 0;
328 fprintf (stderr
, "find_cache_aux %s usemap=%d\n", directory
, usemap
);
330 snprintf (mapfile
, sizeof(mapfile
), "%s/cache.map", directory
);
331 if (find_cache_aux2 (mapfile
, id
, mapname
, sizeof(mapname
)) == OK
)
339 snprintf (buffer
, buflen
, "%s/%s", directory
, id
);
343 if (!usemap
&& access (mapfile
, W_OK
) == NOTOK
)
366 snprintf (mapname
, sizeof(mapname
), "%08x%04x%02x",
367 (unsigned int) (clock
& 0xffffffff),
368 (unsigned int) (pid
& 0xffff),
369 (unsigned int) (partno
++ & 0xff));
372 fprintf (stderr
, "creating mapping %s->%s\n", mapname
, id
);
374 make_intermediates (mapfile
);
375 mask
= umask (writing
== 2 ? 0077 : 0);
376 if (!(fp
= lkfopen (mapfile
, "a")) && errno
== ENOENT
) {
379 if ((fd
= creat (mapfile
, 0666)) != NOTOK
) {
381 fp
= lkfopen (mapfile
, "a");
387 fprintf (fp
, "%s: %s\n", mapname
, id
);
388 lkfclose (fp
, mapfile
);
392 strncpy (buffer
, mapname
, buflen
);
394 snprintf (buffer
, buflen
, "%s/%s", directory
, mapname
);
396 fprintf (stderr
, "use %s\n", buffer
);
403 find_cache_aux2 (char *mapfile
, char *id
, char *mapname
, int namelen
)
406 char buf
[BUFSIZ
], name
[NAMESZ
];
409 if (!(fp
= lkfopen (mapfile
, "r")))
412 for (state
= FLD
;;) {
416 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
420 strncpy (mapname
, name
, namelen
);
421 if (state
!= FLDPLUS
)
424 cp
= add (buf
, NULL
);
425 while (state
== FLDPLUS
) {
426 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
434 fprintf (stderr
, "compare %s to %s <- %s\n", id
, dp
,
436 result
= strcmp (id
, dp
);
439 lkfclose (fp
, mapfile
);
455 lkfclose (fp
, mapfile
);