update gettext
[apps/madmutt.git] / hcache.c
index cef8fc1..fd706db 100644 (file)
--- a/hcache.c
+++ b/hcache.c
@@ -1,22 +1,20 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
  * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
  * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * 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 <inttypes.h>
 #  endif
 # endif
 
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif /* HAVE_CONFIG_H */
-
-#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>
@@ -43,6 +39,9 @@
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
+
+#include <lib-lib/mem.h>
+
 #include "mutt.h"
 #ifdef USE_IMAP
 #include "message.h"
 #include "lib.h"
 #include "md5.h"
 
-#if HAVE_GDBM
+#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;
@@ -72,7 +80,7 @@ static struct
 
 typedef union {
   struct timeval timeval;
-  uint64_t uid_validity;
+  unsigned long uid_validity;
 } validate;
 
 static void *lazy_malloc (size_t siz)
@@ -81,7 +89,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)
@@ -92,7 +100,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)
@@ -120,7 +128,7 @@ static unsigned char *dump_char (char *c, unsigned char *d, int *off)
     return d;
   }
 
-  size = mutt_strlen (c) + 1;
+  size = str_len (c) + 1;
   d = dump_int (size, d, off);
   lazy_realloc (&d, *off + size);
   memcpy (d + *off, c, size);
@@ -159,8 +167,7 @@ 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;
 }
 
@@ -172,9 +179,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);
@@ -194,10 +198,7 @@ static void restore_address (ADDRESS ** a, const unsigned char *d, int *off)
   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, 1);
     restore_char (&(*a)->personal, d, off);
     restore_char (&(*a)->mailbox, d, off);
     restore_int ((unsigned int *) &(*a)->group, d, off);
@@ -233,7 +234,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--;
@@ -271,7 +272,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);
@@ -309,7 +310,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;
@@ -376,6 +377,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);
@@ -409,6 +418,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);
@@ -433,37 +450,36 @@ static int generate_crc32 ()
   int crc = 0;
 
   crc = crc32 (crc, (unsigned char const *)
-               "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
-               mutt_strlen
-               ("sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
+               MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
+               str_len
+               (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
 
 #if HAVE_LANGINFO_CODESET
-  crc = crc32 (crc, (unsigned char const *) Charset, mutt_strlen (Charset));
+  crc = crc32 (crc, (unsigned char const *) Charset, str_len (Charset));
   crc =
     crc32 (crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
-           mutt_strlen ("HAVE_LANGINFO_CODESET"));
-#endif
-
-#if EXACT_ADDRESS
-  crc =
-    crc32 (crc, (unsigned char const *) "EXACT_ADDRESS",
-           mutt_strlen ("EXACT_ADDRESS"));
+           str_len ("HAVE_LANGINFO_CODESET"));
 #endif
 
 #ifdef USE_POP
   crc =
-    crc32 (crc, (unsigned char const *) "USE_POP", mutt_strlen ("USE_POP"));
+    crc32 (crc, (unsigned char const *) "USE_POP", str_len ("USE_POP"));
 #endif
 
 #ifdef MIXMASTER
   crc =
     crc32 (crc, (unsigned char const *) "MIXMASTER",
-           mutt_strlen ("MIXMASTER"));
+           str_len ("MIXMASTER"));
 #endif
 
 #ifdef USE_IMAP
   crc =
-    crc32 (crc, (unsigned char const *) "USE_IMAP", mutt_strlen ("USE_IMAP"));
+    crc32 (crc, (unsigned char const *) "USE_IMAP", str_len ("USE_IMAP"));
+#endif
+
+#ifdef USE_NNTP
+  crc =
+    crc32 (crc, (unsigned char const *) "USE_NNTP", str_len ("USE_NNTP"));
 #endif
   return crc;
 }
@@ -502,7 +518,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, str_len (folder));
   MD5Final (md5sum, &md5);
 
   ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX,
@@ -523,7 +539,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;
@@ -533,7 +549,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;
@@ -581,27 +597,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 = str_dup ((*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 = str_dup(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;
+  char *data = NULL;
+
+  if (!h)
+    return NULL;
+
+  strncpy(path, h->folder, sizeof (path));
+  str_cat(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;
+
+  strncpy(path, h->folder, sizeof (path));
+  str_cat(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;
+
+  strncpy(path, h->folder, sizeof (path));
+  str_cat(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 = str_dup (folder);
   h->crc = generate_crc32 ();
 
   if (!path || path[0] == '\0') {
-    FREE (&h->folder);
-    FREE (&h);
+    p_delete(&h->folder);
+    p_delete(&h);
     return NULL;
   }
 
@@ -618,8 +756,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;
   }
@@ -634,8 +772,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,
@@ -651,7 +789,7 @@ void *mutt_hcache_fetch (void *db, const char *filename,
   }
 
   strncpy (path, h->folder, sizeof (path));
-  strncat (path, filename, sizeof (path) - mutt_strlen (path));
+  strncat (path, filename, sizeof (path) - str_len (path));
 
   key.dptr = path;
   key.dsize = keylen (path);
@@ -659,7 +797,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;
   }
 
@@ -668,7 +806,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;
@@ -681,7 +819,7 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header,
   }
 
   strncpy (path, h->folder, sizeof (path));
-  strncat (path, filename, sizeof (path) - mutt_strlen (path));
+  strncat (path, filename, sizeof (path) - str_len (path));
 
   key.dptr = path;
   key.dsize = keylen (path);
@@ -690,7 +828,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;
 }
@@ -708,7 +846,7 @@ mutt_hcache_delete (void *db, const char *filename,
   }
 
   strncpy (path, h->folder, sizeof (path));
-  strncat (path, filename, sizeof (path) - mutt_strlen (path));
+  strncat (path, filename, sizeof (path) - str_len (path));
 
   key.dptr = path;
   key.dsize = keylen (path);
@@ -744,7 +882,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;
   }
 
@@ -754,13 +892,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;
   }
 
@@ -768,19 +906,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;
     }
   }
@@ -790,13 +928,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;
   }
 
@@ -806,7 +944,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;
@@ -816,7 +953,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,
@@ -839,7 +976,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;
   }
 
@@ -848,7 +985,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;
@@ -871,7 +1008,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;
 }