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.
12 #include <lib-lib/lib-lib.h>
16 #define MUTTNG_HCACHE_ID "0x004"
18 #if defined(HAVE_QDBM)
22 #elif defined(HAVE_GDBM)
24 #elif defined(HAVE_DB4)
28 #include <lib-hash/hash.h>
29 #include <lib-mime/mime.h>
31 #include <imap/message.h>
38 #if defined(HAVE_QDBM)
42 #elif defined(HAVE_GDBM)
46 #elif defined(HAVE_DB4)
51 char lockfile[_POSIX_PATH_MAX];
56 struct timeval timeval;
57 unsigned long uid_validity;
60 #define UPPER4K(i) ((i & ~(4096 - 1)) + 4096)
62 static unsigned char *lazy_malloc(ssize_t siz)
64 return p_new(unsigned char, UPPER4K(siz));
67 static void lazy_realloc(unsigned char **p, ssize_t siz)
69 p_realloc(p, UPPER4K(siz));
72 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
74 lazy_realloc (&d, *off + sizeof (int));
75 memcpy (d + *off, &i, sizeof (int));
76 (*off) += sizeof (int);
81 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
83 memcpy (i, d + *off, sizeof (int));
84 (*off) += sizeof (int);
87 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
93 d = dump_int (size, d, off);
97 size = m_strlen(c) + 1;
98 d = dump_int (size, d, off);
99 lazy_realloc (&d, *off + size);
100 memcpy (d + *off, c, size);
106 static void restore_char (char **c, const unsigned char *d, int *off)
110 restore_int (&size, d, off);
117 *c = p_dup(d + *off, size);
121 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
123 unsigned int counter = 0;
124 unsigned int start_off = *off;
126 d = dump_int (0xdeadbeef, d, off);
129 d = dump_char (a->personal, d, off);
130 d = dump_char (a->mailbox, d, off);
131 d = dump_int (a->group, d, off);
136 memcpy (d + start_off, &counter, sizeof (int));
141 static void restore_address (address_t ** a, const unsigned char *d, int *off)
143 unsigned int counter;
145 restore_int (&counter, d, off);
148 *a = p_new(address_t, 1);
149 restore_char (&(*a)->personal, d, off);
150 restore_char (&(*a)->mailbox, d, off);
151 restore_int ((unsigned int *) &(*a)->group, d, off);
159 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
161 unsigned int counter = 0;
162 unsigned int start_off = *off;
164 d = dump_int (0xdeadbeef, d, off);
167 d = dump_char (l->data, d, off);
172 memcpy (d + start_off, &counter, sizeof (int));
177 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
179 unsigned int counter;
181 restore_int (&counter, d, off);
184 *l = p_new(string_list_t, 1);
185 restore_char (&(*l)->data, d, off);
193 static unsigned char *dump_parameter (parameter_t * p, unsigned char *d,
196 unsigned int counter = 0;
197 unsigned int start_off = *off;
199 d = dump_int (0xdeadbeef, d, off);
202 d = dump_char (p->attribute, d, off);
203 d = dump_char (p->value, d, off);
208 memcpy (d + start_off, &counter, sizeof (int));
214 restore_parameter (parameter_t ** p, const unsigned char *d, int *off)
216 unsigned int counter;
218 restore_int (&counter, d, off);
221 *p = parameter_new();
222 restore_char (&(*p)->attribute, d, off);
223 restore_char (&(*p)->value, d, off);
229 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
231 lazy_realloc (&d, *off + sizeof (BODY));
232 memcpy (d + *off, c, sizeof (BODY));
233 *off += sizeof (BODY);
235 d = dump_char (c->xtype, d, off);
236 d = dump_char (c->subtype, d, off);
238 d = dump_parameter (c->parameter, d, off);
240 d = dump_char (c->description, d, off);
241 d = dump_char (c->form_name, d, off);
242 d = dump_char (c->filename, d, off);
243 d = dump_char (c->d_filename, d, off);
248 static void restore_body (BODY * c, const unsigned char *d, int *off)
250 memcpy (c, d + *off, sizeof (BODY));
251 *off += sizeof (BODY);
253 restore_char (&c->xtype, d, off);
254 restore_char (&c->subtype, d, off);
256 restore_parameter (&c->parameter, d, off);
258 restore_char (&c->description, d, off);
259 restore_char (&c->form_name, d, off);
260 restore_char (&c->filename, d, off);
261 restore_char (&c->d_filename, d, off);
264 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
266 d = dump_address (e->return_path, d, off);
267 d = dump_address (e->from, d, off);
268 d = dump_address (e->to, d, off);
269 d = dump_address (e->cc, d, off);
270 d = dump_address (e->bcc, d, off);
271 d = dump_address (e->sender, d, off);
272 d = dump_address (e->reply_to, d, off);
273 d = dump_address (e->mail_followup_to, d, off);
275 d = dump_char (e->subject, d, off);
277 d = dump_int (e->real_subj - e->subject, d, off);
280 d = dump_int (-1, d, off);
282 d = dump_char (e->message_id, d, off);
283 d = dump_char (e->supersedes, d, off);
284 d = dump_char (e->date, d, off);
285 d = dump_char (e->x_label, d, off);
286 d = dump_char (e->list_post, d, off);
289 d = dump_char (e->newsgroups, d, off);
290 d = dump_char (e->xref, d, off);
291 d = dump_char (e->followup_to, d, off);
292 d = dump_char (e->x_comment_to, d, off);
295 d = dump_list (e->references, d, off);
296 d = dump_list (e->in_reply_to, d, off);
297 d = dump_list (e->userhdrs, d, off);
302 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
306 restore_address (&e->return_path, d, off);
307 restore_address (&e->from, d, off);
308 restore_address (&e->to, d, off);
309 restore_address (&e->cc, d, off);
310 restore_address (&e->bcc, d, off);
311 restore_address (&e->sender, d, off);
312 restore_address (&e->reply_to, d, off);
313 restore_address (&e->mail_followup_to, d, off);
315 restore_char (&e->subject, d, off);
316 restore_int ((unsigned int *) (&real_subj_off), d, off);
317 if (0 <= real_subj_off) {
318 e->real_subj = e->subject + real_subj_off;
323 restore_char (&e->message_id, d, off);
324 restore_char (&e->supersedes, d, off);
325 restore_char (&e->date, d, off);
326 restore_char (&e->x_label, d, off);
327 restore_char (&e->list_post, d, off);
330 restore_char (&e->newsgroups, d, off);
331 restore_char (&e->xref, d, off);
332 restore_char (&e->followup_to, d, off);
333 restore_char (&e->x_comment_to, d, off);
336 restore_list (&e->references, d, off);
337 restore_list (&e->in_reply_to, d, off);
338 restore_list (&e->userhdrs, d, off);
342 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
348 for (i = 0; i < 8; i++)
349 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
354 static int generate_crc32 ()
358 crc = crc32 (crc, (unsigned char const *)
359 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
361 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
363 #ifdef HAVE_LANGINFO_CODESET
364 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
365 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
366 m_strlen("HAVE_LANGINFO_CODESET"));
369 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
371 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
372 m_strlen("MIXMASTER"));
374 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
377 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
382 static int crc32_matches (const char *d, unsigned int crc)
384 int off = sizeof (validate);
385 unsigned int mycrc = 0;
391 restore_int (&mycrc, (unsigned char *) d, &off);
393 return (crc == mycrc);
396 /* Append md5sumed folder to path if path is a directory. */
397 static const char *mutt_hcache_per_folder (const char *path,
400 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
401 struct stat path_stat;
403 unsigned char md5sum[16];
406 ret = stat (path, &path_stat);
411 if (!S_ISDIR (path_stat.st_mode)) {
416 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
417 MD5Final (md5sum, &md5);
419 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
420 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
421 "%02x%02x%02x%02x%02x%02x%02x%02x",
422 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
423 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
424 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
425 md5sum[13], md5sum[14], md5sum[15]);
431 return mutt_hcache_per_folder_path;
434 /* This function transforms a header into a char so that it is useable by
436 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
437 unsigned long uid_validity)
439 struct header_cache *db = _db;
440 unsigned char *d = NULL;
444 d = lazy_malloc (sizeof (validate));
447 memcpy (d, &uid_validity, sizeof (unsigned long));
452 gettimeofday (&now, NULL);
453 memcpy (d, &now, sizeof (struct timeval));
455 *off += sizeof (validate);
457 d = dump_int (db->crc, d, off);
459 lazy_realloc (&d, *off + sizeof (HEADER));
460 memcpy (d + *off, h, sizeof (HEADER));
461 *off += sizeof (HEADER);
463 d = dump_envelope (h->env, d, off);
464 d = dump_body (h->content, d, off);
465 d = dump_char (h->maildir_flags, d, off);
470 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
473 HEADER *h = header_new();
476 off += sizeof (validate);
479 off += sizeof (unsigned int);
481 memcpy (h, d + off, sizeof (HEADER));
482 off += sizeof (HEADER);
484 h->env = envelope_new();
485 restore_envelope (h->env, d, &off);
487 h->content = body_new();
488 restore_body (h->content, d, &off);
490 restore_char (&h->maildir_flags, d, &off);
492 /* this is needed for maildir style mailboxes */
495 h->path = m_strdup((*oh)->path);
502 #if defined(HAVE_QDBM)
504 mutt_hcache_open(const char *path, const char *folder)
506 struct header_cache *h = p_new(struct header_cache, 1);
507 int flags = VL_OWRITER | VL_OCREAT;
509 h->folder = m_strdup(folder);
510 h->crc = generate_crc32();
512 if (!path || path[0] == '\0')
514 p_delete(&h->folder);
519 path = mutt_hcache_per_folder(path, folder);
521 if (option(OPTHCACHECOMPRESS))
524 h->db = vlopen(path, flags, VL_CMPLEX);
529 p_delete(&h->folder);
537 mutt_hcache_close(void *db)
539 struct header_cache *h = db;
545 p_delete(&h->folder);
550 mutt_hcache_fetch(void *db, const char *filename,
551 ssize_t(*keylen) (const char *fn))
553 struct header_cache *h = db;
554 char path[_POSIX_PATH_MAX];
561 m_strcpy(path, sizeof(path), h->folder);
562 m_strcat(path, sizeof(path), filename);
564 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
566 data = vlget(h->db, path, ksize, NULL);
568 if (!crc32_matches(data, h->crc))
578 mutt_hcache_store(void *db, const char *filename, HEADER * header,
579 unsigned long uid_validity,
580 ssize_t(*keylen) (const char *fn))
582 struct header_cache *h = db;
583 char path[_POSIX_PATH_MAX];
591 m_strcpy(path, sizeof(path), h->folder);
592 m_strcat(path, sizeof(path), filename);
594 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
596 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
598 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
606 mutt_hcache_delete(void *db, const char *filename,
607 ssize_t(*keylen) (const char *fn))
609 struct header_cache *h = db;
610 char path[_POSIX_PATH_MAX];
616 m_strcpy(path, sizeof(path), h->folder);
617 m_strcat(path, sizeof(path), filename);
619 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
621 return vlout(h->db, path, ksize);
624 #elif defined(HAVE_GDBM)
626 void *mutt_hcache_open (const char *path, const char *folder)
628 struct header_cache *h = p_new(struct header_cache, 1);
630 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
632 h->folder = m_strdup(folder);
633 h->crc = generate_crc32 ();
635 if (!path || path[0] == '\0') {
636 p_delete(&h->folder);
641 path = mutt_hcache_per_folder (path, folder);
643 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
648 /* if rw failed try ro */
649 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
654 p_delete(&h->folder);
661 void mutt_hcache_close (void *db)
663 struct header_cache *h = db;
670 p_delete(&h->folder);
674 void *mutt_hcache_fetch (void *db, const char *filename,
675 ssize_t (*keylen) (const char *fn))
677 struct header_cache *h = db;
680 char path[_POSIX_PATH_MAX];
686 m_strcpy(path, sizeof(path), h->folder);
687 strncat (path, filename, sizeof (path) - m_strlen(path));
690 key.dsize = keylen (path);
692 data = gdbm_fetch (h->db, key);
694 if (!crc32_matches (data.dptr, h->crc)) {
695 p_delete(&data.dptr);
703 mutt_hcache_store (void *db, const char *filename, HEADER * header,
704 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
706 struct header_cache *h = db;
709 char path[_POSIX_PATH_MAX];
716 m_strcpy(path, sizeof(path), h->folder);
717 strncat (path, filename, sizeof (path) - m_strlen(path));
720 key.dsize = keylen (path);
722 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
724 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
726 p_delete(&data.dptr);
732 mutt_hcache_delete (void *db, const char *filename,
733 ssize_t (*keylen) (const char *fn))
736 struct header_cache *h = db;
737 char path[_POSIX_PATH_MAX];
743 m_strcpy(path, sizeof(path), h->folder);
744 strncat (path, filename, sizeof (path) - m_strlen(path));
747 key.dsize = keylen (path);
749 return gdbm_delete (h->db, key);
751 #elif defined(HAVE_DB4)
753 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
756 dbt->size = dbt->ulen = len;
757 dbt->dlen = dbt->doff = 0;
758 dbt->flags = DB_DBT_USERMEM;
761 static void mutt_hcache_dbt_empty_init (DBT * dbt)
764 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
768 void *mutt_hcache_open (const char *path, const char *folder)
771 u_int32_t createflags = DB_CREATE;
773 struct header_cache *h = p_new(struct header_cache, 1);
774 int pagesize = atoi (HeaderCachePageSize);
777 h->crc = generate_crc32 ();
779 if (!path || path[0] == '\0') {
784 path = mutt_hcache_per_folder (path, folder);
786 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
788 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
794 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
800 ret = db_env_create (&h->env, 0);
802 mx_unlock_file (h->lockfile, h->fd, 0);
809 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
811 ret = db_create (&h->db, h->env, 0);
813 h->env->close (h->env, 0);
814 mx_unlock_file (h->lockfile, h->fd, 0);
821 if (stat (path, &sb) != 0 && errno == ENOENT) {
822 createflags |= DB_EXCL;
823 h->db->set_pagesize (h->db, pagesize);
826 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
828 h->db->close (h->db, 0);
829 h->env->close (h->env, 0);
830 mx_unlock_file (h->lockfile, h->fd, 0);
839 void mutt_hcache_close (void *db)
841 struct header_cache *h = db;
847 h->db->close (h->db, 0);
848 h->env->close (h->env, 0);
849 mx_unlock_file (h->lockfile, h->fd, 0);
854 void *mutt_hcache_fetch (void *db, const char *filename,
855 ssize_t (*keylen) (const char *fn))
859 struct header_cache *h = db;
865 filename++; /* skip '/' */
867 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
868 mutt_hcache_dbt_empty_init (&data);
869 data.flags = DB_DBT_MALLOC;
871 h->db->get (h->db, NULL, &key, &data, 0);
873 if (!crc32_matches (data.data, h->crc)) {
874 p_delete(&data.data);
882 mutt_hcache_store (void *db, const char *filename, HEADER * header,
883 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
888 struct header_cache *h = db;
894 filename++; /* skip '/' */
896 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
898 mutt_hcache_dbt_empty_init (&data);
899 data.flags = DB_DBT_USERMEM;
901 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
902 data.ulen = data.size;
904 ret = h->db->put (h->db, NULL, &key, &data, 0);
906 p_delete(&data.data);
912 mutt_hcache_delete (void *db, const char *filename,
913 ssize_t (*keylen) (const char *fn))
916 struct header_cache *h = db;
922 filename++; /* skip '/' */
924 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
925 return h->db->del (h->db, NULL, &key, 0);
929 #endif /* USE_HCACHE */