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);
373 d = dump_list (e->references, d, off);
374 d = dump_list (e->in_reply_to, d, off);
375 d = dump_list (e->userhdrs, d, off);
380 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
384 restore_address (&e->return_path, d, off);
385 restore_address (&e->from, d, off);
386 restore_address (&e->to, d, off);
387 restore_address (&e->cc, d, off);
388 restore_address (&e->bcc, d, off);
389 restore_address (&e->sender, d, off);
390 restore_address (&e->reply_to, d, off);
391 restore_address (&e->mail_followup_to, d, off);
393 restore_char (&e->subject, d, off);
394 restore_int ((unsigned int *) (&real_subj_off), d, off);
395 if (0 <= real_subj_off) {
396 e->real_subj = e->subject + real_subj_off;
401 restore_char (&e->message_id, d, off);
402 restore_char (&e->supersedes, d, off);
403 restore_char (&e->date, d, off);
404 restore_char (&e->x_label, d, off);
406 restore_list (&e->references, d, off);
407 restore_list (&e->in_reply_to, d, off);
408 restore_list (&e->userhdrs, d, off);
412 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
418 for (i = 0; i < 8; i++)
419 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
424 static int generate_crc32 ()
428 crc = crc32 (crc, (unsigned char const *)
429 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
431 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
433 #if HAVE_LANGINFO_CODESET
434 crc = crc32 (crc, (unsigned char const *) Charset, safe_strlen (Charset));
436 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
437 safe_strlen ("HAVE_LANGINFO_CODESET"));
442 crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
443 safe_strlen ("EXACT_ADDRESS"));
448 crc32 (crc, (unsigned char const *) "USE_POP", safe_strlen ("USE_POP"));
453 crc32 (crc, (unsigned char const *) "MIXMASTER",
454 safe_strlen ("MIXMASTER"));
459 crc32 (crc, (unsigned char const *) "USE_IMAP", safe_strlen ("USE_IMAP"));
464 static int crc32_matches (const char *d, unsigned int crc)
466 int off = sizeof (validate);
467 unsigned int mycrc = 0;
473 restore_int (&mycrc, (unsigned char *) d, &off);
475 return (crc == mycrc);
478 /* Append md5sumed folder to path if path is a directory. */
479 static const char *mutt_hcache_per_folder (const char *path,
482 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
483 struct stat path_stat;
485 unsigned char md5sum[16];
488 ret = stat (path, &path_stat);
493 if (!S_ISDIR (path_stat.st_mode)) {
498 MD5Update (&md5, (unsigned char *) folder, safe_strlen (folder));
499 MD5Final (md5sum, &md5);
501 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
502 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
503 "%02x%02x%02x%02x%02x%02x%02x%02x",
504 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
505 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
506 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
507 md5sum[13], md5sum[14], md5sum[15]);
513 return mutt_hcache_per_folder_path;
516 /* This function transforms a header into a char so that it is useable by
518 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
519 unsigned long uid_validity)
521 struct header_cache *db = _db;
522 unsigned char *d = NULL;
526 d = lazy_malloc (sizeof (validate));
529 memcpy (d, &uid_validity, sizeof (unsigned long));
534 gettimeofday (&now, NULL);
535 memcpy (d, &now, sizeof (struct timeval));
537 *off += sizeof (validate);
539 d = dump_int (db->crc, d, off);
541 lazy_realloc (&d, *off + sizeof (HEADER));
542 memcpy (d + *off, h, sizeof (HEADER));
543 *off += sizeof (HEADER);
545 d = dump_envelope (h->env, d, off);
546 d = dump_body (h->content, d, off);
547 d = dump_char (h->maildir_flags, d, off);
552 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
555 HEADER *h = mutt_new_header ();
558 off += sizeof (validate);
561 off += sizeof (unsigned int);
563 memcpy (h, d + off, sizeof (HEADER));
564 off += sizeof (HEADER);
566 h->env = mutt_new_envelope ();
567 restore_envelope (h->env, d, &off);
569 h->content = mutt_new_body ();
570 restore_body (h->content, d, &off);
572 restore_char (&h->maildir_flags, d, &off);
574 /* this is needed for maildir style mailboxes */
577 h->path = safe_strdup ((*oh)->path);
578 mutt_free_header (oh);
586 void *mutt_hcache_open (const char *path, const char *folder)
588 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
590 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
592 h->folder = safe_strdup (folder);
593 h->crc = generate_crc32 ();
595 if (!path || path[0] == '\0') {
601 path = mutt_hcache_per_folder (path, folder);
603 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
608 /* if rw failed try ro */
609 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
621 void mutt_hcache_close (void *db)
623 struct header_cache *h = db;
634 void *mutt_hcache_fetch (void *db, const char *filename,
635 size_t (*keylen) (const char *fn))
637 struct header_cache *h = db;
640 char path[_POSIX_PATH_MAX];
646 strncpy (path, h->folder, sizeof (path));
647 strncat (path, filename, sizeof (path) - safe_strlen (path));
650 key.dsize = keylen (path);
652 data = gdbm_fetch (h->db, key);
654 if (!crc32_matches (data.dptr, h->crc)) {
663 mutt_hcache_store (void *db, const char *filename, HEADER * header,
664 unsigned long uid_validity, size_t (*keylen) (const char *fn))
666 struct header_cache *h = db;
669 char path[_POSIX_PATH_MAX];
676 strncpy (path, h->folder, sizeof (path));
677 strncat (path, filename, sizeof (path) - safe_strlen (path));
680 key.dsize = keylen (path);
682 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
684 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
692 mutt_hcache_delete (void *db, const char *filename,
693 size_t (*keylen) (const char *fn))
696 struct header_cache *h = db;
697 char path[_POSIX_PATH_MAX];
703 strncpy (path, h->folder, sizeof (path));
704 strncat (path, filename, sizeof (path) - safe_strlen (path));
707 key.dsize = keylen (path);
709 return gdbm_delete (h->db, key);
713 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
716 dbt->size = dbt->ulen = len;
717 dbt->dlen = dbt->doff = 0;
718 dbt->flags = DB_DBT_USERMEM;
721 static void mutt_hcache_dbt_empty_init (DBT * dbt)
724 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
728 void *mutt_hcache_open (const char *path, const char *folder)
731 u_int32_t createflags = DB_CREATE;
733 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
734 int pagesize = atoi (HeaderCachePageSize);
737 h->crc = generate_crc32 ();
739 if (!path || path[0] == '\0') {
744 path = mutt_hcache_per_folder (path, folder);
746 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
748 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
754 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
760 ret = db_env_create (&h->env, 0);
762 mx_unlock_file (h->lockfile, h->fd, 0);
769 h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
771 ret = db_create (&h->db, h->env, 0);
773 h->env->close (h->env, 0);
774 mx_unlock_file (h->lockfile, h->fd, 0);
781 if (stat (path, &sb) != 0 && errno == ENOENT) {
782 createflags |= DB_EXCL;
783 h->db->set_pagesize (h->db, pagesize);
786 ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
788 h->db->close (h->db, 0);
789 h->env->close (h->env, 0);
790 mx_unlock_file (h->lockfile, h->fd, 0);
799 void mutt_hcache_close (void *db)
801 struct header_cache *h = db;
808 h->db->close (h->db, 0);
809 h->env->close (h->env, 0);
810 mx_unlock_file (h->lockfile, h->fd, 0);
815 void *mutt_hcache_fetch (void *db, const char *filename,
816 size_t (*keylen) (const char *fn))
820 struct header_cache *h = db;
826 filename++; /* skip '/' */
828 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
829 mutt_hcache_dbt_empty_init (&data);
830 data.flags = DB_DBT_MALLOC;
832 h->db->get (h->db, NULL, &key, &data, 0);
834 if (!crc32_matches (data.data, h->crc)) {
843 mutt_hcache_store (void *db, const char *filename, HEADER * header,
844 unsigned long uid_validity, size_t (*keylen) (const char *fn))
849 struct header_cache *h = db;
855 filename++; /* skip '/' */
857 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
859 mutt_hcache_dbt_empty_init (&data);
860 data.flags = DB_DBT_USERMEM;
862 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
863 data.ulen = data.size;
865 ret = h->db->put (h->db, NULL, &key, &data, 0);
873 mutt_hcache_delete (void *db, const char *filename,
874 size_t (*keylen) (const char *fn))
877 struct header_cache *h = db;
883 filename++; /* skip '/' */
885 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
886 return h->db->del (h->db, NULL, &key, 0);
890 #endif /* USE_HCACHE */