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.
12 #include <lib-lib/lib-lib.h>
16 #define MUTTNG_HCACHE_ID "0x004"
18 #if defined(HAVE_QDBM)
22 #elif defined(HAVE_GDBM)
24 #elif defined(HAVE_DB4)
28 #include <lib-hash/hash.h>
29 #include <lib-mime/mime.h>
31 #include <imap/message.h>
38 #if defined(HAVE_QDBM)
42 #elif defined(HAVE_GDBM)
46 #elif defined(HAVE_DB4)
51 char lockfile[_POSIX_PATH_MAX];
56 struct timeval timeval;
57 unsigned long uid_validity;
60 #define UPPER4K(i) ((i & ~(4096 - 1)) + 4096)
62 static unsigned char *lazy_malloc(ssize_t siz)
64 return p_new(unsigned char, UPPER4K(siz));
67 static void lazy_realloc(unsigned char **p, ssize_t siz)
69 p_realloc(p, UPPER4K(siz));
72 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
74 lazy_realloc (&d, *off + sizeof (int));
75 memcpy (d + *off, &i, sizeof (int));
76 (*off) += sizeof (int);
81 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
83 memcpy (i, d + *off, sizeof (int));
84 (*off) += sizeof (int);
87 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
93 d = dump_int (size, d, off);
97 size = m_strlen(c) + 1;
98 d = dump_int (size, d, off);
99 lazy_realloc (&d, *off + size);
100 memcpy (d + *off, c, size);
106 static void restore_char (char **c, const unsigned char *d, int *off)
110 restore_int (&size, d, off);
117 *c = p_dup(d + *off, size);
121 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
123 unsigned int counter = 0;
124 unsigned int start_off = *off;
126 d = dump_int (0xdeadbeef, d, off);
129 d = dump_char (a->personal, d, off);
130 d = dump_char (a->mailbox, d, off);
131 d = dump_int (a->group, d, off);
136 memcpy (d + start_off, &counter, sizeof (int));
141 static void restore_address (address_t ** a, const unsigned char *d, int *off)
143 unsigned int counter;
145 restore_int (&counter, d, off);
148 *a = p_new(address_t, 1);
149 restore_char (&(*a)->personal, d, off);
150 restore_char (&(*a)->mailbox, d, off);
151 restore_int ((unsigned int *) &(*a)->group, d, off);
159 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
161 unsigned int counter = 0;
162 unsigned int start_off = *off;
164 d = dump_int (0xdeadbeef, d, off);
167 d = dump_char (l->data, d, off);
172 memcpy (d + start_off, &counter, sizeof (int));
177 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
179 unsigned int counter;
181 restore_int (&counter, d, off);
184 *l = p_new(string_list_t, 1);
185 restore_char (&(*l)->data, d, off);
193 static unsigned char *dump_parameter (parameter_t * p, unsigned char *d,
196 unsigned int counter = 0;
197 unsigned int start_off = *off;
199 d = dump_int (0xdeadbeef, d, off);
202 d = dump_char (p->attribute, d, off);
203 d = dump_char (p->value, d, off);
208 memcpy (d + start_off, &counter, sizeof (int));
214 restore_parameter (parameter_t ** p, const unsigned char *d, int *off)
216 unsigned int counter;
218 restore_int (&counter, d, off);
221 *p = parameter_new();
222 restore_char (&(*p)->attribute, d, off);
223 restore_char (&(*p)->value, d, off);
229 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
231 lazy_realloc (&d, *off + sizeof (BODY));
232 memcpy (d + *off, c, sizeof (BODY));
233 *off += sizeof (BODY);
235 d = dump_char (c->xtype, d, off);
236 d = dump_char (c->subtype, d, off);
238 d = dump_parameter (c->parameter, d, off);
240 d = dump_char (c->description, d, off);
241 d = dump_char (c->form_name, d, off);
242 d = dump_char (c->filename, d, off);
243 d = dump_char (c->d_filename, d, off);
248 static void restore_body (BODY * c, const unsigned char *d, int *off)
250 memcpy (c, d + *off, sizeof (BODY));
251 *off += sizeof (BODY);
253 restore_char (&c->xtype, d, off);
254 restore_char (&c->subtype, d, off);
256 restore_parameter (&c->parameter, d, off);
258 restore_char (&c->description, d, off);
259 restore_char (&c->form_name, d, off);
260 restore_char (&c->filename, d, off);
261 restore_char (&c->d_filename, d, off);
264 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
266 d = dump_address (e->return_path, d, off);
267 d = dump_address (e->from, d, off);
268 d = dump_address (e->to, d, off);
269 d = dump_address (e->cc, d, off);
270 d = dump_address (e->bcc, d, off);
271 d = dump_address (e->sender, d, off);
272 d = dump_address (e->reply_to, d, off);
273 d = dump_address (e->mail_followup_to, d, off);
275 d = dump_char (e->subject, d, off);
277 d = dump_int (e->real_subj - e->subject, d, off);
280 d = dump_int (-1, d, off);
282 d = dump_char (e->message_id, d, off);
283 d = dump_char (e->supersedes, d, off);
284 d = dump_char (e->date, d, off);
285 d = dump_char (e->x_label, d, off);
286 d = dump_char (e->list_post, d, off);
289 d = dump_char (e->newsgroups, d, off);
290 d = dump_char (e->xref, d, off);
291 d = dump_char (e->followup_to, d, off);
292 d = dump_char (e->x_comment_to, d, off);
295 d = dump_list (e->references, d, off);
296 d = dump_list (e->in_reply_to, d, off);
297 d = dump_list (e->userhdrs, d, off);
302 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
306 restore_address (&e->return_path, d, off);
307 restore_address (&e->from, d, off);
308 restore_address (&e->to, d, off);
309 restore_address (&e->cc, d, off);
310 restore_address (&e->bcc, d, off);
311 restore_address (&e->sender, d, off);
312 restore_address (&e->reply_to, d, off);
313 restore_address (&e->mail_followup_to, d, off);
315 restore_char (&e->subject, d, off);
316 restore_int ((unsigned int *) (&real_subj_off), d, off);
317 if (0 <= real_subj_off) {
318 e->real_subj = e->subject + real_subj_off;
323 restore_char (&e->message_id, d, off);
324 restore_char (&e->supersedes, d, off);
325 restore_char (&e->date, d, off);
326 restore_char (&e->x_label, d, off);
327 restore_char (&e->list_post, d, off);
330 restore_char (&e->newsgroups, d, off);
331 restore_char (&e->xref, d, off);
332 restore_char (&e->followup_to, d, off);
333 restore_char (&e->x_comment_to, d, off);
336 restore_list (&e->references, d, off);
337 restore_list (&e->in_reply_to, d, off);
338 restore_list (&e->userhdrs, d, off);
342 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
348 for (i = 0; i < 8; i++)
349 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
354 static int generate_crc32 ()
358 crc = crc32 (crc, (unsigned char const *)
359 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
361 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
363 #ifdef HAVE_LANGINFO_CODESET
364 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
365 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
366 m_strlen("HAVE_LANGINFO_CODESET"));
369 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
372 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
373 m_strlen("MIXMASTER"));
376 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
379 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
384 static int crc32_matches (const char *d, unsigned int crc)
386 int off = sizeof (validate);
387 unsigned int mycrc = 0;
393 restore_int (&mycrc, (unsigned char *) d, &off);
395 return (crc == mycrc);
398 /* Append md5sumed folder to path if path is a directory. */
399 static const char *mutt_hcache_per_folder (const char *path,
402 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
403 struct stat path_stat;
405 unsigned char md5sum[16];
408 ret = stat (path, &path_stat);
413 if (!S_ISDIR (path_stat.st_mode)) {
418 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
419 MD5Final (md5sum, &md5);
421 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
422 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
423 "%02x%02x%02x%02x%02x%02x%02x%02x",
424 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
425 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
426 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
427 md5sum[13], md5sum[14], md5sum[15]);
433 return mutt_hcache_per_folder_path;
436 /* This function transforms a header into a char so that it is useable by
438 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
439 unsigned long uid_validity)
441 struct header_cache *db = _db;
442 unsigned char *d = NULL;
446 d = lazy_malloc (sizeof (validate));
449 memcpy (d, &uid_validity, sizeof (unsigned long));
454 gettimeofday (&now, NULL);
455 memcpy (d, &now, sizeof (struct timeval));
457 *off += sizeof (validate);
459 d = dump_int (db->crc, d, off);
461 lazy_realloc (&d, *off + sizeof (HEADER));
462 memcpy (d + *off, h, sizeof (HEADER));
463 *off += sizeof (HEADER);
465 d = dump_envelope (h->env, d, off);
466 d = dump_body (h->content, d, off);
467 d = dump_char (h->maildir_flags, d, off);
472 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
475 HEADER *h = header_new();
478 off += sizeof (validate);
481 off += sizeof (unsigned int);
483 memcpy (h, d + off, sizeof (HEADER));
484 off += sizeof (HEADER);
486 h->env = envelope_new();
487 restore_envelope (h->env, d, &off);
489 h->content = body_new();
490 restore_body (h->content, d, &off);
492 restore_char (&h->maildir_flags, d, &off);
494 /* this is needed for maildir style mailboxes */
497 h->path = m_strdup((*oh)->path);
504 #if defined(HAVE_QDBM)
506 mutt_hcache_open(const char *path, const char *folder)
508 struct header_cache *h = p_new(struct header_cache, 1);
509 int flags = VL_OWRITER | VL_OCREAT;
511 h->folder = m_strdup(folder);
512 h->crc = generate_crc32();
514 if (!path || path[0] == '\0')
516 p_delete(&h->folder);
521 path = mutt_hcache_per_folder(path, folder);
523 if (option(OPTHCACHECOMPRESS))
526 h->db = vlopen(path, flags, VL_CMPLEX);
531 p_delete(&h->folder);
539 mutt_hcache_close(void *db)
541 struct header_cache *h = db;
547 p_delete(&h->folder);
552 mutt_hcache_fetch(void *db, const char *filename,
553 ssize_t(*keylen) (const char *fn))
555 struct header_cache *h = db;
556 char path[_POSIX_PATH_MAX];
563 m_strcpy(path, sizeof(path), h->folder);
564 m_strcat(path, sizeof(path), filename);
566 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
568 data = vlget(h->db, path, ksize, NULL);
570 if (!crc32_matches(data, h->crc))
580 mutt_hcache_store(void *db, const char *filename, HEADER * header,
581 unsigned long uid_validity,
582 ssize_t(*keylen) (const char *fn))
584 struct header_cache *h = db;
585 char path[_POSIX_PATH_MAX];
593 m_strcpy(path, sizeof(path), h->folder);
594 m_strcat(path, sizeof(path), filename);
596 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
598 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
600 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
608 mutt_hcache_delete(void *db, const char *filename,
609 ssize_t(*keylen) (const char *fn))
611 struct header_cache *h = db;
612 char path[_POSIX_PATH_MAX];
618 m_strcpy(path, sizeof(path), h->folder);
619 m_strcat(path, sizeof(path), filename);
621 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
623 return vlout(h->db, path, ksize);
626 #elif defined(HAVE_GDBM)
628 void *mutt_hcache_open (const char *path, const char *folder)
630 struct header_cache *h = p_new(struct header_cache, 1);
632 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
634 h->folder = m_strdup(folder);
635 h->crc = generate_crc32 ();
637 if (!path || path[0] == '\0') {
638 p_delete(&h->folder);
643 path = mutt_hcache_per_folder (path, folder);
645 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
650 /* if rw failed try ro */
651 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
656 p_delete(&h->folder);
663 void mutt_hcache_close (void *db)
665 struct header_cache *h = db;
672 p_delete(&h->folder);
676 void *mutt_hcache_fetch (void *db, const char *filename,
677 ssize_t (*keylen) (const char *fn))
679 struct header_cache *h = db;
682 char path[_POSIX_PATH_MAX];
688 m_strcpy(path, sizeof(path), h->folder);
689 strncat (path, filename, sizeof (path) - m_strlen(path));
692 key.dsize = keylen (path);
694 data = gdbm_fetch (h->db, key);
696 if (!crc32_matches (data.dptr, h->crc)) {
697 p_delete(&data.dptr);
705 mutt_hcache_store (void *db, const char *filename, HEADER * header,
706 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
708 struct header_cache *h = db;
711 char path[_POSIX_PATH_MAX];
718 m_strcpy(path, sizeof(path), h->folder);
719 strncat (path, filename, sizeof (path) - m_strlen(path));
722 key.dsize = keylen (path);
724 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
726 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
728 p_delete(&data.dptr);
734 mutt_hcache_delete (void *db, const char *filename,
735 ssize_t (*keylen) (const char *fn))
738 struct header_cache *h = db;
739 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 return gdbm_delete (h->db, key);
753 #elif defined(HAVE_DB4)
755 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
758 dbt->size = dbt->ulen = len;
759 dbt->dlen = dbt->doff = 0;
760 dbt->flags = DB_DBT_USERMEM;
763 static void mutt_hcache_dbt_empty_init (DBT * dbt)
766 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
770 void *mutt_hcache_open (const char *path, const char *folder)
773 u_int32_t createflags = DB_CREATE;
775 struct header_cache *h = p_new(struct header_cache, 1);
776 int pagesize = atoi (HeaderCachePageSize);
779 h->crc = generate_crc32 ();
781 if (!path || path[0] == '\0') {
786 path = mutt_hcache_per_folder (path, folder);
788 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
790 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
796 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
802 ret = db_env_create (&h->env, 0);
804 mx_unlock_file (h->lockfile, h->fd, 0);
811 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
813 ret = db_create (&h->db, h->env, 0);
815 h->env->close (h->env, 0);
816 mx_unlock_file (h->lockfile, h->fd, 0);
823 if (stat (path, &sb) != 0 && errno == ENOENT) {
824 createflags |= DB_EXCL;
825 h->db->set_pagesize (h->db, pagesize);
828 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
830 h->db->close (h->db, 0);
831 h->env->close (h->env, 0);
832 mx_unlock_file (h->lockfile, h->fd, 0);
841 void mutt_hcache_close (void *db)
843 struct header_cache *h = db;
849 h->db->close (h->db, 0);
850 h->env->close (h->env, 0);
851 mx_unlock_file (h->lockfile, h->fd, 0);
856 void *mutt_hcache_fetch (void *db, const char *filename,
857 ssize_t (*keylen) (const char *fn))
861 struct header_cache *h = db;
867 filename++; /* skip '/' */
869 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
870 mutt_hcache_dbt_empty_init (&data);
871 data.flags = DB_DBT_MALLOC;
873 h->db->get (h->db, NULL, &key, &data, 0);
875 if (!crc32_matches (data.data, h->crc)) {
876 p_delete(&data.data);
884 mutt_hcache_store (void *db, const char *filename, HEADER * header,
885 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
890 struct header_cache *h = db;
896 filename++; /* skip '/' */
898 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
900 mutt_hcache_dbt_empty_init (&data);
901 data.flags = DB_DBT_USERMEM;
903 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
904 data.ulen = data.size;
906 ret = h->db->put (h->db, NULL, &key, &data, 0);
908 p_delete(&data.data);
914 mutt_hcache_delete (void *db, const char *filename,
915 ssize_t (*keylen) (const char *fn))
918 struct header_cache *h = db;
924 filename++; /* skip '/' */
926 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
927 return h->db->del (h->db, NULL, &key, 0);
931 #endif /* USE_HCACHE */