X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=hcache.c;h=140df180c6b746ebeed019b631cd272dfd6c532e;hp=5e2674357f66f52f0d9fa3e55c78bdc77e415529;hb=83532821ae9fab034d0d630b78330c9ea4ff4cf3;hpb=0f44dc85fc1280372ffab911d701e703d803fb4b diff --git a/hcache.c b/hcache.c index 5e26743..140df18 100644 --- a/hcache.c +++ b/hcache.c @@ -8,6 +8,13 @@ * It's licensed under the GNU General Public License, * please see the file GPL in the top level source directory. */ +#if HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#ifdef USE_HCACHE + +#define MUTTNG_HCACHE_ID "0x004" # if HAVE_INTTYPES_H # include @@ -17,13 +24,11 @@ # endif # endif -#if HAVE_CONFIG_H -#include "config.h" -#endif /* HAVE_CONFIG_H */ - -#ifdef USE_HCACHE - -#if HAVE_GDBM +#if HAVE_QDBM +#include +#include +#include +#elif HAVE_GDBM #include #elif HAVE_DB4 #include @@ -34,18 +39,29 @@ #if HAVE_SYS_TIME_H #include #endif + +#include +#include + +#include + #include "mutt.h" #ifdef USE_IMAP #include "message.h" #endif -#include "mime.h" #include "mx.h" #include "lib.h" -#include "md5.h" -#include "lib/mem.h" +#include "lib/debug.h" -#if HAVE_GDBM +#if HAVE_QDBM +static struct + header_cache { + VILLA *db; + char *folder; + unsigned int crc; +} HEADER_CACHE; +#elif HAVE_GDBM static struct header_cache { GDBM_FILE db; @@ -74,7 +90,7 @@ static void *lazy_malloc (size_t siz) siz = 4096; } - return safe_malloc (siz); + return xmalloc(siz); } static void lazy_realloc (void *ptr, size_t siz) @@ -85,7 +101,7 @@ static void lazy_realloc (void *ptr, size_t siz) return; } - safe_realloc (ptr, siz); + p_realloc(ptr, siz); } static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off) @@ -113,7 +129,7 @@ static unsigned char *dump_char (char *c, unsigned char *d, int *off) return d; } - size = safe_strlen (c) + 1; + size = m_strlen(c) + 1; d = dump_int (size, d, off); lazy_realloc (&d, *off + size); memcpy (d + *off, c, size); @@ -152,12 +168,11 @@ static void restore_char (char **c, const unsigned char *d, int *off) return; } - *c = safe_malloc (size); - memcpy (*c, d + *off, size); + *c = p_dup(d + *off, size); *off += size; } -static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off) +static unsigned char *dump_address (address_t * a, unsigned char *d, int *off) { unsigned int counter = 0; unsigned int start_off = *off; @@ -165,9 +180,6 @@ static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off) d = dump_int (0xdeadbeef, 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); @@ -180,17 +192,14 @@ static unsigned char *dump_address (ADDRESS * a, unsigned char *d, int *off) return d; } -static void restore_address (ADDRESS ** a, const unsigned char *d, int *off) +static void restore_address (address_t ** a, const unsigned char *d, 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 + *a = p_new(address_t, 1); restore_char (&(*a)->personal, d, off); restore_char (&(*a)->mailbox, d, off); restore_int ((unsigned int *) &(*a)->group, d, off); @@ -226,7 +235,7 @@ static void restore_list (LIST ** l, const unsigned char *d, int *off) restore_int (&counter, d, off); while (counter) { - *l = safe_malloc (sizeof (LIST)); + *l = p_new(LIST, 1); restore_char (&(*l)->data, d, off); l = &(*l)->next; counter--; @@ -264,7 +273,7 @@ static void restore_buffer (BUFFER ** b, const unsigned char *d, int *off) return; } - *b = safe_malloc (sizeof (BUFFER)); + *b = p_new(BUFFER, 1); restore_char (&(*b)->data, d, off); restore_int (&offset, d, off); @@ -302,7 +311,7 @@ restore_parameter (PARAMETER ** p, const unsigned char *d, int *off) restore_int (&counter, d, off); while (counter) { - *p = safe_malloc (sizeof (PARAMETER)); + *p = p_new(PARAMETER, 1); restore_char (&(*p)->attribute, d, off); restore_char (&(*p)->value, d, off); p = &(*p)->next; @@ -369,6 +378,14 @@ static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *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_char (e->list_post, d, off); + +#ifdef USE_NNTP + d = dump_char (e->newsgroups, d, off); + d = dump_char (e->xref, d, off); + d = dump_char (e->followup_to, d, off); + d = dump_char (e->x_comment_to, d, off); +#endif d = dump_list (e->references, d, off); d = dump_list (e->in_reply_to, d, off); @@ -402,6 +419,14 @@ static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off) restore_char (&e->supersedes, d, off); restore_char (&e->date, d, off); restore_char (&e->x_label, d, off); + restore_char (&e->list_post, d, off); + +#ifdef USE_NNTP + restore_char (&e->newsgroups, d, off); + restore_char (&e->xref, d, off); + restore_char (&e->followup_to, d, off); + restore_char (&e->x_comment_to, d, off); +#endif restore_list (&e->references, d, off); restore_list (&e->in_reply_to, d, off); @@ -426,37 +451,36 @@ static int generate_crc32 () int crc = 0; crc = crc32 (crc, (unsigned char const *) - "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613", - safe_strlen - ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613")); + MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613", + m_strlen + (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613")); #if HAVE_LANGINFO_CODESET - crc = crc32 (crc, (unsigned char const *) Charset, safe_strlen (Charset)); + crc = crc32 (crc, (unsigned char const *) Charset, m_strlen(Charset)); crc = crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET", - safe_strlen ("HAVE_LANGINFO_CODESET")); -#endif - -#if EXACT_ADDRESS - crc = - crc32 (crc, (unsigned char const *) "EXACT_ADDRESS", - safe_strlen ("EXACT_ADDRESS")); + m_strlen("HAVE_LANGINFO_CODESET")); #endif #ifdef USE_POP crc = - crc32 (crc, (unsigned char const *) "USE_POP", safe_strlen ("USE_POP")); + crc32 (crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP")); #endif #ifdef MIXMASTER crc = crc32 (crc, (unsigned char const *) "MIXMASTER", - safe_strlen ("MIXMASTER")); + m_strlen("MIXMASTER")); #endif #ifdef USE_IMAP crc = - crc32 (crc, (unsigned char const *) "USE_IMAP", safe_strlen ("USE_IMAP")); + crc32 (crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP")); +#endif + +#ifdef USE_NNTP + crc = + crc32 (crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP")); #endif return crc; } @@ -495,7 +519,7 @@ static const char *mutt_hcache_per_folder (const char *path, } MD5Init (&md5); - MD5Update (&md5, (unsigned char *) folder, safe_strlen (folder)); + MD5Update (&md5, (unsigned char *) folder, m_strlen(folder)); MD5Final (md5sum, &md5); ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX, @@ -574,27 +598,149 @@ HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh) /* this is needed for maildir style mailboxes */ if (oh) { h->old = (*oh)->old; - h->path = safe_strdup ((*oh)->path); + h->path = m_strdup((*oh)->path); mutt_free_header (oh); } return h; } -#if HAVE_GDBM +#if HAVE_QDBM +void * +mutt_hcache_open(const char *path, const char *folder) +{ + struct header_cache *h = p_new(HEADER_CACHE, 1); + int flags = VL_OWRITER | VL_OCREAT; + h->db = NULL; + h->folder = m_strdup(folder); + h->crc = generate_crc32(); + + if (!path || path[0] == '\0') + { + p_delete(&h->folder); + p_delete(&h); + return NULL; + } + + path = mutt_hcache_per_folder(path, folder); + + if (option(OPTHCACHECOMPRESS)) + flags |= VL_OZCOMP; + + h->db = vlopen(path, flags, VL_CMPLEX); + if (h->db) + return h; + else + { + p_delete(&h->folder); + p_delete(&h); + + return NULL; + } +} + +void +mutt_hcache_close(void *db) +{ + struct header_cache *h = db; + + if (!h) + return; + + vlclose(h->db); + p_delete(&h->folder); + p_delete(&h); +} + +void * +mutt_hcache_fetch(void *db, const char *filename, + size_t(*keylen) (const char *fn)) +{ + struct header_cache *h = db; + char path[_POSIX_PATH_MAX]; + int ksize, len; + char *data = NULL; + + if (!h) + return NULL; + + m_strcpy(path, sizeof(path), h->folder); + m_strcat(path, sizeof(path), filename); + + ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); + + data = vlget(h->db, path, ksize, NULL); + + if (!crc32_matches(data, h->crc)) + { + p_delete(&data); + return NULL; + } + + return data; +} + +int +mutt_hcache_store(void *db, const char *filename, HEADER * header, + unsigned long uid_validity, + size_t(*keylen) (const char *fn)) +{ + struct header_cache *h = db; + char path[_POSIX_PATH_MAX]; + int ret; + int ksize, dsize; + char *data = NULL; + + if (!h) + return -1; + + m_strcpy(path, sizeof(path), h->folder); + m_strcat(path, sizeof(path), filename); + + ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); + + data = mutt_hcache_dump(db, header, &dsize, uid_validity); + + ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER); + + p_delete(&data); + + return ret; +} + +int +mutt_hcache_delete(void *db, const char *filename, + size_t(*keylen) (const char *fn)) +{ + struct header_cache *h = db; + char path[_POSIX_PATH_MAX]; + int ksize; + + if (!h) + return -1; + + m_strcpy(path, sizeof(path), h->folder); + m_strcat(path, sizeof(path), filename); + + ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); + + return vlout(h->db, path, ksize); +} + +#elif HAVE_GDBM void *mutt_hcache_open (const char *path, const char *folder) { - struct header_cache *h = safe_calloc (1, sizeof (HEADER_CACHE)); + struct header_cache *h = p_new(HEADER_CACHE, 1); int pagesize = atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384; h->db = NULL; - h->folder = safe_strdup (folder); + h->folder = m_strdup(folder); h->crc = generate_crc32 (); if (!path || path[0] == '\0') { - FREE (&h->folder); - FREE (&h); + p_delete(&h->folder); + p_delete(&h); return NULL; } @@ -611,8 +757,8 @@ void *mutt_hcache_open (const char *path, const char *folder) return h; } else { - FREE (&h->folder); - FREE (&h); + p_delete(&h->folder); + p_delete(&h); return NULL; } @@ -627,8 +773,8 @@ void mutt_hcache_close (void *db) } gdbm_close (h->db); - FREE (&h->folder); - FREE (&h); + p_delete(&h->folder); + p_delete(&h); } void *mutt_hcache_fetch (void *db, const char *filename, @@ -643,8 +789,8 @@ void *mutt_hcache_fetch (void *db, const char *filename, return NULL; } - strncpy (path, h->folder, sizeof (path)); - strncat (path, filename, sizeof (path) - safe_strlen (path)); + m_strcpy(path, sizeof(path), h->folder); + strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; key.dsize = keylen (path); @@ -652,7 +798,7 @@ void *mutt_hcache_fetch (void *db, const char *filename, data = gdbm_fetch (h->db, key); if (!crc32_matches (data.dptr, h->crc)) { - free (data.dptr); + p_delete(&data.dptr); return NULL; } @@ -673,8 +819,8 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header, return -1; } - strncpy (path, h->folder, sizeof (path)); - strncat (path, filename, sizeof (path) - safe_strlen (path)); + m_strcpy(path, sizeof(path), h->folder); + strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; key.dsize = keylen (path); @@ -683,7 +829,7 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header, ret = gdbm_store (h->db, key, data, GDBM_REPLACE); - FREE (&data.dptr); + p_delete(&data.dptr); return ret; } @@ -700,8 +846,8 @@ mutt_hcache_delete (void *db, const char *filename, return -1; } - strncpy (path, h->folder, sizeof (path)); - strncat (path, filename, sizeof (path) - safe_strlen (path)); + m_strcpy(path, sizeof(path), h->folder); + strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; key.dsize = keylen (path); @@ -737,7 +883,7 @@ void *mutt_hcache_open (const char *path, const char *folder) h->crc = generate_crc32 (); if (!path || path[0] == '\0') { - FREE (&h); + p_delete(&h); return NULL; } @@ -747,13 +893,13 @@ void *mutt_hcache_open (const char *path, const char *folder) h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (h->fd < 0) { - FREE (&h); + p_delete(&h); return NULL; } if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) { close (h->fd); - FREE (&h); + p_delete(&h); return NULL; } @@ -761,19 +907,19 @@ void *mutt_hcache_open (const char *path, const char *folder) if (ret) { mx_unlock_file (h->lockfile, h->fd, 0); close (h->fd); - FREE (&h); + p_delete(&h); return NULL; } ret = - h->env->open (h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600); + (h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 0600); if (!ret) { ret = db_create (&h->db, h->env, 0); if (ret) { h->env->close (h->env, 0); mx_unlock_file (h->lockfile, h->fd, 0); close (h->fd); - FREE (&h); + p_delete(&h); return NULL; } } @@ -783,13 +929,13 @@ void *mutt_hcache_open (const char *path, const char *folder) h->db->set_pagesize (h->db, pagesize); } - ret = h->db->open (h->db, NULL, path, folder, DB_BTREE, createflags, 0600); + ret = (h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600); if (ret) { h->db->close (h->db, 0); h->env->close (h->env, 0); mx_unlock_file (h->lockfile, h->fd, 0); close (h->fd); - FREE (&h); + p_delete(&h); return NULL; } @@ -799,7 +945,6 @@ void *mutt_hcache_open (const char *path, const char *folder) void mutt_hcache_close (void *db) { struct header_cache *h = db; - int ret; if (!h) { return; @@ -809,7 +954,7 @@ void mutt_hcache_close (void *db) h->env->close (h->env, 0); mx_unlock_file (h->lockfile, h->fd, 0); close (h->fd); - FREE (&h); + p_delete(&h); } void *mutt_hcache_fetch (void *db, const char *filename, @@ -832,7 +977,7 @@ void *mutt_hcache_fetch (void *db, const char *filename, h->db->get (h->db, NULL, &key, &data, 0); if (!crc32_matches (data.data, h->crc)) { - free (data.data); + p_delete(&data.data); return NULL; } @@ -864,7 +1009,7 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header, ret = h->db->put (h->db, NULL, &key, &data, 0); - FREE (&data.data); + p_delete(&data.data); return ret; }