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