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>
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 static void *lazy_malloc (ssize_t siz)
80 if (0 < siz && siz < 4096) {
87 static void lazy_realloc(void *ptr, ssize_t siz)
91 if (p != NULL && 0 < siz && siz < 4096) {
98 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
100 lazy_realloc (&d, *off + sizeof (int));
101 memcpy (d + *off, &i, sizeof (int));
102 (*off) += sizeof (int);
107 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
109 memcpy (i, d + *off, sizeof (int));
110 (*off) += sizeof (int);
113 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
119 d = dump_int (size, d, off);
123 size = m_strlen(c) + 1;
124 d = dump_int (size, d, off);
125 lazy_realloc (&d, *off + size);
126 memcpy (d + *off, c, size);
133 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
138 d = dump_int (size, d, off);
142 d = dump_int (size, d, off);
143 lazy_realloc (&d, *off + size);
144 memcpy (d + *off, c, size);
151 static void restore_char (char **c, const unsigned char *d, int *off)
155 restore_int (&size, d, off);
162 *c = p_dup(d + *off, size);
166 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
168 unsigned int counter = 0;
169 unsigned int start_off = *off;
171 d = dump_int (0xdeadbeef, d, off);
174 d = dump_char (a->personal, d, off);
175 d = dump_char (a->mailbox, d, off);
176 d = dump_int (a->group, d, off);
181 memcpy (d + start_off, &counter, sizeof (int));
186 static void restore_address (address_t ** a, const unsigned char *d, int *off)
188 unsigned int counter;
190 restore_int (&counter, d, off);
193 *a = p_new(address_t, 1);
194 restore_char (&(*a)->personal, d, off);
195 restore_char (&(*a)->mailbox, d, off);
196 restore_int ((unsigned int *) &(*a)->group, d, off);
204 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
206 unsigned int counter = 0;
207 unsigned int start_off = *off;
209 d = dump_int (0xdeadbeef, d, off);
212 d = dump_char (l->data, d, off);
217 memcpy (d + start_off, &counter, sizeof (int));
222 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
224 unsigned int counter;
226 restore_int (&counter, d, off);
229 *l = p_new(string_list_t, 1);
230 restore_char (&(*l)->data, d, off);
239 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
242 d = dump_int (0, d, off);
246 d = dump_int (1, d, off);
249 d = dump_char_size (b->data, d, off, b->dsize + 1);
250 d = dump_int (b->dptr - b->data, d, off);
251 d = dump_int (b->dsize, d, off);
252 d = dump_int (b->destroy, d, off);
257 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
262 restore_int (&used, d, off);
267 *b = p_new(BUFFER, 1);
269 restore_char (&(*b)->data, d, off);
270 restore_int (&offset, d, off);
271 (*b)->dptr = (*b)->data + offset;
272 restore_int (&(*b)->dsize, d, off);
273 restore_int ((unsigned int *) &(*b)->destroy, d, off);
277 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
280 unsigned int counter = 0;
281 unsigned int start_off = *off;
283 d = dump_int (0xdeadbeef, d, off);
286 d = dump_char (p->attribute, d, off);
287 d = dump_char (p->value, d, off);
292 memcpy (d + start_off, &counter, sizeof (int));
298 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
300 unsigned int counter;
302 restore_int (&counter, d, off);
305 *p = p_new(PARAMETER, 1);
306 restore_char (&(*p)->attribute, d, off);
307 restore_char (&(*p)->value, d, off);
315 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
317 lazy_realloc (&d, *off + sizeof (BODY));
318 memcpy (d + *off, c, sizeof (BODY));
319 *off += sizeof (BODY);
321 d = dump_char (c->xtype, d, off);
322 d = dump_char (c->subtype, d, off);
324 d = dump_parameter (c->parameter, d, off);
326 d = dump_char (c->description, d, off);
327 d = dump_char (c->form_name, d, off);
328 d = dump_char (c->filename, d, off);
329 d = dump_char (c->d_filename, d, off);
334 static void restore_body (BODY * c, const unsigned char *d, int *off)
336 memcpy (c, d + *off, sizeof (BODY));
337 *off += sizeof (BODY);
339 restore_char (&c->xtype, d, off);
340 restore_char (&c->subtype, d, off);
342 restore_parameter (&c->parameter, d, off);
344 restore_char (&c->description, d, off);
345 restore_char (&c->form_name, d, off);
346 restore_char (&c->filename, d, off);
347 restore_char (&c->d_filename, d, off);
350 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
352 d = dump_address (e->return_path, d, off);
353 d = dump_address (e->from, d, off);
354 d = dump_address (e->to, d, off);
355 d = dump_address (e->cc, d, off);
356 d = dump_address (e->bcc, d, off);
357 d = dump_address (e->sender, d, off);
358 d = dump_address (e->reply_to, d, off);
359 d = dump_address (e->mail_followup_to, d, off);
361 d = dump_char (e->subject, d, off);
363 d = dump_int (e->real_subj - e->subject, d, off);
366 d = dump_int (-1, d, off);
368 d = dump_char (e->message_id, d, off);
369 d = dump_char (e->supersedes, d, off);
370 d = dump_char (e->date, d, off);
371 d = dump_char (e->x_label, d, off);
372 d = dump_char (e->list_post, d, off);
375 d = dump_char (e->newsgroups, d, off);
376 d = dump_char (e->xref, d, off);
377 d = dump_char (e->followup_to, d, off);
378 d = dump_char (e->x_comment_to, d, off);
381 d = dump_list (e->references, d, off);
382 d = dump_list (e->in_reply_to, d, off);
383 d = dump_list (e->userhdrs, d, off);
388 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
392 restore_address (&e->return_path, d, off);
393 restore_address (&e->from, d, off);
394 restore_address (&e->to, d, off);
395 restore_address (&e->cc, d, off);
396 restore_address (&e->bcc, d, off);
397 restore_address (&e->sender, d, off);
398 restore_address (&e->reply_to, d, off);
399 restore_address (&e->mail_followup_to, d, off);
401 restore_char (&e->subject, d, off);
402 restore_int ((unsigned int *) (&real_subj_off), d, off);
403 if (0 <= real_subj_off) {
404 e->real_subj = e->subject + real_subj_off;
409 restore_char (&e->message_id, d, off);
410 restore_char (&e->supersedes, d, off);
411 restore_char (&e->date, d, off);
412 restore_char (&e->x_label, d, off);
413 restore_char (&e->list_post, d, off);
416 restore_char (&e->newsgroups, d, off);
417 restore_char (&e->xref, d, off);
418 restore_char (&e->followup_to, d, off);
419 restore_char (&e->x_comment_to, d, off);
422 restore_list (&e->references, d, off);
423 restore_list (&e->in_reply_to, d, off);
424 restore_list (&e->userhdrs, d, off);
428 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
434 for (i = 0; i < 8; i++)
435 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
440 static int generate_crc32 ()
444 crc = crc32 (crc, (unsigned char const *)
445 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
447 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
449 #ifdef HAVE_LANGINFO_CODESET
450 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
451 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
452 m_strlen("HAVE_LANGINFO_CODESET"));
455 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
458 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
459 m_strlen("MIXMASTER"));
462 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
465 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
470 static int crc32_matches (const char *d, unsigned int crc)
472 int off = sizeof (validate);
473 unsigned int mycrc = 0;
479 restore_int (&mycrc, (unsigned char *) d, &off);
481 return (crc == mycrc);
484 /* Append md5sumed folder to path if path is a directory. */
485 static const char *mutt_hcache_per_folder (const char *path,
488 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
489 struct stat path_stat;
491 unsigned char md5sum[16];
494 ret = stat (path, &path_stat);
499 if (!S_ISDIR (path_stat.st_mode)) {
504 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
505 MD5Final (md5sum, &md5);
507 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
508 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
509 "%02x%02x%02x%02x%02x%02x%02x%02x",
510 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
511 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
512 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
513 md5sum[13], md5sum[14], md5sum[15]);
519 return mutt_hcache_per_folder_path;
522 /* This function transforms a header into a char so that it is useable by
524 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
525 unsigned long uid_validity)
527 struct header_cache *db = _db;
528 unsigned char *d = NULL;
532 d = lazy_malloc (sizeof (validate));
535 memcpy (d, &uid_validity, sizeof (unsigned long));
540 gettimeofday (&now, NULL);
541 memcpy (d, &now, sizeof (struct timeval));
543 *off += sizeof (validate);
545 d = dump_int (db->crc, d, off);
547 lazy_realloc (&d, *off + sizeof (HEADER));
548 memcpy (d + *off, h, sizeof (HEADER));
549 *off += sizeof (HEADER);
551 d = dump_envelope (h->env, d, off);
552 d = dump_body (h->content, d, off);
553 d = dump_char (h->maildir_flags, d, off);
558 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
561 HEADER *h = header_new();
564 off += sizeof (validate);
567 off += sizeof (unsigned int);
569 memcpy (h, d + off, sizeof (HEADER));
570 off += sizeof (HEADER);
572 h->env = envelope_new();
573 restore_envelope (h->env, d, &off);
575 h->content = mutt_new_body ();
576 restore_body (h->content, d, &off);
578 restore_char (&h->maildir_flags, d, &off);
580 /* this is needed for maildir style mailboxes */
583 h->path = m_strdup((*oh)->path);
590 #if defined(HAVE_QDBM)
592 mutt_hcache_open(const char *path, const char *folder)
594 struct header_cache *h = p_new(struct header_cache, 1);
595 int flags = VL_OWRITER | VL_OCREAT;
597 h->folder = m_strdup(folder);
598 h->crc = generate_crc32();
600 if (!path || path[0] == '\0')
602 p_delete(&h->folder);
607 path = mutt_hcache_per_folder(path, folder);
609 if (option(OPTHCACHECOMPRESS))
612 h->db = vlopen(path, flags, VL_CMPLEX);
617 p_delete(&h->folder);
625 mutt_hcache_close(void *db)
627 struct header_cache *h = db;
633 p_delete(&h->folder);
638 mutt_hcache_fetch(void *db, const char *filename,
639 ssize_t(*keylen) (const char *fn))
641 struct header_cache *h = db;
642 char path[_POSIX_PATH_MAX];
649 m_strcpy(path, sizeof(path), h->folder);
650 m_strcat(path, sizeof(path), filename);
652 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
654 data = vlget(h->db, path, ksize, NULL);
656 if (!crc32_matches(data, h->crc))
666 mutt_hcache_store(void *db, const char *filename, HEADER * header,
667 unsigned long uid_validity,
668 ssize_t(*keylen) (const char *fn))
670 struct header_cache *h = db;
671 char path[_POSIX_PATH_MAX];
679 m_strcpy(path, sizeof(path), h->folder);
680 m_strcat(path, sizeof(path), filename);
682 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
684 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
686 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
694 mutt_hcache_delete(void *db, const char *filename,
695 ssize_t(*keylen) (const char *fn))
697 struct header_cache *h = db;
698 char path[_POSIX_PATH_MAX];
704 m_strcpy(path, sizeof(path), h->folder);
705 m_strcat(path, sizeof(path), filename);
707 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
709 return vlout(h->db, path, ksize);
712 #elif defined(HAVE_GDBM)
714 void *mutt_hcache_open (const char *path, const char *folder)
716 struct header_cache *h = p_new(struct header_cache, 1);
718 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
720 h->folder = m_strdup(folder);
721 h->crc = generate_crc32 ();
723 if (!path || path[0] == '\0') {
724 p_delete(&h->folder);
729 path = mutt_hcache_per_folder (path, folder);
731 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
736 /* if rw failed try ro */
737 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
742 p_delete(&h->folder);
749 void mutt_hcache_close (void *db)
751 struct header_cache *h = db;
758 p_delete(&h->folder);
762 void *mutt_hcache_fetch (void *db, const char *filename,
763 ssize_t (*keylen) (const char *fn))
765 struct header_cache *h = db;
768 char path[_POSIX_PATH_MAX];
774 m_strcpy(path, sizeof(path), h->folder);
775 strncat (path, filename, sizeof (path) - m_strlen(path));
778 key.dsize = keylen (path);
780 data = gdbm_fetch (h->db, key);
782 if (!crc32_matches (data.dptr, h->crc)) {
783 p_delete(&data.dptr);
791 mutt_hcache_store (void *db, const char *filename, HEADER * header,
792 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
794 struct header_cache *h = db;
797 char path[_POSIX_PATH_MAX];
804 m_strcpy(path, sizeof(path), h->folder);
805 strncat (path, filename, sizeof (path) - m_strlen(path));
808 key.dsize = keylen (path);
810 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
812 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
814 p_delete(&data.dptr);
820 mutt_hcache_delete (void *db, const char *filename,
821 ssize_t (*keylen) (const char *fn))
824 struct header_cache *h = db;
825 char path[_POSIX_PATH_MAX];
831 m_strcpy(path, sizeof(path), h->folder);
832 strncat (path, filename, sizeof (path) - m_strlen(path));
835 key.dsize = keylen (path);
837 return gdbm_delete (h->db, key);
839 #elif defined(HAVE_DB4)
841 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
844 dbt->size = dbt->ulen = len;
845 dbt->dlen = dbt->doff = 0;
846 dbt->flags = DB_DBT_USERMEM;
849 static void mutt_hcache_dbt_empty_init (DBT * dbt)
852 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
856 void *mutt_hcache_open (const char *path, const char *folder)
859 u_int32_t createflags = DB_CREATE;
861 struct header_cache *h = p_new(struct header_cache, 1);
862 int pagesize = atoi (HeaderCachePageSize);
865 h->crc = generate_crc32 ();
867 if (!path || path[0] == '\0') {
872 path = mutt_hcache_per_folder (path, folder);
874 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
876 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
882 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
888 ret = db_env_create (&h->env, 0);
890 mx_unlock_file (h->lockfile, h->fd, 0);
897 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
899 ret = db_create (&h->db, h->env, 0);
901 h->env->close (h->env, 0);
902 mx_unlock_file (h->lockfile, h->fd, 0);
909 if (stat (path, &sb) != 0 && errno == ENOENT) {
910 createflags |= DB_EXCL;
911 h->db->set_pagesize (h->db, pagesize);
914 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
916 h->db->close (h->db, 0);
917 h->env->close (h->env, 0);
918 mx_unlock_file (h->lockfile, h->fd, 0);
927 void mutt_hcache_close (void *db)
929 struct header_cache *h = db;
935 h->db->close (h->db, 0);
936 h->env->close (h->env, 0);
937 mx_unlock_file (h->lockfile, h->fd, 0);
942 void *mutt_hcache_fetch (void *db, const char *filename,
943 ssize_t (*keylen) (const char *fn))
947 struct header_cache *h = db;
953 filename++; /* skip '/' */
955 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
956 mutt_hcache_dbt_empty_init (&data);
957 data.flags = DB_DBT_MALLOC;
959 h->db->get (h->db, NULL, &key, &data, 0);
961 if (!crc32_matches (data.data, h->crc)) {
962 p_delete(&data.data);
970 mutt_hcache_store (void *db, const char *filename, HEADER * header,
971 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
976 struct header_cache *h = db;
982 filename++; /* skip '/' */
984 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
986 mutt_hcache_dbt_empty_init (&data);
987 data.flags = DB_DBT_USERMEM;
989 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
990 data.ulen = data.size;
992 ret = h->db->put (h->db, NULL, &key, &data, 0);
994 p_delete(&data.data);
1000 mutt_hcache_delete (void *db, const char *filename,
1001 ssize_t (*keylen) (const char *fn))
1004 struct header_cache *h = db;
1010 filename++; /* skip '/' */
1012 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1013 return h->db->del (h->db, NULL, &key, 0);
1017 #endif /* USE_HCACHE */