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"
29
#include <sys/types.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)
46
/* ### need to fix this */
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 */
60
if (dbm_error((DBM*)dbm->file)) {
62
rv = APR_EGENERAL; /* ### need something better */
65
/* captured it. clear it now. */
66
dbm_clearerr((DBM*)dbm->file);
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,
86
case APR_DBM_READONLY:
87
dbmode = APR_DBM_DBMODE_RO;
89
case APR_DBM_READWRITE:
90
dbmode = APR_DBM_DBMODE_RW;
92
case APR_DBM_RWCREATE:
93
dbmode = APR_DBM_DBMODE_RWCREATE;
96
dbmode = APR_DBM_DBMODE_RWTRUNC;
103
file = dbm_open(pathname, dbmode, apr_posix_perms2mode(perm));
105
return APR_EGENERAL; /* ### need a better error */
108
/* we have an open database... return it */
109
*pdb = apr_pcalloc(pool, sizeof(**pdb));
111
(*pdb)->type = &apr_dbm_type_ndbm;
114
/* ### register a cleanup to close the DBM? */
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)
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,
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)
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;
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)
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)
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)
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);
213
APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_ndbm = {
227
#endif /* APU_HAVE_NDBM */