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
25 #include <stdlib.h> /* for abort() */
28 #include "apu_config.h"
32 #include "apr_dbm_private.h"
35 * We pick up all varieties of Berkeley DB through db.h (included through
36 * apu_select_dbm.h). This code has been compiled/tested against DB1,
37 * DB_185, DB2, DB3, and DB4.
40 #if defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR >= 4)
41 /* We will treat anything greater than 4.1 as DB4.
42 * We can treat 4.0 as DB3.
44 #if DB_VERSION_MAJOR > 4 || (defined(DB_VERSION_MINOR) && (DB_VERSION_MINOR >= 1))
46 #elif DB_VERSION_MAJOR == 4
49 #elif defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR == 3)
51 #elif defined(DB_VERSION_MAJOR) && (DB_VERSION_MAJOR == 2)
71 #define GET_BDB(f) (((real_file_t *)(f))->bdb)
73 #define do_fetch(bdb, k, v) ((*(bdb)->get)(bdb, TXN_ARG &(k), &(v), 0))
76 #include <sys/fcntl.h>
77 #define APR_DBM_DBMODE_RO O_RDONLY
78 #define APR_DBM_DBMODE_RW O_RDWR
79 #define APR_DBM_DBMODE_RWCREATE (O_CREAT | O_RDWR)
80 #define APR_DBM_DBMODE_RWTRUNC (O_CREAT | O_RDWR | O_TRUNC)
82 #define APR_DBM_DBMODE_RO DB_RDONLY
83 #define APR_DBM_DBMODE_RW 0
84 #define APR_DBM_DBMODE_RWCREATE DB_CREATE
85 #define APR_DBM_DBMODE_RWTRUNC DB_TRUNCATE
86 #endif /* DBVER == 1 */
88 /* --------------------------------------------------------------------------
93 /* map a DB error to an apr_status_t */
94 static apr_status_t db2s(int dberr)
97 /* ### need to fix this */
98 return APR_OS_START_USEERR + dberr;
105 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
107 apr_status_t rv = APR_SUCCESS;
109 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
111 if (dbm_said == APR_SUCCESS) {
116 /* ### need to fix. dberr was tossed in db2s(). */
117 /* ### use db_strerror() */
118 dbm->errcode = dbm_said;
119 #if DB_VER == 1 || DB_VER == 2
122 dbm->errmsg = db_strerror(dbm_said - APR_OS_START_USEERR);
130 /* --------------------------------------------------------------------------
132 ** DEFINE THE VTABLE FUNCTIONS FOR BERKELEY DB
134 ** ### we may need three sets of these: db1, db2, db3
137 static apr_status_t vt_db_open(apr_dbm_t **pdb, const char *pathname,
138 apr_int32_t mode, apr_fileperms_t perm,
147 case APR_DBM_READONLY:
148 dbmode = APR_DBM_DBMODE_RO;
150 case APR_DBM_READWRITE:
151 dbmode = APR_DBM_DBMODE_RW;
153 case APR_DBM_RWCREATE:
154 dbmode = APR_DBM_DBMODE_RWCREATE;
156 case APR_DBM_RWTRUNC:
157 dbmode = APR_DBM_DBMODE_RWTRUNC;
167 if ((dberr = db_create(&file.bdb, NULL, 0)) == 0) {
168 if ((dberr = (*file.bdb->open)(file.bdb,
174 apr_posix_perms2mode(perm))) != 0) {
175 /* close the DB handler */
176 (void) (*file.bdb->close)(file.bdb, 0);
181 dberr = db_open(pathname, DB_HASH, dbmode, apr_posix_perms2mode(perm),
182 NULL, NULL, &file.bdb);
185 file.bdb = dbopen(pathname, dbmode, apr_posix_perms2mode(perm),
187 if (file.bdb == NULL)
188 return APR_EGENERAL; /* ### need a better error */
195 /* we have an open database... return it */
196 *pdb = apr_pcalloc(pool, sizeof(**pdb));
198 (*pdb)->type = &apr_dbm_type_db;
199 (*pdb)->file = apr_pmemdup(pool, &file, sizeof(file));
201 /* ### register a cleanup to close the DBM? */
206 static void vt_db_close(apr_dbm_t *dbm)
208 (*GET_BDB(dbm->file)->close)(GET_BDB(dbm->file)
215 static apr_status_t vt_db_fetch(apr_dbm_t *dbm, apr_datum_t key,
216 apr_datum_t * pvalue)
222 ckey.data = key.dptr;
223 ckey.size = key.dsize;
225 dberr = do_fetch(GET_BDB(dbm->file), ckey, rd);
227 /* "not found" is not an error. return zero'd value. */
235 memset(&rd, 0, sizeof(rd));
239 pvalue->dptr = rd.data;
240 pvalue->dsize = rd.size;
242 /* store the error info into DBM, and return a status code. Also, note
243 that *pvalue should have been cleared on error. */
244 return set_error(dbm, db2s(dberr));
247 static apr_status_t vt_db_store(apr_dbm_t *dbm, apr_datum_t key,
254 ckey.data = key.dptr;
255 ckey.size = key.dsize;
257 cvalue.data = value.dptr;
258 cvalue.size = value.dsize;
260 rv = db2s((*GET_BDB(dbm->file)->put)(GET_BDB(dbm->file),
266 /* store any error info into DBM, and return a status code. */
267 return set_error(dbm, rv);
270 static apr_status_t vt_db_del(apr_dbm_t *dbm, apr_datum_t key)
275 ckey.data = key.dptr;
276 ckey.size = key.dsize;
278 rv = db2s((*GET_BDB(dbm->file)->del)(GET_BDB(dbm->file),
283 /* store any error info into DBM, and return a status code. */
284 return set_error(dbm, rv);
287 static int vt_db_exists(apr_dbm_t *dbm, apr_datum_t key)
289 DBT ckey = { 0 }; /* converted key */
293 ckey.data = key.dptr;
294 ckey.size = key.dsize;
296 dberr = do_fetch(GET_BDB(dbm->file), ckey, data);
298 /* note: the result data is "loaned" to us; we don't need to free it */
300 /* DB returns DB_NOTFOUND if it doesn't exist. but we want to say
301 that *any* error means it doesn't exist. */
305 static apr_status_t vt_db_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
307 real_file_t *f = dbm->file;
313 dberr = (*f->bdb->seq)(f->bdb, &first, &data, R_FIRST);
315 if ((dberr = (*f->bdb->cursor)(f->bdb, NULL, &f->curs
316 #if DB_VER >= 3 || ((DB_VERSION_MAJOR == 2) && (DB_VERSION_MINOR > 5))
320 dberr = (*f->curs->c_get)(f->curs, &first, &data, DB_FIRST);
321 if (dberr == DB_NOTFOUND) {
322 memset(&first, 0, sizeof(first));
323 (*f->curs->c_close)(f->curs);
330 pkey->dptr = first.data;
331 pkey->dsize = first.size;
333 /* store any error info into DBM, and return a status code. */
334 return set_error(dbm, db2s(dberr));
337 static apr_status_t vt_db_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
339 real_file_t *f = dbm->file;
344 ckey.data = pkey->dptr;
345 ckey.size = pkey->dsize;
348 dberr = (*f->bdb->seq)(f->bdb, &ckey, &data, R_NEXT);
349 if (dberr == RET_SPECIAL) {
358 dberr = (*f->curs->c_get)(f->curs, &ckey, &data, DB_NEXT);
359 if (dberr == DB_NOTFOUND) {
360 (*f->curs->c_close)(f->curs);
368 pkey->dptr = ckey.data;
369 pkey->dsize = ckey.size;
371 /* store any error info into DBM, and return a status code. */
372 /* ### or use db2s(dberr) instead of APR_SUCCESS? */
373 return set_error(dbm, APR_SUCCESS);
376 static void vt_db_freedatum(apr_dbm_t *dbm, apr_datum_t data)
381 static void vt_db_usednames(apr_pool_t *pool, const char *pathname,
382 const char **used1, const char **used2)
384 *used1 = apr_pstrdup(pool, pathname);
389 APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_db = {
404 #endif /* APU_HAVE_DB */