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