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.
23 #endif /* HAVE_CONFIG_H */
58 char lockfile[_POSIX_PATH_MAX];
64 struct timeval timeval;
65 unsigned long long uid_validity;
69 lazy_malloc(size_t siz)
71 if (0 < siz && siz < 4096) {
75 return safe_malloc(siz);
79 lazy_realloc(void *ptr, size_t siz)
81 void **p = (void **)ptr;
89 safe_realloc(ptr, siz);
92 static unsigned char *
93 dump_int(unsigned int i, unsigned char *d, int *off)
95 lazy_realloc(&d, *off + sizeof(int));
96 memcpy(d + *off, &i, sizeof(int));
97 (*off) += sizeof(int);
103 restore_int(unsigned int *i, const unsigned char *d, int *off)
105 memcpy(i, d + *off, sizeof(int));
106 (*off) += sizeof(int);
109 static unsigned char *
110 dump_char(char *c, unsigned char *d, int *off)
116 d = dump_int(size, d, off);
120 size = mutt_strlen(c) + 1;
121 d = dump_int(size, d, off);
122 lazy_realloc(&d, *off + size);
123 memcpy(d + *off, c, size);
129 static unsigned char *
130 dump_char_size(char *c, unsigned char *d, int *off, ssize_t size)
134 d = dump_int(size, d, off);
138 d = dump_int(size, d, off);
139 lazy_realloc(&d, *off + size);
140 memcpy(d + *off, c, size);
147 restore_char(char **c, const unsigned char *d, int *off)
150 restore_int(&size, d, off);
157 *c = safe_malloc(size);
158 memcpy(*c, d + *off, size);
162 static unsigned char *
163 dump_address(ADDRESS *a, unsigned char *d, int *off)
165 unsigned int counter = 0;
166 unsigned int start_off = *off;
168 d = dump_int(0xdeadbeef, d, off);
172 d = dump_char(a->val, d, off);
174 d = dump_char(a->personal, d, off);
175 d = dump_char(a->mailbox, d, off);
176 d = dump_int(a->group, d, off);
181 memcpy(d + start_off, &counter, sizeof(int));
187 restore_address(ADDRESS **a, const unsigned char *d, int *off)
189 unsigned int counter;
191 restore_int(&counter, d, off);
194 *a = safe_malloc(sizeof(ADDRESS));
196 restore_char(&(*a)->val, d, off);
198 restore_char(&(*a)->personal, d, off);
199 restore_char(&(*a)->mailbox, d, off);
200 restore_int((unsigned int *)&(*a)->group, d, off);
208 static unsigned char *
209 dump_list(LIST *l, unsigned char *d, int *off)
211 unsigned int counter = 0;
212 unsigned int start_off = *off;
214 d = dump_int(0xdeadbeef, d, off);
217 d = dump_char(l->data, d, off);
222 memcpy(d + start_off, &counter, sizeof(int));
228 restore_list(LIST **l, const unsigned char *d, int *off)
230 unsigned int counter;
232 restore_int(&counter, d, off);
235 *l = safe_malloc(sizeof(LIST));
236 restore_char(&(*l)->data, d, off);
244 static unsigned char *
245 dump_buffer(BUFFER *b, unsigned char *d, int *off)
248 d = dump_int(0, d, off);
251 d = dump_int(1, d, off);
254 d = dump_char_size(b->data, d, off, b->dsize + 1);
255 d = dump_int(b->dptr - b->data, d, off);
256 d = dump_int(b->dsize, d, off);
257 d = dump_int(b->destroy, d, off);
263 restore_buffer(BUFFER **b, const unsigned char *d, int *off)
267 restore_int(&used, d, off);
272 *b = safe_malloc(sizeof(BUFFER));
274 restore_char(& (*b)->data, d, off);
275 restore_int(& offset, d, off);
276 (*b)->dptr = (*b)->data + offset;
277 restore_int(& (*b)->dsize, d, off);
278 restore_int((unsigned int *) & (*b)->destroy, d, off);
281 static unsigned char *
282 dump_parameter(PARAMETER *p, unsigned char *d, int *off)
284 unsigned int counter = 0;
285 unsigned int start_off = *off;
287 d = dump_int(0xdeadbeef, d, off);
290 d = dump_char(p->attribute, d, off);
291 d = dump_char(p->value, d, off);
296 memcpy(d + start_off, &counter, sizeof(int));
302 restore_parameter(PARAMETER **p, const unsigned char *d, int *off)
304 unsigned int counter;
306 restore_int(&counter, d, off);
309 *p = safe_malloc(sizeof(PARAMETER));
310 restore_char(&(*p)->attribute, d, off);
311 restore_char(&(*p)->value, d, off);
319 static unsigned char *
320 dump_body(BODY *c, unsigned char *d, int *off)
322 lazy_realloc(&d, *off + sizeof(BODY));
323 memcpy(d + *off, c, sizeof(BODY));
324 *off += sizeof(BODY);
326 d = dump_char(c->xtype, d, off);
327 d = dump_char(c->subtype, d, off);
329 d = dump_parameter(c->parameter, d, off);
331 d = dump_char(c->description, d, off);
332 d = dump_char(c->form_name, d, off);
333 d = dump_char(c->filename, d, off);
334 d = dump_char(c->d_filename, d, off);
340 restore_body(BODY *c, const unsigned char *d, int *off)
342 memcpy(c, d + *off, sizeof(BODY));
343 *off += sizeof(BODY);
345 restore_char(& c->xtype, d, off);
346 restore_char(& c->subtype, d, off);
348 restore_parameter(& c->parameter, d, off);
350 restore_char(& c->description, d, off);
351 restore_char(& c->form_name, d, off);
352 restore_char(& c->filename, d, off);
353 restore_char(& c->d_filename, d, off);
356 static unsigned char *
357 dump_envelope(ENVELOPE *e, unsigned char *d, int *off)
359 d = dump_address(e->return_path, d, off);
360 d = dump_address(e->from, d, off);
361 d = dump_address(e->to, d, off);
362 d = dump_address(e->cc, d, off);
363 d = dump_address(e->bcc, d, off);
364 d = dump_address(e->sender, d, off);
365 d = dump_address(e->reply_to, d, off);
366 d = dump_address(e->mail_followup_to, d, off);
368 d = dump_char(e->subject, d, off);
370 d = dump_int(e->real_subj - e->subject, d, off);
372 d = dump_int(-1, d, off);
374 d = dump_char(e->message_id, d, off);
375 d = dump_char(e->supersedes, d, off);
376 d = dump_char(e->date, d, off);
377 d = dump_char(e->x_label, d, off);
379 d = dump_list(e->references, d, off);
380 d = dump_list(e->in_reply_to, d, off);
381 d = dump_list(e->userhdrs, d, off);
387 restore_envelope(ENVELOPE *e, const unsigned char *d, int *off)
391 restore_address(& e->return_path, d, off);
392 restore_address(& e->from, d, off);
393 restore_address(& e->to, d, off);
394 restore_address(& e->cc, d, off);
395 restore_address(& e->bcc, d, off);
396 restore_address(& e->sender, d, off);
397 restore_address(& e->reply_to, d, off);
398 restore_address(& e->mail_followup_to, d, off);
400 restore_char(& e->subject, d, off);
401 restore_int((unsigned int *) (& real_subj_off), d, off);
402 if (0 <= real_subj_off) {
403 e->real_subj = e->subject + real_subj_off;
407 restore_char(& e->message_id, d, off);
408 restore_char(& e->supersedes, d, off);
409 restore_char(& e->date, d, off);
410 restore_char(& e->x_label, d, off);
412 restore_list(& e->references, d, off);
413 restore_list(& e->in_reply_to, d, off);
414 restore_list(& e->userhdrs, d, off);
418 unsigned int crc32(unsigned int crc, unsigned char const *p, size_t len)
423 for (i = 0; i < 8; i++)
424 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
434 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"));
436 #if HAVE_LANGINFO_CODESET
437 crc = crc32(crc, (unsigned char const *) Charset, mutt_strlen(Charset));
438 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET", mutt_strlen("HAVE_LANGINFO_CODESET"));
442 crc = crc32(crc, (unsigned char const *) "EXACT_ADDRESS", mutt_strlen("EXACT_ADDRESS"));
446 crc = crc32(crc, (unsigned char const *) "USE_POP", mutt_strlen("USE_POP"));
450 crc = crc32(crc, (unsigned char const *) "MIXMASTER", mutt_strlen("MIXMASTER"));
454 crc = crc32(crc, (unsigned char const *) "USE_IMAP", mutt_strlen("USE_IMAP"));
460 crc32_matches(const char *d, unsigned int crc)
462 int off = sizeof(validate);
463 unsigned int mycrc = 0;
469 restore_int(&mycrc, (unsigned char *) d, &off);
471 return (crc == mycrc);
474 /* Append md5sumed folder to path if path is a directory. */
476 mutt_hcache_per_folder(const char *path, const char *folder)
478 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
479 struct stat path_stat;
481 unsigned char md5sum[16];
484 ret = stat(path, &path_stat);
489 if (! S_ISDIR(path_stat.st_mode)) {
494 MD5Update(&md5, (unsigned char *) folder, strlen(folder));
495 MD5Final(md5sum, &md5);
497 ret = snprintf(mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
498 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
499 "%02x%02x%02x%02x%02x%02x%02x%02x",
500 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
501 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
502 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
503 md5sum[13], md5sum[14], md5sum[15]);
509 return mutt_hcache_per_folder_path;
512 /* This function transforms a header into a char so that it is useable by
515 mutt_hcache_dump(void *_db, HEADER *h, int *off, unsigned long long uid_validity)
517 struct header_cache *db = _db;
518 unsigned char *d = NULL;
521 d = lazy_malloc(sizeof(validate));
524 memcpy(d, &uid_validity, sizeof(long long));
527 gettimeofday(&now, NULL);
528 memcpy(d, &now, sizeof(struct timeval));
530 *off += sizeof(validate);
532 d = dump_int(db->crc, d, off);
534 lazy_realloc(&d, *off + sizeof(HEADER));
535 memcpy(d + *off, h, sizeof(HEADER));
536 *off += sizeof(HEADER);
538 d = dump_envelope(h->env, d, off);
539 d = dump_body(h->content, d, off);
540 d = dump_char(h->maildir_flags, d, off);
546 mutt_hcache_restore(const unsigned char *d, HEADER **oh)
549 HEADER *h = mutt_new_header();
552 off += sizeof(validate);
555 off += sizeof(unsigned int);
557 memcpy(h, d + off, sizeof(HEADER));
558 off += sizeof(HEADER);
560 h->env = mutt_new_envelope();
561 restore_envelope(h->env, d, &off);
563 h->content = mutt_new_body();
564 restore_body(h->content, d, &off);
566 restore_char(&h->maildir_flags, d, &off);
568 /* this is needed for maildir style mailboxes */
571 h->path = safe_strdup((*oh)->path);
572 mutt_free_header (oh);
581 mutt_hcache_open(const char *path, const char *folder)
583 struct header_cache *h = safe_calloc(1, sizeof(HEADER_CACHE));
584 int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
586 h->folder = safe_strdup (folder);
587 h->crc = generate_crc32();
589 if (! path || path[0] == '\0') {
595 path = mutt_hcache_per_folder(path, folder);
597 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
602 /* if rw failed try ro */
603 h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
615 mutt_hcache_close(void *db)
617 struct header_cache *h = db;
629 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
631 struct header_cache *h = db;
634 char path[_POSIX_PATH_MAX];
640 strncpy(path, h->folder, sizeof(path));
641 strncat(path, filename, sizeof(path) - mutt_strlen(path));
644 key.dsize = keylen(path);
646 data = gdbm_fetch(h->db, key);
648 if (! crc32_matches(data.dptr, h->crc)) {
657 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
659 struct header_cache *h = db;
662 char path[_POSIX_PATH_MAX];
669 strncpy(path, h->folder, sizeof(path));
670 strncat(path, filename, sizeof(path) - mutt_strlen(path));
673 key.dsize = keylen(path);
675 data.dptr = mutt_hcache_dump(db, header, &data.dsize, uid_validity);
677 ret = gdbm_store(h->db, key, data, GDBM_REPLACE);
685 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
688 struct header_cache *h = db;
689 char path[_POSIX_PATH_MAX];
695 strncpy(path, h->folder, sizeof(path));
696 strncat(path, filename, sizeof(path) - mutt_strlen(path));
699 key.dsize = keylen(path);
701 return gdbm_delete(h->db, key);
706 mutt_hcache_dbt_init(DBT *dbt, void *data, size_t len)
709 dbt->size = dbt->ulen = len;
710 dbt->dlen = dbt->doff = 0;
711 dbt->flags = DB_DBT_USERMEM;
715 mutt_hcache_dbt_empty_init(DBT *dbt)
718 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
723 mutt_hcache_open(const char *path, const char *folder)
726 u_int32_t createflags = DB_CREATE;
728 struct header_cache *h = calloc(1, sizeof(HEADER_CACHE));
729 int pagesize = atoi(HeaderCachePageSize);
732 h->crc = generate_crc32();
734 if (! path || path[0] == '\0') {
739 path = mutt_hcache_per_folder(path, folder);
741 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
743 h->fd = open(h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
749 if (mx_lock_file(h->lockfile, h->fd, 1, 0, 5)) {
755 ret = db_env_create(&h->env, 0);
757 mx_unlock_file(h->lockfile, h->fd, 0);
763 ret = h->env->open(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
765 ret = db_create(&h->db, h->env, 0);
767 h->env->close(h->env, 0);
768 mx_unlock_file(h->lockfile, h->fd, 0);
775 if (stat(path, &sb) != 0 && errno == ENOENT) {
776 createflags |= DB_EXCL;
777 h->db->set_pagesize(h->db, pagesize);
780 ret = h->db->open(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
782 h->db->close(h->db, 0);
783 h->env->close(h->env, 0);
784 mx_unlock_file(h->lockfile, h->fd, 0);
794 mutt_hcache_close(void *db)
796 struct header_cache *h = db;
803 h->db->close(h->db, 0);
804 h->env->close(h->env, 0);
805 mx_unlock_file(h->lockfile, h->fd, 0);
811 mutt_hcache_fetch(void *db, const char *filename, size_t (*keylen)(const char *fn))
815 struct header_cache *h = db;
821 filename++; /* skip '/' */
823 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
824 mutt_hcache_dbt_empty_init(&data);
825 data.flags = DB_DBT_MALLOC;
827 h->db->get(h->db, NULL, &key, &data, 0);
829 if (! crc32_matches(data.data, h->crc)) {
838 mutt_hcache_store(void *db, const char *filename, HEADER *header, unsigned long long uid_validity, size_t (*keylen)(const char *fn))
843 struct header_cache *h = db;
849 filename++; /* skip '/' */
851 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
853 mutt_hcache_dbt_empty_init(&data);
854 data.flags = DB_DBT_USERMEM;
855 data.data = mutt_hcache_dump(db, header, (signed int *) &data.size, uid_validity);
856 data.ulen = data.size;
858 ret = h->db->put(h->db, NULL, &key, &data, 0);
866 mutt_hcache_delete(void *db, const char *filename, size_t (*keylen)(const char *fn))
869 struct header_cache *h = db;
875 filename++; /* skip '/' */
877 mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
878 return h->db->del(h->db, NULL, &key, 0);