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