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>
45 #include <lib-lib/debug.h>
47 #include <lib-mime/mime.h>
50 #include <imap/message.h>
54 static struct header_cache {
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);
132 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
137 d = dump_int (size, d, off);
141 d = dump_int (size, d, off);
142 lazy_realloc (&d, *off + size);
143 memcpy (d + *off, c, size);
150 static void restore_char (char **c, const unsigned char *d, int *off)
154 restore_int (&size, d, off);
161 *c = p_dup(d + *off, size);
165 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
167 unsigned int counter = 0;
168 unsigned int start_off = *off;
170 d = dump_int (0xdeadbeef, d, off);
173 d = dump_char (a->personal, d, off);
174 d = dump_char (a->mailbox, d, off);
175 d = dump_int (a->group, d, off);
180 memcpy (d + start_off, &counter, sizeof (int));
185 static void restore_address (address_t ** a, const unsigned char *d, int *off)
187 unsigned int counter;
189 restore_int (&counter, d, off);
192 *a = p_new(address_t, 1);
193 restore_char (&(*a)->personal, d, off);
194 restore_char (&(*a)->mailbox, d, off);
195 restore_int ((unsigned int *) &(*a)->group, d, off);
203 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
205 unsigned int counter = 0;
206 unsigned int start_off = *off;
208 d = dump_int (0xdeadbeef, d, off);
211 d = dump_char (l->data, d, off);
216 memcpy (d + start_off, &counter, sizeof (int));
221 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
223 unsigned int counter;
225 restore_int (&counter, d, off);
228 *l = p_new(string_list_t, 1);
229 restore_char (&(*l)->data, d, off);
238 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
241 d = dump_int (0, d, off);
245 d = dump_int (1, d, off);
248 d = dump_char_size (b->data, d, off, b->dsize + 1);
249 d = dump_int (b->dptr - b->data, d, off);
250 d = dump_int (b->dsize, d, off);
251 d = dump_int (b->destroy, d, off);
256 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
261 restore_int (&used, d, off);
266 *b = p_new(BUFFER, 1);
268 restore_char (&(*b)->data, d, off);
269 restore_int (&offset, d, off);
270 (*b)->dptr = (*b)->data + offset;
271 restore_int (&(*b)->dsize, d, off);
272 restore_int ((unsigned int *) &(*b)->destroy, d, off);
276 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
279 unsigned int counter = 0;
280 unsigned int start_off = *off;
282 d = dump_int (0xdeadbeef, d, off);
285 d = dump_char (p->attribute, d, off);
286 d = dump_char (p->value, d, off);
291 memcpy (d + start_off, &counter, sizeof (int));
297 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
299 unsigned int counter;
301 restore_int (&counter, d, off);
304 *p = p_new(PARAMETER, 1);
305 restore_char (&(*p)->attribute, d, off);
306 restore_char (&(*p)->value, d, off);
314 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
316 lazy_realloc (&d, *off + sizeof (BODY));
317 memcpy (d + *off, c, sizeof (BODY));
318 *off += sizeof (BODY);
320 d = dump_char (c->xtype, d, off);
321 d = dump_char (c->subtype, d, off);
323 d = dump_parameter (c->parameter, d, off);
325 d = dump_char (c->description, d, off);
326 d = dump_char (c->form_name, d, off);
327 d = dump_char (c->filename, d, off);
328 d = dump_char (c->d_filename, d, off);
333 static void restore_body (BODY * c, const unsigned char *d, int *off)
335 memcpy (c, d + *off, sizeof (BODY));
336 *off += sizeof (BODY);
338 restore_char (&c->xtype, d, off);
339 restore_char (&c->subtype, d, off);
341 restore_parameter (&c->parameter, d, off);
343 restore_char (&c->description, d, off);
344 restore_char (&c->form_name, d, off);
345 restore_char (&c->filename, d, off);
346 restore_char (&c->d_filename, d, off);
349 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
351 d = dump_address (e->return_path, d, off);
352 d = dump_address (e->from, d, off);
353 d = dump_address (e->to, d, off);
354 d = dump_address (e->cc, d, off);
355 d = dump_address (e->bcc, d, off);
356 d = dump_address (e->sender, d, off);
357 d = dump_address (e->reply_to, d, off);
358 d = dump_address (e->mail_followup_to, d, off);
360 d = dump_char (e->subject, d, off);
362 d = dump_int (e->real_subj - e->subject, d, off);
365 d = dump_int (-1, d, off);
367 d = dump_char (e->message_id, d, off);
368 d = dump_char (e->supersedes, d, off);
369 d = dump_char (e->date, d, off);
370 d = dump_char (e->x_label, d, off);
371 d = dump_char (e->list_post, d, off);
374 d = dump_char (e->newsgroups, d, off);
375 d = dump_char (e->xref, d, off);
376 d = dump_char (e->followup_to, d, off);
377 d = dump_char (e->x_comment_to, d, off);
380 d = dump_list (e->references, d, off);
381 d = dump_list (e->in_reply_to, d, off);
382 d = dump_list (e->userhdrs, d, off);
387 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
391 restore_address (&e->return_path, d, off);
392 restore_address (&e->from, d, off);
393 restore_address (&e->to, d, off);
394 restore_address (&e->cc, d, off);
395 restore_address (&e->bcc, d, off);
396 restore_address (&e->sender, d, off);
397 restore_address (&e->reply_to, d, off);
398 restore_address (&e->mail_followup_to, d, off);
400 restore_char (&e->subject, d, off);
401 restore_int ((unsigned int *) (&real_subj_off), d, off);
402 if (0 <= real_subj_off) {
403 e->real_subj = e->subject + real_subj_off;
408 restore_char (&e->message_id, d, off);
409 restore_char (&e->supersedes, d, off);
410 restore_char (&e->date, d, off);
411 restore_char (&e->x_label, d, off);
412 restore_char (&e->list_post, d, off);
415 restore_char (&e->newsgroups, d, off);
416 restore_char (&e->xref, d, off);
417 restore_char (&e->followup_to, d, off);
418 restore_char (&e->x_comment_to, d, off);
421 restore_list (&e->references, d, off);
422 restore_list (&e->in_reply_to, d, off);
423 restore_list (&e->userhdrs, d, off);
427 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
433 for (i = 0; i < 8; i++)
434 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
439 static int generate_crc32 ()
443 crc = crc32 (crc, (unsigned char const *)
444 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
446 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
448 #ifdef HAVE_LANGINFO_CODESET
449 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
450 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
451 m_strlen("HAVE_LANGINFO_CODESET"));
454 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
457 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
458 m_strlen("MIXMASTER"));
461 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
464 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
469 static int crc32_matches (const char *d, unsigned int crc)
471 int off = sizeof (validate);
472 unsigned int mycrc = 0;
478 restore_int (&mycrc, (unsigned char *) d, &off);
480 return (crc == mycrc);
483 /* Append md5sumed folder to path if path is a directory. */
484 static const char *mutt_hcache_per_folder (const char *path,
487 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
488 struct stat path_stat;
490 unsigned char md5sum[16];
493 ret = stat (path, &path_stat);
498 if (!S_ISDIR (path_stat.st_mode)) {
503 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
504 MD5Final (md5sum, &md5);
506 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
507 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
508 "%02x%02x%02x%02x%02x%02x%02x%02x",
509 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
510 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
511 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
512 md5sum[13], md5sum[14], md5sum[15]);
518 return mutt_hcache_per_folder_path;
521 /* This function transforms a header into a char so that it is useable by
523 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
524 unsigned long uid_validity)
526 struct header_cache *db = _db;
527 unsigned char *d = NULL;
531 d = lazy_malloc (sizeof (validate));
534 memcpy (d, &uid_validity, sizeof (unsigned long));
539 gettimeofday (&now, NULL);
540 memcpy (d, &now, sizeof (struct timeval));
542 *off += sizeof (validate);
544 d = dump_int (db->crc, d, off);
546 lazy_realloc (&d, *off + sizeof (HEADER));
547 memcpy (d + *off, h, sizeof (HEADER));
548 *off += sizeof (HEADER);
550 d = dump_envelope (h->env, d, off);
551 d = dump_body (h->content, d, off);
552 d = dump_char (h->maildir_flags, d, off);
557 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
560 HEADER *h = header_new();
563 off += sizeof (validate);
566 off += sizeof (unsigned int);
568 memcpy (h, d + off, sizeof (HEADER));
569 off += sizeof (HEADER);
571 h->env = envelope_new();
572 restore_envelope (h->env, d, &off);
574 h->content = mutt_new_body ();
575 restore_body (h->content, d, &off);
577 restore_char (&h->maildir_flags, d, &off);
579 /* this is needed for maildir style mailboxes */
582 h->path = m_strdup((*oh)->path);
589 #if defined(HAVE_QDBM)
591 mutt_hcache_open(const char *path, const char *folder)
593 struct header_cache *h = p_new(struct header_cache, 1);
594 int flags = VL_OWRITER | VL_OCREAT;
596 h->folder = m_strdup(folder);
597 h->crc = generate_crc32();
599 if (!path || path[0] == '\0')
601 p_delete(&h->folder);
606 path = mutt_hcache_per_folder(path, folder);
608 if (option(OPTHCACHECOMPRESS))
611 h->db = vlopen(path, flags, VL_CMPLEX);
616 p_delete(&h->folder);
624 mutt_hcache_close(void *db)
626 struct header_cache *h = db;
632 p_delete(&h->folder);
637 mutt_hcache_fetch(void *db, const char *filename,
638 ssize_t(*keylen) (const char *fn))
640 struct header_cache *h = db;
641 char path[_POSIX_PATH_MAX];
648 m_strcpy(path, sizeof(path), h->folder);
649 m_strcat(path, sizeof(path), filename);
651 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
653 data = vlget(h->db, path, ksize, NULL);
655 if (!crc32_matches(data, h->crc))
665 mutt_hcache_store(void *db, const char *filename, HEADER * header,
666 unsigned long uid_validity,
667 ssize_t(*keylen) (const char *fn))
669 struct header_cache *h = db;
670 char path[_POSIX_PATH_MAX];
678 m_strcpy(path, sizeof(path), h->folder);
679 m_strcat(path, sizeof(path), filename);
681 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
683 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
685 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
693 mutt_hcache_delete(void *db, const char *filename,
694 ssize_t(*keylen) (const char *fn))
696 struct header_cache *h = db;
697 char path[_POSIX_PATH_MAX];
703 m_strcpy(path, sizeof(path), h->folder);
704 m_strcat(path, sizeof(path), filename);
706 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
708 return vlout(h->db, path, ksize);
711 #elif defined(HAVE_GDBM)
713 void *mutt_hcache_open (const char *path, const char *folder)
715 struct header_cache *h = p_new(HEADER_CACHE, 1);
717 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
719 h->folder = m_strdup(folder);
720 h->crc = generate_crc32 ();
722 if (!path || path[0] == '\0') {
723 p_delete(&h->folder);
728 path = mutt_hcache_per_folder (path, folder);
730 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
735 /* if rw failed try ro */
736 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
741 p_delete(&h->folder);
748 void mutt_hcache_close (void *db)
750 struct header_cache *h = db;
757 p_delete(&h->folder);
761 void *mutt_hcache_fetch (void *db, const char *filename,
762 ssize_t (*keylen) (const char *fn))
764 struct header_cache *h = db;
767 char path[_POSIX_PATH_MAX];
773 m_strcpy(path, sizeof(path), h->folder);
774 strncat (path, filename, sizeof (path) - m_strlen(path));
777 key.dsize = keylen (path);
779 data = gdbm_fetch (h->db, key);
781 if (!crc32_matches (data.dptr, h->crc)) {
782 p_delete(&data.dptr);
790 mutt_hcache_store (void *db, const char *filename, HEADER * header,
791 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
793 struct header_cache *h = db;
796 char path[_POSIX_PATH_MAX];
803 m_strcpy(path, sizeof(path), h->folder);
804 strncat (path, filename, sizeof (path) - m_strlen(path));
807 key.dsize = keylen (path);
809 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
811 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
813 p_delete(&data.dptr);
819 mutt_hcache_delete (void *db, const char *filename,
820 ssize_t (*keylen) (const char *fn))
823 struct header_cache *h = db;
824 char path[_POSIX_PATH_MAX];
830 m_strcpy(path, sizeof(path), h->folder);
831 strncat (path, filename, sizeof (path) - m_strlen(path));
834 key.dsize = keylen (path);
836 return gdbm_delete (h->db, key);
838 #elif defined(HAVE_DB4)
840 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
843 dbt->size = dbt->ulen = len;
844 dbt->dlen = dbt->doff = 0;
845 dbt->flags = DB_DBT_USERMEM;
848 static void mutt_hcache_dbt_empty_init (DBT * dbt)
851 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
855 void *mutt_hcache_open (const char *path, const char *folder)
858 u_int32_t createflags = DB_CREATE;
860 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
861 int pagesize = atoi (HeaderCachePageSize);
864 h->crc = generate_crc32 ();
866 if (!path || path[0] == '\0') {
871 path = mutt_hcache_per_folder (path, folder);
873 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
875 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
881 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
887 ret = db_env_create (&h->env, 0);
889 mx_unlock_file (h->lockfile, h->fd, 0);
896 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
898 ret = db_create (&h->db, h->env, 0);
900 h->env->close (h->env, 0);
901 mx_unlock_file (h->lockfile, h->fd, 0);
908 if (stat (path, &sb) != 0 && errno == ENOENT) {
909 createflags |= DB_EXCL;
910 h->db->set_pagesize (h->db, pagesize);
913 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
915 h->db->close (h->db, 0);
916 h->env->close (h->env, 0);
917 mx_unlock_file (h->lockfile, h->fd, 0);
926 void mutt_hcache_close (void *db)
928 struct header_cache *h = db;
934 h->db->close (h->db, 0);
935 h->env->close (h->env, 0);
936 mx_unlock_file (h->lockfile, h->fd, 0);
941 void *mutt_hcache_fetch (void *db, const char *filename,
942 ssize_t (*keylen) (const char *fn))
946 struct header_cache *h = db;
952 filename++; /* skip '/' */
954 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
955 mutt_hcache_dbt_empty_init (&data);
956 data.flags = DB_DBT_MALLOC;
958 h->db->get (h->db, NULL, &key, &data, 0);
960 if (!crc32_matches (data.data, h->crc)) {
961 p_delete(&data.data);
969 mutt_hcache_store (void *db, const char *filename, HEADER * header,
970 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
975 struct header_cache *h = db;
981 filename++; /* skip '/' */
983 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
985 mutt_hcache_dbt_empty_init (&data);
986 data.flags = DB_DBT_USERMEM;
988 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
989 data.ulen = data.size;
991 ret = h->db->put (h->db, NULL, &key, &data, 0);
993 p_delete(&data.data);
999 mutt_hcache_delete (void *db, const char *filename,
1000 ssize_t (*keylen) (const char *fn))
1003 struct header_cache *h = db;
1009 filename++; /* skip '/' */
1011 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1012 return h->db->del (h->db, NULL, &key, 0);
1016 #endif /* USE_HCACHE */