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() */
26 #include "apr_dbm_private.h"
29 #include <sys/types.h>
33 #define APR_DBM_DBMODE_RO O_RDONLY
34 #define APR_DBM_DBMODE_RW O_RDWR
35 #define APR_DBM_DBMODE_RWCREATE (O_RDWR|O_CREAT)
36 #define APR_DBM_DBMODE_RWTRUNC (O_RDWR|O_CREAT|O_TRUNC)
38 /* map a NDBM error to an apr_status_t */
39 static apr_status_t
ndbm2s(int ndbmerr
)
42 /* ### need to fix this */
49 static apr_status_t
set_error(apr_dbm_t
*dbm
, apr_status_t dbm_said
)
51 apr_status_t rv
= APR_SUCCESS
;
53 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
56 if (dbm_error((DBM
*)dbm
->file
)) {
58 rv
= APR_EGENERAL
; /* ### need something better */
61 /* captured it. clear it now. */
62 dbm_clearerr((DBM
*)dbm
->file
);
67 /* --------------------------------------------------------------------------
69 ** DEFINE THE VTABLE FUNCTIONS FOR NDBM
72 static apr_status_t
vt_ndbm_open(apr_dbm_t
**pdb
, const char *pathname
,
73 apr_int32_t mode
, apr_fileperms_t perm
,
82 case APR_DBM_READONLY
:
83 dbmode
= APR_DBM_DBMODE_RO
;
85 case APR_DBM_READWRITE
:
86 dbmode
= APR_DBM_DBMODE_RW
;
88 case APR_DBM_RWCREATE
:
89 dbmode
= APR_DBM_DBMODE_RWCREATE
;
92 dbmode
= APR_DBM_DBMODE_RWTRUNC
;
99 file
= dbm_open(pathname
, dbmode
, apr_posix_perms2mode(perm
));
101 return APR_EGENERAL
; /* ### need a better error */
104 /* we have an open database... return it */
105 *pdb
= apr_pcalloc(pool
, sizeof(**pdb
));
107 (*pdb
)->type
= &apr_dbm_type_ndbm
;
110 /* ### register a cleanup to close the DBM? */
115 static void vt_ndbm_close(apr_dbm_t
*dbm
)
117 dbm_close(dbm
->file
);
120 static apr_status_t
vt_ndbm_fetch(apr_dbm_t
*dbm
, apr_datum_t key
,
126 kd
.dsize
= key
.dsize
;
128 rd
= dbm_fetch(dbm
->file
, kd
);
130 pvalue
->dptr
= rd
.dptr
;
131 pvalue
->dsize
= rd
.dsize
;
133 /* store the error info into DBM, and return a status code. Also, note
134 that *pvalue should have been cleared on error. */
135 return set_error(dbm
, APR_SUCCESS
);
138 static apr_status_t
vt_ndbm_store(apr_dbm_t
*dbm
, apr_datum_t key
,
145 kd
.dsize
= key
.dsize
;
147 vd
.dptr
= value
.dptr
;
148 vd
.dsize
= value
.dsize
;
150 rc
= dbm_store(dbm
->file
, kd
, vd
, DBM_REPLACE
);
152 /* store any error info into DBM, and return a status code. */
153 return set_error(dbm
, ndbm2s(rc
));
156 static apr_status_t
vt_ndbm_del(apr_dbm_t
*dbm
, apr_datum_t key
)
162 kd
.dsize
= key
.dsize
;
164 rc
= dbm_delete(dbm
->file
, kd
);
166 /* store any error info into DBM, and return a status code. */
167 return set_error(dbm
, ndbm2s(rc
));
170 static int vt_ndbm_exists(apr_dbm_t
*dbm
, apr_datum_t key
)
175 kd
.dsize
= key
.dsize
;
177 rd
= dbm_fetch(dbm
->file
, kd
);
179 return rd
.dptr
!= NULL
;
182 static apr_status_t
vt_ndbm_firstkey(apr_dbm_t
*dbm
, apr_datum_t
*pkey
)
186 rd
= dbm_firstkey(dbm
->file
);
188 pkey
->dptr
= rd
.dptr
;
189 pkey
->dsize
= rd
.dsize
;
191 /* store any error info into DBM, and return a status code. */
192 return set_error(dbm
, APR_SUCCESS
);
195 static apr_status_t
vt_ndbm_nextkey(apr_dbm_t
*dbm
, apr_datum_t
*pkey
)
199 kd
.dptr
= pkey
->dptr
;
200 kd
.dsize
= pkey
->dsize
;
202 rd
= dbm_nextkey(dbm
->file
);
204 pkey
->dptr
= rd
.dptr
;
205 pkey
->dsize
= rd
.dsize
;
207 /* store any error info into DBM, and return a status code. */
208 return set_error(dbm
, APR_SUCCESS
);
211 static void vt_ndbm_freedatum(apr_dbm_t
*dbm
, apr_datum_t data
)
216 static void vt_ndbm_usednames(apr_pool_t
*pool
, const char *pathname
,
217 const char **used1
, const char **used2
)
219 *used1
= apr_pstrdup(pool
, pathname
);
223 APU_DECLARE_DATA
const apr_dbm_type_t apr_dbm_type_ndbm
= {
237 #endif /* APU_HAVE_NDBM */