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"
18 #define APR_WANT_MEMFUNC
19 #define APR_WANT_STRFUNC
22 #include "apu_config.h"
27 #include "apr_dbm_private.h"
30 #define APR_DBM_DBMODE_RO (APR_FOPEN_READ | APR_FOPEN_BUFFERED)
31 #define APR_DBM_DBMODE_RW (APR_FOPEN_READ | APR_FOPEN_WRITE)
32 #define APR_DBM_DBMODE_RWCREATE (APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE)
33 #define APR_DBM_DBMODE_RWTRUNC (APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | \
36 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
38 dbm->errcode = dbm_said;
40 if (dbm_said != APR_SUCCESS) {
41 dbm->errmsg = apr_psprintf(dbm->pool, "%pm", &dbm_said);
49 /* --------------------------------------------------------------------------
51 ** DEFINE THE VTABLE FUNCTIONS FOR SDBM
54 static apr_status_t vt_sdbm_open(apr_dbm_t **pdb, const char *pathname,
55 apr_int32_t mode, apr_fileperms_t perm,
64 case APR_DBM_READONLY:
65 dbmode = APR_DBM_DBMODE_RO;
67 case APR_DBM_READWRITE:
68 dbmode = APR_DBM_DBMODE_RW;
70 case APR_DBM_RWCREATE:
71 dbmode = APR_DBM_DBMODE_RWCREATE;
74 dbmode = APR_DBM_DBMODE_RWTRUNC;
83 rv = apr_sdbm_open(&file, pathname, dbmode, perm, pool);
84 if (rv != APR_SUCCESS)
88 /* we have an open database... return it */
89 *pdb = apr_pcalloc(pool, sizeof(**pdb));
91 (*pdb)->type = &apr_dbm_type_sdbm;
94 /* ### register a cleanup to close the DBM? */
99 static void vt_sdbm_close(apr_dbm_t *dbm)
101 apr_sdbm_close(dbm->file);
104 static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
108 apr_sdbm_datum_t kd, rd;
111 kd.dsize = (int)key.dsize;
113 rv = apr_sdbm_fetch(dbm->file, &rd, kd);
115 pvalue->dptr = rd.dptr;
116 pvalue->dsize = rd.dsize;
118 /* store the error info into DBM, and return a status code. Also, note
119 that *pvalue should have been cleared on error. */
120 return set_error(dbm, rv);
123 static apr_status_t vt_sdbm_store(apr_dbm_t *dbm, apr_datum_t key,
127 apr_sdbm_datum_t kd, vd;
130 kd.dsize = (int)key.dsize;
132 vd.dptr = value.dptr;
133 vd.dsize = (int)value.dsize;
135 rv = apr_sdbm_store(dbm->file, kd, vd, APR_SDBM_REPLACE);
137 /* store any error info into DBM, and return a status code. */
138 return set_error(dbm, rv);
141 static apr_status_t vt_sdbm_del(apr_dbm_t *dbm, apr_datum_t key)
147 kd.dsize = (int)key.dsize;
149 rv = apr_sdbm_delete(dbm->file, kd);
151 /* store any error info into DBM, and return a status code. */
152 return set_error(dbm, rv);
155 static int vt_sdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
158 apr_sdbm_datum_t vd, kd;
161 kd.dsize = (int)key.dsize;
163 if (apr_sdbm_fetch(dbm->file, &vd, kd) != APR_SUCCESS)
166 exists = vd.dptr != NULL;
171 static apr_status_t vt_sdbm_firstkey(apr_dbm_t *dbm, apr_datum_t *pkey)
176 rv = apr_sdbm_firstkey(dbm->file, &rd);
178 pkey->dptr = rd.dptr;
179 pkey->dsize = rd.dsize;
181 /* store any error info into DBM, and return a status code. */
182 return set_error(dbm, rv);
185 static apr_status_t vt_sdbm_nextkey(apr_dbm_t *dbm, apr_datum_t *pkey)
189 (void)apr_sdbm_nextkey(dbm->file, &rd);
191 pkey->dptr = rd.dptr;
192 pkey->dsize = rd.dsize;
194 /* store any error info into DBM, and return a status code. */
195 return set_error(dbm, APR_SUCCESS);
198 static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
202 static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
203 const char **used1, const char **used2)
205 *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
206 *used2 = apr_pstrcat(pool, pathname, APR_SDBM_PAGFEXT, NULL);
209 APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
223 #endif /* APU_HAVE_SDBM */