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>
55 #include "lib/debug.h"
78 char lockfile[_POSIX_PATH_MAX];
83 struct timeval timeval;
84 unsigned long uid_validity;
87 static void *lazy_malloc (size_t siz)
89 if (0 < siz && siz < 4096) {
96 static void lazy_realloc (void *ptr, size_t siz)
98 void **p = (void **) ptr;
100 if (p != NULL && 0 < siz && siz < 4096) {
107 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
109 lazy_realloc (&d, *off + sizeof (int));
110 memcpy (d + *off, &i, sizeof (int));
111 (*off) += sizeof (int);
116 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
118 memcpy (i, d + *off, sizeof (int));
119 (*off) += sizeof (int);
122 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
128 d = dump_int (size, d, off);
132 size = m_strlen(c) + 1;
133 d = dump_int (size, d, off);
134 lazy_realloc (&d, *off + size);
135 memcpy (d + *off, c, size);
142 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
147 d = dump_int (size, d, off);
151 d = dump_int (size, d, off);
152 lazy_realloc (&d, *off + size);
153 memcpy (d + *off, c, size);
160 static void restore_char (char **c, const unsigned char *d, int *off)
164 restore_int (&size, d, off);
171 *c = p_dup(d + *off, size);
175 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
177 unsigned int counter = 0;
178 unsigned int start_off = *off;
180 d = dump_int (0xdeadbeef, d, off);
183 d = dump_char (a->personal, d, off);
184 d = dump_char (a->mailbox, d, off);
185 d = dump_int (a->group, d, off);
190 memcpy (d + start_off, &counter, sizeof (int));
195 static void restore_address (address_t ** a, const unsigned char *d, int *off)
197 unsigned int counter;
199 restore_int (&counter, d, off);
202 *a = p_new(address_t, 1);
203 restore_char (&(*a)->personal, d, off);
204 restore_char (&(*a)->mailbox, d, off);
205 restore_int ((unsigned int *) &(*a)->group, d, off);
213 static unsigned char *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));
231 static void restore_list (LIST ** l, const unsigned char *d, int *off)
233 unsigned int counter;
235 restore_int (&counter, d, off);
239 restore_char (&(*l)->data, d, off);
248 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
251 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);
266 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
271 restore_int (&used, d, off);
276 *b = p_new(BUFFER, 1);
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);
286 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
289 unsigned int counter = 0;
290 unsigned int start_off = *off;
292 d = dump_int (0xdeadbeef, d, off);
295 d = dump_char (p->attribute, d, off);
296 d = dump_char (p->value, d, off);
301 memcpy (d + start_off, &counter, sizeof (int));
307 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
309 unsigned int counter;
311 restore_int (&counter, d, off);
314 *p = p_new(PARAMETER, 1);
315 restore_char (&(*p)->attribute, d, off);
316 restore_char (&(*p)->value, d, off);
324 static unsigned char *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);
343 static void restore_body (BODY * c, const unsigned char *d, int *off)
345 memcpy (c, d + *off, sizeof (BODY));
346 *off += sizeof (BODY);
348 restore_char (&c->xtype, d, off);
349 restore_char (&c->subtype, d, off);
351 restore_parameter (&c->parameter, d, off);
353 restore_char (&c->description, d, off);
354 restore_char (&c->form_name, d, off);
355 restore_char (&c->filename, d, off);
356 restore_char (&c->d_filename, d, off);
359 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
361 d = dump_address (e->return_path, d, off);
362 d = dump_address (e->from, d, off);
363 d = dump_address (e->to, d, off);
364 d = dump_address (e->cc, d, off);
365 d = dump_address (e->bcc, d, off);
366 d = dump_address (e->sender, d, off);
367 d = dump_address (e->reply_to, d, off);
368 d = dump_address (e->mail_followup_to, d, off);
370 d = dump_char (e->subject, d, off);
372 d = dump_int (e->real_subj - e->subject, d, off);
375 d = dump_int (-1, d, off);
377 d = dump_char (e->message_id, d, off);
378 d = dump_char (e->supersedes, d, off);
379 d = dump_char (e->date, d, off);
380 d = dump_char (e->x_label, d, off);
381 d = dump_char (e->list_post, d, off);
384 d = dump_char (e->newsgroups, d, off);
385 d = dump_char (e->xref, d, off);
386 d = dump_char (e->followup_to, d, off);
387 d = dump_char (e->x_comment_to, d, off);
390 d = dump_list (e->references, d, off);
391 d = dump_list (e->in_reply_to, d, off);
392 d = dump_list (e->userhdrs, d, off);
397 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
401 restore_address (&e->return_path, d, off);
402 restore_address (&e->from, d, off);
403 restore_address (&e->to, d, off);
404 restore_address (&e->cc, d, off);
405 restore_address (&e->bcc, d, off);
406 restore_address (&e->sender, d, off);
407 restore_address (&e->reply_to, d, off);
408 restore_address (&e->mail_followup_to, d, off);
410 restore_char (&e->subject, d, off);
411 restore_int ((unsigned int *) (&real_subj_off), d, off);
412 if (0 <= real_subj_off) {
413 e->real_subj = e->subject + real_subj_off;
418 restore_char (&e->message_id, d, off);
419 restore_char (&e->supersedes, d, off);
420 restore_char (&e->date, d, off);
421 restore_char (&e->x_label, d, off);
422 restore_char (&e->list_post, d, off);
425 restore_char (&e->newsgroups, d, off);
426 restore_char (&e->xref, d, off);
427 restore_char (&e->followup_to, d, off);
428 restore_char (&e->x_comment_to, d, off);
431 restore_list (&e->references, d, off);
432 restore_list (&e->in_reply_to, d, off);
433 restore_list (&e->userhdrs, d, off);
437 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
443 for (i = 0; i < 8; i++)
444 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
449 static int generate_crc32 ()
453 crc = crc32 (crc, (unsigned char const *)
454 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
456 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
458 #if HAVE_LANGINFO_CODESET
459 crc = crc32 (crc, (unsigned char const *) Charset, m_strlen(Charset));
461 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
462 m_strlen("HAVE_LANGINFO_CODESET"));
467 crc32 (crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
472 crc32 (crc, (unsigned char const *) "MIXMASTER",
473 m_strlen("MIXMASTER"));
478 crc32 (crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
483 crc32 (crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
488 static int crc32_matches (const char *d, unsigned int crc)
490 int off = sizeof (validate);
491 unsigned int mycrc = 0;
497 restore_int (&mycrc, (unsigned char *) d, &off);
499 return (crc == mycrc);
502 /* Append md5sumed folder to path if path is a directory. */
503 static const char *mutt_hcache_per_folder (const char *path,
506 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
507 struct stat path_stat;
509 unsigned char md5sum[16];
512 ret = stat (path, &path_stat);
517 if (!S_ISDIR (path_stat.st_mode)) {
522 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
523 MD5Final (md5sum, &md5);
525 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
526 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
527 "%02x%02x%02x%02x%02x%02x%02x%02x",
528 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
529 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
530 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
531 md5sum[13], md5sum[14], md5sum[15]);
537 return mutt_hcache_per_folder_path;
540 /* This function transforms a header into a char so that it is useable by
542 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
543 unsigned long uid_validity)
545 struct header_cache *db = _db;
546 unsigned char *d = NULL;
550 d = lazy_malloc (sizeof (validate));
553 memcpy (d, &uid_validity, sizeof (unsigned long));
558 gettimeofday (&now, NULL);
559 memcpy (d, &now, sizeof (struct timeval));
561 *off += sizeof (validate);
563 d = dump_int (db->crc, d, off);
565 lazy_realloc (&d, *off + sizeof (HEADER));
566 memcpy (d + *off, h, sizeof (HEADER));
567 *off += sizeof (HEADER);
569 d = dump_envelope (h->env, d, off);
570 d = dump_body (h->content, d, off);
571 d = dump_char (h->maildir_flags, d, off);
576 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
579 HEADER *h = mutt_new_header ();
582 off += sizeof (validate);
585 off += sizeof (unsigned int);
587 memcpy (h, d + off, sizeof (HEADER));
588 off += sizeof (HEADER);
590 h->env = mutt_new_envelope ();
591 restore_envelope (h->env, d, &off);
593 h->content = mutt_new_body ();
594 restore_body (h->content, d, &off);
596 restore_char (&h->maildir_flags, d, &off);
598 /* this is needed for maildir style mailboxes */
601 h->path = m_strdup((*oh)->path);
602 mutt_free_header (oh);
610 mutt_hcache_open(const char *path, const char *folder)
612 struct header_cache *h = p_new(HEADER_CACHE, 1);
613 int flags = VL_OWRITER | VL_OCREAT;
615 h->folder = m_strdup(folder);
616 h->crc = generate_crc32();
618 if (!path || path[0] == '\0')
620 p_delete(&h->folder);
625 path = mutt_hcache_per_folder(path, folder);
627 if (option(OPTHCACHECOMPRESS))
630 h->db = vlopen(path, flags, VL_CMPLEX);
635 p_delete(&h->folder);
643 mutt_hcache_close(void *db)
645 struct header_cache *h = db;
651 p_delete(&h->folder);
656 mutt_hcache_fetch(void *db, const char *filename,
657 size_t(*keylen) (const char *fn))
659 struct header_cache *h = db;
660 char path[_POSIX_PATH_MAX];
667 m_strcpy(path, sizeof(path), h->folder);
668 m_strcat(path, sizeof(path), filename);
670 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
672 data = vlget(h->db, path, ksize, NULL);
674 if (!crc32_matches(data, h->crc))
684 mutt_hcache_store(void *db, const char *filename, HEADER * header,
685 unsigned long uid_validity,
686 size_t(*keylen) (const char *fn))
688 struct header_cache *h = db;
689 char path[_POSIX_PATH_MAX];
697 m_strcpy(path, sizeof(path), h->folder);
698 m_strcat(path, sizeof(path), filename);
700 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
702 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
704 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
712 mutt_hcache_delete(void *db, const char *filename,
713 size_t(*keylen) (const char *fn))
715 struct header_cache *h = db;
716 char path[_POSIX_PATH_MAX];
722 m_strcpy(path, sizeof(path), h->folder);
723 m_strcat(path, sizeof(path), filename);
725 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
727 return vlout(h->db, path, ksize);
732 void *mutt_hcache_open (const char *path, const char *folder)
734 struct header_cache *h = p_new(HEADER_CACHE, 1);
736 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
738 h->folder = m_strdup(folder);
739 h->crc = generate_crc32 ();
741 if (!path || path[0] == '\0') {
742 p_delete(&h->folder);
747 path = mutt_hcache_per_folder (path, folder);
749 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
754 /* if rw failed try ro */
755 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
760 p_delete(&h->folder);
767 void mutt_hcache_close (void *db)
769 struct header_cache *h = db;
776 p_delete(&h->folder);
780 void *mutt_hcache_fetch (void *db, const char *filename,
781 size_t (*keylen) (const char *fn))
783 struct header_cache *h = db;
786 char path[_POSIX_PATH_MAX];
792 m_strcpy(path, sizeof(path), h->folder);
793 strncat (path, filename, sizeof (path) - m_strlen(path));
796 key.dsize = keylen (path);
798 data = gdbm_fetch (h->db, key);
800 if (!crc32_matches (data.dptr, h->crc)) {
801 p_delete(&data.dptr);
809 mutt_hcache_store (void *db, const char *filename, HEADER * header,
810 unsigned long uid_validity, size_t (*keylen) (const char *fn))
812 struct header_cache *h = db;
815 char path[_POSIX_PATH_MAX];
822 m_strcpy(path, sizeof(path), h->folder);
823 strncat (path, filename, sizeof (path) - m_strlen(path));
826 key.dsize = keylen (path);
828 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
830 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
832 p_delete(&data.dptr);
838 mutt_hcache_delete (void *db, const char *filename,
839 size_t (*keylen) (const char *fn))
842 struct header_cache *h = db;
843 char path[_POSIX_PATH_MAX];
849 m_strcpy(path, sizeof(path), h->folder);
850 strncat (path, filename, sizeof (path) - m_strlen(path));
853 key.dsize = keylen (path);
855 return gdbm_delete (h->db, key);
859 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
862 dbt->size = dbt->ulen = len;
863 dbt->dlen = dbt->doff = 0;
864 dbt->flags = DB_DBT_USERMEM;
867 static void mutt_hcache_dbt_empty_init (DBT * dbt)
870 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
874 void *mutt_hcache_open (const char *path, const char *folder)
877 u_int32_t createflags = DB_CREATE;
879 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
880 int pagesize = atoi (HeaderCachePageSize);
883 h->crc = generate_crc32 ();
885 if (!path || path[0] == '\0') {
890 path = mutt_hcache_per_folder (path, folder);
892 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
894 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
900 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
906 ret = db_env_create (&h->env, 0);
908 mx_unlock_file (h->lockfile, h->fd, 0);
915 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
917 ret = db_create (&h->db, h->env, 0);
919 h->env->close (h->env, 0);
920 mx_unlock_file (h->lockfile, h->fd, 0);
927 if (stat (path, &sb) != 0 && errno == ENOENT) {
928 createflags |= DB_EXCL;
929 h->db->set_pagesize (h->db, pagesize);
932 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
934 h->db->close (h->db, 0);
935 h->env->close (h->env, 0);
936 mx_unlock_file (h->lockfile, h->fd, 0);
945 void mutt_hcache_close (void *db)
947 struct header_cache *h = db;
953 h->db->close (h->db, 0);
954 h->env->close (h->env, 0);
955 mx_unlock_file (h->lockfile, h->fd, 0);
960 void *mutt_hcache_fetch (void *db, const char *filename,
961 size_t (*keylen) (const char *fn))
965 struct header_cache *h = db;
971 filename++; /* skip '/' */
973 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
974 mutt_hcache_dbt_empty_init (&data);
975 data.flags = DB_DBT_MALLOC;
977 h->db->get (h->db, NULL, &key, &data, 0);
979 if (!crc32_matches (data.data, h->crc)) {
980 p_delete(&data.data);
988 mutt_hcache_store (void *db, const char *filename, HEADER * header,
989 unsigned long uid_validity, size_t (*keylen) (const char *fn))
994 struct header_cache *h = db;
1000 filename++; /* skip '/' */
1002 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1004 mutt_hcache_dbt_empty_init (&data);
1005 data.flags = DB_DBT_USERMEM;
1007 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1008 data.ulen = data.size;
1010 ret = h->db->put (h->db, NULL, &key, &data, 0);
1012 p_delete(&data.data);
1018 mutt_hcache_delete (void *db, const char *filename,
1019 size_t (*keylen) (const char *fn))
1022 struct header_cache *h = db;
1028 filename++; /* skip '/' */
1030 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1031 return h->db->del (h->db, NULL, &key, 0);
1035 #endif /* USE_HCACHE */