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-hash/hash.h>
46 #include <lib-mime/mime.h>
50 #include <imap/message.h>
55 #if defined(HAVE_QDBM)
59 #elif defined(HAVE_GDBM)
63 #elif defined(HAVE_DB4)
68 char lockfile[_POSIX_PATH_MAX];
73 struct timeval timeval;
74 unsigned long uid_validity;
77 static void *lazy_malloc (ssize_t siz)
79 if (0 < siz && siz < 4096) {
86 static void lazy_realloc(void *ptr, ssize_t siz)
90 if (p != NULL && 0 < siz && siz < 4096) {
97 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
99 lazy_realloc (&d, *off + sizeof (int));
100 memcpy (d + *off, &i, sizeof (int));
101 (*off) += sizeof (int);
106 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
108 memcpy (i, d + *off, sizeof (int));
109 (*off) += sizeof (int);
112 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
118 d = dump_int (size, d, off);
122 size = m_strlen(c) + 1;
123 d = dump_int (size, d, off);
124 lazy_realloc (&d, *off + size);
125 memcpy (d + *off, c, size);
131 static void restore_char (char **c, const unsigned char *d, int *off)
135 restore_int (&size, d, off);
142 *c = p_dup(d + *off, size);
146 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
148 unsigned int counter = 0;
149 unsigned int start_off = *off;
151 d = dump_int (0xdeadbeef, d, off);
154 d = dump_char (a->personal, d, off);
155 d = dump_char (a->mailbox, d, off);
156 d = dump_int (a->group, d, off);
161 memcpy (d + start_off, &counter, sizeof (int));
166 static void restore_address (address_t ** a, const unsigned char *d, int *off)
168 unsigned int counter;
170 restore_int (&counter, d, off);
173 *a = p_new(address_t, 1);
174 restore_char (&(*a)->personal, d, off);
175 restore_char (&(*a)->mailbox, d, off);
176 restore_int ((unsigned int *) &(*a)->group, d, off);
184 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
186 unsigned int counter = 0;
187 unsigned int start_off = *off;
189 d = dump_int (0xdeadbeef, d, off);
192 d = dump_char (l->data, d, off);
197 memcpy (d + start_off, &counter, sizeof (int));
202 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
204 unsigned int counter;
206 restore_int (&counter, d, off);
209 *l = p_new(string_list_t, 1);
210 restore_char (&(*l)->data, d, off);
218 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
221 unsigned int counter = 0;
222 unsigned int start_off = *off;
224 d = dump_int (0xdeadbeef, d, off);
227 d = dump_char (p->attribute, d, off);
228 d = dump_char (p->value, d, off);
233 memcpy (d + start_off, &counter, sizeof (int));
239 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
241 unsigned int counter;
243 restore_int (&counter, d, off);
246 *p = p_new(PARAMETER, 1);
247 restore_char (&(*p)->attribute, d, off);
248 restore_char (&(*p)->value, d, off);
256 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
258 lazy_realloc (&d, *off + sizeof (BODY));
259 memcpy (d + *off, c, sizeof (BODY));
260 *off += sizeof (BODY);
262 d = dump_char (c->xtype, d, off);
263 d = dump_char (c->subtype, d, off);
265 d = dump_parameter (c->parameter, d, off);
267 d = dump_char (c->description, d, off);
268 d = dump_char (c->form_name, d, off);
269 d = dump_char (c->filename, d, off);
270 d = dump_char (c->d_filename, d, off);
275 static void restore_body (BODY * c, const unsigned char *d, int *off)
277 memcpy (c, d + *off, sizeof (BODY));
278 *off += sizeof (BODY);
280 restore_char (&c->xtype, d, off);
281 restore_char (&c->subtype, d, off);
283 restore_parameter (&c->parameter, d, off);
285 restore_char (&c->description, d, off);
286 restore_char (&c->form_name, d, off);
287 restore_char (&c->filename, d, off);
288 restore_char (&c->d_filename, d, off);
291 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
293 d = dump_address (e->return_path, d, off);
294 d = dump_address (e->from, d, off);
295 d = dump_address (e->to, d, off);
296 d = dump_address (e->cc, d, off);
297 d = dump_address (e->bcc, d, off);
298 d = dump_address (e->sender, d, off);
299 d = dump_address (e->reply_to, d, off);
300 d = dump_address (e->mail_followup_to, d, off);
302 d = dump_char (e->subject, d, off);
304 d = dump_int (e->real_subj - e->subject, d, off);
307 d = dump_int (-1, d, off);
309 d = dump_char (e->message_id, d, off);
310 d = dump_char (e->supersedes, d, off);
311 d = dump_char (e->date, d, off);
312 d = dump_char (e->x_label, d, off);
313 d = dump_char (e->list_post, d, off);
316 d = dump_char (e->newsgroups, d, off);
317 d = dump_char (e->xref, d, off);
318 d = dump_char (e->followup_to, d, off);
319 d = dump_char (e->x_comment_to, d, off);
322 d = dump_list (e->references, d, off);
323 d = dump_list (e->in_reply_to, d, off);
324 d = dump_list (e->userhdrs, d, off);
329 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
333 restore_address (&e->return_path, d, off);
334 restore_address (&e->from, d, off);
335 restore_address (&e->to, d, off);
336 restore_address (&e->cc, d, off);
337 restore_address (&e->bcc, d, off);
338 restore_address (&e->sender, d, off);
339 restore_address (&e->reply_to, d, off);
340 restore_address (&e->mail_followup_to, d, off);
342 restore_char (&e->subject, d, off);
343 restore_int ((unsigned int *) (&real_subj_off), d, off);
344 if (0 <= real_subj_off) {
345 e->real_subj = e->subject + real_subj_off;
350 restore_char (&e->message_id, d, off);
351 restore_char (&e->supersedes, d, off);
352 restore_char (&e->date, d, off);
353 restore_char (&e->x_label, d, off);
354 restore_char (&e->list_post, d, off);
357 restore_char (&e->newsgroups, d, off);
358 restore_char (&e->xref, d, off);
359 restore_char (&e->followup_to, d, off);
360 restore_char (&e->x_comment_to, d, off);
363 restore_list (&e->references, d, off);
364 restore_list (&e->in_reply_to, d, off);
365 restore_list (&e->userhdrs, d, off);
369 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
375 for (i = 0; i < 8; i++)
376 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
381 static int generate_crc32 ()
385 crc = crc32 (crc, (unsigned char const *)
386 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
388 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
390 #ifdef HAVE_LANGINFO_CODESET
391 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
392 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
393 m_strlen("HAVE_LANGINFO_CODESET"));
396 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
399 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
400 m_strlen("MIXMASTER"));
403 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
406 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
411 static int crc32_matches (const char *d, unsigned int crc)
413 int off = sizeof (validate);
414 unsigned int mycrc = 0;
420 restore_int (&mycrc, (unsigned char *) d, &off);
422 return (crc == mycrc);
425 /* Append md5sumed folder to path if path is a directory. */
426 static const char *mutt_hcache_per_folder (const char *path,
429 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
430 struct stat path_stat;
432 unsigned char md5sum[16];
435 ret = stat (path, &path_stat);
440 if (!S_ISDIR (path_stat.st_mode)) {
445 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
446 MD5Final (md5sum, &md5);
448 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
449 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
450 "%02x%02x%02x%02x%02x%02x%02x%02x",
451 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
452 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
453 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
454 md5sum[13], md5sum[14], md5sum[15]);
460 return mutt_hcache_per_folder_path;
463 /* This function transforms a header into a char so that it is useable by
465 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
466 unsigned long uid_validity)
468 struct header_cache *db = _db;
469 unsigned char *d = NULL;
473 d = lazy_malloc (sizeof (validate));
476 memcpy (d, &uid_validity, sizeof (unsigned long));
481 gettimeofday (&now, NULL);
482 memcpy (d, &now, sizeof (struct timeval));
484 *off += sizeof (validate);
486 d = dump_int (db->crc, d, off);
488 lazy_realloc (&d, *off + sizeof (HEADER));
489 memcpy (d + *off, h, sizeof (HEADER));
490 *off += sizeof (HEADER);
492 d = dump_envelope (h->env, d, off);
493 d = dump_body (h->content, d, off);
494 d = dump_char (h->maildir_flags, d, off);
499 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
502 HEADER *h = header_new();
505 off += sizeof (validate);
508 off += sizeof (unsigned int);
510 memcpy (h, d + off, sizeof (HEADER));
511 off += sizeof (HEADER);
513 h->env = envelope_new();
514 restore_envelope (h->env, d, &off);
516 h->content = mutt_new_body ();
517 restore_body (h->content, d, &off);
519 restore_char (&h->maildir_flags, d, &off);
521 /* this is needed for maildir style mailboxes */
524 h->path = m_strdup((*oh)->path);
531 #if defined(HAVE_QDBM)
533 mutt_hcache_open(const char *path, const char *folder)
535 struct header_cache *h = p_new(struct header_cache, 1);
536 int flags = VL_OWRITER | VL_OCREAT;
538 h->folder = m_strdup(folder);
539 h->crc = generate_crc32();
541 if (!path || path[0] == '\0')
543 p_delete(&h->folder);
548 path = mutt_hcache_per_folder(path, folder);
550 if (option(OPTHCACHECOMPRESS))
553 h->db = vlopen(path, flags, VL_CMPLEX);
558 p_delete(&h->folder);
566 mutt_hcache_close(void *db)
568 struct header_cache *h = db;
574 p_delete(&h->folder);
579 mutt_hcache_fetch(void *db, const char *filename,
580 ssize_t(*keylen) (const char *fn))
582 struct header_cache *h = db;
583 char path[_POSIX_PATH_MAX];
590 m_strcpy(path, sizeof(path), h->folder);
591 m_strcat(path, sizeof(path), filename);
593 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
595 data = vlget(h->db, path, ksize, NULL);
597 if (!crc32_matches(data, h->crc))
607 mutt_hcache_store(void *db, const char *filename, HEADER * header,
608 unsigned long uid_validity,
609 ssize_t(*keylen) (const char *fn))
611 struct header_cache *h = db;
612 char path[_POSIX_PATH_MAX];
620 m_strcpy(path, sizeof(path), h->folder);
621 m_strcat(path, sizeof(path), filename);
623 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
625 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
627 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
635 mutt_hcache_delete(void *db, const char *filename,
636 ssize_t(*keylen) (const char *fn))
638 struct header_cache *h = db;
639 char path[_POSIX_PATH_MAX];
645 m_strcpy(path, sizeof(path), h->folder);
646 m_strcat(path, sizeof(path), filename);
648 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
650 return vlout(h->db, path, ksize);
653 #elif defined(HAVE_GDBM)
655 void *mutt_hcache_open (const char *path, const char *folder)
657 struct header_cache *h = p_new(struct header_cache, 1);
659 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
661 h->folder = m_strdup(folder);
662 h->crc = generate_crc32 ();
664 if (!path || path[0] == '\0') {
665 p_delete(&h->folder);
670 path = mutt_hcache_per_folder (path, folder);
672 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
677 /* if rw failed try ro */
678 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
683 p_delete(&h->folder);
690 void mutt_hcache_close (void *db)
692 struct header_cache *h = db;
699 p_delete(&h->folder);
703 void *mutt_hcache_fetch (void *db, const char *filename,
704 ssize_t (*keylen) (const char *fn))
706 struct header_cache *h = db;
709 char path[_POSIX_PATH_MAX];
715 m_strcpy(path, sizeof(path), h->folder);
716 strncat (path, filename, sizeof (path) - m_strlen(path));
719 key.dsize = keylen (path);
721 data = gdbm_fetch (h->db, key);
723 if (!crc32_matches (data.dptr, h->crc)) {
724 p_delete(&data.dptr);
732 mutt_hcache_store (void *db, const char *filename, HEADER * header,
733 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
735 struct header_cache *h = db;
738 char path[_POSIX_PATH_MAX];
745 m_strcpy(path, sizeof(path), h->folder);
746 strncat (path, filename, sizeof (path) - m_strlen(path));
749 key.dsize = keylen (path);
751 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
753 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
755 p_delete(&data.dptr);
761 mutt_hcache_delete (void *db, const char *filename,
762 ssize_t (*keylen) (const char *fn))
765 struct header_cache *h = db;
766 char path[_POSIX_PATH_MAX];
772 m_strcpy(path, sizeof(path), h->folder);
773 strncat (path, filename, sizeof (path) - m_strlen(path));
776 key.dsize = keylen (path);
778 return gdbm_delete (h->db, key);
780 #elif defined(HAVE_DB4)
782 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
785 dbt->size = dbt->ulen = len;
786 dbt->dlen = dbt->doff = 0;
787 dbt->flags = DB_DBT_USERMEM;
790 static void mutt_hcache_dbt_empty_init (DBT * dbt)
793 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
797 void *mutt_hcache_open (const char *path, const char *folder)
800 u_int32_t createflags = DB_CREATE;
802 struct header_cache *h = p_new(struct header_cache, 1);
803 int pagesize = atoi (HeaderCachePageSize);
806 h->crc = generate_crc32 ();
808 if (!path || path[0] == '\0') {
813 path = mutt_hcache_per_folder (path, folder);
815 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
817 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
823 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
829 ret = db_env_create (&h->env, 0);
831 mx_unlock_file (h->lockfile, h->fd, 0);
838 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
840 ret = db_create (&h->db, h->env, 0);
842 h->env->close (h->env, 0);
843 mx_unlock_file (h->lockfile, h->fd, 0);
850 if (stat (path, &sb) != 0 && errno == ENOENT) {
851 createflags |= DB_EXCL;
852 h->db->set_pagesize (h->db, pagesize);
855 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
857 h->db->close (h->db, 0);
858 h->env->close (h->env, 0);
859 mx_unlock_file (h->lockfile, h->fd, 0);
868 void mutt_hcache_close (void *db)
870 struct header_cache *h = db;
876 h->db->close (h->db, 0);
877 h->env->close (h->env, 0);
878 mx_unlock_file (h->lockfile, h->fd, 0);
883 void *mutt_hcache_fetch (void *db, const char *filename,
884 ssize_t (*keylen) (const char *fn))
888 struct header_cache *h = db;
894 filename++; /* skip '/' */
896 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
897 mutt_hcache_dbt_empty_init (&data);
898 data.flags = DB_DBT_MALLOC;
900 h->db->get (h->db, NULL, &key, &data, 0);
902 if (!crc32_matches (data.data, h->crc)) {
903 p_delete(&data.data);
911 mutt_hcache_store (void *db, const char *filename, HEADER * header,
912 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
917 struct header_cache *h = db;
923 filename++; /* skip '/' */
925 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
927 mutt_hcache_dbt_empty_init (&data);
928 data.flags = DB_DBT_USERMEM;
930 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
931 data.ulen = data.size;
933 ret = h->db->put (h->db, NULL, &key, &data, 0);
935 p_delete(&data.data);
941 mutt_hcache_delete (void *db, const char *filename,
942 ssize_t (*keylen) (const char *fn))
945 struct header_cache *h = db;
951 filename++; /* skip '/' */
953 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
954 return h->db->del (h->db, NULL, &key, 0);
958 #endif /* USE_HCACHE */