Apache HTTPD
apr_dbm_gdbm.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apu_config.h"
18#include "apu.h"
19#include "apr_strings.h"
20
21#if APR_HAVE_STDLIB_H
22#include <stdlib.h> /* for free() */
23#endif
24
25#if APU_HAVE_GDBM
26#include "apr_dbm_private.h"
27
28#include <gdbm.h>
29
30#define APR_DBM_DBMODE_RO GDBM_READER
31#define APR_DBM_DBMODE_RW GDBM_WRITER
32#define APR_DBM_DBMODE_RWCREATE GDBM_WRCREAT
33#define APR_DBM_DBMODE_RWTRUNC GDBM_NEWDB
34
35/* map a GDBM error to an apr_status_t */
36static apr_status_t g2s(int gerr)
37{
38 if (gerr == -1) {
40 return APR_SUCCESS;
42 }
43
44 return APR_SUCCESS;
45}
46
48{
49 if (d.dptr == NULL) {
51 return APR_SUCCESS;
53 }
54
55 return APR_SUCCESS;
56}
57
58static apr_status_t datum_cleanup(void *dptr)
59{
60 if (dptr)
61 free(dptr);
62
63 return APR_SUCCESS;
64}
65
67{
68 dbm->errcode = dbm_said;
69
70 if (dbm_said == APR_SUCCESS)
71 dbm->errmsg = NULL;
72 else
74
75 return dbm_said;
76}
77
78/* --------------------------------------------------------------------------
79**
80** DEFINE THE VTABLE FUNCTIONS FOR GDBM
81*/
82
83static apr_status_t vt_gdbm_open(apr_dbm_t **pdb, const char *pathname,
86{
88 int dbmode;
89
90 *pdb = NULL;
91
92 switch (mode) {
95 break;
98 break;
101 break;
102 case APR_DBM_RWTRUNC:
104 break;
105 default:
106 return APR_EINVAL;
107 }
108
109 /* Note: stupid cast to get rid of "const" on the pathname */
111 NULL);
112
113 if (file == NULL)
115
116 /* we have an open database... return it */
117 *pdb = apr_pcalloc(pool, sizeof(**pdb));
118 (*pdb)->pool = pool;
119 (*pdb)->type = &apr_dbm_type_gdbm;
120 (*pdb)->file = file;
121
122 /* ### register a cleanup to close the DBM? */
123
124 return APR_SUCCESS;
125}
126
127static void vt_gdbm_close(apr_dbm_t *dbm)
128{
129 gdbm_close(dbm->file);
130}
131
134{
135 datum kd, rd;
136
137 kd.dptr = key.dptr;
138 kd.dsize = key.dsize;
139
140 rd = gdbm_fetch(dbm->file, kd);
141
142 pvalue->dptr = rd.dptr;
143 pvalue->dsize = rd.dsize;
144
145 if (pvalue->dptr)
148 else
149 pvalue->dsize = 0;
150
151 /* store the error info into DBM, and return a status code. Also, note
152 that *pvalue should have been cleared on error. */
153 return set_error(dbm, gdat2s(rd));
154}
155
158{
159 int rc;
160 datum kd, vd;
161
162 kd.dptr = key.dptr;
163 kd.dsize = key.dsize;
164
165 vd.dptr = value.dptr;
166 vd.dsize = value.dsize;
167
168 rc = gdbm_store(dbm->file, kd, vd, GDBM_REPLACE);
169
170 /* store any error info into DBM, and return a status code. */
171 return set_error(dbm, g2s(rc));
172}
173
175{
176 int rc;
177 datum kd;
178
179 kd.dptr = key.dptr;
180 kd.dsize = key.dsize;
181
182 rc = gdbm_delete(dbm->file, kd);
183
184 /* store any error info into DBM, and return a status code. */
185 return set_error(dbm, g2s(rc));
186}
187
189{
190 datum kd;
191
192 kd.dptr = key.dptr;
193 kd.dsize = key.dsize;
194
195 return gdbm_exists(dbm->file, kd) != 0;
196}
197
199{
200 datum rd;
201
202 rd = gdbm_firstkey(dbm->file);
203
204 pkey->dptr = rd.dptr;
205 pkey->dsize = rd.dsize;
206
207 if (pkey->dptr)
210 else
211 pkey->dsize = 0;
212
213 /* store any error info into DBM, and return a status code. */
214 return set_error(dbm, gdat2s(rd));
215}
216
218{
219 datum kd, rd;
220
221 kd.dptr = pkey->dptr;
222 kd.dsize = pkey->dsize;
223
224 rd = gdbm_nextkey(dbm->file, kd);
225
226 pkey->dptr = rd.dptr;
227 pkey->dsize = rd.dsize;
228
229 if (pkey->dptr)
232 else
233 pkey->dsize = 0;
234
235 /* store any error info into DBM, and return a status code. */
236 return set_error(dbm, gdat2s(rd));
237}
238
240{
241 (void) apr_pool_cleanup_run(dbm->pool, data.dptr, datum_cleanup);
242}
243
244static void vt_gdbm_usednames(apr_pool_t *pool, const char *pathname,
245 const char **used1, const char **used2)
246{
248 *used2 = NULL;
249}
250
252 "gdbm",
263};
264
265#endif /* APU_HAVE_GDBM */
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_gdbm
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
#define APR_EINVAL
Definition apr_errno.h:711
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
apr_datum_t * pkey
Definition apr_dbm.h:158
apr_datum_t apr_datum_t * pvalue
Definition apr_dbm.h:128
#define APR_DBM_RWTRUNC
Definition apr_dbm.h:59
#define APR_DBM_RWCREATE
Definition apr_dbm.h:58
#define APR_DBM_READONLY
Definition apr_dbm.h:56
const char const char const char const char ** used2
Definition apr_dbm.h:203
const char const char const char ** used1
Definition apr_dbm.h:202
#define APR_DBM_READWRITE
Definition apr_dbm.h:57
const char const char * pathname
Definition apr_dbm.h:201
apr_redis_t * rc
Definition apr_redis.h:173
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
const char * value
Definition apr_env.h:51
#define APR_OS_START_USEERR
Definition apr_errno.h:169
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_int32_t apr_fileperms_t
const char * key
void * data
const char apr_file_t * file
const char apr_int32_t apr_fileperms_t perm
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
return NULL
Definition mod_so.c:359
apr_size_t dsize
Definition apr_dbm.h:52
char * dptr
Definition apr_dbm.h:50