Make sure macro int_errorcode is last declaration
[apr-util.git] / dbm / apr_dbm_berkeleydb.c
blob06277421a29839989e96e71618e4a5d2d74301c8
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_strings.h"
18 #define APR_WANT_MEMFUNC
19 #include "apr_want.h"
21 #define APU_WANT_DB
22 #include "apu_want.h"
24 #if APR_HAVE_STDLIB_H
25 #include <stdlib.h> /* for abort() */
26 #endif
28 #include "apu.h"
30 #if APU_HAVE_DB
31 #include "apr_dbm_private.h"
34 * We pick up all varieties of Berkeley DB through db.h (included through
35 * apu_select_dbm.h). This code has been compiled/tested against DB1,
36 * DB_185, DB2, DB3, and DB4.
39 #if defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR == 4)
40 /* We will treat anything greater than 4.1 as DB4.
41 * We can treat 4.0 as DB3.
43 #if defined(DB_VERSION_MINOR) && (DB_VERSION_MINOR >= 1)
44 #define DB_VER 4
45 #else
46 #define DB_VER 3
47 #endif
48 #elif defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR == 3)
49 #define DB_VER 3
50 #elif defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR == 2)
51 #define DB_VER 2
52 #else
53 #define DB_VER 1
54 #endif
56 typedef struct {
57 DB *bdb;
58 #if DB_VER != 1
59 DBC *curs;
60 #endif
61 } real_file_t;
64 #if DB_VER == 1
65 #define TXN_ARG
66 #else
67 #define TXN_ARG NULL,
68 #endif
70 #define GET_BDB(f) (((real_file_t *)(f))->bdb)
72 #define do_fetch(bdb, k, v) ((*(bdb)->get)(bdb, TXN_ARG &(k), &(v), 0))
74 #if DB_VER == 1
75 #include <sys/fcntl.h>
76 #define APR_DBM_DBMODE_RO O_RDONLY
77 #define APR_DBM_DBMODE_RW O_RDWR
78 #define APR_DBM_DBMODE_RWCREATE (O_CREAT | O_RDWR)
79 #define APR_DBM_DBMODE_RWTRUNC (O_CREAT | O_RDWR | O_TRUNC)
80 #else
81 #define APR_DBM_DBMODE_RO DB_RDONLY
82 #define APR_DBM_DBMODE_RW 0
83 #define APR_DBM_DBMODE_RWCREATE DB_CREATE
84 #define APR_DBM_DBMODE_RWTRUNC DB_TRUNCATE
85 #endif /* DBVER == 1 */
87 /* --------------------------------------------------------------------------
89 ** UTILITY FUNCTIONS
92 /* map a DB error to an apr_status_t */
93 static apr_status_t db2s(int dberr)
95 if (dberr != 0) {
96 /* ### need to fix this */
97 return APR_OS_START_USEERR + dberr;
100 return APR_SUCCESS;
104 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
106 apr_status_t rv = APR_SUCCESS;
108 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
110 if (dbm_said == APR_SUCCESS) {
111 dbm->errcode = 0;
112 dbm->errmsg = NULL;
114 else {
115 /* ### need to fix. dberr was tossed in db2s(). */
116 /* ### use db_strerror() */
117 dbm->errcode = dbm_said;
118 #if DB_VER == 1 || DB_VER == 2
119 dbm->errmsg = NULL;
120 #else
121 dbm->errmsg = db_strerror(dbm_said - APR_OS_START_USEERR);
122 #endif
123 rv = dbm_said;
126 return rv;
129 /* --------------------------------------------------------------------------
131 ** DEFINE THE VTABLE FUNCTIONS FOR BERKELEY DB
133 ** ### we may need three sets of these: db1, db2, db3
136 static apr_status_t vt_db_open(apr_dbm_t **pdb, const char *pathname,
137 apr_int32_t mode, apr_fileperms_t perm,
138 apr_pool_t *pool)
140 real_file_t file;
141 int dbmode;
143 *pdb = NULL;
145 switch (mode) {
146 case APR_DBM_READONLY:
147 dbmode = APR_DBM_DBMODE_RO;
148 break;
149 case APR_DBM_READWRITE:
150 dbmode = APR_DBM_DBMODE_RW;
151 break;
152 case APR_DBM_RWCREATE:
153 dbmode = APR_DBM_DBMODE_RWCREATE;
154 break;
155 case APR_DBM_RWTRUNC:
156 dbmode = APR_DBM_DBMODE_RWTRUNC;
157 break;
158 default:
159 return APR_EINVAL;
163 int dberr;
165 #if DB_VER >= 3
166 if ((dberr = db_create(&file.bdb, NULL, 0)) == 0) {
167 if ((dberr = (*file.bdb->open)(file.bdb,
168 #if DB_VER == 4
169 NULL,
170 #endif
171 pathname, NULL,
172 DB_HASH, dbmode,
173 apr_posix_perms2mode(perm))) != 0) {
174 /* close the DB handler */
175 (void) (*file.bdb->close)(file.bdb, 0);
178 file.curs = NULL;
179 #elif DB_VER == 2
180 dberr = db_open(pathname, DB_HASH, dbmode, apr_posix_perms2mode(perm),
181 NULL, NULL, &file.bdb);
182 file.curs = NULL;
183 #else
184 file.bdb = dbopen(pathname, dbmode, apr_posix_perms2mode(perm),
185 DB_HASH, NULL);
186 if (file.bdb == NULL)
187 return APR_EGENERAL; /* ### need a better error */
188 dberr = 0;
189 #endif
190 if (dberr != 0)
191 return db2s(dberr);
194 /* we have an open database... return it */
195 *pdb = apr_pcalloc(pool, sizeof(**pdb));
196 (*pdb)->pool = pool;
197 (*pdb)->type = &apr_dbm_type_db;
198 (*pdb)->file = apr_pmemdup(pool, &file, sizeof(file));
200 /* ### register a cleanup to close the DBM? */
202 return APR_SUCCESS;
205 static void vt_db_close(apr_dbm_t *dbm)
207 (*GET_BDB(dbm->file)->close)(GET_BDB(dbm->file)
208 #if DB_VER != 1
210 #endif
214 static apr_status_t vt_db_fetch(apr_dbm_t *dbm, apr_datum_t key,
215 apr_datum_t * pvalue)
217 DBT ckey = { 0 };
218 DBT rd = { 0 };
219 int dberr;
221 ckey.data = key.dptr;
222 ckey.size = key.dsize;
224 dberr = do_fetch(GET_BDB(dbm->file), ckey, rd);
226 /* "not found" is not an error. return zero'd value. */
227 if (dberr ==
228 #if DB_VER == 1
229 RET_SPECIAL
230 #else
231 DB_NOTFOUND
232 #endif
234 memset(&rd, 0, sizeof(rd));
235 dberr = 0;
238 pvalue->dptr = rd.data;
239 pvalue->dsize = rd.size;
241 /* store the error info into DBM, and return a status code. Also, note
242 that *pvalue should have been cleared on error. */
243 return set_error(dbm, db2s(dberr));
246 static apr_status_t vt_db_store(apr_dbm_t *dbm, apr_datum_t key,
247 apr_datum_t value)
249 apr_status_t rv;
250 DBT ckey = { 0 };
251 DBT cvalue = { 0 };
253 ckey.data = key.dptr;
254 ckey.size = key.dsize;
256 cvalue.data = value.dptr;
257 cvalue.size = value.dsize;
259 rv = db2s((*GET_BDB(dbm->file)->put)(GET_BDB(dbm->file),
260 TXN_ARG
261 &ckey,
262 &cvalue,
263 0));
265 /* store any error info into DBM, and return a status code. */
266 return set_error(dbm, rv);
269 static apr_status_t vt_db_del(apr_dbm_t *dbm, apr_datum_t key)
271 apr_status_t rv;
272 DBT ckey = { 0 };
274 ckey.data = key.dptr;
275 ckey.size = key.dsize;
277 rv = db2s((*GET_BDB(dbm->file)->del)(GET_BDB(dbm->file),
278 TXN_ARG
279 &ckey,
280 0));
282 /* store any error info into DBM, and return a status code. */
283 return set_error(dbm, rv);
286 static int vt_db_exists(apr_dbm_t *dbm, apr_datum_t key)
288 DBT ckey = { 0 }; /* converted key */
289 DBT data = { 0 };
290 int dberr;
292 ckey.data = key.dptr;
293 ckey.size = key.dsize;
295 dberr = do_fetch(GET_BDB(dbm->file), ckey, data);
297 /* note: the result data is "loaned" to us; we don't need to free it */
299 /* DB returns DB_NOTFOUND if it doesn't exist. but we want to say
300 that *any* error means it doesn't exist. */
301 return dberr == 0;
304 static apr_status_t vt_db_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
306 real_file_t *f = dbm->file;
307 DBT first = { 0 };
308 DBT data = { 0 };
309 int dberr;
311 #if DB_VER == 1
312 dberr = (*f->bdb->seq)(f->bdb, &first, &data, R_FIRST);
313 #else
314 if ((dberr = (*f->bdb->cursor)(f->bdb, NULL, &f->curs
315 #if DB_VER >= 3 || ((DB_VERSION_MAJOR == 2) && (DB_VERSION_MINOR > 5))
317 #endif
318 )) == 0) {
319 dberr = (*f->curs->c_get)(f->curs, &first, &data, DB_FIRST);
320 if (dberr == DB_NOTFOUND) {
321 memset(&first, 0, sizeof(first));
322 (*f->curs->c_close)(f->curs);
323 f->curs = NULL;
324 dberr = 0;
327 #endif
329 pkey->dptr = first.data;
330 pkey->dsize = first.size;
332 /* store any error info into DBM, and return a status code. */
333 return set_error(dbm, db2s(dberr));
336 static apr_status_t vt_db_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
338 real_file_t *f = dbm->file;
339 DBT ckey = { 0 };
340 DBT data = { 0 };
341 int dberr;
343 ckey.data = pkey->dptr;
344 ckey.size = pkey->dsize;
346 #if DB_VER == 1
347 dberr = (*f->bdb->seq)(f->bdb, &ckey, &data, R_NEXT);
348 if (dberr == RET_SPECIAL) {
349 dberr = 0;
350 ckey.data = NULL;
351 ckey.size = 0;
353 #else
354 if (f->curs == NULL)
355 return APR_EINVAL;
357 dberr = (*f->curs->c_get)(f->curs, &ckey, &data, DB_NEXT);
358 if (dberr == DB_NOTFOUND) {
359 (*f->curs->c_close)(f->curs);
360 f->curs = NULL;
361 dberr = 0;
362 ckey.data = NULL;
363 ckey.size = 0;
365 #endif
367 pkey->dptr = ckey.data;
368 pkey->dsize = ckey.size;
370 /* store any error info into DBM, and return a status code. */
371 /* ### or use db2s(dberr) instead of APR_SUCCESS? */
372 return set_error(dbm, APR_SUCCESS);
375 static void vt_db_freedatum(apr_dbm_t *dbm, apr_datum_t data)
377 /* nothing to do */
380 static void vt_db_usednames(apr_pool_t *pool, const char *pathname,
381 const char **used1, const char **used2)
383 *used1 = apr_pstrdup(pool, pathname);
384 *used2 = NULL;
388 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_db = {
389 "db",
391 vt_db_open,
392 vt_db_close,
393 vt_db_fetch,
394 vt_db_store,
395 vt_db_del,
396 vt_db_exists,
397 vt_db_firstkey,
398 vt_db_nextkey,
399 vt_db_freedatum,
400 vt_db_usednames
403 #endif /* APU_HAVE_DB */