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>
54 #include "lib/debug.h"
77 char lockfile[_POSIX_PATH_MAX];
82 struct timeval timeval;
83 unsigned long uid_validity;
86 static void *lazy_malloc (size_t siz)
88 if (0 < siz && siz < 4096) {
95 static void lazy_realloc (void *ptr, size_t siz)
97 void **p = (void **) ptr;
99 if (p != NULL && 0 < siz && siz < 4096) {
106 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
108 lazy_realloc (&d, *off + sizeof (int));
109 memcpy (d + *off, &i, sizeof (int));
110 (*off) += sizeof (int);
115 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
117 memcpy (i, d + *off, sizeof (int));
118 (*off) += sizeof (int);
121 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
127 d = dump_int (size, d, off);
131 size = m_strlen(c) + 1;
132 d = dump_int (size, d, off);
133 lazy_realloc (&d, *off + size);
134 memcpy (d + *off, c, size);
141 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
146 d = dump_int (size, d, off);
150 d = dump_int (size, d, off);
151 lazy_realloc (&d, *off + size);
152 memcpy (d + *off, c, size);
159 static void restore_char (char **c, const unsigned char *d, int *off)
163 restore_int (&size, d, off);
170 *c = p_dup(d + *off, size);
174 static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off)
176 unsigned int counter = 0;
177 unsigned int start_off = *off;
179 d = dump_int (0xdeadbeef, d, off);
182 d = dump_char (a->personal, d, off);
183 d = dump_char (a->mailbox, d, off);
184 d = dump_int (a->group, d, off);
189 memcpy (d + start_off, &counter, sizeof (int));
194 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
196 unsigned int counter;
198 restore_int (&counter, d, off);
201 *a = p_new(ADDRESS, 1);
202 restore_char (&(*a)->personal, d, off);
203 restore_char (&(*a)->mailbox, d, off);
204 restore_int ((unsigned int *) &(*a)->group, d, off);
212 static unsigned char *dump_list (LIST * l, unsigned char *d, int *off)
214 unsigned int counter = 0;
215 unsigned int start_off = *off;
217 d = dump_int (0xdeadbeef, d, off);
220 d = dump_char (l->data, d, off);
225 memcpy (d + start_off, &counter, sizeof (int));
230 static void restore_list (LIST ** l, const unsigned char *d, int *off)
232 unsigned int counter;
234 restore_int (&counter, d, off);
238 restore_char (&(*l)->data, d, off);
247 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
250 d = dump_int (0, d, off);
254 d = dump_int (1, d, off);
257 d = dump_char_size (b->data, d, off, b->dsize + 1);
258 d = dump_int (b->dptr - b->data, d, off);
259 d = dump_int (b->dsize, d, off);
260 d = dump_int (b->destroy, d, off);
265 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
270 restore_int (&used, d, off);
275 *b = p_new(BUFFER, 1);
277 restore_char (&(*b)->data, d, off);
278 restore_int (&offset, d, off);
279 (*b)->dptr = (*b)->data + offset;
280 restore_int (&(*b)->dsize, d, off);
281 restore_int ((unsigned int *) &(*b)->destroy, d, off);
285 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
288 unsigned int counter = 0;
289 unsigned int start_off = *off;
291 d = dump_int (0xdeadbeef, d, off);
294 d = dump_char (p->attribute, d, off);
295 d = dump_char (p->value, d, off);
300 memcpy (d + start_off, &counter, sizeof (int));
306 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
308 unsigned int counter;
310 restore_int (&counter, d, off);
313 *p = p_new(PARAMETER, 1);
314 restore_char (&(*p)->attribute, d, off);
315 restore_char (&(*p)->value, d, off);
323 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
325 lazy_realloc (&d, *off + sizeof (BODY));
326 memcpy (d + *off, c, sizeof (BODY));
327 *off += sizeof (BODY);
329 d = dump_char (c->xtype, d, off);
330 d = dump_char (c->subtype, d, off);
332 d = dump_parameter (c->parameter, d, off);
334 d = dump_char (c->description, d, off);
335 d = dump_char (c->form_name, d, off);
336 d = dump_char (c->filename, d, off);
337 d = dump_char (c->d_filename, d, off);
342 static void restore_body (BODY * c, const unsigned char *d, int *off)
344 memcpy (c, d + *off, sizeof (BODY));
345 *off += sizeof (BODY);
347 restore_char (&c->xtype, d, off);
348 restore_char (&c->subtype, d, off);
350 restore_parameter (&c->parameter, d, off);
352 restore_char (&c->description, d, off);
353 restore_char (&c->form_name, d, off);
354 restore_char (&c->filename, d, off);
355 restore_char (&c->d_filename, d, off);
358 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
360 d = dump_address (e->return_path, d, off);
361 d = dump_address (e->from, d, off);
362 d = dump_address (e->to, d, off);
363 d = dump_address (e->cc, d, off);
364 d = dump_address (e->bcc, d, off);
365 d = dump_address (e->sender, d, off);
366 d = dump_address (e->reply_to, d, off);
367 d = dump_address (e->mail_followup_to, d, off);
369 d = dump_char (e->subject, d, off);
371 d = dump_int (e->real_subj - e->subject, d, off);
374 d = dump_int (-1, d, off);
376 d = dump_char (e->message_id, d, off);
377 d = dump_char (e->supersedes, d, off);
378 d = dump_char (e->date, d, off);
379 d = dump_char (e->x_label, d, off);
380 d = dump_char (e->list_post, d, off);
383 d = dump_char (e->newsgroups, d, off);
384 d = dump_char (e->xref, d, off);
385 d = dump_char (e->followup_to, d, off);
386 d = dump_char (e->x_comment_to, d, off);
389 d = dump_list (e->references, d, off);
390 d = dump_list (e->in_reply_to, d, off);
391 d = dump_list (e->userhdrs, d, off);
396 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
400 restore_address (&e->return_path, d, off);
401 restore_address (&e->from, d, off);
402 restore_address (&e->to, d, off);
403 restore_address (&e->cc, d, off);
404 restore_address (&e->bcc, d, off);
405 restore_address (&e->sender, d, off);
406 restore_address (&e->reply_to, d, off);
407 restore_address (&e->mail_followup_to, d, off);
409 restore_char (&e->subject, d, off);
410 restore_int ((unsigned int *) (&real_subj_off), d, off);
411 if (0 <= real_subj_off) {
412 e->real_subj = e->subject + real_subj_off;
417 restore_char (&e->message_id, d, off);
418 restore_char (&e->supersedes, d, off);
419 restore_char (&e->date, d, off);
420 restore_char (&e->x_label, d, off);
421 restore_char (&e->list_post, d, off);
424 restore_char (&e->newsgroups, d, off);
425 restore_char (&e->xref, d, off);
426 restore_char (&e->followup_to, d, off);
427 restore_char (&e->x_comment_to, d, off);
430 restore_list (&e->references, d, off);
431 restore_list (&e->in_reply_to, d, off);
432 restore_list (&e->userhdrs, d, off);
436 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
442 for (i = 0; i < 8; i++)
443 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
448 static int generate_crc32 ()
452 crc = crc32 (crc, (unsigned char const *)
453 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
455 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
457 #if HAVE_LANGINFO_CODESET
458 crc = crc32 (crc, (unsigned char const *) Charset, m_strlen(Charset));
460 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
461 m_strlen("HAVE_LANGINFO_CODESET"));
466 crc32 (crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
471 crc32 (crc, (unsigned char const *) "MIXMASTER",
472 m_strlen("MIXMASTER"));
477 crc32 (crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
482 crc32 (crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
487 static int crc32_matches (const char *d, unsigned int crc)
489 int off = sizeof (validate);
490 unsigned int mycrc = 0;
496 restore_int (&mycrc, (unsigned char *) d, &off);
498 return (crc == mycrc);
501 /* Append md5sumed folder to path if path is a directory. */
502 static const char *mutt_hcache_per_folder (const char *path,
505 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
506 struct stat path_stat;
508 unsigned char md5sum[16];
511 ret = stat (path, &path_stat);
516 if (!S_ISDIR (path_stat.st_mode)) {
521 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
522 MD5Final (md5sum, &md5);
524 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
525 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
526 "%02x%02x%02x%02x%02x%02x%02x%02x",
527 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
528 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
529 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
530 md5sum[13], md5sum[14], md5sum[15]);
536 return mutt_hcache_per_folder_path;
539 /* This function transforms a header into a char so that it is useable by
541 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
542 unsigned long uid_validity)
544 struct header_cache *db = _db;
545 unsigned char *d = NULL;
549 d = lazy_malloc (sizeof (validate));
552 memcpy (d, &uid_validity, sizeof (unsigned long));
557 gettimeofday (&now, NULL);
558 memcpy (d, &now, sizeof (struct timeval));
560 *off += sizeof (validate);
562 d = dump_int (db->crc, d, off);
564 lazy_realloc (&d, *off + sizeof (HEADER));
565 memcpy (d + *off, h, sizeof (HEADER));
566 *off += sizeof (HEADER);
568 d = dump_envelope (h->env, d, off);
569 d = dump_body (h->content, d, off);
570 d = dump_char (h->maildir_flags, d, off);
575 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
578 HEADER *h = mutt_new_header ();
581 off += sizeof (validate);
584 off += sizeof (unsigned int);
586 memcpy (h, d + off, sizeof (HEADER));
587 off += sizeof (HEADER);
589 h->env = mutt_new_envelope ();
590 restore_envelope (h->env, d, &off);
592 h->content = mutt_new_body ();
593 restore_body (h->content, d, &off);
595 restore_char (&h->maildir_flags, d, &off);
597 /* this is needed for maildir style mailboxes */
600 h->path = m_strdup((*oh)->path);
601 mutt_free_header (oh);
609 mutt_hcache_open(const char *path, const char *folder)
611 struct header_cache *h = p_new(HEADER_CACHE, 1);
612 int flags = VL_OWRITER | VL_OCREAT;
614 h->folder = m_strdup(folder);
615 h->crc = generate_crc32();
617 if (!path || path[0] == '\0')
619 p_delete(&h->folder);
624 path = mutt_hcache_per_folder(path, folder);
626 if (option(OPTHCACHECOMPRESS))
629 h->db = vlopen(path, flags, VL_CMPLEX);
634 p_delete(&h->folder);
642 mutt_hcache_close(void *db)
644 struct header_cache *h = db;
650 p_delete(&h->folder);
655 mutt_hcache_fetch(void *db, const char *filename,
656 size_t(*keylen) (const char *fn))
658 struct header_cache *h = db;
659 char path[_POSIX_PATH_MAX];
666 strncpy(path, h->folder, sizeof (path));
667 str_cat(path, sizeof (path), filename);
669 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
671 data = vlget(h->db, path, ksize, NULL);
673 if (! crc32_matches(data, h->crc))
683 mutt_hcache_store(void *db, const char *filename, HEADER * header,
684 unsigned long uid_validity,
685 size_t(*keylen) (const char *fn))
687 struct header_cache *h = db;
688 char path[_POSIX_PATH_MAX];
696 strncpy(path, h->folder, sizeof (path));
697 str_cat(path, sizeof (path), filename);
699 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
701 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
703 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
711 mutt_hcache_delete(void *db, const char *filename,
712 size_t(*keylen) (const char *fn))
714 struct header_cache *h = db;
715 char path[_POSIX_PATH_MAX];
721 strncpy(path, h->folder, sizeof (path));
722 str_cat(path, sizeof (path), filename);
724 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
726 return vlout(h->db, path, ksize);
731 void *mutt_hcache_open (const char *path, const char *folder)
733 struct header_cache *h = p_new(HEADER_CACHE, 1);
735 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
737 h->folder = m_strdup(folder);
738 h->crc = generate_crc32 ();
740 if (!path || path[0] == '\0') {
741 p_delete(&h->folder);
746 path = mutt_hcache_per_folder (path, folder);
748 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
753 /* if rw failed try ro */
754 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
759 p_delete(&h->folder);
766 void mutt_hcache_close (void *db)
768 struct header_cache *h = db;
775 p_delete(&h->folder);
779 void *mutt_hcache_fetch (void *db, const char *filename,
780 size_t (*keylen) (const char *fn))
782 struct header_cache *h = db;
785 char path[_POSIX_PATH_MAX];
791 strncpy (path, h->folder, sizeof (path));
792 strncat (path, filename, sizeof (path) - m_strlen(path));
795 key.dsize = keylen (path);
797 data = gdbm_fetch (h->db, key);
799 if (!crc32_matches (data.dptr, h->crc)) {
800 p_delete(&data.dptr);
808 mutt_hcache_store (void *db, const char *filename, HEADER * header,
809 unsigned long uid_validity, size_t (*keylen) (const char *fn))
811 struct header_cache *h = db;
814 char path[_POSIX_PATH_MAX];
821 strncpy (path, h->folder, sizeof (path));
822 strncat (path, filename, sizeof (path) - m_strlen(path));
825 key.dsize = keylen (path);
827 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
829 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
831 p_delete(&data.dptr);
837 mutt_hcache_delete (void *db, const char *filename,
838 size_t (*keylen) (const char *fn))
841 struct header_cache *h = db;
842 char path[_POSIX_PATH_MAX];
848 strncpy (path, h->folder, sizeof (path));
849 strncat (path, filename, sizeof (path) - m_strlen(path));
852 key.dsize = keylen (path);
854 return gdbm_delete (h->db, key);
858 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
861 dbt->size = dbt->ulen = len;
862 dbt->dlen = dbt->doff = 0;
863 dbt->flags = DB_DBT_USERMEM;
866 static void mutt_hcache_dbt_empty_init (DBT * dbt)
869 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
873 void *mutt_hcache_open (const char *path, const char *folder)
876 u_int32_t createflags = DB_CREATE;
878 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
879 int pagesize = atoi (HeaderCachePageSize);
882 h->crc = generate_crc32 ();
884 if (!path || path[0] == '\0') {
889 path = mutt_hcache_per_folder (path, folder);
891 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
893 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
899 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
905 ret = db_env_create (&h->env, 0);
907 mx_unlock_file (h->lockfile, h->fd, 0);
914 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
916 ret = db_create (&h->db, h->env, 0);
918 h->env->close (h->env, 0);
919 mx_unlock_file (h->lockfile, h->fd, 0);
926 if (stat (path, &sb) != 0 && errno == ENOENT) {
927 createflags |= DB_EXCL;
928 h->db->set_pagesize (h->db, pagesize);
931 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
933 h->db->close (h->db, 0);
934 h->env->close (h->env, 0);
935 mx_unlock_file (h->lockfile, h->fd, 0);
944 void mutt_hcache_close (void *db)
946 struct header_cache *h = db;
952 h->db->close (h->db, 0);
953 h->env->close (h->env, 0);
954 mx_unlock_file (h->lockfile, h->fd, 0);
959 void *mutt_hcache_fetch (void *db, const char *filename,
960 size_t (*keylen) (const char *fn))
964 struct header_cache *h = db;
970 filename++; /* skip '/' */
972 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
973 mutt_hcache_dbt_empty_init (&data);
974 data.flags = DB_DBT_MALLOC;
976 h->db->get (h->db, NULL, &key, &data, 0);
978 if (!crc32_matches (data.data, h->crc)) {
979 p_delete(&data.data);
987 mutt_hcache_store (void *db, const char *filename, HEADER * header,
988 unsigned long uid_validity, size_t (*keylen) (const char *fn))
993 struct header_cache *h = db;
999 filename++; /* skip '/' */
1001 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1003 mutt_hcache_dbt_empty_init (&data);
1004 data.flags = DB_DBT_USERMEM;
1006 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1007 data.ulen = data.size;
1009 ret = h->db->put (h->db, NULL, &key, &data, 0);
1011 p_delete(&data.data);
1017 mutt_hcache_delete (void *db, const char *filename,
1018 size_t (*keylen) (const char *fn))
1021 struct header_cache *h = db;
1027 filename++; /* skip '/' */
1029 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1030 return h->db->del (h->db, NULL, &key, 0);
1034 #endif /* USE_HCACHE */