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.
25 #endif /* HAVE_CONFIG_H */
62 char lockfile[_POSIX_PATH_MAX];
68 struct timeval timeval;
69 unsigned long long uid_validity;
73 lazy_malloc(size_t siz)
75 if (0 < siz && siz < 4096) {
79 return safe_malloc(siz);
83 lazy_realloc(void *ptr, size_t siz)
85 void **p = (void **)ptr;
93 safe_realloc(ptr, siz);
96 static unsigned char *
97 dump_int(unsigned int i, unsigned char *d, int *off)
99 lazy_realloc(&d, *off + sizeof(int));
100 memcpy(d + *off, &i, sizeof(int));
101 (*off) += sizeof(int);
107 restore_int(unsigned int *i, const unsigned char *d, int *off)
109 memcpy(i, d + *off, sizeof(int));
110 (*off) += sizeof(int);
113 static unsigned char *
114 dump_char(char *c, unsigned char *d, int *off)
120 d = dump_int(size, d, off);
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);
133 static unsigned char *
134 dump_char_size(char *c, unsigned char *d, int *off, ssize_t size)
138 d = dump_int(size, d, off);
142 d = dump_int(size, d, off);
143 lazy_realloc(&d, *off + size);
144 memcpy(d + *off, c, size);
151 restore_char(char **c, const unsigned char *d, int *off)
154 restore_int(&size, d, off);
161 *c = safe_malloc(size);
162 memcpy(*c, d + *off, size);
166 static unsigned char *
167 dump_address(ADDRESS *a, unsigned char *d, int *off)
169 unsigned int counter = 0;
170 unsigned int start_off = *off;
172 d = dump_int(0xdeadbeef, d, off);
176 d = dump_char(a->val, d, off);
178 d = dump_char(a->personal, d, off);
179 d = dump_char(a->mailbox, d, off);
180 d = dump_int(a->group, d, off);
185 memcpy(d + start_off, &counter, sizeof(int));
191 restore_address(ADDRESS **a, const unsigned char *d, int *off)
193 unsigned int counter;
195 restore_int(&counter, d, off);
198 *a = safe_malloc(sizeof(ADDRESS));
200 restore_char(&(*a)->val, d, off);
202 restore_char(&(*a)->personal, d, off);
203 restore_char(&(*a)->mailbox, d, off);
204 restore_int((unsigned int *)&(*a)->group, d, off);
212 static unsigned char *
213 dump_list(LIST *l, unsigned char *d, int *off)
215 unsigned int counter = 0;
216 unsigned int start_off = *off;
218 d = dump_int(0xdeadbeef, d, off);
221 d = dump_char(l->data, d, off);
226 memcpy(d + start_off, &counter, sizeof(int));
232 restore_list(LIST **l, const unsigned char *d, int *off)
234 unsigned int counter;
236 restore_int(&counter, d, off);
239 *l = safe_malloc(sizeof(LIST));
240 restore_char(&(*l)->data, d, off);
248 static unsigned char *
249 dump_buffer(BUFFER *b, unsigned char *d, int *off)
252 d = dump_int(0, d, off);
255 d = dump_int(1, d, off);
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);
267 restore_buffer(BUFFER **b, const unsigned char *d, int *off)
271 restore_int(&used, d, off);
276 *b = safe_malloc(sizeof(BUFFER));
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);
285 static unsigned char *
286 dump_parameter(PARAMETER *p, unsigned char *d, int *off)
288 unsigned int counter = 0;
289 unsigned int start_off = *off;
291 d = dump_int(0xdeadbeef, d, off);
294 d = dump_char(p->attribute, d, off);
295 d = dump_char(p->value, d, off);
300 memcpy(d + start_off, &counter, sizeof(int));
306 restore_parameter(PARAMETER **p, const unsigned char *d, int *off)
308 unsigned int counter;
310 restore_int(&counter, d, off);
313 *p = safe_malloc(sizeof(PARAMETER));
314 restore_char(&(*p)->attribute, d, off);
315 restore_char(&(*p)->value, d, off);
323 static unsigned char *
324 dump_body(BODY *c, unsigned char *d, int *off)
326 lazy_realloc(&d, *off + sizeof(BODY));
327 memcpy(d + *off, c, sizeof(BODY));
328 *off += sizeof(BODY);
330 d = dump_char(c->xtype, d, off);
331 d = dump_char(c->subtype, d, off);
333 d = dump_parameter(c->parameter, d, off);
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);
344 restore_body(BODY *c, const unsigned char *d, int *off)
346 memcpy(c, d + *off, sizeof(BODY));
347 *off += sizeof(BODY);
349 restore_char(& c->xtype, d, off);
350 restore_char(& c->subtype, d, off);
352 restore_parameter(& c->parameter, d, off);
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);
360 static unsigned char *
361 dump_envelope(ENVELOPE *e, unsigned char *d, int *off)
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);
372 d = dump_char(e->subject, d, off);
374 d = dump_int(e->real_subj - e->subject, d, off);
376 d = dump_int(-1, d, off);
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);
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);
391 restore_envelope(ENVELOPE *e, const unsigned char *d, int *off)
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);
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;
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);
416 restore_list(& e->references, d, off);
417 restore_list(& e->in_reply_to, d, off);
418 restore_list(& e->userhdrs, d, off);
422 unsigned int crc32(unsigned int crc, unsigned char const *p, size_t len)
427 for (i = 0; i < 8; i++)
428 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
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"));
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"));
446 crc = crc32(crc, (unsigned char const *) "EXACT_ADDRESS", mutt_strlen("EXACT_ADDRESS"));
450 crc = crc32(crc, (unsigned char const *) "USE_POP", mutt_strlen("USE_POP"));
454 crc = crc32(crc, (unsigned char const *) "MIXMASTER", mutt_strlen("MIXMASTER"));
458 crc = crc32(crc, (unsigned char const *) "USE_IMAP", mutt_strlen("USE_IMAP"));
464 crc32_matches(const char *d, unsigned int crc)
466 int off = sizeof(validate);
467 unsigned int mycrc = 0;
473 restore_int(&mycrc, (unsigned char *) d, &off);
475 return (crc == mycrc);
478 /* Append md5sumed folder to path if path is a directory. */
480 mutt_hcache_per_folder(const char *path, const char *folder)
482 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
483 struct stat path_stat;
485 unsigned char md5sum[16];
488 ret = stat(path, &path_stat);
493 if (! S_ISDIR(path_stat.st_mode)) {
498 MD5Update(&md5, (unsigned char *) folder, strlen(folder));
499 MD5Final(md5sum, &md5);
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]);
513 return mutt_hcache_per_folder_path;
516 /* This function transforms a header into a char so that it is useable by
519 mutt_hcache_dump(void *_db, HEADER *h, int *off, unsigned long long uid_validity)
521 struct header_cache *db = _db;
522 unsigned char *d = NULL;
525 d = lazy_malloc(sizeof(validate));
528 memcpy(d, &uid_validity, sizeof(long long));
531 gettimeofday(&now, NULL);
532 memcpy(d, &now, sizeof(struct timeval));
534 *off += sizeof(validate);
536 d = dump_int(db->crc, d, off);
538 lazy_realloc(&d, *off + sizeof(HEADER));
539 memcpy(d + *off, h, sizeof(HEADER));
540 *off += sizeof(HEADER);
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);
550 mutt_hcache_restore(const unsigned char *d, HEADER **oh)
553 HEADER *h = mutt_new_header();
556 off += sizeof(validate);
559 off += sizeof(unsigned int);
561 memcpy(h, d + off, sizeof(HEADER));
562 off += sizeof(HEADER);
564 h->env = mutt_new_envelope();
565 restore_envelope(h->env, d, &off);
567 h->content = mutt_new_body();
568 restore_body(h->content, d, &off);
570 restore_char(&h->maildir_flags, d, &off);
572 /* this is needed for maildir style mailboxes */
575 h->path = safe_strdup((*oh)->path);
576 mutt_free_header (oh);
585 mutt_hcache_open(const char *path, const char *folder)
587 struct header_cache *h = safe_calloc(1, sizeof(HEADER_CACHE));
588 int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
590 h->folder = safe_strdup (folder);
591 h->crc = generate_crc32();
593 if (! path || path[0] == '\0') {
599 path = mutt_hcache_per_folder(path, folder);
601 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
606 /* if rw failed try ro */
607 h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
619 mutt_hcache_close(void *db)
621 struct header_cache *h = db;
633 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
635 struct header_cache *h = db;
638 char path[_POSIX_PATH_MAX];
644 strncpy(path, h->folder, sizeof(path));
645 strncat(path, filename, sizeof(path) - mutt_strlen(path));
648 key.dsize = keylen(path);
650 data = gdbm_fetch(h->db, key);
652 if (! crc32_matches(data.dptr, h->crc)) {
661 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
663 struct header_cache *h = db;
666 char path[_POSIX_PATH_MAX];
673 strncpy(path, h->folder, sizeof(path));
674 strncat(path, filename, sizeof(path) - mutt_strlen(path));
677 key.dsize = keylen(path);
679 data.dptr = mutt_hcache_dump(db, header, &data.dsize, uid_validity);
681 ret = gdbm_store(h->db, key, data, GDBM_REPLACE);
689 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
692 struct header_cache *h = db;
693 char path[_POSIX_PATH_MAX];
699 strncpy(path, h->folder, sizeof(path));
700 strncat(path, filename, sizeof(path) - mutt_strlen(path));
703 key.dsize = keylen(path);
705 return gdbm_delete(h->db, key);
710 mutt_hcache_dbt_init(DBT *dbt, void *data, size_t len)
713 dbt->size = dbt->ulen = len;
714 dbt->dlen = dbt->doff = 0;
715 dbt->flags = DB_DBT_USERMEM;
719 mutt_hcache_dbt_empty_init(DBT *dbt)
722 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
727 mutt_hcache_open(const char *path, const char *folder)
730 u_int32_t createflags = DB_CREATE;
732 struct header_cache *h = calloc(1, sizeof(HEADER_CACHE));
733 int pagesize = atoi(HeaderCachePageSize);
736 h->crc = generate_crc32();
738 if (! path || path[0] == '\0') {
743 path = mutt_hcache_per_folder(path, folder);
745 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
747 h->fd = open(h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
753 if (mx_lock_file(h->lockfile, h->fd, 1, 0, 5)) {
759 ret = db_env_create(&h->env, 0);
761 mx_unlock_file(h->lockfile, h->fd, 0);
767 ret = h->env->open(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
769 ret = db_create(&h->db, h->env, 0);
771 h->env->close(h->env, 0);
772 mx_unlock_file(h->lockfile, h->fd, 0);
779 if (stat(path, &sb) != 0 && errno == ENOENT) {
780 createflags |= DB_EXCL;
781 h->db->set_pagesize(h->db, pagesize);
784 ret = h->db->open(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
786 h->db->close(h->db, 0);
787 h->env->close(h->env, 0);
788 mx_unlock_file(h->lockfile, h->fd, 0);
798 mutt_hcache_close(void *db)
800 struct header_cache *h = db;
807 h->db->close(h->db, 0);
808 h->env->close(h->env, 0);
809 mx_unlock_file(h->lockfile, h->fd, 0);
815 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
819 struct header_cache *h = db;
825 filename++; /* skip '/' */
827 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
828 mutt_hcache_dbt_empty_init(&data);
829 data.flags = DB_DBT_MALLOC;
831 h->db->get(h->db, NULL, &key, &data, 0);
833 if (! crc32_matches(data.data, h->crc)) {
842 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
847 struct header_cache *h = db;
853 filename++; /* skip '/' */
855 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
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;
862 ret = h->db->put(h->db, NULL, &key, &data, 0);
870 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
873 struct header_cache *h = db;
879 filename++; /* skip '/' */
881 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
882 return h->db->del(h->db, NULL, &key, 0);
886 #endif /* USE_HCACHE */