Use of pre_cleanups is not the default for reslists.
[apr-util.git] / dbm / apr_dbm.c
blobd4fed7b168736623f883531a12b06ca9fee85999
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.h"
18 #include "apr_errno.h"
19 #include "apr_pools.h"
20 #include "apr_strings.h"
21 #define APR_WANT_MEMFUNC
22 #define APR_WANT_STRFUNC
23 #include "apr_want.h"
24 #include "apr_general.h"
26 #include "apu.h"
27 #include "apu_select_dbm.h"
28 #include "apr_dbm.h"
29 #include "apr_dbm_private.h"
31 /* ### note: the setting of DBM_VTABLE will go away once we have multiple
32 ### DBMs in here.
33 ### Well, that day is here. So, do we remove DBM_VTABLE and the old
34 ### API entirely? Oh, what to do. We need an APU_DEFAULT_DBM #define.
35 ### Sounds like a job for autoconf. */
37 #if APU_USE_SDBM
38 #define DBM_VTABLE apr_dbm_type_sdbm
39 #elif APU_USE_GDBM
40 #define DBM_VTABLE apr_dbm_type_gdbm
41 #elif APU_USE_DB
42 #define DBM_VTABLE apr_dbm_type_db
43 #elif APU_USE_NDBM
44 #define DBM_VTABLE apr_dbm_type_ndbm
45 #else /* Not in the USE_xDBM list above */
46 #error a DBM implementation was not specified
47 #endif
49 APU_DECLARE(apr_status_t) apr_dbm_open_ex(apr_dbm_t **pdb, const char*type,
50 const char *pathname,
51 apr_int32_t mode, apr_fileperms_t perm,
52 apr_pool_t *pool)
54 #if APU_HAVE_GDBM
55 if (!strcasecmp(type, "GDBM")) {
56 return (*apr_dbm_type_gdbm.open)(pdb, pathname, mode, perm, pool);
58 #endif
59 #if APU_HAVE_SDBM
60 if (!strcasecmp(type, "SDBM")) {
61 return (*apr_dbm_type_sdbm.open)(pdb, pathname, mode, perm, pool);
63 #endif
64 #if APU_HAVE_DB
65 if (!strcasecmp(type, "DB")) {
66 return (*apr_dbm_type_db.open)(pdb, pathname, mode, perm, pool);
68 #endif
69 #if APU_HAVE_NDBM
70 if (!strcasecmp(type, "NDBM")) {
71 return (*apr_dbm_type_ndbm.open)(pdb, pathname, mode, perm, pool);
73 #endif
75 if (!strcasecmp(type, "default")) {
76 return (*DBM_VTABLE.open)(pdb, pathname, mode, perm, pool);
79 return APR_ENOTIMPL;
82 APU_DECLARE(apr_status_t) apr_dbm_open(apr_dbm_t **pdb, const char *pathname,
83 apr_int32_t mode, apr_fileperms_t perm,
84 apr_pool_t *pool)
86 return (*DBM_VTABLE.open)(pdb, pathname, mode, perm, pool);
89 APU_DECLARE(void) apr_dbm_close(apr_dbm_t *dbm)
91 (*dbm->type->close)(dbm);
94 APU_DECLARE(apr_status_t) apr_dbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
95 apr_datum_t *pvalue)
97 return (*dbm->type->fetch)(dbm, key, pvalue);
100 APU_DECLARE(apr_status_t) apr_dbm_store(apr_dbm_t *dbm, apr_datum_t key,
101 apr_datum_t value)
103 return (*dbm->type->store)(dbm, key, value);
106 APU_DECLARE(apr_status_t) apr_dbm_delete(apr_dbm_t *dbm, apr_datum_t key)
108 return (*dbm->type->del)(dbm, key);
111 APU_DECLARE(int) apr_dbm_exists(apr_dbm_t *dbm, apr_datum_t key)
113 return (*dbm->type->exists)(dbm, key);
116 APU_DECLARE(apr_status_t) apr_dbm_firstkey(apr_dbm_t *dbm, apr_datum_t *pkey)
118 return (*dbm->type->firstkey)(dbm, pkey);
121 APU_DECLARE(apr_status_t) apr_dbm_nextkey(apr_dbm_t *dbm, apr_datum_t *pkey)
123 return (*dbm->type->nextkey)(dbm, pkey);
126 APU_DECLARE(void) apr_dbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
128 (*dbm->type->freedatum)(dbm, data);
131 APU_DECLARE(char *) apr_dbm_geterror(apr_dbm_t *dbm, int *errcode,
132 char *errbuf, apr_size_t errbufsize)
134 if (errcode != NULL)
135 *errcode = dbm->errcode;
137 /* assert: errbufsize > 0 */
139 if (dbm->errmsg == NULL)
140 *errbuf = '\0';
141 else
142 (void) apr_cpystrn(errbuf, dbm->errmsg, errbufsize);
143 return errbuf;
146 APU_DECLARE(apr_status_t) apr_dbm_get_usednames_ex(apr_pool_t *p,
147 const char *type,
148 const char *pathname,
149 const char **used1,
150 const char **used2)
152 #if APU_HAVE_GDBM
153 if (!strcasecmp(type, "GDBM")) {
154 (*apr_dbm_type_gdbm.getusednames)(p,pathname,used1,used2);
155 return APR_SUCCESS;
157 #endif
158 #if APU_HAVE_SDBM
159 if (!strcasecmp(type, "SDBM")) {
160 (*apr_dbm_type_sdbm.getusednames)(p,pathname,used1,used2);
161 return APR_SUCCESS;
163 #endif
164 #if APU_HAVE_DB
165 if (!strcasecmp(type, "DB")) {
166 (*apr_dbm_type_db.getusednames)(p,pathname,used1,used2);
167 return APR_SUCCESS;
169 #endif
170 #if APU_HAVE_NDBM
171 if (!strcasecmp(type, "NDBM")) {
172 (*apr_dbm_type_ndbm.getusednames)(p,pathname,used1,used2);
173 return APR_SUCCESS;
175 #endif
177 if (!strcasecmp(type, "default")) {
178 (*DBM_VTABLE.getusednames)(p, pathname, used1, used2);
179 return APR_SUCCESS;
182 return APR_ENOTIMPL;
185 APU_DECLARE(void) apr_dbm_get_usednames(apr_pool_t *p,
186 const char *pathname,
187 const char **used1,
188 const char **used2)
190 /* ### one day, a DBM type name will be passed and we'll need to look it
191 ### up. for now, it is constant. */
193 (*DBM_VTABLE.getusednames)(p, pathname, used1, used2);
196 /* Most DBM libraries take a POSIX mode for creating files. Don't trust
197 * the mode_t type, some platforms may not support it, int is safe.
199 APU_DECLARE(int) apr_posix_perms2mode(apr_fileperms_t perm)
201 int mode = 0;
203 mode |= 0700 & (perm >> 2); /* User is off-by-2 bits */
204 mode |= 0070 & (perm >> 1); /* Group is off-by-1 bit */
205 mode |= 0007 & (perm); /* World maps 1 for 1 */
206 return mode;