Rocco Rutte:
[apps/madmutt.git] / hcache.c
index 058645f..f64a601 100644 (file)
--- a/hcache.c
+++ b/hcache.c
 
 #ifdef USE_HCACHE
 
-#if HAVE_GDBM
+#if HAVE_QDBM
+#include <depot.h>
+#include <cabin.h>
+#include <villa.h>
+#elif HAVE_GDBM
 #include <gdbm.h>
 #elif HAVE_DB4
 #include <db.h>
 #include "lib.h"
 #include "md5.h"
 
-#if HAVE_GDBM
+#include "lib/mem.h"
+#include "lib/debug.h"
+
+#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;
@@ -63,7 +77,7 @@ static struct
 
 typedef union {
   struct timeval timeval;
-  uint64_t uid_validity;
+  unsigned long uid_validity;
 } validate;
 
 static void *lazy_malloc (size_t siz)
@@ -163,9 +177,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);
@@ -186,9 +197,6 @@ static void restore_address (ADDRESS ** a, const unsigned char *d, int *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);
@@ -368,16 +376,26 @@ static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
   d = dump_char (e->date, d, off);
   d = dump_char (e->x_label, 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);
   d = dump_list (e->userhdrs, d, off);
 
+  d = dump_int (e->irt_changed, d, off);
+  d = dump_int (e->refs_changed, d, off);
+
   return d;
 }
 
 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
 {
-  int real_subj_off;
+  int tmp = 0;
 
   restore_address (&e->return_path, d, off);
   restore_address (&e->from, d, off);
@@ -389,9 +407,9 @@ static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
   restore_address (&e->mail_followup_to, d, off);
 
   restore_char (&e->subject, d, off);
-  restore_int ((unsigned int *) (&real_subj_off), d, off);
-  if (0 <= real_subj_off) {
-    e->real_subj = e->subject + real_subj_off;
+  restore_int ((unsigned int *) (&tmp), d, off);
+  if (0 <= tmp) {
+    e->real_subj = e->subject + tmp;
   }
   else {
     e->real_subj = NULL;
@@ -400,10 +418,22 @@ 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);
+  
+#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);
   restore_list (&e->userhdrs, d, off);
+
+  restore_int ((unsigned int *) &tmp, d, off);
+  e->irt_changed = tmp;
+  restore_int ((unsigned int *) &tmp, d, off);
+  e->refs_changed = tmp;
 }
 
 static
@@ -435,12 +465,6 @@ static int generate_crc32 ()
            mutt_strlen ("HAVE_LANGINFO_CODESET"));
 #endif
 
-#if EXACT_ADDRESS
-  crc =
-    crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
-           mutt_strlen ("EXACT_ADDRESS"));
-#endif
-
 #ifdef USE_POP
   crc =
     crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
@@ -456,6 +480,11 @@ static int generate_crc32 ()
   crc =
     crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
 #endif
+
+#ifdef USE_NNTP
+  crc =
+    crc32 (crc, (unsigned char const *) "USE_NNTP", mutt_strlen ("USE_NNTP"));
+#endif
   return crc;
 }
 
@@ -493,7 +522,7 @@ static const char *mutt_hcache_per_folder (const char *path,
   }
 
   MD5Init (&md5);
-  MD5Update (&md5, (unsigned char *) folder, strlen (folder));
+  MD5Update (&md5, (unsigned char *) folder, mutt_strlen (folder));
   MD5Final (md5sum, &md5);
 
   ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
@@ -514,7 +543,7 @@ static const char *mutt_hcache_per_folder (const char *path,
 /* This function transforms a header into a char so that it is useable by
  * db_store */
 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
-                               uint64_t uid_validity)
+                               unsigned long uid_validity)
 {
   struct header_cache *db = _db;
   unsigned char *d = NULL;
@@ -524,7 +553,7 @@ static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
   d = lazy_malloc (sizeof (validate));
 
   if (uid_validity) {
-    memcpy (d, &uid_validity, sizeof (uint64_t));
+    memcpy (d, &uid_validity, sizeof (unsigned long));
   }
   else {
     struct timeval now;
@@ -579,7 +608,129 @@ HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
   return h;
 }
 
-#if HAVE_GDBM
+#if HAVE_QDBM
+void *
+mutt_hcache_open(const char *path, const char *folder)
+{
+  struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE));
+  int    flags = VL_OWRITER | VL_OCREAT;
+  h->db = NULL;
+  h->folder = safe_strdup(folder);
+  h->crc = generate_crc32();
+
+  if (!path || path[0] == '\0')
+  {
+    FREE(&h->folder);
+    FREE(&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
+  {
+    FREE(&h->folder);
+    FREE(&h);
+
+    return NULL;
+  }
+}
+
+void
+mutt_hcache_close(void *db)
+{
+  struct header_cache *h = db;
+
+  if (!h)
+    return;
+
+  vlclose(h->db);
+  FREE(&h->folder);
+  FREE(&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;
+  char *data = NULL;
+
+  if (!h)
+    return NULL;
+
+  strncpy(path, h->folder, sizeof (path));
+  safe_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))
+  {
+    FREE(&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;
+
+  strncpy(path, h->folder, sizeof (path));
+  safe_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);
+
+  FREE(&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;
+
+  strncpy(path, h->folder, sizeof (path));
+  safe_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)
 {
@@ -650,7 +801,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);
+    FREE(&data.dptr);
     return NULL;
   }
 
@@ -659,7 +810,7 @@ void *mutt_hcache_fetch (void *db, const char *filename,
 
 int
 mutt_hcache_store (void *db, const char *filename, HEADER * header,
-                   uint64_t uid_validity, size_t (*keylen) (const char *fn))
+                   unsigned long uid_validity, size_t (*keylen) (const char *fn))
 {
   struct header_cache *h = db;
   datum key;
@@ -764,7 +915,7 @@ void *mutt_hcache_open (const char *path, const char *folder)
   }
 
   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) {
@@ -781,7 +932,7 @@ 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);
@@ -797,7 +948,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;
@@ -830,7 +980,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);
+    FREE(&data.data);
     return NULL;
   }
 
@@ -839,7 +989,7 @@ void *mutt_hcache_fetch (void *db, const char *filename,
 
 int
 mutt_hcache_store (void *db, const char *filename, HEADER * header,
-                   uint64_t uid_validity, size_t (*keylen) (const char *fn))
+                   unsigned long uid_validity, size_t (*keylen) (const char *fn))
 {
   DBT key;
   DBT data;