2 * Copyright notice from original mutt:
3 * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
4 * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
5 * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
13 # include <inttypes.h>
22 #endif /* HAVE_CONFIG_H */
60 char lockfile[_POSIX_PATH_MAX];
65 struct timeval timeval;
66 uint64_t uid_validity;
69 static void *lazy_malloc (size_t siz)
71 if (0 < siz && siz < 4096) {
75 return safe_malloc (siz);
78 static void lazy_realloc (void *ptr, size_t siz)
80 void **p = (void **) ptr;
82 if (p != NULL && 0 < siz && siz < 4096) {
86 safe_realloc (ptr, siz);
89 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
91 lazy_realloc (&d, *off + sizeof (int));
92 memcpy (d + *off, &i, sizeof (int));
93 (*off) += sizeof (int);
98 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
100 memcpy (i, d + *off, sizeof (int));
101 (*off) += sizeof (int);
104 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
110 d = dump_int (size, d, off);
114 size = mutt_strlen (c) + 1;
115 d = dump_int (size, d, off);
116 lazy_realloc (&d, *off + size);
117 memcpy (d + *off, c, size);
124 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
129 d = dump_int (size, d, off);
133 d = dump_int (size, d, off);
134 lazy_realloc (&d, *off + size);
135 memcpy (d + *off, c, size);
142 static void restore_char (char **c, const unsigned char *d, int *off)
146 restore_int (&size, d, off);
153 *c = safe_malloc (size);
154 memcpy (*c, d + *off, size);
158 static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off)
160 unsigned int counter = 0;
161 unsigned int start_off = *off;
163 d = dump_int (0xdeadbeef, d, off);
167 d = dump_char (a->val, d, off);
169 d = dump_char (a->personal, d, off);
170 d = dump_char (a->mailbox, d, off);
171 d = dump_int (a->group, d, off);
176 memcpy (d + start_off, &counter, sizeof (int));
181 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
183 unsigned int counter;
185 restore_int (&counter, d, off);
188 *a = safe_malloc (sizeof (ADDRESS));
190 restore_char (&(*a)->val, d, off);
192 restore_char (&(*a)->personal, d, off);
193 restore_char (&(*a)->mailbox, d, off);
194 restore_int ((unsigned int *) &(*a)->group, d, off);
202 static unsigned char *dump_list (LIST * l, unsigned char *d, int *off)
204 unsigned int counter = 0;
205 unsigned int start_off = *off;
207 d = dump_int (0xdeadbeef, d, off);
210 d = dump_char (l->data, d, off);
215 memcpy (d + start_off, &counter, sizeof (int));
220 static void restore_list (LIST ** l, const unsigned char *d, int *off)
222 unsigned int counter;
224 restore_int (&counter, d, off);
227 *l = safe_malloc (sizeof (LIST));
228 restore_char (&(*l)->data, d, off);
237 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
240 d = dump_int (0, d, off);
244 d = dump_int (1, d, off);
247 d = dump_char_size (b->data, d, off, b->dsize + 1);
248 d = dump_int (b->dptr - b->data, d, off);
249 d = dump_int (b->dsize, d, off);
250 d = dump_int (b->destroy, d, off);
255 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
260 restore_int (&used, d, off);
265 *b = safe_malloc (sizeof (BUFFER));
267 restore_char (&(*b)->data, d, off);
268 restore_int (&offset, d, off);
269 (*b)->dptr = (*b)->data + offset;
270 restore_int (&(*b)->dsize, d, off);
271 restore_int ((unsigned int *) &(*b)->destroy, d, off);
275 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
278 unsigned int counter = 0;
279 unsigned int start_off = *off;
281 d = dump_int (0xdeadbeef, d, off);
284 d = dump_char (p->attribute, d, off);
285 d = dump_char (p->value, d, off);
290 memcpy (d + start_off, &counter, sizeof (int));
296 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
298 unsigned int counter;
300 restore_int (&counter, d, off);
303 *p = safe_malloc (sizeof (PARAMETER));
304 restore_char (&(*p)->attribute, d, off);
305 restore_char (&(*p)->value, d, off);
313 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
315 lazy_realloc (&d, *off + sizeof (BODY));
316 memcpy (d + *off, c, sizeof (BODY));
317 *off += sizeof (BODY);
319 d = dump_char (c->xtype, d, off);
320 d = dump_char (c->subtype, d, off);
322 d = dump_parameter (c->parameter, d, off);
324 d = dump_char (c->description, d, off);
325 d = dump_char (c->form_name, d, off);
326 d = dump_char (c->filename, d, off);
327 d = dump_char (c->d_filename, d, off);
332 static void restore_body (BODY * c, const unsigned char *d, int *off)
334 memcpy (c, d + *off, sizeof (BODY));
335 *off += sizeof (BODY);
337 restore_char (&c->xtype, d, off);
338 restore_char (&c->subtype, d, off);
340 restore_parameter (&c->parameter, d, off);
342 restore_char (&c->description, d, off);
343 restore_char (&c->form_name, d, off);
344 restore_char (&c->filename, d, off);
345 restore_char (&c->d_filename, d, off);
348 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
350 d = dump_address (e->return_path, d, off);
351 d = dump_address (e->from, d, off);
352 d = dump_address (e->to, d, off);
353 d = dump_address (e->cc, d, off);
354 d = dump_address (e->bcc, d, off);
355 d = dump_address (e->sender, d, off);
356 d = dump_address (e->reply_to, d, off);
357 d = dump_address (e->mail_followup_to, d, off);
359 d = dump_char (e->subject, d, off);
361 d = dump_int (e->real_subj - e->subject, d, off);
364 d = dump_int (-1, d, off);
366 d = dump_char (e->message_id, d, off);
367 d = dump_char (e->supersedes, d, off);
368 d = dump_char (e->date, d, off);
369 d = dump_char (e->x_label, d, off);
371 d = dump_list (e->references, d, off);
372 d = dump_list (e->in_reply_to, d, off);
373 d = dump_list (e->userhdrs, d, off);
378 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
382 restore_address (&e->return_path, d, off);
383 restore_address (&e->from, d, off);
384 restore_address (&e->to, d, off);
385 restore_address (&e->cc, d, off);
386 restore_address (&e->bcc, d, off);
387 restore_address (&e->sender, d, off);
388 restore_address (&e->reply_to, d, off);
389 restore_address (&e->mail_followup_to, d, off);
391 restore_char (&e->subject, d, off);
392 restore_int ((unsigned int *) (&real_subj_off), d, off);
393 if (0 <= real_subj_off) {
394 e->real_subj = e->subject + real_subj_off;
399 restore_char (&e->message_id, d, off);
400 restore_char (&e->supersedes, d, off);
401 restore_char (&e->date, d, off);
402 restore_char (&e->x_label, d, off);
404 restore_list (&e->references, d, off);
405 restore_list (&e->in_reply_to, d, off);
406 restore_list (&e->userhdrs, d, off);
410 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
416 for (i = 0; i < 8; i++)
417 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
422 static int generate_crc32 ()
426 crc = crc32 (crc, (unsigned char const *)
427 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
429 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
431 #if HAVE_LANGINFO_CODESET
432 crc = crc32 (crc, (unsigned char const *) Charset, mutt_strlen (Charset));
434 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
435 mutt_strlen ("HAVE_LANGINFO_CODESET"));
440 crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
441 mutt_strlen ("EXACT_ADDRESS"));
446 crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
451 crc32 (crc, (unsigned char const *) "MIXMASTER",
452 mutt_strlen ("MIXMASTER"));
457 crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
462 static int crc32_matches (const char *d, unsigned int crc)
464 int off = sizeof (validate);
465 unsigned int mycrc = 0;
471 restore_int (&mycrc, (unsigned char *) d, &off);
473 return (crc == mycrc);
476 /* Append md5sumed folder to path if path is a directory. */
477 static const char *mutt_hcache_per_folder (const char *path,
480 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
481 struct stat path_stat;
483 unsigned char md5sum[16];
486 ret = stat (path, &path_stat);
491 if (!S_ISDIR (path_stat.st_mode)) {
496 MD5Update (&md5, (unsigned char *) folder, mutt_strlen (folder));
497 MD5Final (md5sum, &md5);
499 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
500 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
501 "%02x%02x%02x%02x%02x%02x%02x%02x",
502 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
503 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
504 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
505 md5sum[13], md5sum[14], md5sum[15]);
511 return mutt_hcache_per_folder_path;
514 /* This function transforms a header into a char so that it is useable by
516 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
517 uint64_t uid_validity)
519 struct header_cache *db = _db;
520 unsigned char *d = NULL;
524 d = lazy_malloc (sizeof (validate));
527 memcpy (d, &uid_validity, sizeof (uint64_t));
532 gettimeofday (&now, NULL);
533 memcpy (d, &now, sizeof (struct timeval));
535 *off += sizeof (validate);
537 d = dump_int (db->crc, d, off);
539 lazy_realloc (&d, *off + sizeof (HEADER));
540 memcpy (d + *off, h, sizeof (HEADER));
541 *off += sizeof (HEADER);
543 d = dump_envelope (h->env, d, off);
544 d = dump_body (h->content, d, off);
545 d = dump_char (h->maildir_flags, d, off);
550 HEADER *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);
584 void *mutt_hcache_open (const char *path, const char *folder)
586 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
588 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 void mutt_hcache_close (void *db)
621 struct header_cache *h = db;
632 void *mutt_hcache_fetch (void *db, const char *filename,
633 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,
662 uint64_t uid_validity, size_t (*keylen) (const char *fn))
664 struct header_cache *h = db;
667 char path[_POSIX_PATH_MAX];
674 strncpy (path, h->folder, sizeof (path));
675 strncat (path, filename, sizeof (path) - mutt_strlen (path));
678 key.dsize = keylen (path);
680 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
682 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
690 mutt_hcache_delete (void *db, const char *filename,
691 size_t (*keylen) (const char *fn))
694 struct header_cache *h = db;
695 char path[_POSIX_PATH_MAX];
701 strncpy (path, h->folder, sizeof (path));
702 strncat (path, filename, sizeof (path) - mutt_strlen (path));
705 key.dsize = keylen (path);
707 return gdbm_delete (h->db, key);
711 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
714 dbt->size = dbt->ulen = len;
715 dbt->dlen = dbt->doff = 0;
716 dbt->flags = DB_DBT_USERMEM;
719 static void mutt_hcache_dbt_empty_init (DBT * dbt)
722 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
726 void *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);
767 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);
797 void 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);
813 void *mutt_hcache_fetch (void *db, const char *filename,
814 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,
842 uint64_t 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;
860 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
861 data.ulen = data.size;
863 ret = h->db->put (h->db, NULL, &key, &data, 0);
871 mutt_hcache_delete (void *db, const char *filename,
872 size_t (*keylen) (const char *fn))
875 struct header_cache *h = db;
881 filename++; /* skip '/' */
883 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
884 return h->db->del (h->db, NULL, &key, 0);
888 #endif /* USE_HCACHE */