+int
+mutt_hcache_delete (void *db, const char *filename,
+ ssize_t (*keylen) (const char *fn))
+{
+ datum key;
+ struct header_cache *h = db;
+ char path[_POSIX_PATH_MAX];
+
+ if (!h) {
+ return -1;
+ }
+
+ m_strcpy(path, sizeof(path), h->folder);
+ strncat (path, filename, sizeof (path) - m_strlen(path));
+
+ key.dptr = path;
+ key.dsize = keylen (path);
+
+ return gdbm_delete (h->db, key);
+}
+#elif defined(HAVE_DB4)
+
+static void mutt_hcache_dbt_init (DBT * dbt, void *data, ssize_t len)
+{
+ dbt->data = data;
+ dbt->size = dbt->ulen = len;
+ dbt->dlen = dbt->doff = 0;
+ dbt->flags = DB_DBT_USERMEM;
+}
+
+static void mutt_hcache_dbt_empty_init (DBT * dbt)
+{
+ dbt->data = NULL;
+ dbt->size = dbt->ulen = dbt->dlen = dbt->doff = 0;
+ dbt->flags = 0;
+}
+
+void *mutt_hcache_open (const char *path, const char *folder)
+{
+ struct stat sb;
+ u_int32_t createflags = DB_CREATE;
+ int ret;
+ struct header_cache *h = p_new(struct header_cache, 1);
+ int pagesize = atoi (HeaderCachePageSize);
+
+
+ h->crc = generate_crc32 ();
+
+ if (!path || path[0] == '\0') {
+ p_delete(&h);
+ return NULL;
+ }
+
+ path = mutt_hcache_per_folder (path, folder);
+
+ snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);
+
+ h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+ if (h->fd < 0) {
+ p_delete(&h);
+ return NULL;
+ }
+
+ if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) {
+ close (h->fd);
+ p_delete(&h);
+ return NULL;
+ }
+
+ ret = db_env_create (&h->env, 0);
+ if (ret) {
+ mx_unlock_file (h->lockfile, h->fd, 0);
+ close (h->fd);
+ p_delete(&h);
+ return NULL;
+ }
+
+ ret =
+ (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);
+ p_delete(&h);
+ return NULL;
+ }
+ }
+
+ if (stat (path, &sb) != 0 && errno == ENOENT) {
+ createflags |= DB_EXCL;
+ h->db->set_pagesize (h->db, pagesize);
+ }
+
+ 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);
+ p_delete(&h);
+ return NULL;
+ }
+
+ return h;
+}
+
+void mutt_hcache_close (void *db)
+{
+ struct header_cache *h = db;
+
+ if (!h) {
+ return;
+ }
+
+ h->db->close (h->db, 0);
+ h->env->close (h->env, 0);
+ mx_unlock_file (h->lockfile, h->fd, 0);
+ close (h->fd);
+ p_delete(&h);
+}
+
+void *mutt_hcache_fetch (void *db, const char *filename,
+ ssize_t (*keylen) (const char *fn))