Fix typos in APU_DSO_BUILD
[apr-util.git] / dbm / apr_dbm_gdbm.c
bloba986a0d62a1c2d89cb852c9c61a046574a6c6ca2
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"
19 #if APR_HAVE_STDLIB_H
20 #include <stdlib.h> /* for free() */
21 #endif
23 #include "apu.h"
25 #if APU_HAVE_GDBM
26 #include "apr_dbm_private.h"
28 #include <gdbm.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) \
56 if ((pdatum)->dptr) \
57 apr_pool_cleanup_register((dbm)->pool, (pdatum)->dptr, \
58 datum_cleanup, apr_pool_cleanup_null); \
59 else
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)
69 if (gerr == -1) {
70 /* ### need to fix this */
71 return APR_EGENERAL;
74 return APR_SUCCESS;
77 static apr_status_t datum_cleanup(void *dptr)
79 if (dptr)
80 free(dptr);
82 return APR_SUCCESS;
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) {
92 dbm->errmsg = NULL;
94 else {
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;
102 return rv;
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,
112 apr_pool_t *pool)
114 real_file_t file;
115 int dbmode;
117 *pdb = NULL;
119 switch (mode) {
120 case APR_DBM_READONLY:
121 dbmode = APR_DBM_DBMODE_RO;
122 break;
123 case APR_DBM_READWRITE:
124 dbmode = APR_DBM_DBMODE_RW;
125 break;
126 case APR_DBM_RWCREATE:
127 dbmode = APR_DBM_DBMODE_RWCREATE;
128 break;
129 case APR_DBM_RWTRUNC:
130 dbmode = APR_DBM_DBMODE_RWTRUNC;
131 break;
132 default:
133 return APR_EINVAL;
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);
140 if (file == NULL)
141 return APR_EGENERAL; /* ### need a better error */
144 /* we have an open database... return it */
145 *pdb = apr_pcalloc(pool, sizeof(**pdb));
146 (*pdb)->pool = pool;
147 (*pdb)->type = &apr_dbm_type_gdbm;
148 SET_FILE(*pdb, file);
150 /* ### register a cleanup to close the DBM? */
152 return APR_SUCCESS;
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)
163 apr_status_t rv;
164 cvt_datum_t ckey;
165 result_datum_t rd;
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,
179 apr_datum_t value)
181 apr_status_t rv;
182 cvt_datum_t ckey;
183 cvt_datum_t cvalue;
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)
195 apr_status_t rv;
196 cvt_datum_t ckey;
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)
214 apr_status_t rv;
215 result_datum_t rd;
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)
228 apr_status_t rv;
229 cvt_datum_t ckey;
230 result_datum_t rd;
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);
251 *used2 = NULL;
255 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_gdbm = {
256 "gdbm",
258 vt_gdbm_open,
259 vt_gdbm_close,
260 vt_gdbm_fetch,
261 vt_gdbm_store,
262 vt_gdbm_del,
263 vt_gdbm_exists,
264 vt_gdbm_firstkey,
265 vt_gdbm_nextkey,
266 vt_gdbm_freedatum,
267 vt_gdbm_usednames
270 #endif /* APU_HAVE_GDBM */