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 #endif /* HAVE_CONFIG_H */
17 #define MUTTNG_HCACHE_ID "0x004"
19 # ifdef HAVE_INTTYPES_H
20 # include <inttypes.h>
27 #if defined(HAVE_QDBM)
31 #elif defined(HAVE_GDBM)
33 #elif defined(HAVE_DB4)
39 #ifdef HAVE_SYS_TIME_H
43 #include <lib-lib/lib-lib.h>
44 #include <lib-hash/hash.h>
46 #include <lib-mime/mime.h>
50 #include <imap/message.h>
55 #if defined(HAVE_QDBM)
59 #elif defined(HAVE_GDBM)
63 #elif defined(HAVE_DB4)
68 char lockfile[_POSIX_PATH_MAX];
73 struct timeval timeval;
74 unsigned long uid_validity;
77 #define UPPER4K(i) ((i & ~(4096 - 1)) + 4096)
79 static unsigned char *lazy_malloc(ssize_t siz)
81 return p_new(unsigned char, UPPER4K(siz));
84 static void lazy_realloc(unsigned char **p, ssize_t siz)
86 p_realloc(p, UPPER4K(siz));
89 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
91 lazy_realloc (&d, *off + sizeof (int));
92 memcpy (d + *off, &i, sizeof (int));
93 (*off) += sizeof (int);
98 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
100 memcpy (i, d + *off, sizeof (int));
101 (*off) += sizeof (int);
104 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
110 d = dump_int (size, d, off);
114 size = m_strlen(c) + 1;
115 d = dump_int (size, d, off);
116 lazy_realloc (&d, *off + size);
117 memcpy (d + *off, c, size);
123 static void restore_char (char **c, const unsigned char *d, int *off)
127 restore_int (&size, d, off);
134 *c = p_dup(d + *off, size);
138 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
140 unsigned int counter = 0;
141 unsigned int start_off = *off;
143 d = dump_int (0xdeadbeef, d, off);
146 d = dump_char (a->personal, d, off);
147 d = dump_char (a->mailbox, d, off);
148 d = dump_int (a->group, d, off);
153 memcpy (d + start_off, &counter, sizeof (int));
158 static void restore_address (address_t ** a, const unsigned char *d, int *off)
160 unsigned int counter;
162 restore_int (&counter, d, off);
165 *a = p_new(address_t, 1);
166 restore_char (&(*a)->personal, d, off);
167 restore_char (&(*a)->mailbox, d, off);
168 restore_int ((unsigned int *) &(*a)->group, d, off);
176 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
178 unsigned int counter = 0;
179 unsigned int start_off = *off;
181 d = dump_int (0xdeadbeef, d, off);
184 d = dump_char (l->data, d, off);
189 memcpy (d + start_off, &counter, sizeof (int));
194 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
196 unsigned int counter;
198 restore_int (&counter, d, off);
201 *l = p_new(string_list_t, 1);
202 restore_char (&(*l)->data, d, off);
210 static unsigned char *dump_parameter (parameter_t * p, unsigned char *d,
213 unsigned int counter = 0;
214 unsigned int start_off = *off;
216 d = dump_int (0xdeadbeef, d, off);
219 d = dump_char (p->attribute, d, off);
220 d = dump_char (p->value, d, off);
225 memcpy (d + start_off, &counter, sizeof (int));
231 restore_parameter (parameter_t ** p, const unsigned char *d, int *off)
233 unsigned int counter;
235 restore_int (&counter, d, off);
238 *p = parameter_new();
239 restore_char (&(*p)->attribute, d, off);
240 restore_char (&(*p)->value, d, off);
246 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
248 lazy_realloc (&d, *off + sizeof (BODY));
249 memcpy (d + *off, c, sizeof (BODY));
250 *off += sizeof (BODY);
252 d = dump_char (c->xtype, d, off);
253 d = dump_char (c->subtype, d, off);
255 d = dump_parameter (c->parameter, d, off);
257 d = dump_char (c->description, d, off);
258 d = dump_char (c->form_name, d, off);
259 d = dump_char (c->filename, d, off);
260 d = dump_char (c->d_filename, d, off);
265 static void restore_body (BODY * c, const unsigned char *d, int *off)
267 memcpy (c, d + *off, sizeof (BODY));
268 *off += sizeof (BODY);
270 restore_char (&c->xtype, d, off);
271 restore_char (&c->subtype, d, off);
273 restore_parameter (&c->parameter, d, off);
275 restore_char (&c->description, d, off);
276 restore_char (&c->form_name, d, off);
277 restore_char (&c->filename, d, off);
278 restore_char (&c->d_filename, d, off);
281 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
283 d = dump_address (e->return_path, d, off);
284 d = dump_address (e->from, d, off);
285 d = dump_address (e->to, d, off);
286 d = dump_address (e->cc, d, off);
287 d = dump_address (e->bcc, d, off);
288 d = dump_address (e->sender, d, off);
289 d = dump_address (e->reply_to, d, off);
290 d = dump_address (e->mail_followup_to, d, off);
292 d = dump_char (e->subject, d, off);
294 d = dump_int (e->real_subj - e->subject, d, off);
297 d = dump_int (-1, d, off);
299 d = dump_char (e->message_id, d, off);
300 d = dump_char (e->supersedes, d, off);
301 d = dump_char (e->date, d, off);
302 d = dump_char (e->x_label, d, off);
303 d = dump_char (e->list_post, d, off);
306 d = dump_char (e->newsgroups, d, off);
307 d = dump_char (e->xref, d, off);
308 d = dump_char (e->followup_to, d, off);
309 d = dump_char (e->x_comment_to, d, off);
312 d = dump_list (e->references, d, off);
313 d = dump_list (e->in_reply_to, d, off);
314 d = dump_list (e->userhdrs, d, off);
319 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
323 restore_address (&e->return_path, d, off);
324 restore_address (&e->from, d, off);
325 restore_address (&e->to, d, off);
326 restore_address (&e->cc, d, off);
327 restore_address (&e->bcc, d, off);
328 restore_address (&e->sender, d, off);
329 restore_address (&e->reply_to, d, off);
330 restore_address (&e->mail_followup_to, d, off);
332 restore_char (&e->subject, d, off);
333 restore_int ((unsigned int *) (&real_subj_off), d, off);
334 if (0 <= real_subj_off) {
335 e->real_subj = e->subject + real_subj_off;
340 restore_char (&e->message_id, d, off);
341 restore_char (&e->supersedes, d, off);
342 restore_char (&e->date, d, off);
343 restore_char (&e->x_label, d, off);
344 restore_char (&e->list_post, d, off);
347 restore_char (&e->newsgroups, d, off);
348 restore_char (&e->xref, d, off);
349 restore_char (&e->followup_to, d, off);
350 restore_char (&e->x_comment_to, d, off);
353 restore_list (&e->references, d, off);
354 restore_list (&e->in_reply_to, d, off);
355 restore_list (&e->userhdrs, d, off);
359 unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
365 for (i = 0; i < 8; i++)
366 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
371 static int generate_crc32 ()
375 crc = crc32 (crc, (unsigned char const *)
376 MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
378 (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
380 #ifdef HAVE_LANGINFO_CODESET
381 crc = crc32(crc, (unsigned char const *) Charset, m_strlen(Charset));
382 crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
383 m_strlen("HAVE_LANGINFO_CODESET"));
386 crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
389 crc = crc32(crc, (unsigned char const *) "MIXMASTER",
390 m_strlen("MIXMASTER"));
393 crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
396 crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
401 static int crc32_matches (const char *d, unsigned int crc)
403 int off = sizeof (validate);
404 unsigned int mycrc = 0;
410 restore_int (&mycrc, (unsigned char *) d, &off);
412 return (crc == mycrc);
415 /* Append md5sumed folder to path if path is a directory. */
416 static const char *mutt_hcache_per_folder (const char *path,
419 static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX];
420 struct stat path_stat;
422 unsigned char md5sum[16];
425 ret = stat (path, &path_stat);
430 if (!S_ISDIR (path_stat.st_mode)) {
435 MD5Update (&md5, (unsigned char *) folder, m_strlen(folder));
436 MD5Final (md5sum, &md5);
438 ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
439 "%s/%02x%02x%02x%02x%02x%02x%02x%02x"
440 "%02x%02x%02x%02x%02x%02x%02x%02x",
441 path, md5sum[0], md5sum[1], md5sum[2], md5sum[3],
442 md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8],
443 md5sum[9], md5sum[10], md5sum[11], md5sum[12],
444 md5sum[13], md5sum[14], md5sum[15]);
450 return mutt_hcache_per_folder_path;
453 /* This function transforms a header into a char so that it is useable by
455 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
456 unsigned long uid_validity)
458 struct header_cache *db = _db;
459 unsigned char *d = NULL;
463 d = lazy_malloc (sizeof (validate));
466 memcpy (d, &uid_validity, sizeof (unsigned long));
471 gettimeofday (&now, NULL);
472 memcpy (d, &now, sizeof (struct timeval));
474 *off += sizeof (validate);
476 d = dump_int (db->crc, d, off);
478 lazy_realloc (&d, *off + sizeof (HEADER));
479 memcpy (d + *off, h, sizeof (HEADER));
480 *off += sizeof (HEADER);
482 d = dump_envelope (h->env, d, off);
483 d = dump_body (h->content, d, off);
484 d = dump_char (h->maildir_flags, d, off);
489 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
492 HEADER *h = header_new();
495 off += sizeof (validate);
498 off += sizeof (unsigned int);
500 memcpy (h, d + off, sizeof (HEADER));
501 off += sizeof (HEADER);
503 h->env = envelope_new();
504 restore_envelope (h->env, d, &off);
506 h->content = body_new();
507 restore_body (h->content, d, &off);
509 restore_char (&h->maildir_flags, d, &off);
511 /* this is needed for maildir style mailboxes */
514 h->path = m_strdup((*oh)->path);
521 #if defined(HAVE_QDBM)
523 mutt_hcache_open(const char *path, const char *folder)
525 struct header_cache *h = p_new(struct header_cache, 1);
526 int flags = VL_OWRITER | VL_OCREAT;
528 h->folder = m_strdup(folder);
529 h->crc = generate_crc32();
531 if (!path || path[0] == '\0')
533 p_delete(&h->folder);
538 path = mutt_hcache_per_folder(path, folder);
540 if (option(OPTHCACHECOMPRESS))
543 h->db = vlopen(path, flags, VL_CMPLEX);
548 p_delete(&h->folder);
556 mutt_hcache_close(void *db)
558 struct header_cache *h = db;
564 p_delete(&h->folder);
569 mutt_hcache_fetch(void *db, const char *filename,
570 ssize_t(*keylen) (const char *fn))
572 struct header_cache *h = db;
573 char path[_POSIX_PATH_MAX];
580 m_strcpy(path, sizeof(path), h->folder);
581 m_strcat(path, sizeof(path), filename);
583 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
585 data = vlget(h->db, path, ksize, NULL);
587 if (!crc32_matches(data, h->crc))
597 mutt_hcache_store(void *db, const char *filename, HEADER * header,
598 unsigned long uid_validity,
599 ssize_t(*keylen) (const char *fn))
601 struct header_cache *h = db;
602 char path[_POSIX_PATH_MAX];
610 m_strcpy(path, sizeof(path), h->folder);
611 m_strcat(path, sizeof(path), filename);
613 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
615 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
617 ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
625 mutt_hcache_delete(void *db, const char *filename,
626 ssize_t(*keylen) (const char *fn))
628 struct header_cache *h = db;
629 char path[_POSIX_PATH_MAX];
635 m_strcpy(path, sizeof(path), h->folder);
636 m_strcat(path, sizeof(path), filename);
638 ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
640 return vlout(h->db, path, ksize);
643 #elif defined(HAVE_GDBM)
645 void *mutt_hcache_open (const char *path, const char *folder)
647 struct header_cache *h = p_new(struct header_cache, 1);
649 atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384;
651 h->folder = m_strdup(folder);
652 h->crc = generate_crc32 ();
654 if (!path || path[0] == '\0') {
655 p_delete(&h->folder);
660 path = mutt_hcache_per_folder (path, folder);
662 h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
667 /* if rw failed try ro */
668 h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL);
673 p_delete(&h->folder);
680 void mutt_hcache_close (void *db)
682 struct header_cache *h = db;
689 p_delete(&h->folder);
693 void *mutt_hcache_fetch (void *db, const char *filename,
694 ssize_t (*keylen) (const char *fn))
696 struct header_cache *h = db;
699 char path[_POSIX_PATH_MAX];
705 m_strcpy(path, sizeof(path), h->folder);
706 strncat (path, filename, sizeof (path) - m_strlen(path));
709 key.dsize = keylen (path);
711 data = gdbm_fetch (h->db, key);
713 if (!crc32_matches (data.dptr, h->crc)) {
714 p_delete(&data.dptr);
722 mutt_hcache_store (void *db, const char *filename, HEADER * header,
723 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
725 struct header_cache *h = db;
728 char path[_POSIX_PATH_MAX];
735 m_strcpy(path, sizeof(path), h->folder);
736 strncat (path, filename, sizeof (path) - m_strlen(path));
739 key.dsize = keylen (path);
741 data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
743 ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
745 p_delete(&data.dptr);
751 mutt_hcache_delete (void *db, const char *filename,
752 ssize_t (*keylen) (const char *fn))
755 struct header_cache *h = db;
756 char path[_POSIX_PATH_MAX];
762 m_strcpy(path, sizeof(path), h->folder);
763 strncat (path, filename, sizeof (path) - m_strlen(path));
766 key.dsize = keylen (path);
768 return gdbm_delete (h->db, key);
770 #elif defined(HAVE_DB4)
772 static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
775 dbt->size = dbt->ulen = len;
776 dbt->dlen = dbt->doff = 0;
777 dbt->flags = DB_DBT_USERMEM;
780 static void mutt_hcache_dbt_empty_init (DBT * dbt)
783 dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
787 void *mutt_hcache_open (const char *path, const char *folder)
790 u_int32_t createflags = DB_CREATE;
792 struct header_cache *h = p_new(struct header_cache, 1);
793 int pagesize = atoi (HeaderCachePageSize);
796 h->crc = generate_crc32 ();
798 if (!path || path[0] == '\0') {
803 path = mutt_hcache_per_folder (path, folder);
805 snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
807 h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
813 if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
819 ret = db_env_create (&h->env, 0);
821 mx_unlock_file (h->lockfile, h->fd, 0);
828 (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600);
830 ret = db_create (&h->db, h->env, 0);
832 h->env->close (h->env, 0);
833 mx_unlock_file (h->lockfile, h->fd, 0);
840 if (stat (path, &sb) != 0 && errno == ENOENT) {
841 createflags |= DB_EXCL;
842 h->db->set_pagesize (h->db, pagesize);
845 ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600);
847 h->db->close (h->db, 0);
848 h->env->close (h->env, 0);
849 mx_unlock_file (h->lockfile, h->fd, 0);
858 void mutt_hcache_close (void *db)
860 struct header_cache *h = db;
866 h->db->close (h->db, 0);
867 h->env->close (h->env, 0);
868 mx_unlock_file (h->lockfile, h->fd, 0);
873 void *mutt_hcache_fetch (void *db, const char *filename,
874 ssize_t (*keylen) (const char *fn))
878 struct header_cache *h = db;
884 filename++; /* skip '/' */
886 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
887 mutt_hcache_dbt_empty_init (&data);
888 data.flags = DB_DBT_MALLOC;
890 h->db->get (h->db, NULL, &key, &data, 0);
892 if (!crc32_matches (data.data, h->crc)) {
893 p_delete(&data.data);
901 mutt_hcache_store (void *db, const char *filename, HEADER * header,
902 unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
907 struct header_cache *h = db;
913 filename++; /* skip '/' */
915 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
917 mutt_hcache_dbt_empty_init (&data);
918 data.flags = DB_DBT_USERMEM;
920 mutt_hcache_dump (db, header, (signed int *) &data.size, uid_validity);
921 data.ulen = data.size;
923 ret = h->db->put (h->db, NULL, &key, &data, 0);
925 p_delete(&data.data);
931 mutt_hcache_delete (void *db, const char *filename,
932 ssize_t (*keylen) (const char *fn))
935 struct header_cache *h = db;
941 filename++; /* skip '/' */
943 mutt_hcache_dbt_init (&key, (void *) filename, keylen (filename));
944 return h->db->del (h->db, NULL, &key, 0);
948 #endif /* USE_HCACHE */