diff -Nru a/Makefile.am b/Makefile.am --- a/Makefile.am Mon Feb 9 16:13:05 2004 +++ b/Makefile.am Mon Feb 9 16:13:05 2004 @@ -28,10 +28,10 @@ score.c send.c sendlib.c signal.c sort.c \ status.c system.c thread.c charset.c history.c lib.c \ muttlib.c editmsg.c utf8.c mbyte.c wcwidth.c \ - url.c ascii.c mutt_idna.c + url.c ascii.c mutt_idna.c cache.c mutt_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(LIBIMAP) $(MUTTLIBS) \ - $(INTLLIBS) $(LIBICONV) + $(INTLLIBS) $(LIBICONV) -lgdbm mutt_DEPENDENCIES = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(LIBIMAPDEPS) \ $(INTLDEPS) diff -Nru a/cache.c b/cache.c --- /dev/null Wed Dec 31 16:00:00 1969 +++ b/cache.c Mon Feb 9 16:13:05 2004 @@ -0,0 +1,400 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include "mutt.h" +#include "mime.h" +#include "mx.h" +#include "lib.h" + + +static unsigned char * +dump_int(unsigned int i, unsigned char *d, unsigned int *off) +{ + safe_realloc(&d, *off + sizeof(int)); + memcpy(d + *off, &i, sizeof(int)); + (*off) += sizeof(int); + + return d; +} + +static void +restore_int(unsigned int *i, unsigned char *d, unsigned int *off) +{ + memcpy(i, d + *off, sizeof(int)); + (*off) += sizeof(int); +} + +static unsigned char * +dump_char(char *c, unsigned char *d, unsigned int *off) +{ + unsigned int size; + + if (c == NULL) { + size = 0; + d = dump_int(size, d, off); + return d; + } + + size = strlen(c) + 1; + d = dump_int(size, d, off); + safe_realloc(&d, *off + size); + memcpy(d + *off, c, size); + *off += size; + + return d; +} + +static void +restore_char(char **c, unsigned char *d, unsigned int *off) +{ + unsigned int size; + restore_int(&size, d, off); + + if (size == 0) { + *c = NULL; + return; + } + + *c = safe_malloc(size); + memcpy(*c, d + *off, size); + *off += size; +} + +static void +skip_char(unsigned char *d, unsigned int *off) +{ + unsigned int size; + restore_int(&size, d, off); + *off += size; +} + +static unsigned char * +dump_address(ADDRESS *a, unsigned char *d, unsigned int *off) +{ + unsigned int counter = 0; + unsigned int start_off = *off; + + d = dump_int(0xdeadbeaf, d, off); + + while (a) { +#ifdef EXACT_ADDRESS + d = dump_char(a->val, d, off); +#endif + d = dump_char(a->personal, d, off); + d = dump_char(a->mailbox, d, off); + d = dump_int(a->group, d, off); + a = a->next; + counter++; + } + + memcpy(d + start_off, &counter, sizeof(int)); + + return d; +} + +static void +restore_address(ADDRESS **a, unsigned char *d, unsigned int *off) +{ + unsigned int counter; + + restore_int(&counter, d, off); + + while (counter) { + *a = safe_malloc(sizeof(ADDRESS)); +#ifdef EXACT_ADDRESS + restore_char(&(*a)->val, d, off); +#endif + restore_char(&(*a)->personal, d, off); + restore_char(&(*a)->mailbox, d, off); + restore_int((unsigned int *)&(*a)->group, d, off); + a = &(*a)->next; + counter--; + } + + *a = NULL; + return; +} + +static unsigned char * +dump_list(LIST *l, unsigned char *d, unsigned int *off) +{ + unsigned int counter = 0; + unsigned int start_off = *off; + + d = dump_int(0xdeadbeaf, d, off); + + while (l) { + d = dump_char(l->data, d, off); + l = l->next; + counter++; + } + + memcpy(d + start_off, &counter, sizeof(int)); + + return d; +} + +static void +restore_list(LIST **l, unsigned char *d, unsigned int *off) +{ + unsigned int counter; + + restore_int(&counter, d, off); + + while (counter) { + *l = safe_malloc(sizeof(LIST)); + restore_char(&(*l)->data, d, off); + l = &(*l)->next; + counter--; + } + + *l = NULL; + return; +} + +static unsigned char * +dump_parameter(PARAMETER *p, unsigned char *d, unsigned int *off) +{ + unsigned int counter = 0; + unsigned int start_off = *off; + + d = dump_int(0xdeadbeaf, d, off); + + while (p) { + d = dump_char(p->attribute, d, off); + d = dump_char(p->value, d, off); + p = p->next; + counter++; + } + + memcpy(d + start_off, &counter, sizeof(int)); + + return d; +} + +static void +restore_parameter(PARAMETER **p, unsigned char *d, unsigned int *off) +{ + unsigned int counter; + + restore_int(&counter, d, off); + + while (counter) { + *p = safe_malloc(sizeof(PARAMETER)); + restore_char(&(*p)->attribute, d, off); + restore_char(&(*p)->value, d, off); + p = &(*p)->next; + counter--; + } + + *p = NULL; + return; +} + +static unsigned char * +dump_body(BODY *c, unsigned char *d, unsigned int *off) +{ + safe_realloc(&d, *off + sizeof(BODY)); + memcpy(d + *off, c, sizeof(BODY)); + *off += sizeof(BODY); + + d = dump_char(c->xtype, d, off); + d = dump_char(c->subtype, d, off); + + d = dump_parameter(c->parameter, d, off); + + d = dump_char(c->description, d, off); + d = dump_char(c->form_name, d, off); + d = dump_char(c->filename, d, off); + d = dump_char(c->d_filename, d, off); + + return d; +} + +static void +restore_body(BODY *c, unsigned char *d, unsigned int *off) +{ + memcpy(c, d + *off, sizeof(BODY)); + *off += sizeof(BODY); + + restore_char(& c->xtype, d, off); + restore_char(& c->subtype, d, off); + + restore_parameter(& c->parameter, d, off); + + restore_char(& c->description, d, off); + restore_char(& c->form_name, d, off); + restore_char(& c->filename, d, off); + restore_char(& c->d_filename, d, off); +} + +static unsigned char * +dump_envelope(ENVELOPE *e, unsigned char *d, unsigned int *off) +{ + d = dump_address(e->return_path, d, off); + d = dump_address(e->from, d, off); + d = dump_address(e->to, d, off); + d = dump_address(e->cc, d, off); + d = dump_address(e->bcc, d, off); + d = dump_address(e->sender, d, off); + d = dump_address(e->reply_to, d, off); + d = dump_address(e->mail_followup_to, d, off); + + d = dump_char(e->subject, d, off); + d = dump_char(e->real_subj, d, off); + d = dump_char(e->message_id, d, off); + d = dump_char(e->supersedes, d, off); + d = dump_char(e->date, d, off); + d = dump_char(e->x_label, d, off); + + d = dump_list(e->references, d, off); + d = dump_list(e->in_reply_to, d, off); + d = dump_list(e->userhdrs, d, off); + + return d; +} + +static void +restore_envelope(ENVELOPE *e, unsigned char *d, unsigned int *off) +{ + restore_address(& e->return_path, d, off); + restore_address(& e->from, d, off); + restore_address(& e->to, d, off); + restore_address(& e->cc, d, off); + restore_address(& e->bcc, d, off); + restore_address(& e->sender, d, off); + restore_address(& e->reply_to, d, off); + restore_address(& e->mail_followup_to, d, off); + + restore_char(& e->subject, d, off); + restore_char(& e->real_subj, d, off); + restore_char(& e->message_id, d, off); + restore_char(& e->supersedes, d, off); + restore_char(& e->date, d, off); + restore_char(& e->x_label, d, off); + + restore_list(& e->references, d, off); + restore_list(& e->in_reply_to, d, off); + restore_list(& e->userhdrs, d, off); +} + + +/* This function transforms a header into a char so that it is useable by + * gdbm_store */ + +#if HAVE_LANGINFO_CODESET +int +mutt_hcache_charset_matches(unsigned char *d) +{ + unsigned int off = sizeof(struct timeval); + char *charset = NULL; + + restore_char(&charset, d, &off); + + return (0 == mutt_strcmp(charset, Charset)); +} +#endif /* HAVE_LANGINFO_CODESET */ + +void * +mutt_hcache_dump(HEADER *h, unsigned int *off) +{ + unsigned char *d = NULL; + *off = 0; + struct timeval now; + + d = safe_malloc(sizeof(struct timeval)); + gettimeofday(&now, NULL); + memcpy(d, &now, sizeof(struct timeval)); + *off += sizeof(struct timeval); + +#if HAVE_LANGINFO_CODESET + d = dump_char(Charset, d, off); +#endif /* HAVE_LANGINFO_CODESET */ + + + safe_realloc(&d, *off + sizeof(HEADER)); + memcpy(d + *off, h, sizeof(HEADER)); + *off += sizeof(HEADER); + + d = dump_envelope(h->env, d, off); + d = dump_body(h->content, d, off); + + return d; +} + +HEADER * +mutt_hcache_restore(unsigned char *d, HEADER **oh) +{ + unsigned int off = 0; + HEADER *h = mutt_new_header(); + + /* skip timeval */ + off += sizeof(struct timeval); + +#if HAVE_LANGINFO_CODESET + skip_char(d, &off); +#endif /* HAVE_LANGINFO_CODESET */ + + memcpy(h, d + off, sizeof(HEADER)); + off += sizeof(HEADER); + + h->env = mutt_new_envelope(); + restore_envelope(h->env, d, &off); + + h->content = mutt_new_body(); + restore_body(h->content, d, &off); + + h->old = (*oh)->old; + h->path = safe_strdup((*oh)->path); + mutt_free_header (oh); + + return h; +} + +GDBM_FILE +mutt_hcache_open(char *path) +{ + GDBM_FILE db = NULL; + + if (! path) { + return NULL; + } + + db = gdbm_open(path, 0, GDBM_WRCREAT, 00600, NULL); + if (db) { + return db; + } + + /* if rw failed try ro */ + return gdbm_open(path, 0, GDBM_READER, 00600, NULL); +} + +void +mutt_hcache_close(GDBM_FILE db) +{ + if (db) { + gdbm_close(db); + } +} + +datum +mutt_hcache_fetch(GDBM_FILE db, datum key) +{ + if (! db) { + datum ret = {NULL, 0}; + return ret; + } + return gdbm_fetch(db, key); +} + +int +mutt_hcache_store(GDBM_FILE db, datum key, datum data) +{ + if (! db) { + return -1; + } + return gdbm_store(db, key, data, GDBM_REPLACE); +} diff -Nru a/globals.h b/globals.h --- a/globals.h Mon Feb 9 16:13:05 2004 +++ b/globals.h Mon Feb 9 16:13:05 2004 @@ -63,6 +63,7 @@ WHERE char *Locale; WHERE char *MailcapPath; WHERE char *Maildir; +WHERE char *MaildirCache; WHERE char *MhFlagged; WHERE char *MhReplied; WHERE char *MhUnseen; diff -Nru a/init.h b/init.h --- a/init.h Mon Feb 9 16:13:05 2004 +++ b/init.h Mon Feb 9 16:13:05 2004 @@ -981,6 +981,11 @@ ** \fBDON'T CHANGE THIS SETTING UNLESS YOU ARE REALLY SURE WHAT YOU ARE ** DOING!\fP */ + { "maildir_cache", DT_PATH, R_NONE, UL &MaildirCache, 0 }, + /* + ** .pp + ** Path to the maildir cache file. If unset no cache will be used. + */ { "maildir_trash", DT_BOOL, R_NONE, OPTMAILDIRTRASH, 0 }, /* ** .pp diff -Nru a/mh.c b/mh.c --- a/mh.c Mon Feb 9 16:13:05 2004 +++ b/mh.c Mon Feb 9 16:13:05 2004 @@ -42,6 +42,8 @@ #include #include +#include + struct maildir { HEADER *h; @@ -779,14 +781,21 @@ return r; } - /* * This function does the second parsing pass for a maildir-style * folder. */ +static size_t +maildir_cache_keylen(const char *fn) +{ + const char * p = strchr (fn, ':'); + return p ? (size_t) (p - fn) : strlen (fn); +} + void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md) { +#if 0 struct maildir *p; char fn[_POSIX_PATH_MAX]; @@ -799,6 +808,59 @@ else mutt_free_header (&p->h); } +#else + struct maildir *p; + void *db = NULL; + char fn[_POSIX_PATH_MAX]; + datum key; + datum data; + unsigned int size; + struct timeval *when = NULL; + struct stat lastchanged; + char *charset = NULL; + + db = mutt_hcache_open(MaildirCache); + + for (p = md; p; p = p->next) { + if ( !(p && p->h && !p->header_parsed)) { + continue; + } + + snprintf(fn, sizeof(fn), "%s/%s", ctx->path, p->h->path + 4); + key.dptr = fn; + key.dsize = maildir_cache_keylen(fn); + data = mutt_hcache_fetch(db, key); + when = (struct timeval *) data.dptr; + + snprintf(fn, sizeof (fn), "%s/%s", ctx->path, p->h->path); + stat(fn, &lastchanged); + + if (data.dptr != NULL + && lastchanged.st_mtime <= when->tv_sec +#if HAVE_LANGINFO_CODESET + && mutt_hcache_charset_matches(data.dptr) +#endif /* HAVE_LANGINFO_CODESET */ + ) { + p->h = mutt_hcache_restore((unsigned char *)data.dptr, &p->h); + FREE(& data.dptr); + maildir_parse_flags(p->h, fn); + + } else if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) { + maildir_parse_flags(p->h, fn); + p->header_parsed = 1; + if (db) { + /* only try this if db connection is available */ + data.dptr = mutt_hcache_dump(p->h, &size); + data.dsize = size; + mutt_hcache_store(db, key, data); + FREE(& data.dptr); + } + } else { + mutt_free_header (&p->h); + } + } + mutt_hcache_close(db); +#endif } diff -Nru a/protos.h b/protos.h --- a/protos.h Mon Feb 9 16:13:05 2004 +++ b/protos.h Mon Feb 9 16:13:05 2004 @@ -99,6 +99,19 @@ ENVELOPE *mutt_read_rfc822_header (FILE *, HEADER *, short, short); HEADER *mutt_dup_header (HEADER *); +#define USE_HCACHE 1 +#if USE_HCACHE +#include +GDBM_FILE mutt_hcache_open(char *path); +void mutt_hcache_close(GDBM_FILE db); +void * mutt_hcache_dump(HEADER *h, unsigned int *off); +HEADER * mutt_hcache_restore(unsigned char *d, HEADER **oh); +datum mutt_hcache_fetch(GDBM_FILE db, datum key); +int mutt_hcache_store(GDBM_FILE db, datum key, datum data); +void mutt_hcache_restore_charset(char **c, unsigned char *d); +#endif /* USE_HCACHE */ + + ATTACHPTR **mutt_gen_attach_list (BODY *, int, ATTACHPTR **, short *, short *, int, int); time_t mutt_decrease_mtime (const char *, struct stat *);