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 #endif /* HAVE_CONFIG_H */
17 #define MUTTNG_HCACHE_ID "0x004"
20 # include <inttypes.h>
43 #include <lib-lib/mem.h>
44 #include <lib-hash/hash.h>
46 #include <lib-mime/mime.h>
49 #include <imap/message.h>
53 #include "lib/debug.h"
76 char lockfile[_POSIX_PATH_MAX];
81 struct timeval timeval;
82 unsigned long uid_validity;
85 static void *lazy_malloc (size_t siz)
87 if (0 < siz && siz < 4096) {
94 static void lazy_realloc (void *ptr, size_t siz)
96 void **p = (void **) ptr;
98 if (p != NULL && 0 < siz && siz < 4096) {
105 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
107 lazy_realloc (&d, *off + sizeof (int));
108 memcpy (d + *off, &i, sizeof (int));
109 (*off) += sizeof (int);
114 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
116 memcpy (i, d + *off, sizeof (int));
117 (*off) += sizeof (int);
120 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
126 d = dump_int (size, d, off);
130 size = m_strlen(c) + 1;
131 d = dump_int (size, d, off);
132 lazy_realloc (&d, *off + size);
133 memcpy (d + *off, c, size);
140 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
145 d = dump_int (size, d, off);
149 d = dump_int (size, d, off);
150 lazy_realloc (&d, *off + size);
151 memcpy (d + *off, c, size);
158 static void restore_char (char **c, const unsigned char *d, int *off)
162 restore_int (&size, d, off);
169 *c = p_dup(d + *off, size);
173 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
175 unsigned int counter = 0;
176 unsigned int start_off = *off;
178 d = dump_int (0xdeadbeef, d, off);
181 d = dump_char (a->personal, d, off);
182 d = dump_char (a->mailbox, d, off);
183 d = dump_int (a->group, d, off);
188 memcpy (d + start_off, &counter, sizeof (int));
193 static void restore_address (address_t ** a, const unsigned char *d, int *off)
195 unsigned int counter;
197 restore_int (&counter, d, off);
200 *a = p_new(address_t, 1);
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);
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 = p_new(BUFFER, 1);
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 = p_new(PARAMETER, 1);
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);
379 d = dump_char (e->list_post, d, off);
382 d = dump_char (e->newsgroups, d, off);
383 d = dump_char (e->xref, d, off);
384 d = dump_char (e->followup_to, d, off);
385 d = dump_char (e->x_comment_to, d, off);
388 d = dump_list (e->references, d, off);
389 d = dump_list (e->in_reply_to, d, off);
390 d = dump_list (e->userhdrs, d, off);
395 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
399 restore_address (&e->return_path, d, off);
400 restore_address (&e->from, d, off);
401 restore_address (&e->to, d, off);
402 restore_address (&e->cc, d, off);
403 restore_address (&e->bcc, d, off);
404 restore_address (&e->sender, d, off);
405 restore_address (&e->reply_to, d, off);
406 restore_address (&e->mail_followup_to, d, off);
408 restore_char (&e->subject, d, off);
409 restore_int ((unsigned int *) (&real_subj_off), d, off);
410 if (0 <= real_subj_off) {
411 e->real_subj = e->subject + real_subj_off;
416 restore_char (&e->message_id, d, off);
417 restore_char (&e->supersedes, d, off);
418 restore_char (&e->date, d, off);
419 restore_char (&e->x_label, d, off);
420 restore_char (&e->list_post, d, off);
423 restore_char (&e->newsgroups, d, off);
424 restore_char (&e->xref, d, off);
425 restore_char (&e->followup_to, d, off);
426 restore_char (&e->x_comment_to, d, off);
429 restore_list (&e->references, d, off);
430 restore_list (&e->in_reply_to, d, off);
431 restore_list (&e->userhdrs, d, off);
435 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
441 for (i = 0; i < 8; i++)
442 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
447 static int generate_crc32 ()
451 crc = crc32 (crc, (unsigned char const *)
452 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
454 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
456 #if HAVE_LANGINFO_CODESET
457 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
458 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
459 m_strlen("HAVE_LANGINFO_CODESET"));
462 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
465 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
466 m_strlen("MIXMASTER"));
469 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
472 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
477 static int crc32_matches (const char *d, unsigned int crc)
479 int off = sizeof (validate);
480 unsigned int mycrc = 0;
486 restore_int (&mycrc, (unsigned char *) d, &off);
488 return (crc == mycrc);
491 /* Append md5sumed folder to path if path is a directory. */
492 static const char *mutt_hcache_per_folder (const char *path,
495 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
496 struct stat path_stat;
498 unsigned char md5sum[16];
501 ret = stat (path, &path_stat);
506 if (!S_ISDIR (path_stat.st_mode)) {
511 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
512 MD5Final (md5sum, &md5);
514 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
515 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
516 "%02x%02x%02x%02x%02x%02x%02x%02x",
517 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
518 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
519 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
520 md5sum[13], md5sum[14], md5sum[15]);
526 return mutt_hcache_per_folder_path;
529 /* This function transforms a header into a char so that it is useable by
531 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
532 unsigned long uid_validity)
534 struct header_cache *db = _db;
535 unsigned char *d = NULL;
539 d = lazy_malloc (sizeof (validate));
542 memcpy (d, &uid_validity, sizeof (unsigned long));
547 gettimeofday (&now, NULL);
548 memcpy (d, &now, sizeof (struct timeval));
550 *off += sizeof (validate);
552 d = dump_int (db->crc, d, off);
554 lazy_realloc (&d, *off + sizeof (HEADER));
555 memcpy (d + *off, h, sizeof (HEADER));
556 *off += sizeof (HEADER);
558 d = dump_envelope (h->env, d, off);
559 d = dump_body (h->content, d, off);
560 d = dump_char (h->maildir_flags, d, off);
565 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
568 HEADER *h = mutt_new_header ();
571 off += sizeof (validate);
574 off += sizeof (unsigned int);
576 memcpy (h, d + off, sizeof (HEADER));
577 off += sizeof (HEADER);
579 h->env = mutt_new_envelope ();
580 restore_envelope (h->env, d, &off);
582 h->content = mutt_new_body ();
583 restore_body (h->content, d, &off);
585 restore_char (&h->maildir_flags, d, &off);
587 /* this is needed for maildir style mailboxes */
590 h->path = m_strdup((*oh)->path);
591 mutt_free_header (oh);
599 mutt_hcache_open(const char *path, const char *folder)
601 struct header_cache *h = p_new(HEADER_CACHE, 1);
602 int flags = VL_OWRITER | VL_OCREAT;
604 h->folder = m_strdup(folder);
605 h->crc = generate_crc32();
607 if (!path || path[0] == '\0')
609 p_delete(&h->folder);
614 path = mutt_hcache_per_folder(path, folder);
616 if (option(OPTHCACHECOMPRESS))
619 h->db = vlopen(path, flags, VL_CMPLEX);
624 p_delete(&h->folder);
632 mutt_hcache_close(void *db)
634 struct header_cache *h = db;
640 p_delete(&h->folder);
645 mutt_hcache_fetch(void *db, const char *filename,
646 size_t(*keylen) (const char *fn))
648 struct header_cache *h = db;
649 char path[_POSIX_PATH_MAX];
656 m_strcpy(path, sizeof(path), h->folder);
657 m_strcat(path, sizeof(path), filename);
659 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
661 data = vlget(h->db, path, ksize, NULL);
663 if (!crc32_matches(data, h->crc))
673 mutt_hcache_store(void *db, const char *filename, HEADER * header,
674 unsigned long uid_validity,
675 size_t(*keylen) (const char *fn))
677 struct header_cache *h = db;
678 char path[_POSIX_PATH_MAX];
686 m_strcpy(path, sizeof(path), h->folder);
687 m_strcat(path, sizeof(path), filename);
689 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
691 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
693 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
701 mutt_hcache_delete(void *db, const char *filename,
702 size_t(*keylen) (const char *fn))
704 struct header_cache *h = db;
705 char path[_POSIX_PATH_MAX];
711 m_strcpy(path, sizeof(path), h->folder);
712 m_strcat(path, sizeof(path), filename);
714 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
716 return vlout(h->db, path, ksize);
721 void *mutt_hcache_open (const char *path, const char *folder)
723 struct header_cache *h = p_new(HEADER_CACHE, 1);
725 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
727 h->folder = m_strdup(folder);
728 h->crc = generate_crc32 ();
730 if (!path || path[0] == '\0') {
731 p_delete(&h->folder);
736 path = mutt_hcache_per_folder (path, folder);
738 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
743 /* if rw failed try ro */
744 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
749 p_delete(&h->folder);
756 void mutt_hcache_close (void *db)
758 struct header_cache *h = db;
765 p_delete(&h->folder);
769 void *mutt_hcache_fetch (void *db, const char *filename,
770 size_t (*keylen) (const char *fn))
772 struct header_cache *h = db;
775 char path[_POSIX_PATH_MAX];
781 m_strcpy(path, sizeof(path), h->folder);
782 strncat (path, filename, sizeof (path) - m_strlen(path));
785 key.dsize = keylen (path);
787 data = gdbm_fetch (h->db, key);
789 if (!crc32_matches (data.dptr, h->crc)) {
790 p_delete(&data.dptr);
798 mutt_hcache_store (void *db, const char *filename, HEADER * header,
799 unsigned long uid_validity, size_t (*keylen) (const char *fn))
801 struct header_cache *h = db;
804 char path[_POSIX_PATH_MAX];
811 m_strcpy(path, sizeof(path), h->folder);
812 strncat (path, filename, sizeof (path) - m_strlen(path));
815 key.dsize = keylen (path);
817 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
819 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
821 p_delete(&data.dptr);
827 mutt_hcache_delete (void *db, const char *filename,
828 size_t (*keylen) (const char *fn))
831 struct header_cache *h = db;
832 char path[_POSIX_PATH_MAX];
838 m_strcpy(path, sizeof(path), h->folder);
839 strncat (path, filename, sizeof (path) - m_strlen(path));
842 key.dsize = keylen (path);
844 return gdbm_delete (h->db, key);
848 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
851 dbt->size = dbt->ulen = len;
852 dbt->dlen = dbt->doff = 0;
853 dbt->flags = DB_DBT_USERMEM;
856 static void mutt_hcache_dbt_empty_init (DBT * dbt)
859 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
863 void *mutt_hcache_open (const char *path, const char *folder)
866 u_int32_t createflags = DB_CREATE;
868 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
869 int pagesize = atoi (HeaderCachePageSize);
872 h->crc = generate_crc32 ();
874 if (!path || path[0] == '\0') {
879 path = mutt_hcache_per_folder (path, folder);
881 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
883 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
889 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
895 ret = db_env_create (&h->env, 0);
897 mx_unlock_file (h->lockfile, h->fd, 0);
904 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
906 ret = db_create (&h->db, h->env, 0);
908 h->env->close (h->env, 0);
909 mx_unlock_file (h->lockfile, h->fd, 0);
916 if (stat (path, &sb) != 0 && errno == ENOENT) {
917 createflags |= DB_EXCL;
918 h->db->set_pagesize (h->db, pagesize);
921 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
923 h->db->close (h->db, 0);
924 h->env->close (h->env, 0);
925 mx_unlock_file (h->lockfile, h->fd, 0);
934 void mutt_hcache_close (void *db)
936 struct header_cache *h = db;
942 h->db->close (h->db, 0);
943 h->env->close (h->env, 0);
944 mx_unlock_file (h->lockfile, h->fd, 0);
949 void *mutt_hcache_fetch (void *db, const char *filename,
950 size_t (*keylen) (const char *fn))
954 struct header_cache *h = db;
960 filename++; /* skip '/' */
962 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
963 mutt_hcache_dbt_empty_init (&data);
964 data.flags = DB_DBT_MALLOC;
966 h->db->get (h->db, NULL, &key, &data, 0);
968 if (!crc32_matches (data.data, h->crc)) {
969 p_delete(&data.data);
977 mutt_hcache_store (void *db, const char *filename, HEADER * header,
978 unsigned long uid_validity, size_t (*keylen) (const char *fn))
983 struct header_cache *h = db;
989 filename++; /* skip '/' */
991 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
993 mutt_hcache_dbt_empty_init (&data);
994 data.flags = DB_DBT_USERMEM;
996 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
997 data.ulen = data.size;
999 ret = h->db->put (h->db, NULL, &key, &data, 0);
1001 p_delete(&data.data);
1007 mutt_hcache_delete (void *db, const char *filename,
1008 size_t (*keylen) (const char *fn))
1011 struct header_cache *h = db;
1017 filename++; /* skip '/' */
1019 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1020 return h->db->del (h->db, NULL, &key, 0);
1024 #endif /* USE_HCACHE */