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>
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.
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.
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.
26 #endif /* HAVE_CONFIG_H */
61 char lockfile[_POSIX_PATH_MAX];
67 struct timeval timeval;
68 unsigned long long uid_validity;
72 lazy_malloc(size_t siz)
74 if (0 < siz && siz < 4096) {
78 return safe_malloc(siz);
82 lazy_realloc(void *ptr, size_t siz)
84 void **p = (void **)ptr;
92 safe_realloc(ptr, siz);
95 static unsigned char *
96 dump_int(unsigned int i, unsigned char *d, int *off)
98 lazy_realloc(&d, *off + sizeof(int));
99 memcpy(d + *off, &i, sizeof(int));
100 (*off) += sizeof(int);
106 restore_int(unsigned int *i, const unsigned char *d, int *off)
108 memcpy(i, d + *off, sizeof(int));
109 (*off) += sizeof(int);
112 static unsigned char *
113 dump_char(char *c, unsigned char *d, int *off)
119 d = dump_int(size, d, off);
123 size = mutt_strlen(c) + 1;
124 d = dump_int(size, d, off);
125 lazy_realloc(&d, *off + size);
126 memcpy(d + *off, c, size);
132 static unsigned char *
133 dump_char_size(char *c, unsigned char *d, int *off, ssize_t size)
137 d = dump_int(size, d, off);
141 d = dump_int(size, d, off);
142 lazy_realloc(&d, *off + size);
143 memcpy(d + *off, c, size);
150 restore_char(char **c, const unsigned char *d, int *off)
153 restore_int(&size, d, off);
160 *c = safe_malloc(size);
161 memcpy(*c, d + *off, size);
165 static unsigned char *
166 dump_address(ADDRESS *a, unsigned char *d, int *off)
168 unsigned int counter = 0;
169 unsigned int start_off = *off;
171 d = dump_int(0xdeadbeef, d, off);
175 d = dump_char(a->val, d, off);
177 d = dump_char(a->personal, d, off);
178 d = dump_char(a->mailbox, d, off);
179 d = dump_int(a->group, d, off);
184 memcpy(d + start_off, &counter, sizeof(int));
190 restore_address(ADDRESS **a, const unsigned char *d, int *off)
192 unsigned int counter;
194 restore_int(&counter, d, off);
197 *a = safe_malloc(sizeof(ADDRESS));
199 restore_char(&(*a)->val, d, off);
201 restore_char(&(*a)->personal, d, off);
202 restore_char(&(*a)->mailbox, d, off);
203 restore_int((unsigned int *)&(*a)->group, d, off);
211 static unsigned char *
212 dump_list(LIST *l, unsigned char *d, int *off)
214 unsigned int counter = 0;
215 unsigned int start_off = *off;
217 d = dump_int(0xdeadbeef, d, off);
220 d = dump_char(l->data, d, off);
225 memcpy(d + start_off, &counter, sizeof(int));
231 restore_list(LIST **l, const unsigned char *d, int *off)
233 unsigned int counter;
235 restore_int(&counter, d, off);
238 *l = safe_malloc(sizeof(LIST));
239 restore_char(&(*l)->data, d, off);
247 static unsigned char *
248 dump_buffer(BUFFER *b, unsigned char *d, int *off)
251 d = dump_int(0, d, off);
254 d = dump_int(1, d, off);
257 d = dump_char_size(b->data, d, off, b->dsize + 1);
258 d = dump_int(b->dptr - b->data, d, off);
259 d = dump_int(b->dsize, d, off);
260 d = dump_int(b->destroy, d, off);
266 restore_buffer(BUFFER **b, const unsigned char *d, int *off)
270 restore_int(&used, d, off);
275 *b = safe_malloc(sizeof(BUFFER));
277 restore_char(& (*b)->data, d, off);
278 restore_int(& offset, d, off);
279 (*b)->dptr = (*b)->data + offset;
280 restore_int(& (*b)->dsize, d, off);
281 restore_int((unsigned int *) & (*b)->destroy, d, off);
284 static unsigned char *
285 dump_parameter(PARAMETER *p, unsigned char *d, int *off)
287 unsigned int counter = 0;
288 unsigned int start_off = *off;
290 d = dump_int(0xdeadbeef, d, off);
293 d = dump_char(p->attribute, d, off);
294 d = dump_char(p->value, d, off);
299 memcpy(d + start_off, &counter, sizeof(int));
305 restore_parameter(PARAMETER **p, const unsigned char *d, int *off)
307 unsigned int counter;
309 restore_int(&counter, d, off);
312 *p = safe_malloc(sizeof(PARAMETER));
313 restore_char(&(*p)->attribute, d, off);
314 restore_char(&(*p)->value, d, off);
322 static unsigned char *
323 dump_body(BODY *c, unsigned char *d, int *off)
325 lazy_realloc(&d, *off + sizeof(BODY));
326 memcpy(d + *off, c, sizeof(BODY));
327 *off += sizeof(BODY);
329 d = dump_char(c->xtype, d, off);
330 d = dump_char(c->subtype, d, off);
332 d = dump_parameter(c->parameter, d, off);
334 d = dump_char(c->description, d, off);
335 d = dump_char(c->form_name, d, off);
336 d = dump_char(c->filename, d, off);
337 d = dump_char(c->d_filename, d, off);
343 restore_body(BODY *c, const unsigned char *d, int *off)
345 memcpy(c, d + *off, sizeof(BODY));
346 *off += sizeof(BODY);
348 restore_char(& c->xtype, d, off);
349 restore_char(& c->subtype, d, off);
351 restore_parameter(& c->parameter, d, off);
353 restore_char(& c->description, d, off);
354 restore_char(& c->form_name, d, off);
355 restore_char(& c->filename, d, off);
356 restore_char(& c->d_filename, d, off);
359 static unsigned char *
360 dump_envelope(ENVELOPE *e, unsigned char *d, int *off)
362 d = dump_address(e->return_path, d, off);
363 d = dump_address(e->from, d, off);
364 d = dump_address(e->to, d, off);
365 d = dump_address(e->cc, d, off);
366 d = dump_address(e->bcc, d, off);
367 d = dump_address(e->sender, d, off);
368 d = dump_address(e->reply_to, d, off);
369 d = dump_address(e->mail_followup_to, d, off);
371 d = dump_char(e->subject, d, off);
373 d = dump_int(e->real_subj - e->subject, d, off);
375 d = dump_int(-1, d, off);
377 d = dump_char(e->message_id, d, off);
378 d = dump_char(e->supersedes, d, off);
379 d = dump_char(e->date, d, off);
380 d = dump_char(e->x_label, d, off);
382 d = dump_list(e->references, d, off);
383 d = dump_list(e->in_reply_to, d, off);
384 d = dump_list(e->userhdrs, d, off);
390 restore_envelope(ENVELOPE *e, const unsigned char *d, int *off)
394 restore_address(& e->return_path, d, off);
395 restore_address(& e->from, d, off);
396 restore_address(& e->to, d, off);
397 restore_address(& e->cc, d, off);
398 restore_address(& e->bcc, d, off);
399 restore_address(& e->sender, d, off);
400 restore_address(& e->reply_to, d, off);
401 restore_address(& e->mail_followup_to, d, off);
403 restore_char(& e->subject, d, off);
404 restore_int((unsigned int *) (& real_subj_off), d, off);
405 if (0 <= real_subj_off) {
406 e->real_subj = e->subject + real_subj_off;
410 restore_char(& e->message_id, d, off);
411 restore_char(& e->supersedes, d, off);
412 restore_char(& e->date, d, off);
413 restore_char(& e->x_label, d, off);
415 restore_list(& e->references, d, off);
416 restore_list(& e->in_reply_to, d, off);
417 restore_list(& e->userhdrs, d, off);
421 unsigned int crc32(unsigned int crc, unsigned char const *p, size_t len)
426 for (i = 0; i < 8; i++)
427 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
437 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 #if HAVE_LANGINFO_CODESET
440 crc = crc32(crc, (unsigned char const *) Charset, mutt_strlen(Charset));
441 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET", mutt_strlen("HAVE_LANGINFO_CODESET"));
445 crc = crc32(crc, (unsigned char const *) "EXACT_ADDRESS", mutt_strlen("EXACT_ADDRESS"));
449 crc = crc32(crc, (unsigned char const *) "USE_POP", mutt_strlen("USE_POP"));
453 crc = crc32(crc, (unsigned char const *) "MIXMASTER", mutt_strlen("MIXMASTER"));
457 crc = crc32(crc, (unsigned char const *) "USE_IMAP", mutt_strlen("USE_IMAP"));
463 crc32_matches(const char *d, unsigned int crc)
465 int off = sizeof(validate);
466 unsigned int mycrc = 0;
472 restore_int(&mycrc, (unsigned char *) d, &off);
474 return (crc == mycrc);
477 /* Append md5sumed folder to path if path is a directory. */
479 mutt_hcache_per_folder(const char *path, const char *folder)
481 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
482 struct stat path_stat;
484 unsigned char md5sum[16];
487 ret = stat(path, &path_stat);
492 if (! S_ISDIR(path_stat.st_mode)) {
497 MD5Update(&md5, (unsigned char *) folder, strlen(folder));
498 MD5Final(md5sum, &md5);
500 ret = snprintf(mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
501 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
502 "%02x%02x%02x%02x%02x%02x%02x%02x",
503 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
504 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
505 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
506 md5sum[13], md5sum[14], md5sum[15]);
512 return mutt_hcache_per_folder_path;
515 /* This function transforms a header into a char so that it is useable by
518 mutt_hcache_dump(void *_db, HEADER *h, int *off, unsigned long long uid_validity)
520 struct header_cache *db = _db;
521 unsigned char *d = NULL;
524 d = lazy_malloc(sizeof(validate));
527 memcpy(d, &uid_validity, sizeof(long long));
530 gettimeofday(&now, NULL);
531 memcpy(d, &now, sizeof(struct timeval));
533 *off += sizeof(validate);
535 d = dump_int(db->crc, d, off);
537 lazy_realloc(&d, *off + sizeof(HEADER));
538 memcpy(d + *off, h, sizeof(HEADER));
539 *off += sizeof(HEADER);
541 d = dump_envelope(h->env, d, off);
542 d = dump_body(h->content, d, off);
543 d = dump_char(h->maildir_flags, d, off);
549 mutt_hcache_restore(const unsigned char *d, HEADER **oh)
552 HEADER *h = mutt_new_header();
555 off += sizeof(validate);
558 off += sizeof(unsigned int);
560 memcpy(h, d + off, sizeof(HEADER));
561 off += sizeof(HEADER);
563 h->env = mutt_new_envelope();
564 restore_envelope(h->env, d, &off);
566 h->content = mutt_new_body();
567 restore_body(h->content, d, &off);
569 restore_char(&h->maildir_flags, d, &off);
571 /* this is needed for maildir style mailboxes */
574 h->path = safe_strdup((*oh)->path);
575 mutt_free_header (oh);
584 mutt_hcache_open(const char *path, const char *folder)
586 struct header_cache *h = safe_calloc(1, sizeof(HEADER_CACHE));
587 int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
589 h->folder = safe_strdup (folder);
590 h->crc = generate_crc32();
592 if (! path || path[0] == '\0') {
598 path = mutt_hcache_per_folder(path, folder);
600 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
605 /* if rw failed try ro */
606 h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
618 mutt_hcache_close(void *db)
620 struct header_cache *h = db;
632 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
634 struct header_cache *h = db;
637 char path[_POSIX_PATH_MAX];
643 strncpy(path, h->folder, sizeof(path));
644 strncat(path, filename, sizeof(path) - mutt_strlen(path));
647 key.dsize = keylen(path);
649 data = gdbm_fetch(h->db, key);
651 if (! crc32_matches(data.dptr, h->crc)) {
660 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
662 struct header_cache *h = db;
665 char path[_POSIX_PATH_MAX];
672 strncpy(path, h->folder, sizeof(path));
673 strncat(path, filename, sizeof(path) - mutt_strlen(path));
676 key.dsize = keylen(path);
678 data.dptr = mutt_hcache_dump(db, header, &data.dsize, uid_validity);
680 ret = gdbm_store(h->db, key, data, GDBM_REPLACE);
688 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
691 struct header_cache *h = db;
692 char path[_POSIX_PATH_MAX];
698 strncpy(path, h->folder, sizeof(path));
699 strncat(path, filename, sizeof(path) - mutt_strlen(path));
702 key.dsize = keylen(path);
704 return gdbm_delete(h->db, key);
709 mutt_hcache_dbt_init(DBT *dbt, void *data, size_t len)
712 dbt->size = dbt->ulen = len;
713 dbt->dlen = dbt->doff = 0;
714 dbt->flags = DB_DBT_USERMEM;
718 mutt_hcache_dbt_empty_init(DBT *dbt)
721 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
726 mutt_hcache_open(const char *path, const char *folder)
729 u_int32_t createflags = DB_CREATE;
731 struct header_cache *h = calloc(1, sizeof(HEADER_CACHE));
732 int pagesize = atoi(HeaderCachePageSize);
735 h->crc = generate_crc32();
737 if (! path || path[0] == '\0') {
742 path = mutt_hcache_per_folder(path, folder);
744 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
746 h->fd = open(h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
752 if (mx_lock_file(h->lockfile, h->fd, 1, 0, 5)) {
758 ret = db_env_create(&h->env, 0);
760 mx_unlock_file(h->lockfile, h->fd, 0);
766 ret = h->env->open(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
768 ret = db_create(&h->db, h->env, 0);
770 h->env->close(h->env, 0);
771 mx_unlock_file(h->lockfile, h->fd, 0);
778 if (stat(path, &sb) != 0 && errno == ENOENT) {
779 createflags |= DB_EXCL;
780 h->db->set_pagesize(h->db, pagesize);
783 ret = h->db->open(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
785 h->db->close(h->db, 0);
786 h->env->close(h->env, 0);
787 mx_unlock_file(h->lockfile, h->fd, 0);
797 mutt_hcache_close(void *db)
799 struct header_cache *h = db;
806 h->db->close(h->db, 0);
807 h->env->close(h->env, 0);
808 mx_unlock_file(h->lockfile, h->fd, 0);
814 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
818 struct header_cache *h = db;
824 filename++; /* skip '/' */
826 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
827 mutt_hcache_dbt_empty_init(&data);
828 data.flags = DB_DBT_MALLOC;
830 h->db->get(h->db, NULL, &key, &data, 0);
832 if (! crc32_matches(data.data, h->crc)) {
841 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
846 struct header_cache *h = db;
852 filename++; /* skip '/' */
854 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
856 mutt_hcache_dbt_empty_init(&data);
857 data.flags = DB_DBT_USERMEM;
858 data.data = mutt_hcache_dump(db, header, (signed int *) &data.size, uid_validity);
859 data.ulen = data.size;
861 ret = h->db->put(h->db, NULL, &key, &data, 0);
869 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
872 struct header_cache *h = db;
878 filename++; /* skip '/' */
880 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
881 return h->db->del(h->db, NULL, &key, 0);
885 #endif /* USE_HCACHE */