Fix typos in APU_DSO_BUILD
[apr-util.git] / dbm / apr_dbm_ndbm.c
blobc4bcfcf73d991f8f60fa30fb5672fad6bd10f21a
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_NDBM
26 #include "apr_dbm_private.h"
28 #include <ndbm.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
33 /* this is used in a few places to define a noop "function". it is needed
34 to stop "no effect" warnings from GCC. */
35 #define NOOP_FUNCTION if (0) ; else
37 #define APR_DBM_DBMODE_RO O_RDONLY
38 #define APR_DBM_DBMODE_RW O_RDWR
39 #define APR_DBM_DBMODE_RWCREATE (O_RDWR|O_CREAT)
40 #define APR_DBM_DBMODE_RWTRUNC (O_RDWR|O_CREAT|O_TRUNC)
42 /* map a NDBM error to an apr_status_t */
43 static apr_status_t ndbm2s(int ndbmerr)
45 if (ndbmerr == -1) {
46 /* ### need to fix this */
47 return APR_EGENERAL;
50 return APR_SUCCESS;
53 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
55 apr_status_t rv = APR_SUCCESS;
57 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
59 dbm->errmsg = NULL;
60 if (dbm_error((DBM*)dbm->file)) {
61 dbm->errmsg = NULL;
62 rv = APR_EGENERAL; /* ### need something better */
65 /* captured it. clear it now. */
66 dbm_clearerr((DBM*)dbm->file);
68 return rv;
71 /* --------------------------------------------------------------------------
73 ** DEFINE THE VTABLE FUNCTIONS FOR NDBM
76 static apr_status_t vt_ndbm_open(apr_dbm_t **pdb, const char *pathname,
77 apr_int32_t mode, apr_fileperms_t perm,
78 apr_pool_t *pool)
80 DBM *file;
81 int dbmode;
83 *pdb = NULL;
85 switch (mode) {
86 case APR_DBM_READONLY:
87 dbmode = APR_DBM_DBMODE_RO;
88 break;
89 case APR_DBM_READWRITE:
90 dbmode = APR_DBM_DBMODE_RW;
91 break;
92 case APR_DBM_RWCREATE:
93 dbmode = APR_DBM_DBMODE_RWCREATE;
94 break;
95 case APR_DBM_RWTRUNC:
96 dbmode = APR_DBM_DBMODE_RWTRUNC;
97 break;
98 default:
99 return APR_EINVAL;
103 file = dbm_open(pathname, dbmode, apr_posix_perms2mode(perm));
104 if (file == NULL)
105 return APR_EGENERAL; /* ### need a better error */
108 /* we have an open database... return it */
109 *pdb = apr_pcalloc(pool, sizeof(**pdb));
110 (*pdb)->pool = pool;
111 (*pdb)->type = &apr_dbm_type_ndbm;
112 (*pdb)->file = file;
114 /* ### register a cleanup to close the DBM? */
116 return APR_SUCCESS;
119 static void vt_ndbm_close(apr_dbm_t *dbm)
121 dbm_close(dbm->file);
124 static apr_status_t vt_ndbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
125 apr_datum_t * pvalue)
127 datum *ckey;
128 datum rd;
130 ckey = (datum*)&key;
131 rd = dbm_fetch(dbm->file, *ckey);
132 *pvalue = *(apr_datum_t*)&rd;
134 /* store the error info into DBM, and return a status code. Also, note
135 that *pvalue should have been cleared on error. */
136 return set_error(dbm, APR_SUCCESS);
139 static apr_status_t vt_ndbm_store(apr_dbm_t *dbm, apr_datum_t key,
140 apr_datum_t value)
142 apr_status_t rv;
143 datum *ckey;
144 datum *cvalue;
146 ckey = (datum*)&key;
147 cvalue = (datum*)&value;
148 rv = ndbm2s( dbm_store( dbm->file, *ckey, *cvalue, DBM_REPLACE));
150 /* store any error info into DBM, and return a status code. */
151 return set_error(dbm, rv);
154 static apr_status_t vt_ndbm_del(apr_dbm_t *dbm, apr_datum_t key)
156 apr_status_t rv;
157 datum *ckey;
159 ckey = (datum*)&key;
160 rv = ndbm2s( dbm_delete(dbm->file, *ckey));
162 /* store any error info into DBM, and return a status code. */
163 return set_error(dbm, rv);
166 static int vt_ndbm_exists(apr_dbm_t *dbm, apr_datum_t key)
168 datum *ckey = (datum *)&key;
169 datum value;
171 value = dbm_fetch( dbm->file, *ckey);
173 return value.dptr != NULL;
176 static apr_status_t vt_ndbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
178 datum rd;
180 rd = dbm_firstkey(dbm->file);
181 *pkey = *(apr_datum_t*)&rd;
183 /* store any error info into DBM, and return a status code. */
184 return set_error(dbm, APR_SUCCESS);
187 static apr_status_t vt_ndbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
189 datum *ckey;
190 datum rd;
192 ckey = (datum*)pkey;
193 rd = dbm_nextkey(dbm->file);
194 *pkey = *(apr_datum_t*)&rd;
196 /* store any error info into DBM, and return a status code. */
197 return set_error(dbm, APR_SUCCESS);
200 static void vt_ndbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
202 /* nothing to do */
205 static void vt_ndbm_usednames(apr_pool_t *pool, const char *pathname,
206 const char **used1, const char **used2)
208 *used1 = apr_pstrdup(pool, pathname);
209 *used2 = NULL;
213 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_ndbm = {
214 "ndbm",
216 vt_ndbm_open,
217 vt_ndbm_close,
218 vt_ndbm_fetch,
219 vt_ndbm_store,
220 vt_ndbm_del,
221 vt_ndbm_exists,
222 vt_ndbm_firstkey,
223 vt_ndbm_nextkey,
224 vt_ndbm_freedatum,
225 vt_ndbm_usednames
227 #endif /* APU_HAVE_NDBM */