NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
gdbm.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <stddef.h>
35 #include <gdbm.h>
36 #include "mutt/lib.h"
37 #include "lib.h"
38 
42 static void *store_gdbm_open(const char *path)
43 {
44  if (!path)
45  return NULL;
46 
47  const int pagesize = 4096;
48 
49  GDBM_FILE db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
50  if (db)
51  return db;
52 
53  /* if rw failed try ro */
54  return gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
55 }
56 
60 static void *store_gdbm_fetch(void *store, const char *key, size_t klen, size_t *vlen)
61 {
62  if (!store)
63  return NULL;
64 
65  datum dkey;
66  datum data;
67 
68  GDBM_FILE db = store;
69 
70  dkey.dptr = (char *) key;
71  dkey.dsize = klen;
72  data = gdbm_fetch(db, dkey);
73 
74  *vlen = data.dsize;
75  return data.dptr;
76 }
77 
81 static void store_gdbm_free(void *store, void **ptr)
82 {
83  FREE(ptr);
84 }
85 
89 static int store_gdbm_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
90 {
91  if (!store)
92  return -1;
93 
94  datum dkey;
95  datum databuf;
96 
97  GDBM_FILE db = store;
98 
99  dkey.dptr = (char *) key;
100  dkey.dsize = klen;
101 
102  databuf.dsize = vlen;
103  databuf.dptr = value;
104 
105  return gdbm_store(db, dkey, databuf, GDBM_REPLACE);
106 }
107 
111 static int store_gdbm_delete_record(void *store, const char *key, size_t klen)
112 {
113  if (!store)
114  return -1;
115 
116  datum dkey;
117 
118  GDBM_FILE db = store;
119 
120  dkey.dptr = (char *) key;
121  dkey.dsize = klen;
122 
123  return gdbm_delete(db, dkey);
124 }
125 
129 static void store_gdbm_close(void **ptr)
130 {
131  if (!ptr || !*ptr)
132  return;
133 
134  GDBM_FILE db = *ptr;
135  gdbm_close(db);
136  *ptr = NULL;
137 }
138 
142 static const char *store_gdbm_version(void)
143 {
144  return gdbm_version;
145 }
146 
147 STORE_BACKEND_OPS(gdbm)
#define STORE_BACKEND_OPS(_name)
Definition: lib.h:157
DB * db
Definition: bdb.c:51
static int store_gdbm_delete_record(void *store, const char *key, size_t klen)
Implements StoreOps::delete_record() -.
Definition: gdbm.c:111
static void * store_gdbm_fetch(void *store, const char *key, size_t klen, size_t *vlen)
Implements StoreOps::fetch() -.
Definition: gdbm.c:60
static int store_gdbm_store(void *store, const char *key, size_t klen, void *value, size_t vlen)
Implements StoreOps::store() -.
Definition: gdbm.c:89
static void store_gdbm_close(void **ptr)
Implements StoreOps::close() -.
Definition: gdbm.c:129
static void * store_gdbm_open(const char *path)
Implements StoreOps::open() -.
Definition: gdbm.c:42
#define FREE(x)
Definition: memory.h:40
static const char * store_gdbm_version(void)
Implements StoreOps::version() -.
Definition: gdbm.c:142
Convenience wrapper for the library headers.
static void store_gdbm_free(void *store, void **ptr)
Implements StoreOps::free() -.
Definition: gdbm.c:81