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"
20 #include <stdlib.h> /* for free() */
23 #include "apu_config.h"
27 #include "apr_dbm_private.h"
30 #include <sys/types.h>
34 #define APR_DBM_DBMODE_RO O_RDONLY
35 #define APR_DBM_DBMODE_RW O_RDWR
36 #define APR_DBM_DBMODE_RWCREATE (O_RDWR|O_CREAT)
37 #define APR_DBM_DBMODE_RWTRUNC (O_RDWR|O_CREAT|O_TRUNC)
39 /* map a NDBM error to an apr_status_t */
40 static apr_status_t ndbm2s(int ndbmerr)
43 /* ### need to fix this */
50 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
52 apr_status_t rv = APR_SUCCESS;
54 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
57 if (dbm_error((DBM*)dbm->file)) {
59 rv = APR_EGENERAL; /* ### need something better */
62 /* captured it. clear it now. */
63 dbm_clearerr((DBM*)dbm->file);
68 /* --------------------------------------------------------------------------
70 ** DEFINE THE VTABLE FUNCTIONS FOR NDBM
73 static apr_status_t vt_ndbm_open(apr_dbm_t **pdb, const char *pathname,
74 apr_int32_t mode, apr_fileperms_t perm,
83 case APR_DBM_READONLY:
84 dbmode = APR_DBM_DBMODE_RO;
86 case APR_DBM_READWRITE:
87 dbmode = APR_DBM_DBMODE_RW;
89 case APR_DBM_RWCREATE:
90 dbmode = APR_DBM_DBMODE_RWCREATE;
93 dbmode = APR_DBM_DBMODE_RWTRUNC;
100 file = dbm_open(pathname, dbmode, apr_posix_perms2mode(perm));
102 return APR_EGENERAL; /* ### need a better error */
105 /* we have an open database... return it */
106 *pdb = apr_pcalloc(pool, sizeof(**pdb));
108 (*pdb)->type = &apr_dbm_type_ndbm;
111 /* ### register a cleanup to close the DBM? */
116 static void vt_ndbm_close(apr_dbm_t *dbm)
118 dbm_close(dbm->file);
121 static apr_status_t vt_ndbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
127 kd.dsize = key.dsize;
129 rd = dbm_fetch(dbm->file, kd);
131 pvalue->dptr = rd.dptr;
132 pvalue->dsize = rd.dsize;
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,
146 kd.dsize = key.dsize;
148 vd.dptr = value.dptr;
149 vd.dsize = value.dsize;
151 rc = dbm_store(dbm->file, kd, vd, DBM_REPLACE);
153 /* store any error info into DBM, and return a status code. */
154 return set_error(dbm, ndbm2s(rc));
157 static apr_status_t vt_ndbm_del(apr_dbm_t *dbm, apr_datum_t key)
163 kd.dsize = key.dsize;
165 rc = dbm_delete(dbm->file, kd);
167 /* store any error info into DBM, and return a status code. */
168 return set_error(dbm, ndbm2s(rc));
171 static int vt_ndbm_exists(apr_dbm_t *dbm, apr_datum_t key)
176 kd.dsize = key.dsize;
178 rd = dbm_fetch(dbm->file, kd);
180 return rd.dptr != NULL;
183 static apr_status_t vt_ndbm_firstkey(apr_dbm_t *dbm, apr_datum_t *pkey)
187 rd = dbm_firstkey(dbm->file);
189 pkey->dptr = rd.dptr;
190 pkey->dsize = rd.dsize;
192 /* store any error info into DBM, and return a status code. */
193 return set_error(dbm, APR_SUCCESS);
196 static apr_status_t vt_ndbm_nextkey(apr_dbm_t *dbm, apr_datum_t *pkey)
200 kd.dptr = pkey->dptr;
201 kd.dsize = pkey->dsize;
203 rd = dbm_nextkey(dbm->file);
205 pkey->dptr = rd.dptr;
206 pkey->dsize = rd.dsize;
208 /* store any error info into DBM, and return a status code. */
209 return set_error(dbm, APR_SUCCESS);
212 static void vt_ndbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
217 static void vt_ndbm_usednames(apr_pool_t *pool, const char *pathname,
218 const char **used1, const char **used2)
220 *used1 = apr_pstrdup(pool, pathname);
224 APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_ndbm = {
238 #endif /* APU_HAVE_NDBM */