Rocco Rutte:
[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   h->db = NULL;
622   h->folder = safe_strdup(folder);
623   h->crc = generate_crc32();
624
625   if (!path || path[0] == '\0')
626   {
627     FREE(&h->folder);
628     FREE(&h);
629     return NULL;
630   }
631
632   path = mutt_hcache_per_folder(path, folder);
633
634   if (option(OPTHCACHECOMPRESS))
635     flags |= VL_OZCOMP;
636
637   h->db = vlopen(path, flags, VL_CMPLEX);
638   if (h->db)
639     return h;
640   else
641   {
642     FREE(&h->folder);
643     FREE(&h);
644
645     return NULL;
646   }
647 }
648
649 void
650 mutt_hcache_close(void *db)
651 {
652   struct header_cache *h = db;
653
654   if (!h)
655     return;
656
657   vlclose(h->db);
658   FREE(&h->folder);
659   FREE(&h);
660 }
661
662 void *
663 mutt_hcache_fetch(void *db, const char *filename,
664                   size_t(*keylen) (const char *fn))
665 {
666   struct header_cache *h = db;
667   char path[_POSIX_PATH_MAX];
668   int ksize;
669   char *data = NULL;
670
671   if (!h)
672     return NULL;
673
674   strncpy(path, h->folder, sizeof (path));
675   safe_strcat(path, sizeof (path), filename);
676
677   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
678
679   data = vlget(h->db, path, ksize, NULL);
680
681   if (! crc32_matches(data, h->crc))
682   {
683     if (data) { 
684       FREE(&data);
685     }
686     return NULL;
687   }
688
689   return data;
690 }
691
692 int
693 mutt_hcache_store(void *db, const char *filename, HEADER * header,
694                   unsigned long uid_validity,
695                   size_t(*keylen) (const char *fn))
696 {
697   struct header_cache *h = db;
698   char path[_POSIX_PATH_MAX];
699   int ret;
700   int ksize, dsize;
701   char *data = NULL;
702
703   if (!h)
704     return -1;
705
706   strncpy(path, h->folder, sizeof (path));
707   safe_strcat(path, sizeof (path), filename);
708
709   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
710
711   data  = mutt_hcache_dump(db, header, &dsize, uid_validity);
712
713   ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
714
715   FREE(&data);
716
717   return ret;
718 }
719
720 int
721 mutt_hcache_delete(void *db, const char *filename,
722                    size_t(*keylen) (const char *fn))
723 {
724   struct header_cache *h = db;
725   char path[_POSIX_PATH_MAX];
726   int ksize;
727
728   if (!h)
729     return -1;
730
731   strncpy(path, h->folder, sizeof (path));
732   safe_strcat(path, sizeof (path), filename);
733
734   ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
735
736   return vlout(h->db, path, ksize);
737 }
738
739 #elif HAVE_GDBM
740
741 void *mutt_hcache_open (const char *path, const char *folder)
742 {
743   struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
744   int pagesize =
745     atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
746   h->db = NULL;
747   h->folder = safe_strdup (folder);
748   h->crc = generate_crc32 ();
749
750   if (!path || path[0] == '\0') {
751     FREE (&h->folder);
752     FREE (&h);
753     return NULL;
754   }
755
756   path = mutt_hcache_per_folder (path, folder);
757
758   h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
759   if (h->db) {
760     return h;
761   }
762
763   /* if rw failed try ro */
764   h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
765   if (h->db) {
766     return h;
767   }
768   else {
769     FREE (&h->folder);
770     FREE (&h);
771
772     return NULL;
773   }
774 }
775
776 void mutt_hcache_close (void *db)
777 {
778   struct header_cache *h = db;
779
780   if (!h) {
781     return;
782   }
783
784   gdbm_close (h->db);
785   FREE (&h->folder);
786   FREE (&h);
787 }
788
789 void *mutt_hcache_fetch (void *db, const char *filename,
790                          size_t (*keylen) (const char *fn))
791 {
792   struct header_cache *h = db;
793   datum key;
794   datum data;
795   char path[_POSIX_PATH_MAX];
796
797   if (!h) {
798     return NULL;
799   }
800
801   strncpy (path, h->folder, sizeof (path));
802   strncat (path, filename, sizeof (path) - safe_strlen (path));
803
804   key.dptr = path;
805   key.dsize = keylen (path);
806
807   data = gdbm_fetch (h->db, key);
808
809   if (!crc32_matches (data.dptr, h->crc)) {
810     free (data.dptr);
811     return NULL;
812   }
813
814   return data.dptr;
815 }
816
817 int
818 mutt_hcache_store (void *db, const char *filename, HEADER * header,
819                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
820 {
821   struct header_cache *h = db;
822   datum key;
823   datum data;
824   char path[_POSIX_PATH_MAX];
825   int ret;
826
827   if (!h) {
828     return -1;
829   }
830
831   strncpy (path, h->folder, sizeof (path));
832   strncat (path, filename, sizeof (path) - safe_strlen (path));
833
834   key.dptr = path;
835   key.dsize = keylen (path);
836
837   data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
838
839   ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
840
841   FREE (&data.dptr);
842
843   return ret;
844 }
845
846 int
847 mutt_hcache_delete (void *db, const char *filename,
848                     size_t (*keylen) (const char *fn))
849 {
850   datum key;
851   struct header_cache *h = db;
852   char path[_POSIX_PATH_MAX];
853
854   if (!h) {
855     return -1;
856   }
857
858   strncpy (path, h->folder, sizeof (path));
859   strncat (path, filename, sizeof (path) - safe_strlen (path));
860
861   key.dptr = path;
862   key.dsize = keylen (path);
863
864   return gdbm_delete (h->db, key);
865 }
866 #elif HAVE_DB4
867
868 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
869 {
870   dbt->data = data;
871   dbt->size = dbt->ulen = len;
872   dbt->dlen = dbt->doff = 0;
873   dbt->flags = DB_DBT_USERMEM;
874 }
875
876 static void mutt_hcache_dbt_empty_init (DBT * dbt)
877 {
878   dbt->data = NULL;
879   dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
880   dbt->flags = 0;
881 }
882
883 void *mutt_hcache_open (const char *path, const char *folder)
884 {
885   struct stat sb;
886   u_int32_t createflags = DB_CREATE;
887   int ret;
888   struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
889   int pagesize = atoi (HeaderCachePageSize);
890
891
892   h->crc = generate_crc32 ();
893
894   if (!path || path[0] == '\0') {
895     FREE (&h);
896     return NULL;
897   }
898
899   path = mutt_hcache_per_folder (path, folder);
900
901   snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
902
903   h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
904   if (h->fd < 0) {
905     FREE (&h);
906     return NULL;
907   }
908
909   if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
910     close (h->fd);
911     FREE (&h);
912     return NULL;
913   }
914
915   ret = db_env_create (&h->env, 0);
916   if (ret) {
917     mx_unlock_file (h->lockfile, h->fd, 0);
918     close (h->fd);
919     FREE (&h);
920     return NULL;
921   }
922
923   ret =
924     h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
925   if (!ret) {
926     ret = db_create (&h->db, h->env, 0);
927     if (ret) {
928       h->env->close (h->env, 0);
929       mx_unlock_file (h->lockfile, h->fd, 0);
930       close (h->fd);
931       FREE (&h);
932       return NULL;
933     }
934   }
935
936   if (stat (path, &sb) != 0 && errno == ENOENT) {
937     createflags |= DB_EXCL;
938     h->db->set_pagesize (h->db, pagesize);
939   }
940
941   ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
942   if (ret) {
943     h->db->close (h->db, 0);
944     h->env->close (h->env, 0);
945     mx_unlock_file (h->lockfile, h->fd, 0);
946     close (h->fd);
947     FREE (&h);
948     return NULL;
949   }
950
951   return h;
952 }
953
954 void mutt_hcache_close (void *db)
955 {
956   struct header_cache *h = db;
957   int ret;
958
959   if (!h) {
960     return;
961   }
962
963   h->db->close (h->db, 0);
964   h->env->close (h->env, 0);
965   mx_unlock_file (h->lockfile, h->fd, 0);
966   close (h->fd);
967   FREE (&h);
968 }
969
970 void *mutt_hcache_fetch (void *db, const char *filename,
971                          size_t (*keylen) (const char *fn))
972 {
973   DBT key;
974   DBT data;
975   struct header_cache *h = db;
976
977   if (!h) {
978     return NULL;
979   }
980
981   filename++;                   /* skip '/' */
982
983   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
984   mutt_hcache_dbt_empty_init (&data);
985   data.flags = DB_DBT_MALLOC;
986
987   h->db->get (h->db, NULL, &key, &data, 0);
988
989   if (!crc32_matches (data.data, h->crc)) {
990     free (data.data);
991     return NULL;
992   }
993
994   return data.data;
995 }
996
997 int
998 mutt_hcache_store (void *db, const char *filename, HEADER * header,
999                    unsigned long uid_validity, size_t (*keylen) (const char *fn))
1000 {
1001   DBT key;
1002   DBT data;
1003   int ret;
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
1014   mutt_hcache_dbt_empty_init (&data);
1015   data.flags = DB_DBT_USERMEM;
1016   data.data =
1017     mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1018   data.ulen = data.size;
1019
1020   ret = h->db->put (h->db, NULL, &key, &data, 0);
1021
1022   FREE (&data.data);
1023
1024   return ret;
1025 }
1026
1027 int
1028 mutt_hcache_delete (void *db, const char *filename,
1029                     size_t (*keylen) (const char *fn))
1030 {
1031   DBT key;
1032   struct header_cache *h = db;
1033
1034   if (!h) {
1035     return -1;
1036   }
1037
1038   filename++;                   /* skip '/' */
1039
1040   mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1041   return h->db->del (h->db, NULL, &key, 0);
1042 }
1043 #endif
1044
1045 #endif /* USE_HCACHE */