Rocco Rutte:
[apps/madmutt.git] / hcache.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
4  * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
5  * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
6  *
7  * This file is part of mutt-ng, see http://www.muttng.org/.
8  * It's licensed under the GNU General Public License,
9  * please see the file GPL in the top level source directory.
10  */
11
12 # if HAVE_INTTYPES_H
13 #  include <inttypes.h>
14 # else
15 #  if HAVE_STDINT_H
16 #   include <stdint.h>
17 #  endif
18 # endif
19
20 #if HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24 #ifdef USE_HCACHE
25
26 #if HAVE_QDBM
27 #include <depot.h>
28 #include <cabin.h>
29 #include <villa.h>
30 #elif HAVE_GDBM
31 #include <gdbm.h>
32 #elif HAVE_DB4
33 #include <db.h>
34 #endif
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #if HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #include "mutt.h"
42 #ifdef USE_IMAP
43 #include "message.h"
44 #endif
45 #include "mime.h"
46 #include "mx.h"
47 #include "lib.h"
48 #include "md5.h"
49
50 #include "lib/mem.h"
51 #include "lib/debug.h"
52
53 #if HAVE_QDBM
54 static struct
55   header_cache {
56   VILLA *db;
57   char *folder;
58   unsigned int crc;
59 } HEADER_CACHE;
60 #elif HAVE_GDBM
61 static struct
62   header_cache {
63   GDBM_FILE db;
64   char *folder;
65   unsigned int crc;
66 } HEADER_CACHE;
67 #elif HAVE_DB4
68 static struct
69   header_cache {
70   DB_ENV *env;
71   DB *db;
72   unsigned int crc;
73   int fd;
74   char lockfile[_POSIX_PATH_MAX];
75 } HEADER_CACHE;
76 #endif
77
78 typedef union {
79   struct timeval timeval;
80   unsigned long uid_validity;
81 } validate;
82
83 static void *lazy_malloc (size_t siz)
84 {
85   if (0 < siz && siz < 4096) {
86     siz = 4096;
87   }
88
89   return safe_malloc (siz);
90 }
91
92 static void lazy_realloc (void *ptr, size_t siz)
93 {
94   void **p = (void **) ptr;
95
96   if (p != NULL && 0 < siz && siz < 4096) {
97     return;
98   }
99
100   safe_realloc (ptr, siz);
101 }
102
103 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
104 {
105   lazy_realloc (&d, *off + sizeof (int));
106   memcpy (d + *off, &i, sizeof (int));
107   (*off) += sizeof (int);
108
109   return d;
110 }
111
112 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
113 {
114   memcpy (i, d + *off, sizeof (int));
115   (*off) += sizeof (int);
116 }
117
118 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
119 {
120   unsigned int size;
121
122   if (c == NULL) {
123     size = 0;
124     d = dump_int (size, d, off);
125     return d;
126   }
127
128   size = safe_strlen (c) + 1;
129   d = dump_int (size, d, off);
130   lazy_realloc (&d, *off + size);
131   memcpy (d + *off, c, size);
132   *off += size;
133
134   return d;
135 }
136
137 #if 0
138 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
139                                       ssize_t size)
140 {
141   if (c == NULL) {
142     size = 0;
143     d = dump_int (size, d, off);
144     return d;
145   }
146
147   d = dump_int (size, d, off);
148   lazy_realloc (&d, *off + size);
149   memcpy (d + *off, c, size);
150   *off += size;
151
152   return d;
153 }
154 #endif
155
156 static void restore_char (char **c, const unsigned char *d, int *off)
157 {
158   unsigned int size;
159
160   restore_int (&size, d, off);
161
162   if (size == 0) {
163     *c = NULL;
164     return;
165   }
166
167   *c = safe_malloc (size);
168   memcpy (*c, d + *off, size);
169   *off += size;
170 }
171
172 static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off)
173 {
174   unsigned int counter = 0;
175   unsigned int start_off = *off;
176
177   d = dump_int (0xdeadbeef, d, off);
178
179   while (a) {
180 #ifdef EXACT_ADDRESS
181     d = dump_char (a->val, d, off);
182 #endif
183     d = dump_char (a->personal, d, off);
184     d = dump_char (a->mailbox, d, off);
185     d = dump_int (a->group, d, off);
186     a = a->next;
187     counter++;
188   }
189
190   memcpy (d + start_off, &counter, sizeof (int));
191
192   return d;
193 }
194
195 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
196 {
197   unsigned int counter;
198
199   restore_int (&counter, d, off);
200
201   while (counter) {
202     *a = safe_malloc (sizeof (ADDRESS));
203 #ifdef EXACT_ADDRESS
204     restore_char (&(*a)->val, d, off);
205 #endif
206     restore_char (&(*a)->personal, d, off);
207     restore_char (&(*a)->mailbox, d, off);
208     restore_int ((unsigned int *) &(*a)->group, d, off);
209     a = &(*a)->next;
210     counter--;
211   }
212
213   *a = NULL;
214 }
215
216 static unsigned char *dump_list (LIST * l, unsigned char *d, int *off)
217 {
218   unsigned int counter = 0;
219   unsigned int start_off = *off;
220
221   d = dump_int (0xdeadbeef, d, off);
222
223   while (l) {
224     d = dump_char (l->data, d, off);
225     l = l->next;
226     counter++;
227   }
228
229   memcpy (d + start_off, &counter, sizeof (int));
230
231   return d;
232 }
233
234 static void restore_list (LIST ** l, const unsigned char *d, int *off)
235 {
236   unsigned int counter;
237
238   restore_int (&counter, d, off);
239
240   while (counter) {
241     *l = safe_malloc (sizeof (LIST));
242     restore_char (&(*l)->data, d, off);
243     l = &(*l)->next;
244     counter--;
245   }
246
247   *l = NULL;
248 }
249
250 #if 0
251 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
252 {
253   if (!b) {
254     d = dump_int (0, d, off);
255     return d;
256   }
257   else {
258     d = dump_int (1, d, off);
259   }
260
261   d = dump_char_size (b->data, d, off, b->dsize + 1);
262   d = dump_int (b->dptr - b->data, d, off);
263   d = dump_int (b->dsize, d, off);
264   d = dump_int (b->destroy, d, off);
265
266   return d;
267 }
268
269 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
270 {
271   unsigned int used;
272   unsigned int offset;
273
274   restore_int (&used, d, off);
275   if (!used) {
276     return;
277   }
278
279   *b = safe_malloc (sizeof (BUFFER));
280
281   restore_char (&(*b)->data, d, off);
282   restore_int (&offset, d, off);
283   (*b)->dptr = (*b)->data + offset;
284   restore_int (&(*b)->dsize, d, off);
285   restore_int ((unsigned int *) &(*b)->destroy, d, off);
286 }
287 #endif
288
289 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
290                                       int *off)
291 {
292   unsigned int counter = 0;
293   unsigned int start_off = *off;
294
295   d = dump_int (0xdeadbeef, d, off);
296
297   while (p) {
298     d = dump_char (p->attribute, d, off);
299     d = dump_char (p->value, d, off);
300     p = p->next;
301     counter++;
302   }
303
304   memcpy (d + start_off, &counter, sizeof (int));
305
306   return d;
307 }
308
309 static void
310 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
311 {
312   unsigned int counter;
313
314   restore_int (&counter, d, off);
315
316   while (counter) {
317     *p = safe_malloc (sizeof (PARAMETER));
318     restore_char (&(*p)->attribute, d, off);
319     restore_char (&(*p)->value, d, off);
320     p = &(*p)->next;
321     counter--;
322   }
323
324   *p = NULL;
325 }
326
327 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
328 {
329   lazy_realloc (&d, *off + sizeof (BODY));
330   memcpy (d + *off, c, sizeof (BODY));
331   *off += sizeof (BODY);
332
333   d = dump_char (c->xtype, d, off);
334   d = dump_char (c->subtype, d, off);
335
336   d = dump_parameter (c->parameter, d, off);
337
338   d = dump_char (c->description, d, off);
339   d = dump_char (c->form_name, d, off);
340   d = dump_char (c->filename, d, off);
341   d = dump_char (c->d_filename, d, off);
342
343   return d;
344 }
345
346 static void restore_body (BODY * c, const unsigned char *d, int *off)
347 {
348   memcpy (c, d + *off, sizeof (BODY));
349   *off += sizeof (BODY);
350
351   restore_char (&c->xtype, d, off);
352   restore_char (&c->subtype, d, off);
353
354   restore_parameter (&c->parameter, d, off);
355
356   restore_char (&c->description, d, off);
357   restore_char (&c->form_name, d, off);
358   restore_char (&c->filename, d, off);
359   restore_char (&c->d_filename, d, off);
360 }
361
362 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
363 {
364   d = dump_address (e->return_path, d, off);
365   d = dump_address (e->from, d, off);
366   d = dump_address (e->to, d, off);
367   d = dump_address (e->cc, d, off);
368   d = dump_address (e->bcc, d, off);
369   d = dump_address (e->sender, d, off);
370   d = dump_address (e->reply_to, d, off);
371   d = dump_address (e->mail_followup_to, d, off);
372
373   d = dump_char (e->subject, d, off);
374   if (e->real_subj) {
375     d = dump_int (e->real_subj - e->subject, d, off);
376   }
377   else {
378     d = dump_int (-1, d, off);
379   }
380   d = dump_char (e->message_id, d, off);
381   d = dump_char (e->supersedes, d, off);
382   d = dump_char (e->date, d, off);
383   d = dump_char (e->x_label, d, off);
384
385 #ifdef USE_NNTP
386   d = dump_char (e->newsgroups, d, off);
387   d = dump_char (e->xref, d, off);
388   d = dump_char (e->followup_to, d, off);
389   d = dump_char (e->x_comment_to, d, off);
390 #endif
391
392   d = dump_list (e->references, d, off);
393   d = dump_list (e->in_reply_to, d, off);
394   d = dump_list (e->userhdrs, d, off);
395
396   return d;
397 }
398
399 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
400 {
401   int real_subj_off;
402
403   restore_address (&e->return_path, d, off);
404   restore_address (&e->from, d, off);
405   restore_address (&e->to, d, off);
406   restore_address (&e->cc, d, off);
407   restore_address (&e->bcc, d, off);
408   restore_address (&e->sender, d, off);
409   restore_address (&e->reply_to, d, off);
410   restore_address (&e->mail_followup_to, d, off);
411
412   restore_char (&e->subject, d, off);
413   restore_int ((unsigned int *) (&real_subj_off), d, off);
414   if (0 <= real_subj_off) {
415     e->real_subj = e->subject + real_subj_off;
416   }
417   else {
418     e->real_subj = NULL;
419   }
420   restore_char (&e->message_id, d, off);
421   restore_char (&e->supersedes, d, off);
422   restore_char (&e->date, d, off);
423   restore_char (&e->x_label, d, off);
424   
425 #ifdef USE_NNTP
426   restore_char (&e->newsgroups, d, off);
427   restore_char (&e->xref, d, off);
428   restore_char (&e->followup_to, d, off);
429   restore_char (&e->x_comment_to, d, off);
430 #endif
431
432   restore_list (&e->references, d, off);
433   restore_list (&e->in_reply_to, d, off);
434   restore_list (&e->userhdrs, d, off);
435 }
436
437 static
438 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
439 {
440   int i;
441
442   while (len--) {
443     crc ^= *p++;
444     for (i = 0; i < 8; i++)
445       crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
446   }
447   return crc;
448 }
449
450 static int generate_crc32 ()
451 {
452   int crc = 0;
453
454   crc = crc32 (crc, (unsigned char const *)
455                "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
456                safe_strlen
457                ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
458
459 #if HAVE_LANGINFO_CODESET
460   crc = crc32 (crc, (unsigned char const *) Charset, safe_strlen (Charset));
461   crc =
462     crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
463            safe_strlen ("HAVE_LANGINFO_CODESET"));
464 #endif
465
466 #if EXACT_ADDRESS
467   crc =
468     crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
469            safe_strlen ("EXACT_ADDRESS"));
470 #endif
471
472 #ifdef USE_POP
473   crc =
474     crc32 (crc, (unsigned char const *) "USE_POP", safe_strlen ("USE_POP"));
475 #endif
476
477 #ifdef MIXMASTER
478   crc =
479     crc32 (crc, (unsigned char const *) "MIXMASTER",
480            safe_strlen ("MIXMASTER"));
481 #endif
482
483 #ifdef USE_IMAP
484   crc =
485     crc32 (crc, (unsigned char const *) "USE_IMAP", safe_strlen ("USE_IMAP"));
486 #endif
487
488 #ifdef USE_NNTP
489   crc =
490     crc32 (crc, (unsigned char const *) "USE_NNTP", safe_strlen ("USE_NNTP"));
491 #endif
492   return crc;
493 }
494
495 static int crc32_matches (const char *d, unsigned int crc)
496 {
497   int off = sizeof (validate);
498   unsigned int mycrc = 0;
499
500   if (!d) {
501     return 0;
502   }
503
504   restore_int (&mycrc, (unsigned char *) d, &off);
505
506   return (crc == mycrc);
507 }
508
509 /* Append md5sumed folder to path if path is a directory. */
510 static const char *mutt_hcache_per_folder (const char *path,
511                                            const char *folder)
512 {
513   static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
514   struct stat path_stat;
515   MD5_CTX md5;
516   unsigned char md5sum[16];
517   int ret;
518
519   ret = stat (path, &path_stat);
520   if (ret < 0) {
521     return path;
522   }
523
524   if (!S_ISDIR (path_stat.st_mode)) {
525     return path;
526   }
527
528   MD5Init (&md5);
529   MD5Update (&md5, (unsigned char *) folder, safe_strlen (folder));
530   MD5Final (md5sum, &md5);
531
532   ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
533                   "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
534                   "%02x%02x%02x%02x%02x%02x%02x%02x",
535                   path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
536                   md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
537                   md5sum[9], md5sum[10], md5sum[11], md5sum[12],
538                   md5sum[13], md5sum[14], md5sum[15]);
539
540   if (ret <= 0) {
541     return path;
542   }
543
544   return mutt_hcache_per_folder_path;
545 }
546
547 /* This function transforms a header into a char so that it is useable by
548  * db_store */
549 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
550                                unsigned long uid_validity)
551 {
552   struct header_cache *db = _db;
553   unsigned char *d = NULL;
554
555   *off = 0;
556
557   d = lazy_malloc (sizeof (validate));
558
559   if (uid_validity) {
560     memcpy (d, &uid_validity, sizeof (unsigned long));
561   }
562   else {
563     struct timeval now;
564
565     gettimeofday (&now, NULL);
566     memcpy (d, &now, sizeof (struct timeval));
567   }
568   *off += sizeof (validate);
569
570   d = dump_int (db->crc, d, off);
571
572   lazy_realloc (&d, *off + sizeof (HEADER));
573   memcpy (d + *off, h, sizeof (HEADER));
574   *off += sizeof (HEADER);
575
576   d = dump_envelope (h->env, d, off);
577   d = dump_body (h->content, d, off);
578   d = dump_char (h->maildir_flags, d, off);
579
580   return d;
581 }
582
583 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
584 {
585   int off = 0;
586   HEADER *h = mutt_new_header ();
587
588   /* skip validate */
589   off += sizeof (validate);
590
591   /* skip crc */
592   off += sizeof (unsigned int);
593
594   memcpy (h, d + off, sizeof (HEADER));
595   off += sizeof (HEADER);
596
597   h->env = mutt_new_envelope ();
598   restore_envelope (h->env, d, &off);
599
600   h->content = mutt_new_body ();
601   restore_body (h->content, d, &off);
602
603   restore_char (&h->maildir_flags, d, &off);
604
605   /* this is needed for maildir style mailboxes */
606   if (oh) {
607     h->old = (*oh)->old;
608     h->path = safe_strdup ((*oh)->path);
609     mutt_free_header (oh);
610   }
611
612   return h;
613 }
614
615 #if HAVE_QDBM
616 void *
617 mutt_hcache_open(const char *path, const char *folder)
618 {
619   struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE));
620   int    flags = 0;
621 #if 0 /* FIXME */
622   int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
623 #endif
624   h->db = NULL;
625   h->folder = safe_strdup(folder);
626   h->crc = generate_crc32();
627
628   if (!path || path[0] == '\0')
629   {
630     FREE(&h->folder);
631     FREE(&h);
632     return NULL;
633   }
634
635   path = mutt_hcache_per_folder(path, folder);
636
637   if (option(OPTHCACHECOMPRESS))
638     flags = VL_OZCOMP;
639
640   h->db = vlopen(path, flags | VL_OWRITER | VL_OCREAT, VL_CMPLEX);
641   if (h->db)
642     return h;
643
644   /* if rw failed try ro */
645   h->db = vlopen(path, flags | VL_OREADER, VL_CMPLEX);
646   if (h->db)
647     return h;
648   else
649   {
650     FREE(&h->folder);
651     FREE(&h);
652
653     return NULL;
654   }
655 }
656
657 void
658 mutt_hcache_close(void *db)
659 {
660   struct header_cache *h = db;
661
662   if (!h)
663     return;
664
665   vlclose(h->db);
666   FREE(&h->folder);
667   FREE(&h);
668 }
669
670 void *
671 mutt_hcache_fetch(void *db, const char *filename,
672                   size_t(*keylen) (const char *fn))
673 {
674   struct header_cache *h = db;
675   char path[_POSIX_PATH_MAX];
676   int ksize;
677   char *data = NULL;
678
679   if (!h)
680     return NULL;
681
682   strncpy(path, h->folder, sizeof (path));
683   safe_strcat(path, sizeof (path), filename);
684
685   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
686
687   data = vlget(h->db, path, ksize, NULL);
688
689   if (! crc32_matches(data, h->crc))
690   {
691     if (data) { 
692       FREE(&data);
693     }
694     return NULL;
695   }
696
697   return data;
698 }
699
700 int
701 mutt_hcache_store(void *db, const char *filename, HEADER * header,
702                   unsigned long uid_validity,
703                   size_t(*keylen) (const char *fn))
704 {
705   struct header_cache *h = db;
706   char path[_POSIX_PATH_MAX];
707   int ret;
708   int ksize, dsize;
709   char *data = NULL;
710
711   if (!h)
712     return -1;
713
714   strncpy(path, h->folder, sizeof (path));
715   safe_strcat(path, sizeof (path), filename);
716
717   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
718
719   data  = mutt_hcache_dump(db, header, &dsize, uid_validity);
720
721   ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
722
723   FREE(&data);
724
725   return ret;
726 }
727
728 int
729 mutt_hcache_delete(void *db, const char *filename,
730                    size_t(*keylen) (const char *fn))
731 {
732   struct header_cache *h = db;
733   char path[_POSIX_PATH_MAX];
734   int ksize;
735
736   if (!h)
737     return -1;
738
739   strncpy(path, h->folder, sizeof (path));
740   safe_strcat(path, sizeof (path), filename);
741
742   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
743
744   return vlout(h->db, path, ksize);
745 }
746
747 #elif HAVE_GDBM
748
749 void *mutt_hcache_open (const char *path, const char *folder)
750 {
751   struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
752   int pagesize =
753     atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
754   h->db = NULL;
755   h->folder = safe_strdup (folder);
756   h->crc = generate_crc32 ();
757
758   if (!path || path[0] == '\0') {
759     FREE (&h->folder);
760     FREE (&h);
761     return NULL;
762   }
763
764   path = mutt_hcache_per_folder (path, folder);
765
766   h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
767   if (h->db) {
768     return h;
769   }
770
771   /* if rw failed try ro */
772   h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
773   if (h->db) {
774     return h;
775   }
776   else {
777     FREE (&h->folder);
778     FREE (&h);
779
780     return NULL;
781   }
782 }
783
784 void mutt_hcache_close (void *db)
785 {
786   struct header_cache *h = db;
787
788   if (!h) {
789     return;
790   }
791
792   gdbm_close (h->db);
793   FREE (&h->folder);
794   FREE (&h);
795 }
796
797 void *mutt_hcache_fetch (void *db, const char *filename,
798                          size_t (*keylen) (const char *fn))
799 {
800   struct header_cache *h = db;
801   datum key;
802   datum data;
803   char path[_POSIX_PATH_MAX];
804
805   if (!h) {
806     return NULL;
807   }
808
809   strncpy (path, h->folder, sizeof (path));
810   strncat (path, filename, sizeof (path) - safe_strlen (path));
811
812   key.dptr = path;
813   key.dsize = keylen (path);
814
815   data = gdbm_fetch (h->db, key);
816
817   if (!crc32_matches (data.dptr, h->crc)) {
818     free (data.dptr);
819     return NULL;
820   }
821
822   return data.dptr;
823 }
824
825 int
826 mutt_hcache_store (void *db, const char *filename, HEADER * header,
827                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
828 {
829   struct header_cache *h = db;
830   datum key;
831   datum data;
832   char path[_POSIX_PATH_MAX];
833   int ret;
834
835   if (!h) {
836     return -1;
837   }
838
839   strncpy (path, h->folder, sizeof (path));
840   strncat (path, filename, sizeof (path) - safe_strlen (path));
841
842   key.dptr = path;
843   key.dsize = keylen (path);
844
845   data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
846
847   ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
848
849   FREE (&data.dptr);
850
851   return ret;
852 }
853
854 int
855 mutt_hcache_delete (void *db, const char *filename,
856                     size_t (*keylen) (const char *fn))
857 {
858   datum key;
859   struct header_cache *h = db;
860   char path[_POSIX_PATH_MAX];
861
862   if (!h) {
863     return -1;
864   }
865
866   strncpy (path, h->folder, sizeof (path));
867   strncat (path, filename, sizeof (path) - safe_strlen (path));
868
869   key.dptr = path;
870   key.dsize = keylen (path);
871
872   return gdbm_delete (h->db, key);
873 }
874 #elif HAVE_DB4
875
876 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
877 {
878   dbt->data = data;
879   dbt->size = dbt->ulen = len;
880   dbt->dlen = dbt->doff = 0;
881   dbt->flags = DB_DBT_USERMEM;
882 }
883
884 static void mutt_hcache_dbt_empty_init (DBT * dbt)
885 {
886   dbt->data = NULL;
887   dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
888   dbt->flags = 0;
889 }
890
891 void *mutt_hcache_open (const char *path, const char *folder)
892 {
893   struct stat sb;
894   u_int32_t createflags = DB_CREATE;
895   int ret;
896   struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
897   int pagesize = atoi (HeaderCachePageSize);
898
899
900   h->crc = generate_crc32 ();
901
902   if (!path || path[0] == '\0') {
903     FREE (&h);
904     return NULL;
905   }
906
907   path = mutt_hcache_per_folder (path, folder);
908
909   snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
910
911   h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
912   if (h->fd < 0) {
913     FREE (&h);
914     return NULL;
915   }
916
917   if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
918     close (h->fd);
919     FREE (&h);
920     return NULL;
921   }
922
923   ret = db_env_create (&h->env, 0);
924   if (ret) {
925     mx_unlock_file (h->lockfile, h->fd, 0);
926     close (h->fd);
927     FREE (&h);
928     return NULL;
929   }
930
931   ret =
932     h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
933   if (!ret) {
934     ret = db_create (&h->db, h->env, 0);
935     if (ret) {
936       h->env->close (h->env, 0);
937       mx_unlock_file (h->lockfile, h->fd, 0);
938       close (h->fd);
939       FREE (&h);
940       return NULL;
941     }
942   }
943
944   if (stat (path, &sb) != 0 && errno == ENOENT) {
945     createflags |= DB_EXCL;
946     h->db->set_pagesize (h->db, pagesize);
947   }
948
949   ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
950   if (ret) {
951     h->db->close (h->db, 0);
952     h->env->close (h->env, 0);
953     mx_unlock_file (h->lockfile, h->fd, 0);
954     close (h->fd);
955     FREE (&h);
956     return NULL;
957   }
958
959   return h;
960 }
961
962 void mutt_hcache_close (void *db)
963 {
964   struct header_cache *h = db;
965   int ret;
966
967   if (!h) {
968     return;
969   }
970
971   h->db->close (h->db, 0);
972   h->env->close (h->env, 0);
973   mx_unlock_file (h->lockfile, h->fd, 0);
974   close (h->fd);
975   FREE (&h);
976 }
977
978 void *mutt_hcache_fetch (void *db, const char *filename,
979                          size_t (*keylen) (const char *fn))
980 {
981   DBT key;
982   DBT data;
983   struct header_cache *h = db;
984
985   if (!h) {
986     return NULL;
987   }
988
989   filename++;                   /* skip '/' */
990
991   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
992   mutt_hcache_dbt_empty_init (&data);
993   data.flags = DB_DBT_MALLOC;
994
995   h->db->get (h->db, NULL, &key, &data, 0);
996
997   if (!crc32_matches (data.data, h->crc)) {
998     free (data.data);
999     return NULL;
1000   }
1001
1002   return data.data;
1003 }
1004
1005 int
1006 mutt_hcache_store (void *db, const char *filename, HEADER * header,
1007                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
1008 {
1009   DBT key;
1010   DBT data;
1011   int ret;
1012   struct header_cache *h = db;
1013
1014   if (!h) {
1015     return -1;
1016   }
1017
1018   filename++;                   /* skip '/' */
1019
1020   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1021
1022   mutt_hcache_dbt_empty_init (&data);
1023   data.flags = DB_DBT_USERMEM;
1024   data.data =
1025     mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1026   data.ulen = data.size;
1027
1028   ret = h->db->put (h->db, NULL, &key, &data, 0);
1029
1030   FREE (&data.data);
1031
1032   return ret;
1033 }
1034
1035 int
1036 mutt_hcache_delete (void *db, const char *filename,
1037                     size_t (*keylen) (const char *fn))
1038 {
1039   DBT key;
1040   struct header_cache *h = db;
1041
1042   if (!h) {
1043     return -1;
1044   }
1045
1046   filename++;                   /* skip '/' */
1047
1048   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1049   return h->db->del (h->db, NULL, &key, 0);
1050 }
1051 #endif
1052
1053 #endif /* USE_HCACHE */