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