1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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"
20 #include <stdlib.h> /* for free() */
26 #include "apr_dbm_private.h"
30 /* this is used in a few places to define a noop "function". it is needed
31 to stop "no effect" warnings from GCC. */
32 #define NOOP_FUNCTION if (0) ; else
34 /* ### define defaults for now; these will go away in a while */
35 #define REGISTER_CLEANUP(dbm, pdatum) NOOP_FUNCTION
36 #define SET_FILE(pdb, f) ((pdb)->file = (f))
38 typedef GDBM_FILE real_file_t;
40 typedef datum *cvt_datum_t;
41 #define CONVERT_DATUM(cvt, pinput) ((cvt) = (datum *)(pinput))
43 typedef datum result_datum_t;
44 #define RETURN_DATUM(poutput, rd) (*(poutput) = *(apr_datum_t *)&(rd))
46 #define APR_DBM_CLOSE(f) gdbm_close(f)
47 #define APR_DBM_FETCH(f, k, v) ((v) = gdbm_fetch(f, *(k)), APR_SUCCESS)
48 #define APR_DBM_STORE(f, k, v) g2s(gdbm_store(f, *(k), *(v), GDBM_REPLACE))
49 #define APR_DBM_DELETE(f, k) g2s(gdbm_delete(f, *(k)))
50 #define APR_DBM_FIRSTKEY(f, k) ((k) = gdbm_firstkey(f), APR_SUCCESS)
51 #define APR_DBM_NEXTKEY(f, k, nk) ((nk) = gdbm_nextkey(f, *(k)), APR_SUCCESS)
52 #define APR_DBM_FREEDPTR(dptr) ((dptr) ? free(dptr) : 0)
54 #undef REGISTER_CLEANUP
55 #define REGISTER_CLEANUP(dbm, pdatum) \
57 apr_pool_cleanup_register((dbm)->pool, (pdatum)->dptr, \
58 datum_cleanup, apr_pool_cleanup_null); \
61 #define APR_DBM_DBMODE_RO GDBM_READER
62 #define APR_DBM_DBMODE_RW GDBM_WRITER
63 #define APR_DBM_DBMODE_RWCREATE GDBM_WRCREAT
64 #define APR_DBM_DBMODE_RWTRUNC GDBM_NEWDB
66 /* map a GDBM error to an apr_status_t */
67 static apr_status_t g2s(int gerr)
70 /* ### need to fix this */
77 static apr_status_t datum_cleanup(void *dptr)
85 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
87 apr_status_t rv = APR_SUCCESS;
89 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
91 if ((dbm->errcode = gdbm_errno) == GDBM_NO_ERROR) {
95 dbm->errmsg = gdbm_strerror(gdbm_errno);
96 rv = APR_EGENERAL; /* ### need something better */
99 /* captured it. clear it now. */
100 gdbm_errno = GDBM_NO_ERROR;
105 /* --------------------------------------------------------------------------
107 ** DEFINE THE VTABLE FUNCTIONS FOR GDBM
110 static apr_status_t vt_gdbm_open(apr_dbm_t **pdb, const char *pathname,
111 apr_int32_t mode, apr_fileperms_t perm,
120 case APR_DBM_READONLY:
121 dbmode = APR_DBM_DBMODE_RO;
123 case APR_DBM_READWRITE:
124 dbmode = APR_DBM_DBMODE_RW;
126 case APR_DBM_RWCREATE:
127 dbmode = APR_DBM_DBMODE_RWCREATE;
129 case APR_DBM_RWTRUNC:
130 dbmode = APR_DBM_DBMODE_RWTRUNC;
137 /* Note: stupid cast to get rid of "const" on the pathname */
138 file = gdbm_open((char *) pathname, 0, dbmode,
139 apr_posix_perms2mode(perm), NULL);
141 return APR_EGENERAL; /* ### need a better error */
144 /* we have an open database... return it */
145 *pdb = apr_pcalloc(pool, sizeof(**pdb));
147 (*pdb)->type = &apr_dbm_type_gdbm;
148 SET_FILE(*pdb, file);
150 /* ### register a cleanup to close the DBM? */
155 static void vt_gdbm_close(apr_dbm_t *dbm)
157 APR_DBM_CLOSE(dbm->file);
160 static apr_status_t vt_gdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
161 apr_datum_t * pvalue)
167 CONVERT_DATUM(ckey, &key);
168 rv = APR_DBM_FETCH(dbm->file, ckey, rd);
169 RETURN_DATUM(pvalue, rd);
171 REGISTER_CLEANUP(dbm, pvalue);
173 /* store the error info into DBM, and return a status code. Also, note
174 that *pvalue should have been cleared on error. */
175 return set_error(dbm, rv);
178 static apr_status_t vt_gdbm_store(apr_dbm_t *dbm, apr_datum_t key,
185 CONVERT_DATUM(ckey, &key);
186 CONVERT_DATUM(cvalue, &value);
187 rv = APR_DBM_STORE(dbm->file, ckey, cvalue);
189 /* store any error info into DBM, and return a status code. */
190 return set_error(dbm, rv);
193 static apr_status_t vt_gdbm_del(apr_dbm_t *dbm, apr_datum_t key)
198 CONVERT_DATUM(ckey, &key);
199 rv = APR_DBM_DELETE(dbm->file, ckey);
201 /* store any error info into DBM, and return a status code. */
202 return set_error(dbm, rv);
205 static int vt_gdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
207 datum *ckey = (datum *)&key;
209 return gdbm_exists(dbm->file, *ckey) != 0;
212 static apr_status_t vt_gdbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
217 rv = APR_DBM_FIRSTKEY(dbm->file, rd);
218 RETURN_DATUM(pkey, rd);
220 REGISTER_CLEANUP(dbm, pkey);
222 /* store any error info into DBM, and return a status code. */
223 return set_error(dbm, rv);
226 static apr_status_t vt_gdbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
232 CONVERT_DATUM(ckey, pkey);
233 rv = APR_DBM_NEXTKEY(dbm->file, ckey, rd);
234 RETURN_DATUM(pkey, rd);
236 REGISTER_CLEANUP(dbm, pkey);
238 /* store any error info into DBM, and return a status code. */
239 return set_error(dbm, APR_SUCCESS);
242 static void vt_gdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
244 (void) apr_pool_cleanup_run(dbm->pool, data.dptr, datum_cleanup);
247 static void vt_gdbm_usednames(apr_pool_t *pool, const char *pathname,
248 const char **used1, const char **used2)
250 *used1 = apr_pstrdup(pool, pathname);
255 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_gdbm = {
270 #endif /* APU_HAVE_GDBM */