diff -Nru a/Makefile.am b/Makefile.am --- a/Makefile.am Sat Feb 7 17:05:59 2004 +++ b/Makefile.am Sat Feb 7 17:05:59 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 Sat Feb 7 17:05:59 2004 @@ -0,0 +1,506 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include "mutt.h" +#include "mime.h" +#include "mx.h" +#include "lib.h" + +#define HAVE_GDBM 1 +#define HAVE_NDBM 0 + +#if HAVE_NDBM + +#include + +DBM * +maildir_cache_open(char * path) +{ + return dbm_open(path, O_RDWR | O_CREAT, 00600); +} + +void +maildir_cache_close(DBM *db) +{ + dbm_close(db); +} + +#elif HAVE_GDBM + +#include +GDBM_FILE +maildir_cache_open(char * path) +{ + return gdbm_open(path, 0, GDBM_WRCREAT, 00600, NULL); +} + +void +maildir_cache_close(GDBM_FILE db) +{ + gdbm_close(db); +} + +datum +fetch(GDBM_FILE db, datum key) +{ + return gdbm_fetch(db, key); +} + +int +store(GDBM_FILE db, datum key, datum data) +{ + return gdbm_store(db, key, data, GDBM_REPLACE); +} + +#else +#error no suitable database found +#endif + +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 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); +} + + +#if 1 /* debugging purposes */ + +/* This function transforms a header into a char so that it is useable by + * gdbm_store */ + +void * +dump_header(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); + + + 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 * +restore_header(unsigned char *d, HEADER **oh) +{ + unsigned int off = 0; + HEADER *h = mutt_new_header(); + + /* skip timeval */ + off += sizeof(struct timeval); + + 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; +} + +#else + +void +hexdump(int off, unsigned char *buf, int len) +{ + int i = off; + + fprintf(stderr, "-[%d]-------------------------------------------\n 0\t", + len); + + while (i < len) { + fprintf(stderr," 0x%02x", buf[i]); + i++; + if ((! ((i - off) % 8)) || i == len) { + if (i == len) { + putc('\n', stderr); + } else { + fprintf(stderr, "\n%3d\t", i); + } + } + } + putc('\n', stderr); +} + +int +main(int argc, char **argv) +{ + unsigned int off = 0; + unsigned int i = 0xFFFFFFFF; + unsigned char *c = "Hello"; + unsigned char *d = NULL; + ADDRESS *a = NULL; + ADDRESS *b = NULL; + LIST *l = NULL; + LIST *s = NULL; + + printf("Header size: %d\n", sizeof(HEADER)); + d = safe_malloc(sizeof(HEADER)); + off += sizeof(HEADER); + +#if 0 /* test int stuff */ + printf("[TG] off = %d\n", off); + d = dump_int(i, d, &off); + printf("[TG] off = %d\n", off); + hexdump(0, d, off); + + off = sizeof(HEADER); + + i = 0; + restore_int(&i, d, &off); + printf("[TG] i = 0x%08x off = %d\n", i, off); +#endif + +#if 0 /* char stuff */ + printf("[TG] off = %d\n", off); + c = NULL; + d = dump_char(c, d, &off); + printf("[TG] off = %d\n", off); + hexdump(0, d, off); + + off = sizeof(HEADER); +#if 0 /* test NULL pointer */ + c = NULL; +#endif + restore_char((char **)&c, d, &off); + printf("[TG] <%s>\n", c); +#endif + +#if 0 /* address testing */ + a = b = safe_malloc(sizeof(ADDRESS)); + a->personal = NULL; + a->mailbox = "lutz"; + a->group = 0x42; + a->next = safe_malloc(sizeof(ADDRESS)); + a = a->next; + a->personal = "Step by Step as usual"; + a->mailbox = "Gielen"; + a->group = 0x21; + a->next = NULL; + + printf("addr = 0x%08x\n", d); + + printf("[TG] off = %d\n", off); + hexdump(0, d, off); + b = NULL; + d = dump_address(b, d, &off); + hexdump(0, d, off); + printf("[TG] off = %d\n", off); + printf("addr = 0x%08x\n", d); + + a = NULL; + off = sizeof(HEADER); + restore_address(&a, d, &off); + + while(a) { + printf("<%s> <%s> <0x%02x> <0x%02x>\n", + a->personal, + a->mailbox, + a->group, + a->next); + a = a->next; + } +#endif + +#if 0 /* list testing */ + l = s = safe_malloc(sizeof(LIST)); + l->data = NULL; + l->next = safe_malloc(sizeof(LIST)); + l = l->next; + l->data = "Hello World"; + l->next = NULL; + + d = dump_list(s, d, &off); + hexdump(0, d, off); + l = NULL; + off = sizeof(HEADER); + restore_list(&l, d, &off); + + while(l) { + printf("<%s> <0x%02x>\n", + l->data, + l->next); + l = l->next; + } +#endif + + return 0; +} +#endif diff -Nru a/mh.c b/mh.c --- a/mh.c Sat Feb 7 17:05:59 2004 +++ b/mh.c Sat Feb 7 17:05:59 2004 @@ -42,6 +42,8 @@ #include #include +#include + struct maildir { HEADER *h; @@ -779,14 +781,28 @@ return r; } - /* * This function does the second parsing pass for a maildir-style * folder. */ +void *maildir_cache_open(char *path); +void *maildir_cache_close(void *db); +void * dump_header(HEADER *h, unsigned int *off); +HEADER * restore_header(unsigned char *d, HEADER **oh); +datum fetch(void *db, datum key); +int store(void *db, datum key, datum data); + +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 +815,54 @@ 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; + + snprintf(fn, sizeof (fn), "%s/hcache", ctx->path); + db = maildir_cache_open(fn); + + 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); + stat(fn, &lastchanged); + + key.dptr = p->h->path + 4; + key.dsize = maildir_cache_keylen(p->h->path + 4); + data = fetch(db, key); + when = (struct timeval *) data.dptr; + + if (data.dptr != NULL + && lastchanged.st_mtime <= when->tv_sec) { + p->h = restore_header((unsigned char *)data.dptr, &p->h); +#if 1 + FREE(& data.dptr); +#endif + 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; + data.dptr = dump_header(p->h, &size); + data.dsize = size; + store(db, key, data); + FREE(& data.dptr); + + } else { + mutt_free_header (&p->h); + } + } + maildir_cache_close(db); +#endif }