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"
18 #define APR_WANT_MEMFUNC
19 #define APR_WANT_STRFUNC
26 #include "apr_dbm_private.h"
30 #include <stdlib.h> /* For abort() */
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 defaults for now; these will go away in a while */
38 #define REGISTER_CLEANUP(dbm, pdatum) NOOP_FUNCTION
39 #define SET_FILE(pdb, f) ((pdb)->file = (f))
41 typedef apr_sdbm_t *real_file_t;
43 typedef apr_sdbm_datum_t cvt_datum_t;
44 #define CONVERT_DATUM(cvt, pinput) ((cvt).dptr = (pinput)->dptr, (cvt).dsize = (pinput)->dsize)
46 typedef apr_sdbm_datum_t result_datum_t;
47 #define RETURN_DATUM(poutput, rd) ((poutput)->dptr = (rd).dptr, (poutput)->dsize = (rd).dsize)
49 #define APR_DBM_CLOSE(f) apr_sdbm_close(f)
50 #define APR_DBM_FETCH(f, k, v) apr_sdbm_fetch(f, &(v), (k))
51 #define APR_DBM_STORE(f, k, v) apr_sdbm_store(f, (k), (v), APR_SDBM_REPLACE)
52 #define APR_DBM_DELETE(f, k) apr_sdbm_delete(f, (k))
53 #define APR_DBM_FIRSTKEY(f, k) apr_sdbm_firstkey(f, &(k))
54 #define APR_DBM_NEXTKEY(f, k, nk) apr_sdbm_nextkey(f, &(nk))
55 #define APR_DBM_FREEDPTR(dptr) NOOP_FUNCTION
57 #define APR_DBM_DBMODE_RO APR_READ
58 #define APR_DBM_DBMODE_RW (APR_READ | APR_WRITE)
59 #define APR_DBM_DBMODE_RWCREATE (APR_READ | APR_WRITE | APR_CREATE)
60 #define APR_DBM_DBMODE_RWTRUNC (APR_READ | APR_WRITE | APR_CREATE | \
63 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
65 apr_status_t rv = APR_SUCCESS;
67 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
69 if ((dbm->errcode = dbm_said) == APR_SUCCESS) {
73 dbm->errmsg = "I/O error occurred.";
74 rv = APR_EGENERAL; /* ### need something better */
80 /* --------------------------------------------------------------------------
82 ** DEFINE THE VTABLE FUNCTIONS FOR SDBM
85 static apr_status_t vt_sdbm_open(apr_dbm_t **pdb, const char *pathname,
86 apr_int32_t mode, apr_fileperms_t perm,
95 case APR_DBM_READONLY:
96 dbmode = APR_DBM_DBMODE_RO;
98 case APR_DBM_READWRITE:
99 dbmode = APR_DBM_DBMODE_RW;
101 case APR_DBM_RWCREATE:
102 dbmode = APR_DBM_DBMODE_RWCREATE;
104 case APR_DBM_RWTRUNC:
105 dbmode = APR_DBM_DBMODE_RWTRUNC;
114 rv = apr_sdbm_open(&file, pathname, dbmode, perm, pool);
115 if (rv != APR_SUCCESS)
119 /* we have an open database... return it */
120 *pdb = apr_pcalloc(pool, sizeof(**pdb));
122 (*pdb)->type = &apr_dbm_type_sdbm;
123 SET_FILE(*pdb, file);
125 /* ### register a cleanup to close the DBM? */
130 static void vt_sdbm_close(apr_dbm_t *dbm)
132 APR_DBM_CLOSE(dbm->file);
135 static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
136 apr_datum_t * pvalue)
142 CONVERT_DATUM(ckey, &key);
143 rv = APR_DBM_FETCH(dbm->file, ckey, rd);
144 RETURN_DATUM(pvalue, rd);
146 REGISTER_CLEANUP(dbm, pvalue);
148 /* store the error info into DBM, and return a status code. Also, note
149 that *pvalue should have been cleared on error. */
150 return set_error(dbm, rv);
153 static apr_status_t vt_sdbm_store(apr_dbm_t *dbm, apr_datum_t key,
160 CONVERT_DATUM(ckey, &key);
161 CONVERT_DATUM(cvalue, &value);
162 rv = APR_DBM_STORE(dbm->file, ckey, cvalue);
164 /* store any error info into DBM, and return a status code. */
165 return set_error(dbm, rv);
168 static apr_status_t vt_sdbm_del(apr_dbm_t *dbm, apr_datum_t key)
173 CONVERT_DATUM(ckey, &key);
174 rv = APR_DBM_DELETE(dbm->file, ckey);
176 /* store any error info into DBM, and return a status code. */
177 return set_error(dbm, rv);
180 static int vt_sdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
183 apr_sdbm_datum_t ckey;
185 CONVERT_DATUM(ckey, &key);
188 apr_sdbm_datum_t value;
189 if (apr_sdbm_fetch(dbm->file, &value, ckey) != APR_SUCCESS) {
193 exists = value.dptr != NULL;
199 static apr_status_t vt_sdbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
204 rv = APR_DBM_FIRSTKEY(dbm->file, rd);
205 RETURN_DATUM(pkey, rd);
207 REGISTER_CLEANUP(dbm, pkey);
209 /* store any error info into DBM, and return a status code. */
210 return set_error(dbm, rv);
213 static apr_status_t vt_sdbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
219 CONVERT_DATUM(ckey, pkey);
220 rv = APR_DBM_NEXTKEY(dbm->file, ckey, rd);
221 RETURN_DATUM(pkey, rd);
223 REGISTER_CLEANUP(dbm, pkey);
225 /* store any error info into DBM, and return a status code. */
226 return set_error(dbm, APR_SUCCESS);
229 static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
231 APR_DBM_FREEDPTR(data.dptr);
234 static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
235 const char **used1, const char **used2)
239 /* ### this could be optimized by computing strlen() once and using
240 ### memcpy and pmemdup instead. but why bother? */
242 *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
243 *used2 = work = apr_pstrdup(pool, *used1);
245 /* we know the extension is 4 characters */
246 memcpy(&work[strlen(work) - 4], APR_SDBM_PAGFEXT, 4);
250 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
265 #endif /* APU_HAVE_SDBM */