deal with sendmail and dotlock in LUA.
[apps/madmutt.git] / lib-mx / 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 <imap/message.h>
32
33 #include "charset.h"
34 #include "mutt.h"
35 #include "hcache.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   crc = crc32(crc, (unsigned char const *) "MIXMASTER",
372               m_strlen("MIXMASTER"));
373
374   crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
375
376 #ifdef USE_NNTP
377   crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
378 #endif
379   return crc;
380 }
381
382 static int crc32_matches (const char *d, unsigned int crc)
383 {
384   int off = sizeof (validate);
385   unsigned int mycrc = 0;
386
387   if (!d) {
388     return 0;
389   }
390
391   restore_int (&mycrc, (unsigned char *) d, &off);
392
393   return (crc == mycrc);
394 }
395
396 /* Append md5sumed folder to path if path is a directory. */
397 static const char *mutt_hcache_per_folder (const char *path,
398                                            const char *folder)
399 {
400   static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
401   struct stat path_stat;
402   MD5_CTX md5;
403   unsigned char md5sum[16];
404   int ret;
405
406   ret = stat (path, &path_stat);
407   if (ret < 0) {
408     return path;
409   }
410
411   if (!S_ISDIR (path_stat.st_mode)) {
412     return path;
413   }
414
415   MD5Init (&md5);
416   MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
417   MD5Final (md5sum, &md5);
418
419   ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
420                   "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
421                   "%02x%02x%02x%02x%02x%02x%02x%02x",
422                   path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
423                   md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
424                   md5sum[9], md5sum[10], md5sum[11], md5sum[12],
425                   md5sum[13], md5sum[14], md5sum[15]);
426
427   if (ret <= 0) {
428     return path;
429   }
430
431   return mutt_hcache_per_folder_path;
432 }
433
434 /* This function transforms a header into a char so that it is useable by
435  * db_store */
436 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
437                                unsigned long uid_validity)
438 {
439   struct header_cache *db = _db;
440   unsigned char *d = NULL;
441
442   *off = 0;
443
444   d = lazy_malloc (sizeof (validate));
445
446   if (uid_validity) {
447     memcpy (d, &uid_validity, sizeof (unsigned long));
448   }
449   else {
450     struct timeval now;
451
452     gettimeofday (&now, NULL);
453     memcpy (d, &now, sizeof (struct timeval));
454   }
455   *off += sizeof (validate);
456
457   d = dump_int (db->crc, d, off);
458
459   lazy_realloc (&d, *off + sizeof (HEADER));
460   memcpy (d + *off, h, sizeof (HEADER));
461   *off += sizeof (HEADER);
462
463   d = dump_envelope (h->env, d, off);
464   d = dump_body (h->content, d, off);
465   d = dump_char (h->maildir_flags, d, off);
466
467   return d;
468 }
469
470 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
471 {
472   int off = 0;
473   HEADER *h = header_new();
474
475   /* skip validate */
476   off += sizeof (validate);
477
478   /* skip crc */
479   off += sizeof (unsigned int);
480
481   memcpy (h, d + off, sizeof (HEADER));
482   off += sizeof (HEADER);
483
484   h->env = envelope_new();
485   restore_envelope (h->env, d, &off);
486
487   h->content = body_new();
488   restore_body (h->content, d, &off);
489
490   restore_char (&h->maildir_flags, d, &off);
491
492   /* this is needed for maildir style mailboxes */
493   if (oh) {
494     h->old = (*oh)->old;
495     h->path = m_strdup((*oh)->path);
496     header_delete(oh);
497   }
498
499   return h;
500 }
501
502 #if defined(HAVE_QDBM)
503 void *
504 mutt_hcache_open(const char *path, const char *folder)
505 {
506   struct header_cache *h = p_new(struct header_cache, 1);
507   int    flags = VL_OWRITER | VL_OCREAT;
508   h->db = NULL;
509   h->folder = m_strdup(folder);
510   h->crc = generate_crc32();
511
512   if (!path || path[0] == '\0')
513   {
514     p_delete(&h->folder);
515     p_delete(&h);
516     return NULL;
517   }
518
519   path = mutt_hcache_per_folder(path, folder);
520
521   if (option(OPTHCACHECOMPRESS))
522     flags |= VL_OZCOMP;
523
524   h->db = vlopen(path, flags, VL_CMPLEX);
525   if (h->db)
526     return h;
527   else
528   {
529     p_delete(&h->folder);
530     p_delete(&h);
531
532     return NULL;
533   }
534 }
535
536 void
537 mutt_hcache_close(void *db)
538 {
539   struct header_cache *h = db;
540
541   if (!h)
542     return;
543
544   vlclose(h->db);
545   p_delete(&h->folder);
546   p_delete(&h);
547 }
548
549 void *
550 mutt_hcache_fetch(void *db, const char *filename,
551                   ssize_t(*keylen) (const char *fn))
552 {
553   struct header_cache *h = db;
554   char path[_POSIX_PATH_MAX];
555   int ksize;
556   char *data = NULL;
557
558   if (!h)
559     return NULL;
560
561   m_strcpy(path, sizeof(path), h->folder);
562   m_strcat(path, sizeof(path), filename);
563
564   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
565
566   data = vlget(h->db, path, ksize, NULL);
567
568   if (!crc32_matches(data, h->crc))
569   {
570     p_delete(&data);
571     return NULL;
572   }
573
574   return data;
575 }
576
577 int
578 mutt_hcache_store(void *db, const char *filename, HEADER * header,
579                   unsigned long uid_validity,
580                   ssize_t(*keylen) (const char *fn))
581 {
582   struct header_cache *h = db;
583   char path[_POSIX_PATH_MAX];
584   int ret;
585   int ksize, dsize;
586   char *data = NULL;
587
588   if (!h)
589     return -1;
590
591   m_strcpy(path, sizeof(path), h->folder);
592   m_strcat(path, sizeof(path), filename);
593
594   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
595
596   data  = mutt_hcache_dump(db, header, &dsize, uid_validity);
597
598   ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
599
600   p_delete(&data);
601
602   return ret;
603 }
604
605 int
606 mutt_hcache_delete(void *db, const char *filename,
607                    ssize_t(*keylen) (const char *fn))
608 {
609   struct header_cache *h = db;
610   char path[_POSIX_PATH_MAX];
611   int ksize;
612
613   if (!h)
614     return -1;
615
616   m_strcpy(path, sizeof(path), h->folder);
617   m_strcat(path, sizeof(path), filename);
618
619   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
620
621   return vlout(h->db, path, ksize);
622 }
623
624 #elif defined(HAVE_GDBM)
625
626 void *mutt_hcache_open (const char *path, const char *folder)
627 {
628   struct header_cache *h = p_new(struct header_cache, 1);
629   int pagesize =
630     atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
631   h->db = NULL;
632   h->folder = m_strdup(folder);
633   h->crc = generate_crc32 ();
634
635   if (!path || path[0] == '\0') {
636     p_delete(&h->folder);
637     p_delete(&h);
638     return NULL;
639   }
640
641   path = mutt_hcache_per_folder (path, folder);
642
643   h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
644   if (h->db) {
645     return h;
646   }
647
648   /* if rw failed try ro */
649   h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
650   if (h->db) {
651     return h;
652   }
653   else {
654     p_delete(&h->folder);
655     p_delete(&h);
656
657     return NULL;
658   }
659 }
660
661 void mutt_hcache_close (void *db)
662 {
663   struct header_cache *h = db;
664
665   if (!h) {
666     return;
667   }
668
669   gdbm_close (h->db);
670   p_delete(&h->folder);
671   p_delete(&h);
672 }
673
674 void *mutt_hcache_fetch (void *db, const char *filename,
675                          ssize_t (*keylen) (const char *fn))
676 {
677   struct header_cache *h = db;
678   datum key;
679   datum data;
680   char path[_POSIX_PATH_MAX];
681
682   if (!h) {
683     return NULL;
684   }
685
686   m_strcpy(path, sizeof(path), h->folder);
687   strncat (path, filename, sizeof (path) - m_strlen(path));
688
689   key.dptr = path;
690   key.dsize = keylen (path);
691
692   data = gdbm_fetch (h->db, key);
693
694   if (!crc32_matches (data.dptr, h->crc)) {
695     p_delete(&data.dptr);
696     return NULL;
697   }
698
699   return data.dptr;
700 }
701
702 int
703 mutt_hcache_store (void *db, const char *filename, HEADER * header,
704                    unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
705 {
706   struct header_cache *h = db;
707   datum key;
708   datum data;
709   char path[_POSIX_PATH_MAX];
710   int ret;
711
712   if (!h) {
713     return -1;
714   }
715
716   m_strcpy(path, sizeof(path), h->folder);
717   strncat (path, filename, sizeof (path) - m_strlen(path));
718
719   key.dptr = path;
720   key.dsize = keylen (path);
721
722   data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
723
724   ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
725
726   p_delete(&data.dptr);
727
728   return ret;
729 }
730
731 int
732 mutt_hcache_delete (void *db, const char *filename,
733                     ssize_t (*keylen) (const char *fn))
734 {
735   datum key;
736   struct header_cache *h = db;
737   char path[_POSIX_PATH_MAX];
738
739   if (!h) {
740     return -1;
741   }
742
743   m_strcpy(path, sizeof(path), h->folder);
744   strncat (path, filename, sizeof (path) - m_strlen(path));
745
746   key.dptr = path;
747   key.dsize = keylen (path);
748
749   return gdbm_delete (h->db, key);
750 }
751 #elif defined(HAVE_DB4)
752
753 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
754 {
755   dbt->data = data;
756   dbt->size = dbt->ulen = len;
757   dbt->dlen = dbt->doff = 0;
758   dbt->flags = DB_DBT_USERMEM;
759 }
760
761 static void mutt_hcache_dbt_empty_init (DBT * dbt)
762 {
763   dbt->data = NULL;
764   dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
765   dbt->flags = 0;
766 }
767
768 void *mutt_hcache_open (const char *path, const char *folder)
769 {
770   struct stat sb;
771   u_int32_t createflags = DB_CREATE;
772   int ret;
773   struct header_cache *h = p_new(struct header_cache, 1);
774   int pagesize = atoi (HeaderCachePageSize);
775
776
777   h->crc = generate_crc32 ();
778
779   if (!path || path[0] == '\0') {
780     p_delete(&h);
781     return NULL;
782   }
783
784   path = mutt_hcache_per_folder (path, folder);
785
786   snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
787
788   h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
789   if (h->fd < 0) {
790     p_delete(&h);
791     return NULL;
792   }
793
794   if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
795     close (h->fd);
796     p_delete(&h);
797     return NULL;
798   }
799
800   ret = db_env_create (&h->env, 0);
801   if (ret) {
802     mx_unlock_file (h->lockfile, h->fd, 0);
803     close (h->fd);
804     p_delete(&h);
805     return NULL;
806   }
807
808   ret =
809     (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
810   if (!ret) {
811     ret = db_create (&h->db, h->env, 0);
812     if (ret) {
813       h->env->close (h->env, 0);
814       mx_unlock_file (h->lockfile, h->fd, 0);
815       close (h->fd);
816       p_delete(&h);
817       return NULL;
818     }
819   }
820
821   if (stat (path, &sb) != 0 && errno == ENOENT) {
822     createflags |= DB_EXCL;
823     h->db->set_pagesize (h->db, pagesize);
824   }
825
826   ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
827   if (ret) {
828     h->db->close (h->db, 0);
829     h->env->close (h->env, 0);
830     mx_unlock_file (h->lockfile, h->fd, 0);
831     close (h->fd);
832     p_delete(&h);
833     return NULL;
834   }
835
836   return h;
837 }
838
839 void mutt_hcache_close (void *db)
840 {
841   struct header_cache *h = db;
842
843   if (!h) {
844     return;
845   }
846
847   h->db->close (h->db, 0);
848   h->env->close (h->env, 0);
849   mx_unlock_file (h->lockfile, h->fd, 0);
850   close (h->fd);
851   p_delete(&h);
852 }
853
854 void *mutt_hcache_fetch (void *db, const char *filename,
855                          ssize_t (*keylen) (const char *fn))
856 {
857   DBT key;
858   DBT data;
859   struct header_cache *h = db;
860
861   if (!h) {
862     return NULL;
863   }
864
865   filename++;                   /* skip '/' */
866
867   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
868   mutt_hcache_dbt_empty_init (&data);
869   data.flags = DB_DBT_MALLOC;
870
871   h->db->get (h->db, NULL, &key, &data, 0);
872
873   if (!crc32_matches (data.data, h->crc)) {
874     p_delete(&data.data);
875     return NULL;
876   }
877
878   return data.data;
879 }
880
881 int
882 mutt_hcache_store (void *db, const char *filename, HEADER * header,
883                    unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
884 {
885   DBT key;
886   DBT data;
887   int ret;
888   struct header_cache *h = db;
889
890   if (!h) {
891     return -1;
892   }
893
894   filename++;                   /* skip '/' */
895
896   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
897
898   mutt_hcache_dbt_empty_init (&data);
899   data.flags = DB_DBT_USERMEM;
900   data.data =
901     mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
902   data.ulen = data.size;
903
904   ret = h->db->put (h->db, NULL, &key, &data, 0);
905
906   p_delete(&data.data);
907
908   return ret;
909 }
910
911 int
912 mutt_hcache_delete (void *db, const char *filename,
913                     ssize_t (*keylen) (const char *fn))
914 {
915   DBT key;
916   struct header_cache *h = db;
917
918   if (!h) {
919     return -1;
920   }
921
922   filename++;                   /* skip '/' */
923
924   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
925   return h->db->del (h->db, NULL, &key, 0);
926 }
927 #endif
928
929 #endif /* USE_HCACHE */