NeoMutt
2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Toggle main menu visibility
Loading...
Searching...
No Matches
gdbm.c
Go to the documentation of this file.
1
23
30
31
#include "config.h"
32
#include <gdbm.h>
33
#include <limits.h>
34
#include <stdbool.h>
35
#include <stddef.h>
36
#include "
mutt/lib.h
"
37
#include "
lib.h
"
38
42
static
StoreHandle
*
store_gdbm_open
(
const
char
*path,
bool
create)
43
{
44
if
(!path)
45
return
NULL;
46
47
const
int
pagesize = 4096;
48
49
GDBM_FILE db = gdbm_open((
char
*) path, pagesize,
50
create ? GDBM_WRCREAT : GDBM_WRITER, 00600, NULL);
51
if
(!db)
52
{
53
/* if rw failed try ro */
54
db = gdbm_open((
char
*) path, pagesize, GDBM_READER, 00600, NULL);
55
}
56
57
// Return an opaque pointer
58
return
(
StoreHandle
*) db;
59
}
60
64
static
void
*
store_gdbm_fetch
(
StoreHandle
*store,
const
char
*key,
size_t
klen,
size_t
*vlen)
65
{
66
if
(!store || (klen > INT_MAX))
67
return
NULL;
68
69
datum dkey = { 0 };
70
datum data = { 0 };
71
72
// Decloak an opaque pointer
73
GDBM_FILE db = store;
74
75
dkey.dptr = (
char
*) key;
76
dkey.dsize = klen;
77
data = gdbm_fetch(db, dkey);
78
79
*vlen = data.dsize;
80
return
data.dptr;
81
}
82
86
static
void
store_gdbm_free
(
StoreHandle
*store,
void
**ptr)
87
{
88
FREE
(ptr);
89
}
90
94
static
int
store_gdbm_store
(
StoreHandle
*store,
const
char
*key,
size_t
klen,
95
void
*value,
size_t
vlen)
96
{
97
if
(!store || (klen > INT_MAX) || (vlen > INT_MAX))
98
return
-1;
99
100
datum dkey = { 0 };
101
datum databuf = { 0 };
102
103
// Decloak an opaque pointer
104
GDBM_FILE db = store;
105
106
dkey.dptr = (
char
*) key;
107
dkey.dsize = klen;
108
109
databuf.dsize = vlen;
110
databuf.dptr = value;
111
112
return
gdbm_store(db, dkey, databuf, GDBM_REPLACE);
113
}
114
118
static
int
store_gdbm_delete_record
(
StoreHandle
*store,
const
char
*key,
size_t
klen)
119
{
120
if
(!store || (klen > INT_MAX))
121
return
-1;
122
123
datum dkey = { 0 };
124
125
// Decloak an opaque pointer
126
GDBM_FILE db = store;
127
128
dkey.dptr = (
char
*) key;
129
dkey.dsize = klen;
130
131
return
gdbm_delete(db, dkey);
132
}
133
137
static
void
store_gdbm_close
(
StoreHandle
**ptr)
138
{
139
if
(!ptr || !*ptr)
140
return
;
141
142
// Decloak an opaque pointer
143
GDBM_FILE db = *ptr;
144
gdbm_close(db);
145
*ptr = NULL;
146
}
147
151
static
const
char
*
store_gdbm_version
(
void
)
152
{
153
return
gdbm_version;
154
}
155
156
STORE_BACKEND_OPS
(gdbm)
store_gdbm_close
static void store_gdbm_close(StoreHandle **ptr)
Close a Store connection - Implements StoreOps::close() -.
Definition
gdbm.c:137
store_gdbm_delete_record
static int store_gdbm_delete_record(StoreHandle *store, const char *key, size_t klen)
Delete a record from the Store - Implements StoreOps::delete_record() -.
Definition
gdbm.c:118
store_gdbm_fetch
static void * store_gdbm_fetch(StoreHandle *store, const char *key, size_t klen, size_t *vlen)
Fetch a Value from the Store - Implements StoreOps::fetch() -.
Definition
gdbm.c:64
store_gdbm_free
static void store_gdbm_free(StoreHandle *store, void **ptr)
Free a Value returned by fetch() - Implements StoreOps::free() -.
Definition
gdbm.c:86
store_gdbm_open
static StoreHandle * store_gdbm_open(const char *path, bool create)
Open a connection to a Store - Implements StoreOps::open() -.
Definition
gdbm.c:42
store_gdbm_store
static int store_gdbm_store(StoreHandle *store, const char *key, size_t klen, void *value, size_t vlen)
Write a Value to the Store - Implements StoreOps::store() -.
Definition
gdbm.c:94
store_gdbm_version
static const char * store_gdbm_version(void)
Get a Store version string - Implements StoreOps::version() -.
Definition
gdbm.c:151
FREE
#define FREE(x)
Free memory and set the pointer to NULL.
Definition
memory.h:68
lib.h
Convenience wrapper for the library headers.
lib.h
Key value store.
StoreHandle
void StoreHandle
Opaque type for store backend.
Definition
lib.h:58
STORE_BACKEND_OPS
#define STORE_BACKEND_OPS(_name)
Definition
lib.h:160