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