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);
390 d = dump_int (e->irt_changed, d, off);
391 d = dump_int (e->refs_changed, d, off);
396 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
400 restore_address (&e->return_path, d, off);
401 restore_address (&e->from, d, off);
402 restore_address (&e->to, d, off);
403 restore_address (&e->cc, d, off);
404 restore_address (&e->bcc, d, off);
405 restore_address (&e->sender, d, off);
406 restore_address (&e->reply_to, d, off);
407 restore_address (&e->mail_followup_to, d, off);
409 restore_char (&e->subject, d, off);
410 restore_int ((unsigned int *) (&tmp), d, off);
412 e->real_subj = e->subject + tmp;
417 restore_char (&e->message_id, d, off);
418 restore_char (&e->supersedes, d, off);
419 restore_char (&e->date, d, off);
420 restore_char (&e->x_label, d, off);
423 restore_char (&e->newsgroups, d, off);
424 restore_char (&e->xref, d, off);
425 restore_char (&e->followup_to, d, off);
426 restore_char (&e->x_comment_to, d, off);
429 restore_list (&e->references, d, off);
430 restore_list (&e->in_reply_to, d, off);
431 restore_list (&e->userhdrs, d, off);
433 restore_int ((unsigned int *) &tmp, d, off);
434 e->irt_changed = tmp;
435 restore_int ((unsigned int *) &tmp, d, off);
436 e->refs_changed = tmp;
440 unsigned int crc32 (unsigned int crc, unsigned char const *p, size_t len)
446 for (i = 0; i < 8; i++)
447 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
452 static int generate_crc32 ()
456 crc = crc32 (crc, (unsigned char const *)
457 "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
459 ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
461 #if HAVE_LANGINFO_CODESET
462 crc = crc32 (crc, (unsigned char const *) Charset, mutt_strlen (Charset));
464 crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
465 mutt_strlen ("HAVE_LANGINFO_CODESET"));
470 crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
475 crc32 (crc, (unsigned char const *) "MIXMASTER",
476 mutt_strlen ("MIXMASTER"));
481 crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
486 crc32 (crc, (unsigned char const *) "USE_NNTP", mutt_strlen ("USE_NNTP"));
491 static int crc32_matches (const char *d, unsigned int crc)
493 int off = sizeof (validate);
494 unsigned int mycrc = 0;
500 restore_int (&mycrc, (unsigned char *) d, &off);
502 return (crc == mycrc);
505 /* Append md5sumed folder to path if path is a directory. */
506 static const char *mutt_hcache_per_folder (const char *path,
509 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
510 struct stat path_stat;
512 unsigned char md5sum[16];
515 ret = stat (path, &path_stat);
520 if (!S_ISDIR (path_stat.st_mode)) {
525 MD5Update (&md5, (unsigned char *) folder, mutt_strlen (folder));
526 MD5Final (md5sum, &md5);
528 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
529 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
530 "%02x%02x%02x%02x%02x%02x%02x%02x",
531 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
532 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
533 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
534 md5sum[13], md5sum[14], md5sum[15]);
540 return mutt_hcache_per_folder_path;
543 /* This function transforms a header into a char so that it is useable by
545 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
546 unsigned long uid_validity)
548 struct header_cache *db = _db;
549 unsigned char *d = NULL;
553 d = lazy_malloc (sizeof (validate));
556 memcpy (d, &uid_validity, sizeof (unsigned long));
561 gettimeofday (&now, NULL);
562 memcpy (d, &now, sizeof (struct timeval));
564 *off += sizeof (validate);
566 d = dump_int (db->crc, d, off);
568 lazy_realloc (&d, *off + sizeof (HEADER));
569 memcpy (d + *off, h, sizeof (HEADER));
570 *off += sizeof (HEADER);
572 d = dump_envelope (h->env, d, off);
573 d = dump_body (h->content, d, off);
574 d = dump_char (h->maildir_flags, d, off);
579 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
582 HEADER *h = mutt_new_header ();
585 off += sizeof (validate);
588 off += sizeof (unsigned int);
590 memcpy (h, d + off, sizeof (HEADER));
591 off += sizeof (HEADER);
593 h->env = mutt_new_envelope ();
594 restore_envelope (h->env, d, &off);
596 h->content = mutt_new_body ();
597 restore_body (h->content, d, &off);
599 restore_char (&h->maildir_flags, d, &off);
601 /* this is needed for maildir style mailboxes */
604 h->path = safe_strdup ((*oh)->path);
605 mutt_free_header (oh);
613 mutt_hcache_open(const char *path, const char *folder)
615 struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE));
616 int flags = VL_OWRITER | VL_OCREAT;
618 h->folder = safe_strdup(folder);
619 h->crc = generate_crc32();
621 if (!path || path[0] == '\0')
628 path = mutt_hcache_per_folder(path, folder);
630 if (option(OPTHCACHECOMPRESS))
633 h->db = vlopen(path, flags, VL_CMPLEX);
646 mutt_hcache_close(void *db)
648 struct header_cache *h = db;
659 mutt_hcache_fetch(void *db, const char *filename,
660 size_t(*keylen) (const char *fn))
662 struct header_cache *h = db;
663 char path[_POSIX_PATH_MAX];
670 strncpy(path, h->folder, sizeof (path));
671 safe_strcat(path, sizeof (path), filename);
673 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
675 data = vlget(h->db, path, ksize, NULL);
677 if (! crc32_matches(data, h->crc))
687 mutt_hcache_store(void *db, const char *filename, HEADER * header,
688 unsigned long uid_validity,
689 size_t(*keylen) (const char *fn))
691 struct header_cache *h = db;
692 char path[_POSIX_PATH_MAX];
700 strncpy(path, h->folder, sizeof (path));
701 safe_strcat(path, sizeof (path), filename);
703 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
705 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
707 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
715 mutt_hcache_delete(void *db, const char *filename,
716 size_t(*keylen) (const char *fn))
718 struct header_cache *h = db;
719 char path[_POSIX_PATH_MAX];
725 strncpy(path, h->folder, sizeof (path));
726 safe_strcat(path, sizeof (path), filename);
728 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
730 return vlout(h->db, path, ksize);
735 void *mutt_hcache_open (const char *path, const char *folder)
737 struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE));
739 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
741 h->folder = safe_strdup (folder);
742 h->crc = generate_crc32 ();
744 if (!path || path[0] == '\0') {
750 path = mutt_hcache_per_folder (path, folder);
752 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
757 /* if rw failed try ro */
758 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
770 void mutt_hcache_close (void *db)
772 struct header_cache *h = db;
783 void *mutt_hcache_fetch (void *db, const char *filename,
784 size_t (*keylen) (const char *fn))
786 struct header_cache *h = db;
789 char path[_POSIX_PATH_MAX];
795 strncpy (path, h->folder, sizeof (path));
796 strncat (path, filename, sizeof (path) - mutt_strlen (path));
799 key.dsize = keylen (path);
801 data = gdbm_fetch (h->db, key);
803 if (!crc32_matches (data.dptr, h->crc)) {
812 mutt_hcache_store (void *db, const char *filename, HEADER * header,
813 unsigned long uid_validity, size_t (*keylen) (const char *fn))
815 struct header_cache *h = db;
818 char path[_POSIX_PATH_MAX];
825 strncpy (path, h->folder, sizeof (path));
826 strncat (path, filename, sizeof (path) - mutt_strlen (path));
829 key.dsize = keylen (path);
831 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
833 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
841 mutt_hcache_delete (void *db, const char *filename,
842 size_t (*keylen) (const char *fn))
845 struct header_cache *h = db;
846 char path[_POSIX_PATH_MAX];
852 strncpy (path, h->folder, sizeof (path));
853 strncat (path, filename, sizeof (path) - mutt_strlen (path));
856 key.dsize = keylen (path);
858 return gdbm_delete (h->db, key);
862 static void mutt_hcache_dbt_init (DBT * dbt, void *data, size_t len)
865 dbt->size = dbt->ulen = len;
866 dbt->dlen = dbt->doff = 0;
867 dbt->flags = DB_DBT_USERMEM;
870 static void mutt_hcache_dbt_empty_init (DBT * dbt)
873 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
877 void *mutt_hcache_open (const char *path, const char *folder)
880 u_int32_t createflags = DB_CREATE;
882 struct header_cache *h = calloc (1, sizeof (HEADER_CACHE));
883 int pagesize = atoi (HeaderCachePageSize);
886 h->crc = generate_crc32 ();
888 if (!path || path[0] == '\0') {
893 path = mutt_hcache_per_folder (path, folder);
895 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
897 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
903 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
909 ret = db_env_create (&h->env, 0);
911 mx_unlock_file (h->lockfile, h->fd, 0);
918 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
920 ret = db_create (&h->db, h->env, 0);
922 h->env->close (h->env, 0);
923 mx_unlock_file (h->lockfile, h->fd, 0);
930 if (stat (path, &sb) != 0 && errno == ENOENT) {
931 createflags |= DB_EXCL;
932 h->db->set_pagesize (h->db, pagesize);
935 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
937 h->db->close (h->db, 0);
938 h->env->close (h->env, 0);
939 mx_unlock_file (h->lockfile, h->fd, 0);
948 void mutt_hcache_close (void *db)
950 struct header_cache *h = db;
956 h->db->close (h->db, 0);
957 h->env->close (h->env, 0);
958 mx_unlock_file (h->lockfile, h->fd, 0);
963 void *mutt_hcache_fetch (void *db, const char *filename,
964 size_t (*keylen) (const char *fn))
968 struct header_cache *h = db;
974 filename++; /* skip '/' */
976 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
977 mutt_hcache_dbt_empty_init (&data);
978 data.flags = DB_DBT_MALLOC;
980 h->db->get (h->db, NULL, &key, &data, 0);
982 if (!crc32_matches (data.data, h->crc)) {
991 mutt_hcache_store (void *db, const char *filename, HEADER * header,
992 unsigned long uid_validity, size_t (*keylen) (const char *fn))
997 struct header_cache *h = db;
1003 filename++; /* skip '/' */
1005 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1007 mutt_hcache_dbt_empty_init (&data);
1008 data.flags = DB_DBT_USERMEM;
1010 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
1011 data.ulen = data.size;
1013 ret = h->db->put (h->db, NULL, &key, &data, 0);
1021 mutt_hcache_delete (void *db, const char *filename,
1022 size_t (*keylen) (const char *fn))
1025 struct header_cache *h = db;
1031 filename++; /* skip '/' */
1033 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
1034 return h->db->del (h->db, NULL, &key, 0);
1038 #endif /* USE_HCACHE */