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"
19 # ifdef HAVE_INTTYPES_H
20 # include <inttypes.h>
27 #if defined(HAVE_QDBM)
31 #elif defined(HAVE_GDBM)
33 #elif defined(HAVE_DB4)
39 #ifdef HAVE_SYS_TIME_H
43 #include <lib-lib/mem.h>
44 #include <lib-lib/macros.h>
45 #include <lib-hash/hash.h>
47 #include <lib-mime/mime.h>
51 #include <imap/message.h>
56 #if defined(HAVE_QDBM)
60 #elif defined(HAVE_GDBM)
64 #elif defined(HAVE_DB4)
69 char lockfile[_POSIX_PATH_MAX];
74 struct timeval timeval;
75 unsigned long uid_validity;
78 #define UPPER4K(i) ((i & ~(4096 - 1)) + 4096)
80 static unsigned char *lazy_malloc(ssize_t siz)
82 return p_new(unsigned char, UPPER4K(siz));
85 static void lazy_realloc(unsigned char **p, ssize_t siz)
87 p_realloc(p, UPPER4K(siz));
90 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
92 lazy_realloc (&d, *off + sizeof (int));
93 memcpy (d + *off, &i, sizeof (int));
94 (*off) += sizeof (int);
99 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
101 memcpy (i, d + *off, sizeof (int));
102 (*off) += sizeof (int);
105 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
111 d = dump_int (size, d, off);
115 size = m_strlen(c) + 1;
116 d = dump_int (size, d, off);
117 lazy_realloc (&d, *off + size);
118 memcpy (d + *off, c, size);
124 static void restore_char (char **c, const unsigned char *d, int *off)
128 restore_int (&size, d, off);
135 *c = p_dup(d + *off, size);
139 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
141 unsigned int counter = 0;
142 unsigned int start_off = *off;
144 d = dump_int (0xdeadbeef, d, off);
147 d = dump_char (a->personal, d, off);
148 d = dump_char (a->mailbox, d, off);
149 d = dump_int (a->group, d, off);
154 memcpy (d + start_off, &counter, sizeof (int));
159 static void restore_address (address_t ** a, const unsigned char *d, int *off)
161 unsigned int counter;
163 restore_int (&counter, d, off);
166 *a = p_new(address_t, 1);
167 restore_char (&(*a)->personal, d, off);
168 restore_char (&(*a)->mailbox, d, off);
169 restore_int ((unsigned int *) &(*a)->group, d, off);
177 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
179 unsigned int counter = 0;
180 unsigned int start_off = *off;
182 d = dump_int (0xdeadbeef, d, off);
185 d = dump_char (l->data, d, off);
190 memcpy (d + start_off, &counter, sizeof (int));
195 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
197 unsigned int counter;
199 restore_int (&counter, d, off);
202 *l = p_new(string_list_t, 1);
203 restore_char (&(*l)->data, d, off);
211 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
214 unsigned int counter = 0;
215 unsigned int start_off = *off;
217 d = dump_int (0xdeadbeef, d, off);
220 d = dump_char (p->attribute, d, off);
221 d = dump_char (p->value, d, off);
226 memcpy (d + start_off, &counter, sizeof (int));
232 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
234 unsigned int counter;
236 restore_int (&counter, d, off);
239 *p = p_new(PARAMETER, 1);
240 restore_char (&(*p)->attribute, d, off);
241 restore_char (&(*p)->value, d, off);
249 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
251 lazy_realloc (&d, *off + sizeof (BODY));
252 memcpy (d + *off, c, sizeof (BODY));
253 *off += sizeof (BODY);
255 d = dump_char (c->xtype, d, off);
256 d = dump_char (c->subtype, d, off);
258 d = dump_parameter (c->parameter, d, off);
260 d = dump_char (c->description, d, off);
261 d = dump_char (c->form_name, d, off);
262 d = dump_char (c->filename, d, off);
263 d = dump_char (c->d_filename, d, off);
268 static void restore_body (BODY * c, const unsigned char *d, int *off)
270 memcpy (c, d + *off, sizeof (BODY));
271 *off += sizeof (BODY);
273 restore_char (&c->xtype, d, off);
274 restore_char (&c->subtype, d, off);
276 restore_parameter (&c->parameter, d, off);
278 restore_char (&c->description, d, off);
279 restore_char (&c->form_name, d, off);
280 restore_char (&c->filename, d, off);
281 restore_char (&c->d_filename, d, off);
284 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
286 d = dump_address (e->return_path, d, off);
287 d = dump_address (e->from, d, off);
288 d = dump_address (e->to, d, off);
289 d = dump_address (e->cc, d, off);
290 d = dump_address (e->bcc, d, off);
291 d = dump_address (e->sender, d, off);
292 d = dump_address (e->reply_to, d, off);
293 d = dump_address (e->mail_followup_to, d, off);
295 d = dump_char (e->subject, d, off);
297 d = dump_int (e->real_subj - e->subject, d, off);
300 d = dump_int (-1, d, off);
302 d = dump_char (e->message_id, d, off);
303 d = dump_char (e->supersedes, d, off);
304 d = dump_char (e->date, d, off);
305 d = dump_char (e->x_label, d, off);
306 d = dump_char (e->list_post, d, off);
309 d = dump_char (e->newsgroups, d, off);
310 d = dump_char (e->xref, d, off);
311 d = dump_char (e->followup_to, d, off);
312 d = dump_char (e->x_comment_to, d, off);
315 d = dump_list (e->references, d, off);
316 d = dump_list (e->in_reply_to, d, off);
317 d = dump_list (e->userhdrs, d, off);
322 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
326 restore_address (&e->return_path, d, off);
327 restore_address (&e->from, d, off);
328 restore_address (&e->to, d, off);
329 restore_address (&e->cc, d, off);
330 restore_address (&e->bcc, d, off);
331 restore_address (&e->sender, d, off);
332 restore_address (&e->reply_to, d, off);
333 restore_address (&e->mail_followup_to, d, off);
335 restore_char (&e->subject, d, off);
336 restore_int ((unsigned int *) (&real_subj_off), d, off);
337 if (0 <= real_subj_off) {
338 e->real_subj = e->subject + real_subj_off;
343 restore_char (&e->message_id, d, off);
344 restore_char (&e->supersedes, d, off);
345 restore_char (&e->date, d, off);
346 restore_char (&e->x_label, d, off);
347 restore_char (&e->list_post, d, off);
350 restore_char (&e->newsgroups, d, off);
351 restore_char (&e->xref, d, off);
352 restore_char (&e->followup_to, d, off);
353 restore_char (&e->x_comment_to, d, off);
356 restore_list (&e->references, d, off);
357 restore_list (&e->in_reply_to, d, off);
358 restore_list (&e->userhdrs, d, off);
362 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
368 for (i = 0; i < 8; i++)
369 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
374 static int generate_crc32 ()
378 crc = crc32 (crc, (unsigned char const *)
379 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
381 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
383 #ifdef HAVE_LANGINFO_CODESET
384 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
385 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
386 m_strlen("HAVE_LANGINFO_CODESET"));
389 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
392 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
393 m_strlen("MIXMASTER"));
396 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
399 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
404 static int crc32_matches (const char *d, unsigned int crc)
406 int off = sizeof (validate);
407 unsigned int mycrc = 0;
413 restore_int (&mycrc, (unsigned char *) d, &off);
415 return (crc == mycrc);
418 /* Append md5sumed folder to path if path is a directory. */
419 static const char *mutt_hcache_per_folder (const char *path,
422 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
423 struct stat path_stat;
425 unsigned char md5sum[16];
428 ret = stat (path, &path_stat);
433 if (!S_ISDIR (path_stat.st_mode)) {
438 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
439 MD5Final (md5sum, &md5);
441 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
442 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
443 "%02x%02x%02x%02x%02x%02x%02x%02x",
444 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
445 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
446 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
447 md5sum[13], md5sum[14], md5sum[15]);
453 return mutt_hcache_per_folder_path;
456 /* This function transforms a header into a char so that it is useable by
458 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
459 unsigned long uid_validity)
461 struct header_cache *db = _db;
462 unsigned char *d = NULL;
466 d = lazy_malloc (sizeof (validate));
469 memcpy (d, &uid_validity, sizeof (unsigned long));
474 gettimeofday (&now, NULL);
475 memcpy (d, &now, sizeof (struct timeval));
477 *off += sizeof (validate);
479 d = dump_int (db->crc, d, off);
481 lazy_realloc (&d, *off + sizeof (HEADER));
482 memcpy (d + *off, h, sizeof (HEADER));
483 *off += sizeof (HEADER);
485 d = dump_envelope (h->env, d, off);
486 d = dump_body (h->content, d, off);
487 d = dump_char (h->maildir_flags, d, off);
492 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
495 HEADER *h = header_new();
498 off += sizeof (validate);
501 off += sizeof (unsigned int);
503 memcpy (h, d + off, sizeof (HEADER));
504 off += sizeof (HEADER);
506 h->env = envelope_new();
507 restore_envelope (h->env, d, &off);
509 h->content = mutt_new_body ();
510 restore_body (h->content, d, &off);
512 restore_char (&h->maildir_flags, d, &off);
514 /* this is needed for maildir style mailboxes */
517 h->path = m_strdup((*oh)->path);
524 #if defined(HAVE_QDBM)
526 mutt_hcache_open(const char *path, const char *folder)
528 struct header_cache *h = p_new(struct header_cache, 1);
529 int flags = VL_OWRITER | VL_OCREAT;
531 h->folder = m_strdup(folder);
532 h->crc = generate_crc32();
534 if (!path || path[0] == '\0')
536 p_delete(&h->folder);
541 path = mutt_hcache_per_folder(path, folder);
543 if (option(OPTHCACHECOMPRESS))
546 h->db = vlopen(path, flags, VL_CMPLEX);
551 p_delete(&h->folder);
559 mutt_hcache_close(void *db)
561 struct header_cache *h = db;
567 p_delete(&h->folder);
572 mutt_hcache_fetch(void *db, const char *filename,
573 ssize_t(*keylen) (const char *fn))
575 struct header_cache *h = db;
576 char path[_POSIX_PATH_MAX];
583 m_strcpy(path, sizeof(path), h->folder);
584 m_strcat(path, sizeof(path), filename);
586 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
588 data = vlget(h->db, path, ksize, NULL);
590 if (!crc32_matches(data, h->crc))
600 mutt_hcache_store(void *db, const char *filename, HEADER * header,
601 unsigned long uid_validity,
602 ssize_t(*keylen) (const char *fn))
604 struct header_cache *h = db;
605 char path[_POSIX_PATH_MAX];
613 m_strcpy(path, sizeof(path), h->folder);
614 m_strcat(path, sizeof(path), filename);
616 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
618 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
620 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
628 mutt_hcache_delete(void *db, const char *filename,
629 ssize_t(*keylen) (const char *fn))
631 struct header_cache *h = db;
632 char path[_POSIX_PATH_MAX];
638 m_strcpy(path, sizeof(path), h->folder);
639 m_strcat(path, sizeof(path), filename);
641 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
643 return vlout(h->db, path, ksize);
646 #elif defined(HAVE_GDBM)
648 void *mutt_hcache_open (const char *path, const char *folder)
650 struct header_cache *h = p_new(struct header_cache, 1);
652 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
654 h->folder = m_strdup(folder);
655 h->crc = generate_crc32 ();
657 if (!path || path[0] == '\0') {
658 p_delete(&h->folder);
663 path = mutt_hcache_per_folder (path, folder);
665 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
670 /* if rw failed try ro */
671 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
676 p_delete(&h->folder);
683 void mutt_hcache_close (void *db)
685 struct header_cache *h = db;
692 p_delete(&h->folder);
696 void *mutt_hcache_fetch (void *db, const char *filename,
697 ssize_t (*keylen) (const char *fn))
699 struct header_cache *h = db;
702 char path[_POSIX_PATH_MAX];
708 m_strcpy(path, sizeof(path), h->folder);
709 strncat (path, filename, sizeof (path) - m_strlen(path));
712 key.dsize = keylen (path);
714 data = gdbm_fetch (h->db, key);
716 if (!crc32_matches (data.dptr, h->crc)) {
717 p_delete(&data.dptr);
725 mutt_hcache_store (void *db, const char *filename, HEADER * header,
726 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
728 struct header_cache *h = db;
731 char path[_POSIX_PATH_MAX];
738 m_strcpy(path, sizeof(path), h->folder);
739 strncat (path, filename, sizeof (path) - m_strlen(path));
742 key.dsize = keylen (path);
744 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
746 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
748 p_delete(&data.dptr);
754 mutt_hcache_delete (void *db, const char *filename,
755 ssize_t (*keylen) (const char *fn))
758 struct header_cache *h = db;
759 char path[_POSIX_PATH_MAX];
765 m_strcpy(path, sizeof(path), h->folder);
766 strncat (path, filename, sizeof (path) - m_strlen(path));
769 key.dsize = keylen (path);
771 return gdbm_delete (h->db, key);
773 #elif defined(HAVE_DB4)
775 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
778 dbt->size = dbt->ulen = len;
779 dbt->dlen = dbt->doff = 0;
780 dbt->flags = DB_DBT_USERMEM;
783 static void mutt_hcache_dbt_empty_init (DBT * dbt)
786 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
790 void *mutt_hcache_open (const char *path, const char *folder)
793 u_int32_t createflags = DB_CREATE;
795 struct header_cache *h = p_new(struct header_cache, 1);
796 int pagesize = atoi (HeaderCachePageSize);
799 h->crc = generate_crc32 ();
801 if (!path || path[0] == '\0') {
806 path = mutt_hcache_per_folder (path, folder);
808 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
810 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
816 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
822 ret = db_env_create (&h->env, 0);
824 mx_unlock_file (h->lockfile, h->fd, 0);
831 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
833 ret = db_create (&h->db, h->env, 0);
835 h->env->close (h->env, 0);
836 mx_unlock_file (h->lockfile, h->fd, 0);
843 if (stat (path, &sb) != 0 && errno == ENOENT) {
844 createflags |= DB_EXCL;
845 h->db->set_pagesize (h->db, pagesize);
848 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
850 h->db->close (h->db, 0);
851 h->env->close (h->env, 0);
852 mx_unlock_file (h->lockfile, h->fd, 0);
861 void mutt_hcache_close (void *db)
863 struct header_cache *h = db;
869 h->db->close (h->db, 0);
870 h->env->close (h->env, 0);
871 mx_unlock_file (h->lockfile, h->fd, 0);
876 void *mutt_hcache_fetch (void *db, const char *filename,
877 ssize_t (*keylen) (const char *fn))
881 struct header_cache *h = db;
887 filename++; /* skip '/' */
889 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
890 mutt_hcache_dbt_empty_init (&data);
891 data.flags = DB_DBT_MALLOC;
893 h->db->get (h->db, NULL, &key, &data, 0);
895 if (!crc32_matches (data.data, h->crc)) {
896 p_delete(&data.data);
904 mutt_hcache_store (void *db, const char *filename, HEADER * header,
905 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
910 struct header_cache *h = db;
916 filename++; /* skip '/' */
918 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
920 mutt_hcache_dbt_empty_init (&data);
921 data.flags = DB_DBT_USERMEM;
923 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
924 data.ulen = data.size;
926 ret = h->db->put (h->db, NULL, &key, &data, 0);
928 p_delete(&data.data);
934 mutt_hcache_delete (void *db, const char *filename,
935 ssize_t (*keylen) (const char *fn))
938 struct header_cache *h = db;
944 filename++; /* skip '/' */
946 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
947 return h->db->del (h->db, NULL, &key, 0);
951 #endif /* USE_HCACHE */