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