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 # include <inttypes.h>
22 #endif /* HAVE_CONFIG_H */
62 char lockfile[_POSIX_PATH_MAX];
67 struct timeval timeval;
68 unsigned long uid_validity;
71 static void *lazy_malloc (size_t siz)
73 if (0 < siz && siz < 4096) {
77 return safe_malloc (siz);
80 static void lazy_realloc (void *ptr, size_t siz)
82 void **p = (void **) ptr;
84 if (p != NULL && 0 < siz && siz < 4096) {
88 safe_realloc (ptr, siz);
91 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
93 lazy_realloc (&d, *off + sizeof (int));
94 memcpy (d + *off, &i, sizeof (int));
95 (*off) += sizeof (int);
100 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
102 memcpy (i, d + *off, sizeof (int));
103 (*off) += sizeof (int);
106 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
112 d = dump_int (size, d, off);
116 size = safe_strlen (c) + 1;
117 d = dump_int (size, d, off);
118 lazy_realloc (&d, *off + size);
119 memcpy (d + *off, c, size);
126 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
131 d = dump_int (size, d, off);
135 d = dump_int (size, d, off);
136 lazy_realloc (&d, *off + size);
137 memcpy (d + *off, c, size);
144 static void restore_char (char **c, const unsigned char *d, int *off)
148 restore_int (&size, d, off);
155 *c = safe_malloc (size);
156 memcpy (*c, d + *off, size);
160 static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off)
162 unsigned int counter = 0;
163 unsigned int start_off = *off;
165 d = dump_int (0xdeadbeef, d, off);
169 d = dump_char (a->val, d, off);
171 d = dump_char (a->personal, d, off);
172 d = dump_char (a->mailbox, d, off);
173 d = dump_int (a->group, d, off);
178 memcpy (d + start_off, &counter, sizeof (int));
183 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
185 unsigned int counter;
187 restore_int (&counter, d, off);
190 *a = safe_malloc (sizeof (ADDRESS));
192 restore_char (&(*a)->val, d, off);
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 (LIST * 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 (LIST ** l, const unsigned char *d, int *off)
224 unsigned int counter;
226 restore_int (&counter, d, off);
229 *l = safe_malloc (sizeof (LIST));
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 = safe_malloc (sizeof (BUFFER));
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 = safe_malloc (sizeof (PARAMETER));
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);
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);
414 restore_char (&e->newsgroups, d, off);
415 restore_char (&e->xref, d, off);
416 restore_char (&e->followup_to, d, off);
417 restore_char (&e->x_comment_to, d, off);
420 restore_list (&e->references, d, off);
421 restore_list (&e->in_reply_to, d, off);
422 restore_list (&e->userhdrs, d, off);
426 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
432 for (i = 0; i < 8; i++)
433 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
438 static int generate_crc32 ()
442 crc = crc32 (crc, (unsigned char const *)
443 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
445 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
447 #if HAVE_LANGINFO_CODESET
448 crc = crc32 (crc, (unsigned char const *) Charset, safe_strlen (Charset));
450 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
451 safe_strlen ("HAVE_LANGINFO_CODESET"));
456 crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
457 safe_strlen ("EXACT_ADDRESS"));
462 crc32 (crc, (unsigned char const *) "USE_POP", safe_strlen ("USE_POP"));
467 crc32 (crc, (unsigned char const *) "MIXMASTER",
468 safe_strlen ("MIXMASTER"));
473 crc32 (crc, (unsigned char const *) "USE_IMAP", safe_strlen ("USE_IMAP"));
478 crc32 (crc, (unsigned char const *) "USE_NNTP", safe_strlen ("USE_NNTP"));
483 static int crc32_matches (const char *d, unsigned int crc)
485 int off = sizeof (validate);
486 unsigned int mycrc = 0;
492 restore_int (&mycrc, (unsigned char *) d, &off);
494 return (crc == mycrc);
497 /* Append md5sumed folder to path if path is a directory. */
498 static const char *mutt_hcache_per_folder (const char *path,
501 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
502 struct stat path_stat;
504 unsigned char md5sum[16];
507 ret = stat (path, &path_stat);
512 if (!S_ISDIR (path_stat.st_mode)) {
517 MD5Update (&md5, (unsigned char *) folder, safe_strlen (folder));
518 MD5Final (md5sum, &md5);
520 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
521 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
522 "%02x%02x%02x%02x%02x%02x%02x%02x",
523 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
524 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
525 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
526 md5sum[13], md5sum[14], md5sum[15]);
532 return mutt_hcache_per_folder_path;
535 /* This function transforms a header into a char so that it is useable by
537 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
538 unsigned long uid_validity)
540 struct header_cache *db = _db;
541 unsigned char *d = NULL;
545 d = lazy_malloc (sizeof (validate));
548 memcpy (d, &uid_validity, sizeof (unsigned long));
553 gettimeofday (&now, NULL);
554 memcpy (d, &now, sizeof (struct timeval));
556 *off += sizeof (validate);
558 d = dump_int (db->crc, d, off);
560 lazy_realloc (&d, *off + sizeof (HEADER));
561 memcpy (d + *off, h, sizeof (HEADER));
562 *off += sizeof (HEADER);
564 d = dump_envelope (h->env, d, off);
565 d = dump_body (h->content, d, off);
566 d = dump_char (h->maildir_flags, d, off);
571 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
574 HEADER *h = mutt_new_header ();
577 off += sizeof (validate);
580 off += sizeof (unsigned int);
582 memcpy (h, d + off, sizeof (HEADER));
583 off += sizeof (HEADER);
585 h->env = mutt_new_envelope ();
586 restore_envelope (h->env, d, &off);
588 h->content = mutt_new_body ();
589 restore_body (h->content, d, &off);
591 restore_char (&h->maildir_flags, d, &off);
593 /* this is needed for maildir style mailboxes */
596 h->path = safe_strdup ((*oh)->path);
597 mutt_free_header (oh);
605 void *mutt_hcache_open (const char *path, const char *folder)
607 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
609 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
611 h->folder = safe_strdup (folder);
612 h->crc = generate_crc32 ();
614 if (!path || path[0] == '\0') {
620 path = mutt_hcache_per_folder (path, folder);
622 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
627 /* if rw failed try ro */
628 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
640 void mutt_hcache_close (void *db)
642 struct header_cache *h = db;
653 void *mutt_hcache_fetch (void *db, const char *filename,
654 size_t (*keylen) (const char *fn))
656 struct header_cache *h = db;
659 char path[_POSIX_PATH_MAX];
665 strncpy (path, h->folder, sizeof (path));
666 strncat (path, filename, sizeof (path) - safe_strlen (path));
669 key.dsize = keylen (path);
671 data = gdbm_fetch (h->db, key);
673 if (!crc32_matches (data.dptr, h->crc)) {
682 mutt_hcache_store (void *db, const char *filename, HEADER * header,
683 unsigned long uid_validity, size_t (*keylen) (const char *fn))
685 struct header_cache *h = db;
688 char path[_POSIX_PATH_MAX];
695 strncpy (path, h->folder, sizeof (path));
696 strncat (path, filename, sizeof (path) - safe_strlen (path));
699 key.dsize = keylen (path);
701 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
703 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
711 mutt_hcache_delete (void *db, const char *filename,
712 size_t (*keylen) (const char *fn))
715 struct header_cache *h = db;
716 char path[_POSIX_PATH_MAX];
722 strncpy (path, h->folder, sizeof (path));
723 strncat (path, filename, sizeof (path) - safe_strlen (path));
726 key.dsize = keylen (path);
728 return gdbm_delete (h->db, key);
732 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
735 dbt->size = dbt->ulen = len;
736 dbt->dlen = dbt->doff = 0;
737 dbt->flags = DB_DBT_USERMEM;
740 static void mutt_hcache_dbt_empty_init (DBT * dbt)
743 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
747 void *mutt_hcache_open (const char *path, const char *folder)
750 u_int32_t createflags = DB_CREATE;
752 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
753 int pagesize = atoi (HeaderCachePageSize);
756 h->crc = generate_crc32 ();
758 if (!path || path[0] == '\0') {
763 path = mutt_hcache_per_folder (path, folder);
765 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
767 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
773 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
779 ret = db_env_create (&h->env, 0);
781 mx_unlock_file (h->lockfile, h->fd, 0);
788 h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
790 ret = db_create (&h->db, h->env, 0);
792 h->env->close (h->env, 0);
793 mx_unlock_file (h->lockfile, h->fd, 0);
800 if (stat (path, &sb) != 0 && errno == ENOENT) {
801 createflags |= DB_EXCL;
802 h->db->set_pagesize (h->db, pagesize);
805 ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
807 h->db->close (h->db, 0);
808 h->env->close (h->env, 0);
809 mx_unlock_file (h->lockfile, h->fd, 0);
818 void mutt_hcache_close (void *db)
820 struct header_cache *h = db;
827 h->db->close (h->db, 0);
828 h->env->close (h->env, 0);
829 mx_unlock_file (h->lockfile, h->fd, 0);
834 void *mutt_hcache_fetch (void *db, const char *filename,
835 size_t (*keylen) (const char *fn))
839 struct header_cache *h = db;
845 filename++; /* skip '/' */
847 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
848 mutt_hcache_dbt_empty_init (&data);
849 data.flags = DB_DBT_MALLOC;
851 h->db->get (h->db, NULL, &key, &data, 0);
853 if (!crc32_matches (data.data, h->crc)) {
862 mutt_hcache_store (void *db, const char *filename, HEADER * header,
863 unsigned long uid_validity, size_t (*keylen) (const char *fn))
868 struct header_cache *h = db;
874 filename++; /* skip '/' */
876 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
878 mutt_hcache_dbt_empty_init (&data);
879 data.flags = DB_DBT_USERMEM;
881 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
882 data.ulen = data.size;
884 ret = h->db->put (h->db, NULL, &key, &data, 0);
892 mutt_hcache_delete (void *db, const char *filename,
893 size_t (*keylen) (const char *fn))
896 struct header_cache *h = db;
902 filename++; /* skip '/' */
904 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
905 return h->db->del (h->db, NULL, &key, 0);
909 #endif /* USE_HCACHE */