some muttbug remains
[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-hash/hash.h>
45
46 #include <lib-mime/mime.h>
47
48 #include "mutt.h"
49 #include <imap/message.h>
50 #include "mx.h"
51 #include "lib.h"
52
53 #include "lib/debug.h"
54
55 static 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 } HEADER_CACHE;
72
73 typedef union {
74   struct timeval timeval;
75   unsigned long uid_validity;
76 } validate;
77
78 static void *lazy_malloc (size_t siz)
79 {
80   if (0 < siz && siz < 4096) {
81     siz = 4096;
82   }
83
84   return xmalloc(siz);
85 }
86
87 static void lazy_realloc(void *ptr, ssize_t siz)
88 {
89   void **p = (void **) ptr;
90
91   if (p != NULL && 0 < siz && siz < 4096) {
92     return;
93   }
94
95   p_realloc(p, siz);
96 }
97
98 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
99 {
100   lazy_realloc (&d, *off + sizeof (int));
101   memcpy (d + *off, &i, sizeof (int));
102   (*off) += sizeof (int);
103
104   return d;
105 }
106
107 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
108 {
109   memcpy (i, d + *off, sizeof (int));
110   (*off) += sizeof (int);
111 }
112
113 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
114 {
115   unsigned int size;
116
117   if (c == NULL) {
118     size = 0;
119     d = dump_int (size, d, off);
120     return d;
121   }
122
123   size = m_strlen(c) + 1;
124   d = dump_int (size, d, off);
125   lazy_realloc (&d, *off + size);
126   memcpy (d + *off, c, size);
127   *off += size;
128
129   return d;
130 }
131
132 #if 0
133 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
134                                       ssize_t size)
135 {
136   if (c == NULL) {
137     size = 0;
138     d = dump_int (size, d, off);
139     return d;
140   }
141
142   d = dump_int (size, d, off);
143   lazy_realloc (&d, *off + size);
144   memcpy (d + *off, c, size);
145   *off += size;
146
147   return d;
148 }
149 #endif
150
151 static void restore_char (char **c, const unsigned char *d, int *off)
152 {
153   unsigned int size;
154
155   restore_int (&size, d, off);
156
157   if (size == 0) {
158     *c = NULL;
159     return;
160   }
161
162   *c = p_dup(d + *off, size);
163   *off += size;
164 }
165
166 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
167 {
168   unsigned int counter = 0;
169   unsigned int start_off = *off;
170
171   d = dump_int (0xdeadbeef, d, off);
172
173   while (a) {
174     d = dump_char (a->personal, d, off);
175     d = dump_char (a->mailbox, d, off);
176     d = dump_int (a->group, d, off);
177     a = a->next;
178     counter++;
179   }
180
181   memcpy (d + start_off, &counter, sizeof (int));
182
183   return d;
184 }
185
186 static void restore_address (address_t ** a, const unsigned char *d, int *off)
187 {
188   unsigned int counter;
189
190   restore_int (&counter, d, off);
191
192   while (counter) {
193     *a = p_new(address_t, 1);
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 = p_new(LIST, 1);
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 = p_new(BUFFER, 1);
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 = p_new(PARAMETER, 1);
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   d = dump_char (e->list_post, d, off);
373
374 #ifdef USE_NNTP
375   d = dump_char (e->newsgroups, d, off);
376   d = dump_char (e->xref, d, off);
377   d = dump_char (e->followup_to, d, off);
378   d = dump_char (e->x_comment_to, d, off);
379 #endif
380
381   d = dump_list (e->references, d, off);
382   d = dump_list (e->in_reply_to, d, off);
383   d = dump_list (e->userhdrs, d, off);
384
385   return d;
386 }
387
388 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
389 {
390   int real_subj_off;
391
392   restore_address (&e->return_path, d, off);
393   restore_address (&e->from, d, off);
394   restore_address (&e->to, d, off);
395   restore_address (&e->cc, d, off);
396   restore_address (&e->bcc, d, off);
397   restore_address (&e->sender, d, off);
398   restore_address (&e->reply_to, d, off);
399   restore_address (&e->mail_followup_to, d, off);
400
401   restore_char (&e->subject, d, off);
402   restore_int ((unsigned int *) (&real_subj_off), d, off);
403   if (0 <= real_subj_off) {
404     e->real_subj = e->subject + real_subj_off;
405   }
406   else {
407     e->real_subj = NULL;
408   }
409   restore_char (&e->message_id, d, off);
410   restore_char (&e->supersedes, d, off);
411   restore_char (&e->date, d, off);
412   restore_char (&e->x_label, d, off);
413   restore_char (&e->list_post, d, off);
414
415 #ifdef USE_NNTP
416   restore_char (&e->newsgroups, d, off);
417   restore_char (&e->xref, d, off);
418   restore_char (&e->followup_to, d, off);
419   restore_char (&e->x_comment_to, d, off);
420 #endif
421
422   restore_list (&e->references, d, off);
423   restore_list (&e->in_reply_to, d, off);
424   restore_list (&e->userhdrs, d, off);
425 }
426
427 static
428 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
429 {
430   int i;
431
432   while (len--) {
433     crc ^= *p++;
434     for (i = 0; i < 8; i++)
435       crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
436   }
437   return crc;
438 }
439
440 static int generate_crc32 ()
441 {
442   int crc = 0;
443
444   crc = crc32 (crc, (unsigned char const *)
445                MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
446                m_strlen
447                (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
448
449 #ifdef HAVE_LANGINFO_CODESET
450   crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
451   crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
452               m_strlen("HAVE_LANGINFO_CODESET"));
453 #endif
454
455   crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
456
457 #ifdef MIXMASTER
458   crc = crc32(crc, (unsigned char const *) "MIXMASTER",
459               m_strlen("MIXMASTER"));
460 #endif
461
462   crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
463
464 #ifdef USE_NNTP
465   crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
466 #endif
467   return crc;
468 }
469
470 static int crc32_matches (const char *d, unsigned int crc)
471 {
472   int off = sizeof (validate);
473   unsigned int mycrc = 0;
474
475   if (!d) {
476     return 0;
477   }
478
479   restore_int (&mycrc, (unsigned char *) d, &off);
480
481   return (crc == mycrc);
482 }
483
484 /* Append md5sumed folder to path if path is a directory. */
485 static const char *mutt_hcache_per_folder (const char *path,
486                                            const char *folder)
487 {
488   static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
489   struct stat path_stat;
490   MD5_CTX md5;
491   unsigned char md5sum[16];
492   int ret;
493
494   ret = stat (path, &path_stat);
495   if (ret < 0) {
496     return path;
497   }
498
499   if (!S_ISDIR (path_stat.st_mode)) {
500     return path;
501   }
502
503   MD5Init (&md5);
504   MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
505   MD5Final (md5sum, &md5);
506
507   ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
508                   "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
509                   "%02x%02x%02x%02x%02x%02x%02x%02x",
510                   path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
511                   md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
512                   md5sum[9], md5sum[10], md5sum[11], md5sum[12],
513                   md5sum[13], md5sum[14], md5sum[15]);
514
515   if (ret <= 0) {
516     return path;
517   }
518
519   return mutt_hcache_per_folder_path;
520 }
521
522 /* This function transforms a header into a char so that it is useable by
523  * db_store */
524 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
525                                unsigned long uid_validity)
526 {
527   struct header_cache *db = _db;
528   unsigned char *d = NULL;
529
530   *off = 0;
531
532   d = lazy_malloc (sizeof (validate));
533
534   if (uid_validity) {
535     memcpy (d, &uid_validity, sizeof (unsigned long));
536   }
537   else {
538     struct timeval now;
539
540     gettimeofday (&now, NULL);
541     memcpy (d, &now, sizeof (struct timeval));
542   }
543   *off += sizeof (validate);
544
545   d = dump_int (db->crc, d, off);
546
547   lazy_realloc (&d, *off + sizeof (HEADER));
548   memcpy (d + *off, h, sizeof (HEADER));
549   *off += sizeof (HEADER);
550
551   d = dump_envelope (h->env, d, off);
552   d = dump_body (h->content, d, off);
553   d = dump_char (h->maildir_flags, d, off);
554
555   return d;
556 }
557
558 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
559 {
560   int off = 0;
561   HEADER *h = header_new();
562
563   /* skip validate */
564   off += sizeof (validate);
565
566   /* skip crc */
567   off += sizeof (unsigned int);
568
569   memcpy (h, d + off, sizeof (HEADER));
570   off += sizeof (HEADER);
571
572   h->env = envelope_new();
573   restore_envelope (h->env, d, &off);
574
575   h->content = mutt_new_body ();
576   restore_body (h->content, d, &off);
577
578   restore_char (&h->maildir_flags, d, &off);
579
580   /* this is needed for maildir style mailboxes */
581   if (oh) {
582     h->old = (*oh)->old;
583     h->path = m_strdup((*oh)->path);
584     header_delete(oh);
585   }
586
587   return h;
588 }
589
590 #if defined(HAVE_QDBM)
591 void *
592 mutt_hcache_open(const char *path, const char *folder)
593 {
594   struct header_cache *h = p_new(struct header_cache, 1);
595   int    flags = VL_OWRITER | VL_OCREAT;
596   h->db = NULL;
597   h->folder = m_strdup(folder);
598   h->crc = generate_crc32();
599
600   if (!path || path[0] == '\0')
601   {
602     p_delete(&h->folder);
603     p_delete(&h);
604     return NULL;
605   }
606
607   path = mutt_hcache_per_folder(path, folder);
608
609   if (option(OPTHCACHECOMPRESS))
610     flags |= VL_OZCOMP;
611
612   h->db = vlopen(path, flags, VL_CMPLEX);
613   if (h->db)
614     return h;
615   else
616   {
617     p_delete(&h->folder);
618     p_delete(&h);
619
620     return NULL;
621   }
622 }
623
624 void
625 mutt_hcache_close(void *db)
626 {
627   struct header_cache *h = db;
628
629   if (!h)
630     return;
631
632   vlclose(h->db);
633   p_delete(&h->folder);
634   p_delete(&h);
635 }
636
637 void *
638 mutt_hcache_fetch(void *db, const char *filename,
639                   size_t(*keylen) (const char *fn))
640 {
641   struct header_cache *h = db;
642   char path[_POSIX_PATH_MAX];
643   int ksize;
644   char *data = NULL;
645
646   if (!h)
647     return NULL;
648
649   m_strcpy(path, sizeof(path), h->folder);
650   m_strcat(path, sizeof(path), filename);
651
652   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
653
654   data = vlget(h->db, path, ksize, NULL);
655
656   if (!crc32_matches(data, h->crc))
657   {
658     p_delete(&data);
659     return NULL;
660   }
661
662   return data;
663 }
664
665 int
666 mutt_hcache_store(void *db, const char *filename, HEADER * header,
667                   unsigned long uid_validity,
668                   size_t(*keylen) (const char *fn))
669 {
670   struct header_cache *h = db;
671   char path[_POSIX_PATH_MAX];
672   int ret;
673   int ksize, dsize;
674   char *data = NULL;
675
676   if (!h)
677     return -1;
678
679   m_strcpy(path, sizeof(path), h->folder);
680   m_strcat(path, sizeof(path), filename);
681
682   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
683
684   data  = mutt_hcache_dump(db, header, &dsize, uid_validity);
685
686   ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
687
688   p_delete(&data);
689
690   return ret;
691 }
692
693 int
694 mutt_hcache_delete(void *db, const char *filename,
695                    size_t(*keylen) (const char *fn))
696 {
697   struct header_cache *h = db;
698   char path[_POSIX_PATH_MAX];
699   int ksize;
700
701   if (!h)
702     return -1;
703
704   m_strcpy(path, sizeof(path), h->folder);
705   m_strcat(path, sizeof(path), filename);
706
707   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
708
709   return vlout(h->db, path, ksize);
710 }
711
712 #elif defined(HAVE_GDBM)
713
714 void *mutt_hcache_open (const char *path, const char *folder)
715 {
716   struct header_cache *h = p_new(HEADER_CACHE, 1);
717   int pagesize =
718     atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
719   h->db = NULL;
720   h->folder = m_strdup(folder);
721   h->crc = generate_crc32 ();
722
723   if (!path || path[0] == '\0') {
724     p_delete(&h->folder);
725     p_delete(&h);
726     return NULL;
727   }
728
729   path = mutt_hcache_per_folder (path, folder);
730
731   h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
732   if (h->db) {
733     return h;
734   }
735
736   /* if rw failed try ro */
737   h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
738   if (h->db) {
739     return h;
740   }
741   else {
742     p_delete(&h->folder);
743     p_delete(&h);
744
745     return NULL;
746   }
747 }
748
749 void mutt_hcache_close (void *db)
750 {
751   struct header_cache *h = db;
752
753   if (!h) {
754     return;
755   }
756
757   gdbm_close (h->db);
758   p_delete(&h->folder);
759   p_delete(&h);
760 }
761
762 void *mutt_hcache_fetch (void *db, const char *filename,
763                          size_t (*keylen) (const char *fn))
764 {
765   struct header_cache *h = db;
766   datum key;
767   datum data;
768   char path[_POSIX_PATH_MAX];
769
770   if (!h) {
771     return NULL;
772   }
773
774   m_strcpy(path, sizeof(path), h->folder);
775   strncat (path, filename, sizeof (path) - m_strlen(path));
776
777   key.dptr = path;
778   key.dsize = keylen (path);
779
780   data = gdbm_fetch (h->db, key);
781
782   if (!crc32_matches (data.dptr, h->crc)) {
783     p_delete(&data.dptr);
784     return NULL;
785   }
786
787   return data.dptr;
788 }
789
790 int
791 mutt_hcache_store (void *db, const char *filename, HEADER * header,
792                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
793 {
794   struct header_cache *h = db;
795   datum key;
796   datum data;
797   char path[_POSIX_PATH_MAX];
798   int ret;
799
800   if (!h) {
801     return -1;
802   }
803
804   m_strcpy(path, sizeof(path), h->folder);
805   strncat (path, filename, sizeof (path) - m_strlen(path));
806
807   key.dptr = path;
808   key.dsize = keylen (path);
809
810   data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
811
812   ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
813
814   p_delete(&data.dptr);
815
816   return ret;
817 }
818
819 int
820 mutt_hcache_delete (void *db, const char *filename,
821                     size_t (*keylen) (const char *fn))
822 {
823   datum key;
824   struct header_cache *h = db;
825   char path[_POSIX_PATH_MAX];
826
827   if (!h) {
828     return -1;
829   }
830
831   m_strcpy(path, sizeof(path), h->folder);
832   strncat (path, filename, sizeof (path) - m_strlen(path));
833
834   key.dptr = path;
835   key.dsize = keylen (path);
836
837   return gdbm_delete (h->db, key);
838 }
839 #elif defined(HAVE_DB4)
840
841 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
842 {
843   dbt->data = data;
844   dbt->size = dbt->ulen = len;
845   dbt->dlen = dbt->doff = 0;
846   dbt->flags = DB_DBT_USERMEM;
847 }
848
849 static void mutt_hcache_dbt_empty_init (DBT * dbt)
850 {
851   dbt->data = NULL;
852   dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
853   dbt->flags = 0;
854 }
855
856 void *mutt_hcache_open (const char *path, const char *folder)
857 {
858   struct stat sb;
859   u_int32_t createflags = DB_CREATE;
860   int ret;
861   struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
862   int pagesize = atoi (HeaderCachePageSize);
863
864
865   h->crc = generate_crc32 ();
866
867   if (!path || path[0] == '\0') {
868     p_delete(&h);
869     return NULL;
870   }
871
872   path = mutt_hcache_per_folder (path, folder);
873
874   snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
875
876   h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
877   if (h->fd < 0) {
878     p_delete(&h);
879     return NULL;
880   }
881
882   if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
883     close (h->fd);
884     p_delete(&h);
885     return NULL;
886   }
887
888   ret = db_env_create (&h->env, 0);
889   if (ret) {
890     mx_unlock_file (h->lockfile, h->fd, 0);
891     close (h->fd);
892     p_delete(&h);
893     return NULL;
894   }
895
896   ret =
897     (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
898   if (!ret) {
899     ret = db_create (&h->db, h->env, 0);
900     if (ret) {
901       h->env->close (h->env, 0);
902       mx_unlock_file (h->lockfile, h->fd, 0);
903       close (h->fd);
904       p_delete(&h);
905       return NULL;
906     }
907   }
908
909   if (stat (path, &sb) != 0 && errno == ENOENT) {
910     createflags |= DB_EXCL;
911     h->db->set_pagesize (h->db, pagesize);
912   }
913
914   ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
915   if (ret) {
916     h->db->close (h->db, 0);
917     h->env->close (h->env, 0);
918     mx_unlock_file (h->lockfile, h->fd, 0);
919     close (h->fd);
920     p_delete(&h);
921     return NULL;
922   }
923
924   return h;
925 }
926
927 void mutt_hcache_close (void *db)
928 {
929   struct header_cache *h = db;
930
931   if (!h) {
932     return;
933   }
934
935   h->db->close (h->db, 0);
936   h->env->close (h->env, 0);
937   mx_unlock_file (h->lockfile, h->fd, 0);
938   close (h->fd);
939   p_delete(&h);
940 }
941
942 void *mutt_hcache_fetch (void *db, const char *filename,
943                          size_t (*keylen) (const char *fn))
944 {
945   DBT key;
946   DBT data;
947   struct header_cache *h = db;
948
949   if (!h) {
950     return NULL;
951   }
952
953   filename++;                   /* skip '/' */
954
955   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
956   mutt_hcache_dbt_empty_init (&data);
957   data.flags = DB_DBT_MALLOC;
958
959   h->db->get (h->db, NULL, &key, &data, 0);
960
961   if (!crc32_matches (data.data, h->crc)) {
962     p_delete(&data.data);
963     return NULL;
964   }
965
966   return data.data;
967 }
968
969 int
970 mutt_hcache_store (void *db, const char *filename, HEADER * header,
971                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
972 {
973   DBT key;
974   DBT data;
975   int ret;
976   struct header_cache *h = db;
977
978   if (!h) {
979     return -1;
980   }
981
982   filename++;                   /* skip '/' */
983
984   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
985
986   mutt_hcache_dbt_empty_init (&data);
987   data.flags = DB_DBT_USERMEM;
988   data.data =
989     mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
990   data.ulen = data.size;
991
992   ret = h->db->put (h->db, NULL, &key, &data, 0);
993
994   p_delete(&data.data);
995
996   return ret;
997 }
998
999 int
1000 mutt_hcache_delete (void *db, const char *filename,
1001                     size_t (*keylen) (const char *fn))
1002 {
1003   DBT key;
1004   struct header_cache *h = db;
1005
1006   if (!h) {
1007     return -1;
1008   }
1009
1010   filename++;                   /* skip '/' */
1011
1012   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1013   return h->db->del (h->db, NULL, &key, 0);
1014 }
1015 #endif
1016
1017 #endif /* USE_HCACHE */