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 = safe_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);
181 d = dump_char (a->val, d, off);
183 d = dump_char (a->personal, d, off);
184 d = dump_char (a->mailbox, d, off);
185 d = dump_int (a->group, d, off);
190 memcpy (d + start_off, &counter, sizeof (int));
195 static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
197 unsigned int counter;
199 restore_int (&counter, d, off);
202 *a = safe_malloc (sizeof (ADDRESS));
204 restore_char (&(*a)->val, d, off);
206 restore_char (&(*a)->personal, d, off);
207 restore_char (&(*a)->mailbox, d, off);
208 restore_int ((unsigned int *) &(*a)->group, d, off);
216 static unsigned char *dump_list (LIST * l, unsigned char *d, int *off)
218 unsigned int counter = 0;
219 unsigned int start_off = *off;
221 d = dump_int (0xdeadbeef, d, off);
224 d = dump_char (l->data, d, off);
229 memcpy (d + start_off, &counter, sizeof (int));
234 static void restore_list (LIST ** l, const unsigned char *d, int *off)
236 unsigned int counter;
238 restore_int (&counter, d, off);
241 *l = safe_malloc (sizeof (LIST));
242 restore_char (&(*l)->data, d, off);
251 static unsigned char *dump_buffer (BUFFER * b, unsigned char *d, int *off)
254 d = dump_int (0, d, off);
258 d = dump_int (1, d, off);
261 d = dump_char_size (b->data, d, off, b->dsize + 1);
262 d = dump_int (b->dptr - b->data, d, off);
263 d = dump_int (b->dsize, d, off);
264 d = dump_int (b->destroy, d, off);
269 static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off)
274 restore_int (&used, d, off);
279 *b = safe_malloc (sizeof (BUFFER));
281 restore_char (&(*b)->data, d, off);
282 restore_int (&offset, d, off);
283 (*b)->dptr = (*b)->data + offset;
284 restore_int (&(*b)->dsize, d, off);
285 restore_int ((unsigned int *) &(*b)->destroy, d, off);
289 static unsigned char *dump_parameter (PARAMETER * p, unsigned char *d,
292 unsigned int counter = 0;
293 unsigned int start_off = *off;
295 d = dump_int (0xdeadbeef, d, off);
298 d = dump_char (p->attribute, d, off);
299 d = dump_char (p->value, d, off);
304 memcpy (d + start_off, &counter, sizeof (int));
310 restore_parameter (PARAMETER ** p, const unsigned char *d, int *off)
312 unsigned int counter;
314 restore_int (&counter, d, off);
317 *p = safe_malloc (sizeof (PARAMETER));
318 restore_char (&(*p)->attribute, d, off);
319 restore_char (&(*p)->value, d, off);
327 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
329 lazy_realloc (&d, *off + sizeof (BODY));
330 memcpy (d + *off, c, sizeof (BODY));
331 *off += sizeof (BODY);
333 d = dump_char (c->xtype, d, off);
334 d = dump_char (c->subtype, d, off);
336 d = dump_parameter (c->parameter, d, off);
338 d = dump_char (c->description, d, off);
339 d = dump_char (c->form_name, d, off);
340 d = dump_char (c->filename, d, off);
341 d = dump_char (c->d_filename, d, off);
346 static void restore_body (BODY * c, const unsigned char *d, int *off)
348 memcpy (c, d + *off, sizeof (BODY));
349 *off += sizeof (BODY);
351 restore_char (&c->xtype, d, off);
352 restore_char (&c->subtype, d, off);
354 restore_parameter (&c->parameter, d, off);
356 restore_char (&c->description, d, off);
357 restore_char (&c->form_name, d, off);
358 restore_char (&c->filename, d, off);
359 restore_char (&c->d_filename, d, off);
362 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
364 d = dump_address (e->return_path, d, off);
365 d = dump_address (e->from, d, off);
366 d = dump_address (e->to, d, off);
367 d = dump_address (e->cc, d, off);
368 d = dump_address (e->bcc, d, off);
369 d = dump_address (e->sender, d, off);
370 d = dump_address (e->reply_to, d, off);
371 d = dump_address (e->mail_followup_to, d, off);
373 d = dump_char (e->subject, d, off);
375 d = dump_int (e->real_subj - e->subject, d, off);
378 d = dump_int (-1, d, off);
380 d = dump_char (e->message_id, d, off);
381 d = dump_char (e->supersedes, d, off);
382 d = dump_char (e->date, d, off);
383 d = dump_char (e->x_label, d, off);
386 d = dump_char (e->newsgroups, d, off);
387 d = dump_char (e->xref, d, off);
388 d = dump_char (e->followup_to, d, off);
389 d = dump_char (e->x_comment_to, d, off);
392 d = dump_list (e->references, d, off);
393 d = dump_list (e->in_reply_to, d, off);
394 d = dump_list (e->userhdrs, d, off);
399 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
403 restore_address (&e->return_path, d, off);
404 restore_address (&e->from, d, off);
405 restore_address (&e->to, d, off);
406 restore_address (&e->cc, d, off);
407 restore_address (&e->bcc, d, off);
408 restore_address (&e->sender, d, off);
409 restore_address (&e->reply_to, d, off);
410 restore_address (&e->mail_followup_to, d, off);
412 restore_char (&e->subject, d, off);
413 restore_int ((unsigned int *) (&real_subj_off), d, off);
414 if (0 <= real_subj_off) {
415 e->real_subj = e->subject + real_subj_off;
420 restore_char (&e->message_id, d, off);
421 restore_char (&e->supersedes, d, off);
422 restore_char (&e->date, d, off);
423 restore_char (&e->x_label, d, off);
426 restore_char (&e->newsgroups, d, off);
427 restore_char (&e->xref, d, off);
428 restore_char (&e->followup_to, d, off);
429 restore_char (&e->x_comment_to, d, off);
432 restore_list (&e->references, d, off);
433 restore_list (&e->in_reply_to, d, off);
434 restore_list (&e->userhdrs, d, off);
438 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
444 for (i = 0; i < 8; i++)
445 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
450 static int generate_crc32 ()
454 crc = crc32 (crc, (unsigned char const *)
455 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
457 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
459 #if HAVE_LANGINFO_CODESET
460 crc = crc32 (crc, (unsigned char const *) Charset, safe_strlen (Charset));
462 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
463 safe_strlen ("HAVE_LANGINFO_CODESET"));
468 crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
469 safe_strlen ("EXACT_ADDRESS"));
474 crc32 (crc, (unsigned char const *) "USE_POP", safe_strlen ("USE_POP"));
479 crc32 (crc, (unsigned char const *) "MIXMASTER",
480 safe_strlen ("MIXMASTER"));
485 crc32 (crc, (unsigned char const *) "USE_IMAP", safe_strlen ("USE_IMAP"));
490 crc32 (crc, (unsigned char const *) "USE_NNTP", safe_strlen ("USE_NNTP"));
495 static int crc32_matches (const char *d, unsigned int crc)
497 int off = sizeof (validate);
498 unsigned int mycrc = 0;
504 restore_int (&mycrc, (unsigned char *) d, &off);
506 return (crc == mycrc);
509 /* Append md5sumed folder to path if path is a directory. */
510 static const char *mutt_hcache_per_folder (const char *path,
513 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
514 struct stat path_stat;
516 unsigned char md5sum[16];
519 ret = stat (path, &path_stat);
524 if (!S_ISDIR (path_stat.st_mode)) {
529 MD5Update (&md5, (unsigned char *) folder, safe_strlen (folder));
530 MD5Final (md5sum, &md5);
532 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
533 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
534 "%02x%02x%02x%02x%02x%02x%02x%02x",
535 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
536 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
537 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
538 md5sum[13], md5sum[14], md5sum[15]);
544 return mutt_hcache_per_folder_path;
547 /* This function transforms a header into a char so that it is useable by
549 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
550 unsigned long uid_validity)
552 struct header_cache *db = _db;
553 unsigned char *d = NULL;
557 d = lazy_malloc (sizeof (validate));
560 memcpy (d, &uid_validity, sizeof (unsigned long));
565 gettimeofday (&now, NULL);
566 memcpy (d, &now, sizeof (struct timeval));
568 *off += sizeof (validate);
570 d = dump_int (db->crc, d, off);
572 lazy_realloc (&d, *off + sizeof (HEADER));
573 memcpy (d + *off, h, sizeof (HEADER));
574 *off += sizeof (HEADER);
576 d = dump_envelope (h->env, d, off);
577 d = dump_body (h->content, d, off);
578 d = dump_char (h->maildir_flags, d, off);
583 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
586 HEADER *h = mutt_new_header ();
589 off += sizeof (validate);
592 off += sizeof (unsigned int);
594 memcpy (h, d + off, sizeof (HEADER));
595 off += sizeof (HEADER);
597 h->env = mutt_new_envelope ();
598 restore_envelope (h->env, d, &off);
600 h->content = mutt_new_body ();
601 restore_body (h->content, d, &off);
603 restore_char (&h->maildir_flags, d, &off);
605 /* this is needed for maildir style mailboxes */
608 h->path = safe_strdup ((*oh)->path);
609 mutt_free_header (oh);
617 mutt_hcache_open(const char *path, const char *folder)
619 struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE));
622 int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384;
625 h->folder = safe_strdup(folder);
626 h->crc = generate_crc32();
628 if (!path || path[0] == '\0')
635 path = mutt_hcache_per_folder(path, folder);
637 if (option(OPTHCACHECOMPRESS))
640 h->db = vlopen(path, flags | VL_OWRITER | VL_OCREAT, VL_CMPLEX);
644 /* if rw failed try ro */
645 h->db = vlopen(path, flags | VL_OREADER, VL_CMPLEX);
658 mutt_hcache_close(void *db)
660 struct header_cache *h = db;
671 mutt_hcache_fetch(void *db, const char *filename,
672 size_t(*keylen) (const char *fn))
674 struct header_cache *h = db;
675 char path[_POSIX_PATH_MAX];
682 strncpy(path, h->folder, sizeof (path));
683 safe_strcat(path, sizeof (path), filename);
685 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
687 data = vlget(h->db, path, ksize, NULL);
689 if (! crc32_matches(data, h->crc))
701 mutt_hcache_store(void *db, const char *filename, HEADER * header,
702 unsigned long uid_validity,
703 size_t(*keylen) (const char *fn))
705 struct header_cache *h = db;
706 char path[_POSIX_PATH_MAX];
714 strncpy(path, h->folder, sizeof (path));
715 safe_strcat(path, sizeof (path), filename);
717 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
719 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
721 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
729 mutt_hcache_delete(void *db, const char *filename,
730 size_t(*keylen) (const char *fn))
732 struct header_cache *h = db;
733 char path[_POSIX_PATH_MAX];
739 strncpy(path, h->folder, sizeof (path));
740 safe_strcat(path, sizeof (path), filename);
742 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
744 return vlout(h->db, path, ksize);
749 void *mutt_hcache_open (const char *path, const char *folder)
751 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
753 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
755 h->folder = safe_strdup (folder);
756 h->crc = generate_crc32 ();
758 if (!path || path[0] == '\0') {
764 path = mutt_hcache_per_folder (path, folder);
766 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
771 /* if rw failed try ro */
772 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
784 void mutt_hcache_close (void *db)
786 struct header_cache *h = db;
797 void *mutt_hcache_fetch (void *db, const char *filename,
798 size_t (*keylen) (const char *fn))
800 struct header_cache *h = db;
803 char path[_POSIX_PATH_MAX];
809 strncpy (path, h->folder, sizeof (path));
810 strncat (path, filename, sizeof (path) - safe_strlen (path));
813 key.dsize = keylen (path);
815 data = gdbm_fetch (h->db, key);
817 if (!crc32_matches (data.dptr, h->crc)) {
826 mutt_hcache_store (void *db, const char *filename, HEADER * header,
827 unsigned long uid_validity, size_t (*keylen) (const char *fn))
829 struct header_cache *h = db;
832 char path[_POSIX_PATH_MAX];
839 strncpy (path, h->folder, sizeof (path));
840 strncat (path, filename, sizeof (path) - safe_strlen (path));
843 key.dsize = keylen (path);
845 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
847 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
855 mutt_hcache_delete (void *db, const char *filename,
856 size_t (*keylen) (const char *fn))
859 struct header_cache *h = db;
860 char path[_POSIX_PATH_MAX];
866 strncpy (path, h->folder, sizeof (path));
867 strncat (path, filename, sizeof (path) - safe_strlen (path));
870 key.dsize = keylen (path);
872 return gdbm_delete (h->db, key);
876 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
879 dbt->size = dbt->ulen = len;
880 dbt->dlen = dbt->doff = 0;
881 dbt->flags = DB_DBT_USERMEM;
884 static void mutt_hcache_dbt_empty_init (DBT * dbt)
887 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
891 void *mutt_hcache_open (const char *path, const char *folder)
894 u_int32_t createflags = DB_CREATE;
896 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
897 int pagesize = atoi (HeaderCachePageSize);
900 h->crc = generate_crc32 ();
902 if (!path || path[0] == '\0') {
907 path = mutt_hcache_per_folder (path, folder);
909 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
911 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
917 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
923 ret = db_env_create (&h->env, 0);
925 mx_unlock_file (h->lockfile, h->fd, 0);
932 h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
934 ret = db_create (&h->db, h->env, 0);
936 h->env->close (h->env, 0);
937 mx_unlock_file (h->lockfile, h->fd, 0);
944 if (stat (path, &sb) != 0 && errno == ENOENT) {
945 createflags |= DB_EXCL;
946 h->db->set_pagesize (h->db, pagesize);
949 ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
951 h->db->close (h->db, 0);
952 h->env->close (h->env, 0);
953 mx_unlock_file (h->lockfile, h->fd, 0);
962 void mutt_hcache_close (void *db)
964 struct header_cache *h = db;
971 h->db->close (h->db, 0);
972 h->env->close (h->env, 0);
973 mx_unlock_file (h->lockfile, h->fd, 0);
978 void *mutt_hcache_fetch (void *db, const char *filename,
979 size_t (*keylen) (const char *fn))
983 struct header_cache *h = db;
989 filename++; /* skip '/' */
991 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
992 mutt_hcache_dbt_empty_init (&data);
993 data.flags = DB_DBT_MALLOC;
995 h->db->get (h->db, NULL, &key, &data, 0);
997 if (!crc32_matches (data.data, h->crc)) {
1006 mutt_hcache_store (void *db, const char *filename, HEADER * header,
1007 unsigned long uid_validity, size_t (*keylen) (const char *fn))
1012 struct header_cache *h = db;
1018 filename++; /* skip '/' */
1020 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1022 mutt_hcache_dbt_empty_init (&data);
1023 data.flags = DB_DBT_USERMEM;
1025 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1026 data.ulen = data.size;
1028 ret = h->db->put (h->db, NULL, &key, &data, 0);
1036 mutt_hcache_delete (void *db, const char *filename,
1037 size_t (*keylen) (const char *fn))
1040 struct header_cache *h = db;
1046 filename++; /* skip '/' */
1048 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1049 return h->db->del (h->db, NULL, &key, 0);
1053 #endif /* USE_HCACHE */