Rocco Rutte:
[apps/madmutt.git] / hcache.c
1 /*
2  * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
3  * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
4  * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
5  *
6  *     This program is free software; you can redistribute it and/or modify
7  *     it under the terms of the GNU General Public License as published by
8  *     the Free Software Foundation; either version 2 of the License, or
9  *     (at your option) any later version.
10  *
11  *     This program is distributed in the hope that it will be useful,
12  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *     GNU General Public License for more details.
15  *
16  *     You should have received a copy of the GNU General Public License
17  *     along with this program; if not, write to the Free Software
18  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
19  */
20
21 # if HAVE_INTTYPES_H
22 #  include <inttypes.h>
23 # else
24 #  if HAVE_STDINT_H
25 #   include <stdint.h>
26 #  endif
27 # endif
28
29 #if HAVE_CONFIG_H
30 #include "config.h"
31 #endif                          /* HAVE_CONFIG_H */
32
33 #ifdef USE_HCACHE
34
35 #if HAVE_GDBM
36 #include <gdbm.h>
37 #elif HAVE_DB4
38 #include <db.h>
39 #endif
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include "mutt.h"
44 #ifdef USE_IMAP
45 #include "message.h"
46 #endif
47 #include "mime.h"
48 #include "mx.h"
49 #include "lib.h"
50 #include "md5.h"
51
52 #if HAVE_GDBM
53 static struct
54 header_cache
55 {
56         GDBM_FILE db;
57         char *folder;
58         unsigned int crc;
59 } HEADER_CACHE;
60 #elif HAVE_DB4
61 static struct
62 header_cache
63 {
64         DB_ENV *env;
65         DB *db;
66         unsigned int crc;
67         int fd;
68         char lockfile[_POSIX_PATH_MAX];
69 } HEADER_CACHE;
70 #endif
71
72 typedef union
73 {
74         struct timeval timeval;
75         uint64_t uid_validity;
76 } validate;
77
78 static void *
79 lazy_malloc(size_t siz)
80 {
81         if (0 < siz && siz < 4096) {
82                 siz = 4096;
83         }
84
85         return safe_malloc(siz);
86 }
87
88 static void
89 lazy_realloc(void *ptr, size_t siz)
90 {
91         void **p = (void **)ptr;
92
93         if ( p != NULL
94         &&   0 < siz
95         && siz < 4096) {
96                 return;
97         }
98
99         safe_realloc(ptr, siz);
100 }
101
102 static unsigned char *
103 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
113 restore_int(unsigned int *i, const unsigned char *d, int *off)
114 {
115         memcpy(i, d + *off, sizeof(int));
116         (*off) += sizeof(int);
117 }
118
119 static unsigned char *
120 dump_char(char *c, unsigned char *d, int *off)
121 {
122         unsigned int size;
123
124         if (c == NULL) {
125                 size = 0;
126                 d = dump_int(size, d, off);
127                 return d;
128         }
129
130         size = mutt_strlen(c) + 1;
131         d = dump_int(size, d, off);
132         lazy_realloc(&d, *off + size);
133         memcpy(d + *off, c, size);
134         *off += size;
135
136         return d;
137 }
138
139 static unsigned char *
140 dump_char_size(char *c, unsigned char *d, int *off, ssize_t size)
141 {
142         if (c == NULL) {
143                 size = 0;
144                 d = dump_int(size, d, off);
145                 return d;
146         }
147
148         d = dump_int(size, d, off);
149         lazy_realloc(&d, *off + size);
150         memcpy(d + *off, c, size);
151         *off += size;
152
153         return d;
154 }
155
156 static void
157 restore_char(char **c, const unsigned char *d, int *off)
158 {
159         unsigned int size;
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 *
173 dump_address(ADDRESS *a, unsigned char *d, int *off)
174 {
175         unsigned int counter = 0;
176         unsigned int start_off = *off;
177
178         d = dump_int(0xdeadbeef, d, off);
179
180         while (a) {
181 #ifdef EXACT_ADDRESS
182                 d = dump_char(a->val, d, off);
183 #endif
184                 d = dump_char(a->personal, d, off);
185                 d = dump_char(a->mailbox, d, off);
186                 d = dump_int(a->group, d, off);
187                 a = a->next;
188                 counter++;
189         }
190
191         memcpy(d + start_off, &counter, sizeof(int));
192
193         return d;
194 }
195
196 static void
197 restore_address(ADDRESS **a, const unsigned char *d, int *off)
198 {
199         unsigned int counter;
200
201         restore_int(&counter, d, off);
202
203         while (counter) {
204                 *a = safe_malloc(sizeof(ADDRESS));
205 #ifdef EXACT_ADDRESS
206                 restore_char(&(*a)->val, d, off);
207 #endif
208                 restore_char(&(*a)->personal, d, off);
209                 restore_char(&(*a)->mailbox, d, off);
210                 restore_int((unsigned int *)&(*a)->group, d, off);
211                 a = &(*a)->next;
212                 counter--;
213         }
214
215         *a = NULL;
216 }
217
218 static unsigned char *
219 dump_list(LIST *l, unsigned char *d, int *off)
220 {
221         unsigned int counter = 0;
222         unsigned int start_off = *off;
223
224         d = dump_int(0xdeadbeef, d, off);
225
226         while (l) {
227                 d = dump_char(l->data, d, off);
228                 l = l->next;
229                 counter++;
230         }
231
232         memcpy(d + start_off, &counter, sizeof(int));
233
234         return d;
235 }
236
237 static void
238 restore_list(LIST **l, const unsigned char *d, int *off)
239 {
240         unsigned int counter;
241
242         restore_int(&counter, d, off);
243
244         while (counter) {
245                 *l = safe_malloc(sizeof(LIST));
246                 restore_char(&(*l)->data, d, off);
247                 l = &(*l)->next;
248                 counter--;
249         }
250
251         *l = NULL;
252 }
253
254 static unsigned char *
255 dump_buffer(BUFFER *b, unsigned char *d, int *off)
256 {
257         if (! b) {
258                 d = dump_int(0, d, off);
259                 return d;
260         } else {
261                 d = dump_int(1, d, off);
262         }
263
264         d = dump_char_size(b->data, d, off, b->dsize + 1);
265         d = dump_int(b->dptr - b->data, d, off);
266         d = dump_int(b->dsize, d, off);
267         d = dump_int(b->destroy, d, off);
268
269         return d;
270 }
271
272 static void
273 restore_buffer(BUFFER **b, const unsigned char *d, int *off)
274 {
275         unsigned int used;
276         unsigned int offset;
277         restore_int(&used, d, off);
278         if (! used) {
279                 return;
280         }
281
282         *b = safe_malloc(sizeof(BUFFER));
283
284         restore_char(& (*b)->data, d, off);
285         restore_int(& offset, d, off);
286         (*b)->dptr = (*b)->data + offset;
287         restore_int(& (*b)->dsize, d, off);
288         restore_int((unsigned int *) & (*b)->destroy, d, off);
289 }
290
291 static unsigned char *
292 dump_parameter(PARAMETER *p, unsigned char *d, int *off)
293 {
294         unsigned int counter = 0;
295         unsigned int start_off = *off;
296
297         d = dump_int(0xdeadbeef, d, off);
298
299         while (p) {
300                 d = dump_char(p->attribute, d, off);
301                 d = dump_char(p->value, d, off);
302                 p = p->next;
303                 counter++;
304         }
305
306         memcpy(d + start_off, &counter, sizeof(int));
307
308         return d;
309 }
310
311 static void
312 restore_parameter(PARAMETER **p, const unsigned char *d, int *off)
313 {
314         unsigned int counter;
315
316         restore_int(&counter, d, off);
317
318         while (counter) {
319                 *p = safe_malloc(sizeof(PARAMETER));
320                 restore_char(&(*p)->attribute, d, off);
321                 restore_char(&(*p)->value, d, off);
322                 p = &(*p)->next;
323                 counter--;
324         }
325
326         *p = NULL;
327 }
328
329 static unsigned char *
330 dump_body(BODY *c, unsigned char *d, int *off)
331 {
332         lazy_realloc(&d, *off + sizeof(BODY));
333         memcpy(d + *off, c, sizeof(BODY));
334         *off += sizeof(BODY);
335
336         d = dump_char(c->xtype, d, off);
337         d = dump_char(c->subtype, d, off);
338
339         d = dump_parameter(c->parameter, d, off);
340
341         d = dump_char(c->description, d, off);
342         d = dump_char(c->form_name, d, off);
343         d = dump_char(c->filename, d, off);
344         d = dump_char(c->d_filename, d, off);
345
346         return d;
347 }
348
349 static void
350 restore_body(BODY *c, const unsigned char *d, int *off)
351 {
352         memcpy(c, d + *off, sizeof(BODY));
353         *off += sizeof(BODY);
354
355         restore_char(& c->xtype, d, off);
356         restore_char(& c->subtype, d, off);
357
358         restore_parameter(& c->parameter, d, off);
359
360         restore_char(& c->description, d, off);
361         restore_char(& c->form_name, d, off);
362         restore_char(& c->filename, d, off);
363         restore_char(& c->d_filename, d, off);
364 }
365
366 static unsigned char *
367 dump_envelope(ENVELOPE *e, unsigned char *d, int *off)
368 {
369         d = dump_address(e->return_path, d, off);
370         d = dump_address(e->from, d, off);
371         d = dump_address(e->to, d, off);
372         d = dump_address(e->cc, d, off);
373         d = dump_address(e->bcc, d, off);
374         d = dump_address(e->sender, d, off);
375         d = dump_address(e->reply_to, d, off);
376         d = dump_address(e->mail_followup_to, d, off);
377
378         d = dump_char(e->subject, d, off);
379         if (e->real_subj) {
380                 d = dump_int(e->real_subj - e->subject, d, off);
381         } else {
382                 d = dump_int(-1, d, off);
383         }
384         d = dump_char(e->message_id, d, off);
385         d = dump_char(e->supersedes, d, off);
386         d = dump_char(e->date, d, off);
387         d = dump_char(e->x_label, d, off);
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
397 restore_envelope(ENVELOPE *e, const unsigned char *d, int *off)
398 {
399         int real_subj_off;
400
401         restore_address(& e->return_path, d, off);
402         restore_address(& e->from, d, off);
403         restore_address(& e->to, d, off);
404         restore_address(& e->cc, d, off);
405         restore_address(& e->bcc, d, off);
406         restore_address(& e->sender, d, off);
407         restore_address(& e->reply_to, d, off);
408         restore_address(& e->mail_followup_to, d, off);
409
410         restore_char(& e->subject, d, off);
411         restore_int((unsigned int *) (& real_subj_off), d, off);
412         if (0 <= real_subj_off) {
413                 e->real_subj = e->subject + real_subj_off;
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
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         while (len--) {
432                 crc ^= *p++;
433                 for (i = 0; i < 8; i++)
434                         crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
435         }
436         return crc;
437 }
438
439 static int
440 generate_crc32()
441 {
442         int crc = 0;
443
444         crc = crc32(crc, (unsigned char const *) "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613", mutt_strlen("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
445
446 #if HAVE_LANGINFO_CODESET
447         crc = crc32(crc, (unsigned char const *) Charset, mutt_strlen(Charset));
448         crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET", mutt_strlen("HAVE_LANGINFO_CODESET"));
449 #endif
450
451 #if EXACT_ADDRESS
452         crc = crc32(crc, (unsigned char const *) "EXACT_ADDRESS", mutt_strlen("EXACT_ADDRESS"));
453 #endif
454
455 #ifdef USE_POP
456         crc = crc32(crc, (unsigned char const *) "USE_POP", mutt_strlen("USE_POP"));
457 #endif
458
459 #ifdef MIXMASTER
460         crc = crc32(crc, (unsigned char const *) "MIXMASTER", mutt_strlen("MIXMASTER"));
461 #endif
462
463 #ifdef USE_IMAP
464         crc = crc32(crc, (unsigned char const *) "USE_IMAP", mutt_strlen("USE_IMAP"));
465 #endif
466         return crc;
467 }
468
469 static int
470 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 *
486 mutt_hcache_per_folder(const char *path, 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, 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 *
525 mutt_hcache_dump(void *_db, HEADER *h, int *off, uint64_t uid_validity)
526 {
527         struct header_cache *db = _db;
528         unsigned char *d = NULL;
529         *off             = 0;
530
531         d = lazy_malloc(sizeof(validate));
532
533         if (uid_validity) {
534                 memcpy(d, &uid_validity, sizeof(uint64_t));
535         } else {
536                 struct timeval now;
537                 gettimeofday(&now, NULL);
538                 memcpy(d, &now, sizeof(struct timeval));
539         }
540         *off += sizeof(validate);
541
542         d = dump_int(db->crc, d, off);
543
544         lazy_realloc(&d, *off + sizeof(HEADER));
545         memcpy(d + *off, h, sizeof(HEADER));
546         *off += sizeof(HEADER);
547
548         d = dump_envelope(h->env, d, off);
549         d = dump_body(h->content, d, off);
550         d = dump_char(h->maildir_flags, d, off);
551
552         return d;
553 }
554
555 HEADER *
556 mutt_hcache_restore(const unsigned char *d, HEADER **oh)
557 {
558         int off = 0;
559         HEADER *h        = mutt_new_header();
560
561         /* skip validate */
562         off += sizeof(validate);
563
564         /* skip crc */
565         off += sizeof(unsigned int);
566
567         memcpy(h, d + off, sizeof(HEADER));
568         off += sizeof(HEADER);
569
570         h->env = mutt_new_envelope();
571         restore_envelope(h->env, d, &off);
572
573         h->content = mutt_new_body();
574         restore_body(h->content, d, &off);
575
576         restore_char(&h->maildir_flags, d, &off);
577
578         /* this is needed for maildir style mailboxes */
579         if (oh) {
580                 h->old  = (*oh)->old;
581                 h->path = safe_strdup((*oh)->path);
582                 mutt_free_header (oh);
583         }
584
585         return h;
586 }
587
588 #if HAVE_GDBM
589
590 void *
591 mutt_hcache_open(const char *path, const char *folder)
592 {
593         struct header_cache *h = safe_calloc(1, sizeof(HEADER_CACHE));
594         int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
595         h->db     = NULL;
596         h->folder = safe_strdup (folder);
597         h->crc    = generate_crc32();
598
599         if (! path || path[0] == '\0') {
600                 FREE(& h->folder);
601                 FREE(& h);
602                 return NULL;
603         }
604
605         path = mutt_hcache_per_folder(path, folder);
606
607         h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
608         if (h->db) {
609                 return h;
610         }
611
612         /* if rw failed try ro */
613         h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
614         if(h->db) {
615                 return h;
616         } else {
617                 FREE(& h->folder);
618                 FREE(& 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
633         gdbm_close(h->db);
634         FREE(& h->folder);
635         FREE(& h);
636 }
637
638 void *
639 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
640 {
641         struct header_cache *h = db;
642         datum key;
643         datum data;
644         char path[_POSIX_PATH_MAX];
645
646         if (! h) {
647                 return NULL;
648         }
649
650         strncpy(path, h->folder, sizeof(path));
651         strncat(path, filename, sizeof(path) - mutt_strlen(path));
652
653         key.dptr  = path;
654         key.dsize = keylen(path);
655
656         data = gdbm_fetch(h->db, key);
657
658         if (! crc32_matches(data.dptr, h->crc)) {
659                 free(data.dptr);
660                 return NULL;
661         }
662
663         return data.dptr;
664 }
665
666 int
667 mutt_hcache_store(void *db, const char *filename, HEADER *header, uint64_t uid_validity, size_t (*keylen)(const char *fn))
668 {
669         struct header_cache *h = db;
670         datum key;
671         datum data;
672         char path[_POSIX_PATH_MAX];
673         int ret;
674
675         if (! h) {
676                 return -1;
677         }
678
679         strncpy(path, h->folder, sizeof(path));
680         strncat(path, filename, sizeof(path) - mutt_strlen(path));
681
682         key.dptr  = path;
683         key.dsize = keylen(path);
684
685         data.dptr = mutt_hcache_dump(db, header, &data.dsize, uid_validity);
686
687         ret = gdbm_store(h->db, key, data, GDBM_REPLACE);
688
689         FREE(& data.dptr);
690
691         return ret;
692 }
693
694 int
695 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
696 {
697         datum key;
698         struct header_cache *h = db;
699         char path[_POSIX_PATH_MAX];
700
701         if (! h) {
702                 return -1;
703         }
704
705         strncpy(path, h->folder, sizeof(path));
706         strncat(path, filename, sizeof(path) - mutt_strlen(path));
707
708         key.dptr  = path;
709         key.dsize = keylen(path);
710
711         return gdbm_delete(h->db, key);
712 }
713 #elif HAVE_DB4
714
715 static void
716 mutt_hcache_dbt_init(DBT *dbt, void *data, size_t len)
717 {
718         dbt->data = data;
719         dbt->size = dbt->ulen = len;
720         dbt->dlen = dbt->doff = 0;
721         dbt->flags = DB_DBT_USERMEM;
722 }
723
724 static void
725 mutt_hcache_dbt_empty_init(DBT *dbt)
726 {
727         dbt->data = NULL;
728         dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
729         dbt->flags = 0;
730 }
731
732 void *
733 mutt_hcache_open(const char *path, const char *folder)
734 {
735         struct stat sb;
736         u_int32_t createflags = DB_CREATE;
737         int ret;
738         struct header_cache *h = calloc(1, sizeof(HEADER_CACHE));
739         int pagesize = atoi(HeaderCachePageSize);
740
741
742         h->crc = generate_crc32();
743
744         if (! path || path[0] == '\0') {
745                 FREE(& h);
746                 return NULL;
747         }
748
749         path = mutt_hcache_per_folder(path, folder);
750
751         snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
752
753         h->fd = open(h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
754         if (h->fd < 0) {
755                 FREE (&h);
756                 return NULL;
757         }
758
759         if (mx_lock_file(h->lockfile, h->fd, 1, 0, 5)) {
760                 close(h->fd);
761                 FREE (&h);
762                 return NULL;
763         }
764
765         ret = db_env_create(&h->env, 0);
766         if (ret) {
767                 mx_unlock_file(h->lockfile, h->fd, 0);
768                 close(h->fd);
769                 FREE(& h);
770                 return NULL;
771         }
772
773         ret = h->env->open(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
774         if (! ret) {
775                 ret = db_create(&h->db, h->env, 0);
776                 if (ret) {
777                         h->env->close(h->env, 0);
778                         mx_unlock_file(h->lockfile, h->fd, 0);
779                         close(h->fd);
780                         FREE(& h);
781                         return NULL;
782                 }
783         }
784
785         if (stat(path, &sb) != 0 && errno == ENOENT) {
786                 createflags |= DB_EXCL;
787                 h->db->set_pagesize(h->db, pagesize);
788         }
789
790         ret = h->db->open(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
791         if (ret) {
792                 h->db->close(h->db, 0);
793                 h->env->close(h->env, 0);
794                 mx_unlock_file(h->lockfile, h->fd, 0);
795                 close(h->fd);
796                 FREE(& h);
797                 return NULL;
798         }
799
800         return h;
801 }
802
803 void
804 mutt_hcache_close(void *db)
805 {
806         struct header_cache *h = db;
807         int ret;
808
809         if (! h) {
810                 return;
811         }
812
813         h->db->close(h->db, 0);
814         h->env->close(h->env, 0);
815         mx_unlock_file(h->lockfile, h->fd, 0);
816         close(h->fd);
817         FREE(& h);
818 }
819
820 void *
821 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
822 {
823         DBT key;
824         DBT data;
825         struct header_cache *h = db;
826
827         if (! h) {
828                 return NULL;
829         }
830
831         filename++; /* skip '/' */
832
833         mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
834         mutt_hcache_dbt_empty_init(&data);
835         data.flags = DB_DBT_MALLOC;
836
837         h->db->get(h->db, NULL, &key, &data, 0);
838
839         if (! crc32_matches(data.data, h->crc)) {
840                 free(data.data);
841                 return NULL;
842         }
843
844         return data.data;
845 }
846
847 int
848 mutt_hcache_store(void *db, const char *filename, HEADER *header, uint64_t uid_validity, size_t (*keylen)(const char *fn))
849 {
850         DBT key;
851         DBT data;
852         int ret;
853         struct header_cache *h = db;
854
855         if (! h) {
856                 return -1;
857         }
858
859         filename++; /* skip '/' */
860
861         mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
862
863         mutt_hcache_dbt_empty_init(&data);
864         data.flags = DB_DBT_USERMEM;
865         data.data = mutt_hcache_dump(db, header, (signed int *) &data.size, uid_validity);
866         data.ulen = data.size;
867
868         ret = h->db->put(h->db, NULL, &key, &data, 0);
869
870         FREE(& data.data);
871
872         return ret;
873 }
874
875 int
876 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
877 {
878         DBT key;
879         struct header_cache *h = db;
880
881         if (! h) {
882                 return -1;
883         }
884
885         filename++; /* skip '/' */
886
887         mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
888         return h->db->del(h->db, NULL, &key, 0);
889 }
890 #endif
891
892 #endif /* USE_HCACHE */