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.
22 # include <inttypes.h>
31 #endif /* HAVE_CONFIG_H */
69 char lockfile[_POSIX_PATH_MAX];
74 struct timeval timeval;
75 uint64_t uid_validity;
78 static void *lazy_malloc (size_t siz)
80 if (0 < siz && siz < 4096) {
84 return safe_malloc (siz);
87 static void lazy_realloc (void *ptr, size_t siz)
89 void **p = (void **) ptr;
91 if (p != NULL && 0 < siz && siz < 4096) {
95 safe_realloc (ptr, siz);
98 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
100 lazy_realloc (&d, *off + sizeof (int));
101 memcpy (d + *off, &i, sizeof (int));
102 (*off) += sizeof (int);
107 static void 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 *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);
133 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
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 static void restore_char (char **c, const unsigned char *d, int *off)
155 restore_int (&size, d, off);
162 *c = safe_malloc (size);
163 memcpy (*c, d + *off, size);
167 static unsigned char *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));
190 static void 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 *dump_list (LIST * l, unsigned char *d, int *off)
213 unsigned int counter = 0;
214 unsigned int start_off = *off;
216 d = dump_int (0xdeadbeef, d, off);
219 d = dump_char (l->data, d, off);
224 memcpy (d + start_off, &counter, sizeof (int));
229 static void restore_list (LIST ** l, const unsigned char *d, int *off)
231 unsigned int counter;
233 restore_int (&counter, d, off);
236 *l = safe_malloc (sizeof (LIST));
237 restore_char (&(*l)->data, d, off);
246 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
249 d = dump_int (0, d, off);
253 d = dump_int (1, d, off);
256 d = dump_char_size (b->data, d, off, b->dsize + 1);
257 d = dump_int (b->dptr - b->data, d, off);
258 d = dump_int (b->dsize, d, off);
259 d = dump_int (b->destroy, d, off);
264 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
269 restore_int (&used, d, off);
274 *b = safe_malloc (sizeof (BUFFER));
276 restore_char (&(*b)->data, d, off);
277 restore_int (&offset, d, off);
278 (*b)->dptr = (*b)->data + offset;
279 restore_int (&(*b)->dsize, d, off);
280 restore_int ((unsigned int *) &(*b)->destroy, d, off);
284 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
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 *dump_body (BODY * c, unsigned char *d, int *off)
324 lazy_realloc (&d, *off + sizeof (BODY));
325 memcpy (d + *off, c, sizeof (BODY));
326 *off += sizeof (BODY);
328 d = dump_char (c->xtype, d, off);
329 d = dump_char (c->subtype, d, off);
331 d = dump_parameter (c->parameter, d, off);
333 d = dump_char (c->description, d, off);
334 d = dump_char (c->form_name, d, off);
335 d = dump_char (c->filename, d, off);
336 d = dump_char (c->d_filename, d, off);
341 static void restore_body (BODY * c, const unsigned char *d, int *off)
343 memcpy (c, d + *off, sizeof (BODY));
344 *off += sizeof (BODY);
346 restore_char (&c->xtype, d, off);
347 restore_char (&c->subtype, d, off);
349 restore_parameter (&c->parameter, d, off);
351 restore_char (&c->description, d, off);
352 restore_char (&c->form_name, d, off);
353 restore_char (&c->filename, d, off);
354 restore_char (&c->d_filename, d, off);
357 static unsigned char *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);
373 d = dump_int (-1, d, off);
375 d = dump_char (e->message_id, d, off);
376 d = dump_char (e->supersedes, d, off);
377 d = dump_char (e->date, d, off);
378 d = dump_char (e->x_label, d, off);
380 d = dump_list (e->references, d, off);
381 d = dump_list (e->in_reply_to, d, off);
382 d = dump_list (e->userhdrs, d, off);
387 static void 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;
408 restore_char (&e->message_id, d, off);
409 restore_char (&e->supersedes, d, off);
410 restore_char (&e->date, d, off);
411 restore_char (&e->x_label, d, off);
413 restore_list (&e->references, d, off);
414 restore_list (&e->in_reply_to, d, off);
415 restore_list (&e->userhdrs, d, off);
419 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
425 for (i = 0; i < 8; i++)
426 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
431 static int generate_crc32 ()
435 crc = crc32 (crc, (unsigned char const *)
436 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
438 ("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));
443 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
444 mutt_strlen ("HAVE_LANGINFO_CODESET"));
449 crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
450 mutt_strlen ("EXACT_ADDRESS"));
455 crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
460 crc32 (crc, (unsigned char const *) "MIXMASTER",
461 mutt_strlen ("MIXMASTER"));
466 crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
471 static int crc32_matches (const char *d, unsigned int crc)
473 int off = sizeof (validate);
474 unsigned int mycrc = 0;
480 restore_int (&mycrc, (unsigned char *) d, &off);
482 return (crc == mycrc);
485 /* Append md5sumed folder to path if path is a directory. */
486 static const char *mutt_hcache_per_folder (const char *path,
489 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
490 struct stat path_stat;
492 unsigned char md5sum[16];
495 ret = stat (path, &path_stat);
500 if (!S_ISDIR (path_stat.st_mode)) {
505 MD5Update (&md5, (unsigned char *) folder, strlen (folder));
506 MD5Final (md5sum, &md5);
508 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
509 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
510 "%02x%02x%02x%02x%02x%02x%02x%02x",
511 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
512 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
513 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
514 md5sum[13], md5sum[14], md5sum[15]);
520 return mutt_hcache_per_folder_path;
523 /* This function transforms a header into a char so that it is useable by
525 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
526 uint64_t uid_validity)
528 struct header_cache *db = _db;
529 unsigned char *d = NULL;
533 d = lazy_malloc (sizeof (validate));
536 memcpy (d, &uid_validity, sizeof (uint64_t));
541 gettimeofday (&now, NULL);
542 memcpy (d, &now, sizeof (struct timeval));
544 *off += sizeof (validate);
546 d = dump_int (db->crc, d, off);
548 lazy_realloc (&d, *off + sizeof (HEADER));
549 memcpy (d + *off, h, sizeof (HEADER));
550 *off += sizeof (HEADER);
552 d = dump_envelope (h->env, d, off);
553 d = dump_body (h->content, d, off);
554 d = dump_char (h->maildir_flags, d, off);
559 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
562 HEADER *h = mutt_new_header ();
565 off += sizeof (validate);
568 off += sizeof (unsigned int);
570 memcpy (h, d + off, sizeof (HEADER));
571 off += sizeof (HEADER);
573 h->env = mutt_new_envelope ();
574 restore_envelope (h->env, d, &off);
576 h->content = mutt_new_body ();
577 restore_body (h->content, d, &off);
579 restore_char (&h->maildir_flags, d, &off);
581 /* this is needed for maildir style mailboxes */
584 h->path = safe_strdup ((*oh)->path);
585 mutt_free_header (oh);
593 void *mutt_hcache_open (const char *path, const char *folder)
595 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
597 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
599 h->folder = safe_strdup (folder);
600 h->crc = generate_crc32 ();
602 if (!path || path[0] == '\0') {
608 path = mutt_hcache_per_folder (path, folder);
610 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
615 /* if rw failed try ro */
616 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
628 void mutt_hcache_close (void *db)
630 struct header_cache *h = db;
641 void *mutt_hcache_fetch (void *db, const char *filename,
642 size_t (*keylen) (const char *fn))
644 struct header_cache *h = db;
647 char path[_POSIX_PATH_MAX];
653 strncpy (path, h->folder, sizeof (path));
654 strncat (path, filename, sizeof (path) - mutt_strlen (path));
657 key.dsize = keylen (path);
659 data = gdbm_fetch (h->db, key);
661 if (!crc32_matches (data.dptr, h->crc)) {
670 mutt_hcache_store (void *db, const char *filename, HEADER * header,
671 uint64_t uid_validity, size_t (*keylen) (const char *fn))
673 struct header_cache *h = db;
676 char path[_POSIX_PATH_MAX];
683 strncpy (path, h->folder, sizeof (path));
684 strncat (path, filename, sizeof (path) - mutt_strlen (path));
687 key.dsize = keylen (path);
689 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
691 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
699 mutt_hcache_delete (void *db, const char *filename,
700 size_t (*keylen) (const char *fn))
703 struct header_cache *h = db;
704 char path[_POSIX_PATH_MAX];
710 strncpy (path, h->folder, sizeof (path));
711 strncat (path, filename, sizeof (path) - mutt_strlen (path));
714 key.dsize = keylen (path);
716 return gdbm_delete (h->db, key);
720 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
723 dbt->size = dbt->ulen = len;
724 dbt->dlen = dbt->doff = 0;
725 dbt->flags = DB_DBT_USERMEM;
728 static void mutt_hcache_dbt_empty_init (DBT * dbt)
731 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
735 void *mutt_hcache_open (const char *path, const char *folder)
738 u_int32_t createflags = DB_CREATE;
740 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
741 int pagesize = atoi (HeaderCachePageSize);
744 h->crc = generate_crc32 ();
746 if (!path || path[0] == '\0') {
751 path = mutt_hcache_per_folder (path, folder);
753 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
755 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
761 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
767 ret = db_env_create (&h->env, 0);
769 mx_unlock_file (h->lockfile, h->fd, 0);
776 h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
778 ret = db_create (&h->db, h->env, 0);
780 h->env->close (h->env, 0);
781 mx_unlock_file (h->lockfile, h->fd, 0);
788 if (stat (path, &sb) != 0 && errno == ENOENT) {
789 createflags |= DB_EXCL;
790 h->db->set_pagesize (h->db, pagesize);
793 ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
795 h->db->close (h->db, 0);
796 h->env->close (h->env, 0);
797 mx_unlock_file (h->lockfile, h->fd, 0);
806 void mutt_hcache_close (void *db)
808 struct header_cache *h = db;
815 h->db->close (h->db, 0);
816 h->env->close (h->env, 0);
817 mx_unlock_file (h->lockfile, h->fd, 0);
822 void *mutt_hcache_fetch (void *db, const char *filename,
823 size_t (*keylen) (const char *fn))
827 struct header_cache *h = db;
833 filename++; /* skip '/' */
835 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
836 mutt_hcache_dbt_empty_init (&data);
837 data.flags = DB_DBT_MALLOC;
839 h->db->get (h->db, NULL, &key, &data, 0);
841 if (!crc32_matches (data.data, h->crc)) {
850 mutt_hcache_store (void *db, const char *filename, HEADER * header,
851 uint64_t uid_validity, size_t (*keylen) (const char *fn))
856 struct header_cache *h = db;
862 filename++; /* skip '/' */
864 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
866 mutt_hcache_dbt_empty_init (&data);
867 data.flags = DB_DBT_USERMEM;
869 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
870 data.ulen = data.size;
872 ret = h->db->put (h->db, NULL, &key, &data, 0);
880 mutt_hcache_delete (void *db, const char *filename,
881 size_t (*keylen) (const char *fn))
884 struct header_cache *h = db;
890 filename++; /* skip '/' */
892 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
893 return h->db->del (h->db, NULL, &key, 0);
897 #endif /* USE_HCACHE */