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 */
51 #include "lib/debug.h"
74 char lockfile[_POSIX_PATH_MAX];
79 struct timeval timeval;
80 unsigned long uid_validity;
83 static void *lazy_malloc (size_t siz)
85 if (0 < siz && siz < 4096) {
89 return safe_malloc (siz);
92 static void lazy_realloc (void *ptr, size_t siz)
94 void **p = (void **) ptr;
96 if (p != NULL && 0 < siz && siz < 4096) {
100 safe_realloc (ptr, siz);
103 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
105 lazy_realloc (&d, *off + sizeof (int));
106 memcpy (d + *off, &i, sizeof (int));
107 (*off) += sizeof (int);
112 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
114 memcpy (i, d + *off, sizeof (int));
115 (*off) += sizeof (int);
118 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
124 d = dump_int (size, d, off);
128 size = mutt_strlen (c) + 1;
129 d = dump_int (size, d, off);
130 lazy_realloc (&d, *off + size);
131 memcpy (d + *off, c, size);
138 static unsigned char *dump_char_size (char *c, unsigned char *d, int *off,
143 d = dump_int (size, d, off);
147 d = dump_int (size, d, off);
148 lazy_realloc (&d, *off + size);
149 memcpy (d + *off, c, size);
156 static void restore_char (char **c, const unsigned char *d, int *off)
160 restore_int (&size, d, off);
167 *c = safe_malloc (size);
168 memcpy (*c, d + *off, size);
172 static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off)
174 unsigned int counter = 0;
175 unsigned int start_off = *off;
177 d = dump_int (0xdeadbeef, d, off);
180 d = dump_char (a->personal, d, off);
181 d = dump_char (a->mailbox, d, off);
182 d = dump_int (a->group, d, off);
187 memcpy (d + start_off, &counter, sizeof (int));
192 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
194 unsigned int counter;
196 restore_int (&counter, d, off);
199 *a = safe_malloc (sizeof (ADDRESS));
200 restore_char (&(*a)->personal, d, off);
201 restore_char (&(*a)->mailbox, d, off);
202 restore_int ((unsigned int *) &(*a)->group, d, off);
210 static unsigned char *dump_list (LIST * l, unsigned char *d, int *off)
212 unsigned int counter = 0;
213 unsigned int start_off = *off;
215 d = dump_int (0xdeadbeef, d, off);
218 d = dump_char (l->data, d, off);
223 memcpy (d + start_off, &counter, sizeof (int));
228 static void restore_list (LIST ** l, const unsigned char *d, int *off)
230 unsigned int counter;
232 restore_int (&counter, d, off);
235 *l = safe_malloc (sizeof (LIST));
236 restore_char (&(*l)->data, d, off);
245 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
248 d = dump_int (0, d, off);
252 d = dump_int (1, d, off);
255 d = dump_char_size (b->data, d, off, b->dsize + 1);
256 d = dump_int (b->dptr - b->data, d, off);
257 d = dump_int (b->dsize, d, off);
258 d = dump_int (b->destroy, d, off);
263 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
268 restore_int (&used, d, off);
273 *b = safe_malloc (sizeof (BUFFER));
275 restore_char (&(*b)->data, d, off);
276 restore_int (&offset, d, off);
277 (*b)->dptr = (*b)->data + offset;
278 restore_int (&(*b)->dsize, d, off);
279 restore_int ((unsigned int *) &(*b)->destroy, d, off);
283 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
286 unsigned int counter = 0;
287 unsigned int start_off = *off;
289 d = dump_int (0xdeadbeef, d, off);
292 d = dump_char (p->attribute, d, off);
293 d = dump_char (p->value, d, off);
298 memcpy (d + start_off, &counter, sizeof (int));
304 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
306 unsigned int counter;
308 restore_int (&counter, d, off);
311 *p = safe_malloc (sizeof (PARAMETER));
312 restore_char (&(*p)->attribute, d, off);
313 restore_char (&(*p)->value, d, off);
321 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
323 lazy_realloc (&d, *off + sizeof (BODY));
324 memcpy (d + *off, c, sizeof (BODY));
325 *off += sizeof (BODY);
327 d = dump_char (c->xtype, d, off);
328 d = dump_char (c->subtype, d, off);
330 d = dump_parameter (c->parameter, d, off);
332 d = dump_char (c->description, d, off);
333 d = dump_char (c->form_name, d, off);
334 d = dump_char (c->filename, d, off);
335 d = dump_char (c->d_filename, d, off);
340 static void restore_body (BODY * c, const unsigned char *d, int *off)
342 memcpy (c, d + *off, sizeof (BODY));
343 *off += sizeof (BODY);
345 restore_char (&c->xtype, d, off);
346 restore_char (&c->subtype, d, off);
348 restore_parameter (&c->parameter, d, off);
350 restore_char (&c->description, d, off);
351 restore_char (&c->form_name, d, off);
352 restore_char (&c->filename, d, off);
353 restore_char (&c->d_filename, d, off);
356 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
358 d = dump_address (e->return_path, d, off);
359 d = dump_address (e->from, d, off);
360 d = dump_address (e->to, d, off);
361 d = dump_address (e->cc, d, off);
362 d = dump_address (e->bcc, d, off);
363 d = dump_address (e->sender, d, off);
364 d = dump_address (e->reply_to, d, off);
365 d = dump_address (e->mail_followup_to, d, off);
367 d = dump_char (e->subject, d, off);
369 d = dump_int (e->real_subj - e->subject, d, off);
372 d = dump_int (-1, d, off);
374 d = dump_char (e->message_id, d, off);
375 d = dump_char (e->supersedes, d, off);
376 d = dump_char (e->date, d, off);
377 d = dump_char (e->x_label, d, off);
380 d = dump_char (e->newsgroups, d, off);
381 d = dump_char (e->xref, d, off);
382 d = dump_char (e->followup_to, d, off);
383 d = dump_char (e->x_comment_to, d, off);
386 d = dump_list (e->references, d, off);
387 d = dump_list (e->in_reply_to, d, off);
388 d = dump_list (e->userhdrs, d, off);
393 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
397 restore_address (&e->return_path, d, off);
398 restore_address (&e->from, d, off);
399 restore_address (&e->to, d, off);
400 restore_address (&e->cc, d, off);
401 restore_address (&e->bcc, d, off);
402 restore_address (&e->sender, d, off);
403 restore_address (&e->reply_to, d, off);
404 restore_address (&e->mail_followup_to, d, off);
406 restore_char (&e->subject, d, off);
407 restore_int ((unsigned int *) (&real_subj_off), d, off);
408 if (0 <= real_subj_off) {
409 e->real_subj = e->subject + real_subj_off;
414 restore_char (&e->message_id, d, off);
415 restore_char (&e->supersedes, d, off);
416 restore_char (&e->date, d, off);
417 restore_char (&e->x_label, d, off);
420 restore_char (&e->newsgroups, d, off);
421 restore_char (&e->xref, d, off);
422 restore_char (&e->followup_to, d, off);
423 restore_char (&e->x_comment_to, d, off);
426 restore_list (&e->references, d, off);
427 restore_list (&e->in_reply_to, d, off);
428 restore_list (&e->userhdrs, d, off);
432 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
438 for (i = 0; i < 8; i++)
439 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
444 static int generate_crc32 ()
448 crc = crc32 (crc, (unsigned char const *)
449 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
451 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
453 #if HAVE_LANGINFO_CODESET
454 crc = crc32 (crc, (unsigned char const *) Charset, mutt_strlen (Charset));
456 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
457 mutt_strlen ("HAVE_LANGINFO_CODESET"));
462 crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
467 crc32 (crc, (unsigned char const *) "MIXMASTER",
468 mutt_strlen ("MIXMASTER"));
473 crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
478 crc32 (crc, (unsigned char const *) "USE_NNTP", mutt_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, mutt_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 mutt_hcache_open(const char *path, const char *folder)
607 struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE));
608 int flags = VL_OWRITER | VL_OCREAT;
610 h->folder = safe_strdup(folder);
611 h->crc = generate_crc32();
613 if (!path || path[0] == '\0')
620 path = mutt_hcache_per_folder(path, folder);
622 if (option(OPTHCACHECOMPRESS))
625 h->db = vlopen(path, flags, VL_CMPLEX);
638 mutt_hcache_close(void *db)
640 struct header_cache *h = db;
651 mutt_hcache_fetch(void *db, const char *filename,
652 size_t(*keylen) (const char *fn))
654 struct header_cache *h = db;
655 char path[_POSIX_PATH_MAX];
662 strncpy(path, h->folder, sizeof (path));
663 safe_strcat(path, sizeof (path), filename);
665 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
667 data = vlget(h->db, path, ksize, NULL);
669 if (! crc32_matches(data, h->crc))
679 mutt_hcache_store(void *db, const char *filename, HEADER * header,
680 unsigned long uid_validity,
681 size_t(*keylen) (const char *fn))
683 struct header_cache *h = db;
684 char path[_POSIX_PATH_MAX];
692 strncpy(path, h->folder, sizeof (path));
693 safe_strcat(path, sizeof (path), filename);
695 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
697 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
699 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
707 mutt_hcache_delete(void *db, const char *filename,
708 size_t(*keylen) (const char *fn))
710 struct header_cache *h = db;
711 char path[_POSIX_PATH_MAX];
717 strncpy(path, h->folder, sizeof (path));
718 safe_strcat(path, sizeof (path), filename);
720 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
722 return vlout(h->db, path, ksize);
727 void *mutt_hcache_open (const char *path, const char *folder)
729 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
731 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
733 h->folder = safe_strdup (folder);
734 h->crc = generate_crc32 ();
736 if (!path || path[0] == '\0') {
742 path = mutt_hcache_per_folder (path, folder);
744 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
749 /* if rw failed try ro */
750 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
762 void mutt_hcache_close (void *db)
764 struct header_cache *h = db;
775 void *mutt_hcache_fetch (void *db, const char *filename,
776 size_t (*keylen) (const char *fn))
778 struct header_cache *h = db;
781 char path[_POSIX_PATH_MAX];
787 strncpy (path, h->folder, sizeof (path));
788 strncat (path, filename, sizeof (path) - mutt_strlen (path));
791 key.dsize = keylen (path);
793 data = gdbm_fetch (h->db, key);
795 if (!crc32_matches (data.dptr, h->crc)) {
804 mutt_hcache_store (void *db, const char *filename, HEADER * header,
805 unsigned long uid_validity, size_t (*keylen) (const char *fn))
807 struct header_cache *h = db;
810 char path[_POSIX_PATH_MAX];
817 strncpy (path, h->folder, sizeof (path));
818 strncat (path, filename, sizeof (path) - mutt_strlen (path));
821 key.dsize = keylen (path);
823 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
825 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
833 mutt_hcache_delete (void *db, const char *filename,
834 size_t (*keylen) (const char *fn))
837 struct header_cache *h = db;
838 char path[_POSIX_PATH_MAX];
844 strncpy (path, h->folder, sizeof (path));
845 strncat (path, filename, sizeof (path) - mutt_strlen (path));
848 key.dsize = keylen (path);
850 return gdbm_delete (h->db, key);
854 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
857 dbt->size = dbt->ulen = len;
858 dbt->dlen = dbt->doff = 0;
859 dbt->flags = DB_DBT_USERMEM;
862 static void mutt_hcache_dbt_empty_init (DBT * dbt)
865 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
869 void *mutt_hcache_open (const char *path, const char *folder)
872 u_int32_t createflags = DB_CREATE;
874 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
875 int pagesize = atoi (HeaderCachePageSize);
878 h->crc = generate_crc32 ();
880 if (!path || path[0] == '\0') {
885 path = mutt_hcache_per_folder (path, folder);
887 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
889 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
895 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
901 ret = db_env_create (&h->env, 0);
903 mx_unlock_file (h->lockfile, h->fd, 0);
910 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
912 ret = db_create (&h->db, h->env, 0);
914 h->env->close (h->env, 0);
915 mx_unlock_file (h->lockfile, h->fd, 0);
922 if (stat (path, &sb) != 0 && errno == ENOENT) {
923 createflags |= DB_EXCL;
924 h->db->set_pagesize (h->db, pagesize);
927 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
929 h->db->close (h->db, 0);
930 h->env->close (h->env, 0);
931 mx_unlock_file (h->lockfile, h->fd, 0);
940 void mutt_hcache_close (void *db)
942 struct header_cache *h = db;
948 h->db->close (h->db, 0);
949 h->env->close (h->env, 0);
950 mx_unlock_file (h->lockfile, h->fd, 0);
955 void *mutt_hcache_fetch (void *db, const char *filename,
956 size_t (*keylen) (const char *fn))
960 struct header_cache *h = db;
966 filename++; /* skip '/' */
968 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
969 mutt_hcache_dbt_empty_init (&data);
970 data.flags = DB_DBT_MALLOC;
972 h->db->get (h->db, NULL, &key, &data, 0);
974 if (!crc32_matches (data.data, h->crc)) {
983 mutt_hcache_store (void *db, const char *filename, HEADER * header,
984 unsigned long uid_validity, size_t (*keylen) (const char *fn))
989 struct header_cache *h = db;
995 filename++; /* skip '/' */
997 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
999 mutt_hcache_dbt_empty_init (&data);
1000 data.flags = DB_DBT_USERMEM;
1002 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1003 data.ulen = data.size;
1005 ret = h->db->put (h->db, NULL, &key, &data, 0);
1013 mutt_hcache_delete (void *db, const char *filename,
1014 size_t (*keylen) (const char *fn))
1017 struct header_cache *h = db;
1023 filename++; /* skip '/' */
1025 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1026 return h->db->del (h->db, NULL, &key, 0);
1030 #endif /* USE_HCACHE */