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_t * 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_t ** p, const unsigned char *d, int *off)
234 unsigned int counter;
236 restore_int (&counter, d, off);
239 *p = parameter_new();
240 restore_char (&(*p)->attribute, d, off);
241 restore_char (&(*p)->value, d, off);
247 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
249 lazy_realloc (&d, *off + sizeof (BODY));
250 memcpy (d + *off, c, sizeof (BODY));
251 *off += sizeof (BODY);
253 d = dump_char (c->xtype, d, off);
254 d = dump_char (c->subtype, d, off);
256 d = dump_parameter (c->parameter, d, off);
258 d = dump_char (c->description, d, off);
259 d = dump_char (c->form_name, d, off);
260 d = dump_char (c->filename, d, off);
261 d = dump_char (c->d_filename, d, off);
266 static void restore_body (BODY * c, const unsigned char *d, int *off)
268 memcpy (c, d + *off, sizeof (BODY));
269 *off += sizeof (BODY);
271 restore_char (&c->xtype, d, off);
272 restore_char (&c->subtype, d, off);
274 restore_parameter (&c->parameter, d, off);
276 restore_char (&c->description, d, off);
277 restore_char (&c->form_name, d, off);
278 restore_char (&c->filename, d, off);
279 restore_char (&c->d_filename, d, off);
282 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
284 d = dump_address (e->return_path, d, off);
285 d = dump_address (e->from, d, off);
286 d = dump_address (e->to, d, off);
287 d = dump_address (e->cc, d, off);
288 d = dump_address (e->bcc, d, off);
289 d = dump_address (e->sender, d, off);
290 d = dump_address (e->reply_to, d, off);
291 d = dump_address (e->mail_followup_to, d, off);
293 d = dump_char (e->subject, d, off);
295 d = dump_int (e->real_subj - e->subject, d, off);
298 d = dump_int (-1, d, off);
300 d = dump_char (e->message_id, d, off);
301 d = dump_char (e->supersedes, d, off);
302 d = dump_char (e->date, d, off);
303 d = dump_char (e->x_label, d, off);
304 d = dump_char (e->list_post, d, off);
307 d = dump_char (e->newsgroups, d, off);
308 d = dump_char (e->xref, d, off);
309 d = dump_char (e->followup_to, d, off);
310 d = dump_char (e->x_comment_to, d, off);
313 d = dump_list (e->references, d, off);
314 d = dump_list (e->in_reply_to, d, off);
315 d = dump_list (e->userhdrs, d, off);
320 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
324 restore_address (&e->return_path, d, off);
325 restore_address (&e->from, d, off);
326 restore_address (&e->to, d, off);
327 restore_address (&e->cc, d, off);
328 restore_address (&e->bcc, d, off);
329 restore_address (&e->sender, d, off);
330 restore_address (&e->reply_to, d, off);
331 restore_address (&e->mail_followup_to, d, off);
333 restore_char (&e->subject, d, off);
334 restore_int ((unsigned int *) (&real_subj_off), d, off);
335 if (0 <= real_subj_off) {
336 e->real_subj = e->subject + real_subj_off;
341 restore_char (&e->message_id, d, off);
342 restore_char (&e->supersedes, d, off);
343 restore_char (&e->date, d, off);
344 restore_char (&e->x_label, d, off);
345 restore_char (&e->list_post, d, off);
348 restore_char (&e->newsgroups, d, off);
349 restore_char (&e->xref, d, off);
350 restore_char (&e->followup_to, d, off);
351 restore_char (&e->x_comment_to, d, off);
354 restore_list (&e->references, d, off);
355 restore_list (&e->in_reply_to, d, off);
356 restore_list (&e->userhdrs, d, off);
360 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
366 for (i = 0; i < 8; i++)
367 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
372 static int generate_crc32 ()
376 crc = crc32 (crc, (unsigned char const *)
377 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
379 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
381 #ifdef HAVE_LANGINFO_CODESET
382 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
383 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
384 m_strlen("HAVE_LANGINFO_CODESET"));
387 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
390 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
391 m_strlen("MIXMASTER"));
394 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
397 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
402 static int crc32_matches (const char *d, unsigned int crc)
404 int off = sizeof (validate);
405 unsigned int mycrc = 0;
411 restore_int (&mycrc, (unsigned char *) d, &off);
413 return (crc == mycrc);
416 /* Append md5sumed folder to path if path is a directory. */
417 static const char *mutt_hcache_per_folder (const char *path,
420 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
421 struct stat path_stat;
423 unsigned char md5sum[16];
426 ret = stat (path, &path_stat);
431 if (!S_ISDIR (path_stat.st_mode)) {
436 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
437 MD5Final (md5sum, &md5);
439 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
440 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
441 "%02x%02x%02x%02x%02x%02x%02x%02x",
442 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
443 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
444 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
445 md5sum[13], md5sum[14], md5sum[15]);
451 return mutt_hcache_per_folder_path;
454 /* This function transforms a header into a char so that it is useable by
456 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
457 unsigned long uid_validity)
459 struct header_cache *db = _db;
460 unsigned char *d = NULL;
464 d = lazy_malloc (sizeof (validate));
467 memcpy (d, &uid_validity, sizeof (unsigned long));
472 gettimeofday (&now, NULL);
473 memcpy (d, &now, sizeof (struct timeval));
475 *off += sizeof (validate);
477 d = dump_int (db->crc, d, off);
479 lazy_realloc (&d, *off + sizeof (HEADER));
480 memcpy (d + *off, h, sizeof (HEADER));
481 *off += sizeof (HEADER);
483 d = dump_envelope (h->env, d, off);
484 d = dump_body (h->content, d, off);
485 d = dump_char (h->maildir_flags, d, off);
490 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
493 HEADER *h = header_new();
496 off += sizeof (validate);
499 off += sizeof (unsigned int);
501 memcpy (h, d + off, sizeof (HEADER));
502 off += sizeof (HEADER);
504 h->env = envelope_new();
505 restore_envelope (h->env, d, &off);
507 h->content = body_new();
508 restore_body (h->content, d, &off);
510 restore_char (&h->maildir_flags, d, &off);
512 /* this is needed for maildir style mailboxes */
515 h->path = m_strdup((*oh)->path);
522 #if defined(HAVE_QDBM)
524 mutt_hcache_open(const char *path, const char *folder)
526 struct header_cache *h = p_new(struct header_cache, 1);
527 int flags = VL_OWRITER | VL_OCREAT;
529 h->folder = m_strdup(folder);
530 h->crc = generate_crc32();
532 if (!path || path[0] == '\0')
534 p_delete(&h->folder);
539 path = mutt_hcache_per_folder(path, folder);
541 if (option(OPTHCACHECOMPRESS))
544 h->db = vlopen(path, flags, VL_CMPLEX);
549 p_delete(&h->folder);
557 mutt_hcache_close(void *db)
559 struct header_cache *h = db;
565 p_delete(&h->folder);
570 mutt_hcache_fetch(void *db, const char *filename,
571 ssize_t(*keylen) (const char *fn))
573 struct header_cache *h = db;
574 char path[_POSIX_PATH_MAX];
581 m_strcpy(path, sizeof(path), h->folder);
582 m_strcat(path, sizeof(path), filename);
584 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
586 data = vlget(h->db, path, ksize, NULL);
588 if (!crc32_matches(data, h->crc))
598 mutt_hcache_store(void *db, const char *filename, HEADER * header,
599 unsigned long uid_validity,
600 ssize_t(*keylen) (const char *fn))
602 struct header_cache *h = db;
603 char path[_POSIX_PATH_MAX];
611 m_strcpy(path, sizeof(path), h->folder);
612 m_strcat(path, sizeof(path), filename);
614 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
616 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
618 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
626 mutt_hcache_delete(void *db, const char *filename,
627 ssize_t(*keylen) (const char *fn))
629 struct header_cache *h = db;
630 char path[_POSIX_PATH_MAX];
636 m_strcpy(path, sizeof(path), h->folder);
637 m_strcat(path, sizeof(path), filename);
639 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
641 return vlout(h->db, path, ksize);
644 #elif defined(HAVE_GDBM)
646 void *mutt_hcache_open (const char *path, const char *folder)
648 struct header_cache *h = p_new(struct header_cache, 1);
650 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
652 h->folder = m_strdup(folder);
653 h->crc = generate_crc32 ();
655 if (!path || path[0] == '\0') {
656 p_delete(&h->folder);
661 path = mutt_hcache_per_folder (path, folder);
663 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
668 /* if rw failed try ro */
669 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
674 p_delete(&h->folder);
681 void mutt_hcache_close (void *db)
683 struct header_cache *h = db;
690 p_delete(&h->folder);
694 void *mutt_hcache_fetch (void *db, const char *filename,
695 ssize_t (*keylen) (const char *fn))
697 struct header_cache *h = db;
700 char path[_POSIX_PATH_MAX];
706 m_strcpy(path, sizeof(path), h->folder);
707 strncat (path, filename, sizeof (path) - m_strlen(path));
710 key.dsize = keylen (path);
712 data = gdbm_fetch (h->db, key);
714 if (!crc32_matches (data.dptr, h->crc)) {
715 p_delete(&data.dptr);
723 mutt_hcache_store (void *db, const char *filename, HEADER * header,
724 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
726 struct header_cache *h = db;
729 char path[_POSIX_PATH_MAX];
736 m_strcpy(path, sizeof(path), h->folder);
737 strncat (path, filename, sizeof (path) - m_strlen(path));
740 key.dsize = keylen (path);
742 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
744 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
746 p_delete(&data.dptr);
752 mutt_hcache_delete (void *db, const char *filename,
753 ssize_t (*keylen) (const char *fn))
756 struct header_cache *h = db;
757 char path[_POSIX_PATH_MAX];
763 m_strcpy(path, sizeof(path), h->folder);
764 strncat (path, filename, sizeof (path) - m_strlen(path));
767 key.dsize = keylen (path);
769 return gdbm_delete (h->db, key);
771 #elif defined(HAVE_DB4)
773 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
776 dbt->size = dbt->ulen = len;
777 dbt->dlen = dbt->doff = 0;
778 dbt->flags = DB_DBT_USERMEM;
781 static void mutt_hcache_dbt_empty_init (DBT * dbt)
784 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
788 void *mutt_hcache_open (const char *path, const char *folder)
791 u_int32_t createflags = DB_CREATE;
793 struct header_cache *h = p_new(struct header_cache, 1);
794 int pagesize = atoi (HeaderCachePageSize);
797 h->crc = generate_crc32 ();
799 if (!path || path[0] == '\0') {
804 path = mutt_hcache_per_folder (path, folder);
806 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
808 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
814 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
820 ret = db_env_create (&h->env, 0);
822 mx_unlock_file (h->lockfile, h->fd, 0);
829 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
831 ret = db_create (&h->db, h->env, 0);
833 h->env->close (h->env, 0);
834 mx_unlock_file (h->lockfile, h->fd, 0);
841 if (stat (path, &sb) != 0 && errno == ENOENT) {
842 createflags |= DB_EXCL;
843 h->db->set_pagesize (h->db, pagesize);
846 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
848 h->db->close (h->db, 0);
849 h->env->close (h->env, 0);
850 mx_unlock_file (h->lockfile, h->fd, 0);
859 void mutt_hcache_close (void *db)
861 struct header_cache *h = db;
867 h->db->close (h->db, 0);
868 h->env->close (h->env, 0);
869 mx_unlock_file (h->lockfile, h->fd, 0);
874 void *mutt_hcache_fetch (void *db, const char *filename,
875 ssize_t (*keylen) (const char *fn))
879 struct header_cache *h = db;
885 filename++; /* skip '/' */
887 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
888 mutt_hcache_dbt_empty_init (&data);
889 data.flags = DB_DBT_MALLOC;
891 h->db->get (h->db, NULL, &key, &data, 0);
893 if (!crc32_matches (data.data, h->crc)) {
894 p_delete(&data.data);
902 mutt_hcache_store (void *db, const char *filename, HEADER * header,
903 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
908 struct header_cache *h = db;
914 filename++; /* skip '/' */
916 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
918 mutt_hcache_dbt_empty_init (&data);
919 data.flags = DB_DBT_USERMEM;
921 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
922 data.ulen = data.size;
924 ret = h->db->put (h->db, NULL, &key, &data, 0);
926 p_delete(&data.data);
932 mutt_hcache_delete (void *db, const char *filename,
933 ssize_t (*keylen) (const char *fn))
936 struct header_cache *h = db;
942 filename++; /* skip '/' */
944 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
945 return h->db->del (h->db, NULL, &key, 0);
949 #endif /* USE_HCACHE */