upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / dbm / apr_dbm_sdbm.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "apr_strings.h"
18 #define APR_WANT_MEMFUNC
19 #define APR_WANT_STRFUNC
20 #include "apr_want.h"
21
22 #include "apu.h"
23
24 #if APU_HAVE_SDBM
25
26 #include "apr_dbm_private.h"
27
28 #include "apr_sdbm.h"
29 #if APR_HAVE_STDLIB_H
30 #include <stdlib.h>  /* For abort() */
31 #endif
32
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
36
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))
40
41 typedef apr_sdbm_t *real_file_t;
42
43 typedef apr_sdbm_datum_t cvt_datum_t;
44 #define CONVERT_DATUM(cvt, pinput) ((cvt).dptr = (pinput)->dptr, (cvt).dsize = (pinput)->dsize)
45
46 typedef apr_sdbm_datum_t result_datum_t;
47 #define RETURN_DATUM(poutput, rd) ((poutput)->dptr = (rd).dptr, (poutput)->dsize = (rd).dsize)
48
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
56
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 | \
61                                  APR_TRUNCATE)
62
63 static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
64 {
65     apr_status_t rv = APR_SUCCESS;
66
67     /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
68
69     if ((dbm->errcode = dbm_said) == APR_SUCCESS) {
70         dbm->errmsg = NULL;
71     }
72     else {
73         dbm->errmsg = "I/O error occurred.";
74         rv = APR_EGENERAL;        /* ### need something better */
75     }
76
77     return rv;
78 }
79
80 /* --------------------------------------------------------------------------
81 **
82 ** DEFINE THE VTABLE FUNCTIONS FOR SDBM
83 */
84
85 static apr_status_t vt_sdbm_open(apr_dbm_t **pdb, const char *pathname,
86                                  apr_int32_t mode, apr_fileperms_t perm,
87                                  apr_pool_t *pool)
88 {
89     real_file_t file;
90     int dbmode;
91
92     *pdb = NULL;
93
94     switch (mode) {
95     case APR_DBM_READONLY:
96         dbmode = APR_DBM_DBMODE_RO;
97         break;
98     case APR_DBM_READWRITE:
99         dbmode = APR_DBM_DBMODE_RW;
100         break;
101     case APR_DBM_RWCREATE:
102         dbmode = APR_DBM_DBMODE_RWCREATE;
103         break;
104     case APR_DBM_RWTRUNC:
105         dbmode = APR_DBM_DBMODE_RWTRUNC;
106         break;
107     default:
108         return APR_EINVAL;
109     }
110
111     {
112         apr_status_t rv;
113
114         rv = apr_sdbm_open(&file, pathname, dbmode, perm, pool);
115         if (rv != APR_SUCCESS)
116             return rv;
117     }
118
119     /* we have an open database... return it */
120     *pdb = apr_pcalloc(pool, sizeof(**pdb));
121     (*pdb)->pool = pool;
122     (*pdb)->type = &apr_dbm_type_sdbm;
123     SET_FILE(*pdb, file);
124
125     /* ### register a cleanup to close the DBM? */
126
127     return APR_SUCCESS;
128 }
129
130 static void vt_sdbm_close(apr_dbm_t *dbm)
131 {
132     APR_DBM_CLOSE(dbm->file);
133 }
134
135 static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
136                                   apr_datum_t * pvalue)
137 {
138     apr_status_t rv;
139     cvt_datum_t ckey;
140     result_datum_t rd;
141
142     CONVERT_DATUM(ckey, &key);
143     rv = APR_DBM_FETCH(dbm->file, ckey, rd);
144     RETURN_DATUM(pvalue, rd);
145
146     REGISTER_CLEANUP(dbm, pvalue);
147
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);
151 }
152
153 static apr_status_t vt_sdbm_store(apr_dbm_t *dbm, apr_datum_t key,
154                                   apr_datum_t value)
155 {
156     apr_status_t rv;
157     cvt_datum_t ckey;
158     cvt_datum_t cvalue;
159
160     CONVERT_DATUM(ckey, &key);
161     CONVERT_DATUM(cvalue, &value);
162     rv = APR_DBM_STORE(dbm->file, ckey, cvalue);
163
164     /* store any error info into DBM, and return a status code. */
165     return set_error(dbm, rv);
166 }
167
168 static apr_status_t vt_sdbm_del(apr_dbm_t *dbm, apr_datum_t key)
169 {
170     apr_status_t rv;
171     cvt_datum_t ckey;
172
173     CONVERT_DATUM(ckey, &key);
174     rv = APR_DBM_DELETE(dbm->file, ckey);
175
176     /* store any error info into DBM, and return a status code. */
177     return set_error(dbm, rv);
178 }
179
180 static int vt_sdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
181 {
182     int exists;
183     apr_sdbm_datum_t ckey;
184
185     CONVERT_DATUM(ckey, &key);
186
187     {
188         apr_sdbm_datum_t value;
189         if (apr_sdbm_fetch(dbm->file, &value, ckey) != APR_SUCCESS) {
190             exists = 0;
191         }
192         else
193             exists = value.dptr != NULL;
194     }
195
196     return exists;
197 }
198
199 static apr_status_t vt_sdbm_firstkey(apr_dbm_t *dbm, apr_datum_t * pkey)
200 {
201     apr_status_t rv;
202     result_datum_t rd;
203
204     rv = APR_DBM_FIRSTKEY(dbm->file, rd);
205     RETURN_DATUM(pkey, rd);
206
207     REGISTER_CLEANUP(dbm, pkey);
208
209     /* store any error info into DBM, and return a status code. */
210     return set_error(dbm, rv);
211 }
212
213 static apr_status_t vt_sdbm_nextkey(apr_dbm_t *dbm, apr_datum_t * pkey)
214 {
215     apr_status_t rv;
216     cvt_datum_t ckey;
217     result_datum_t rd;
218
219     CONVERT_DATUM(ckey, pkey);
220     rv = APR_DBM_NEXTKEY(dbm->file, ckey, rd);
221     RETURN_DATUM(pkey, rd);
222
223     REGISTER_CLEANUP(dbm, pkey);
224
225     /* store any error info into DBM, and return a status code. */
226     return set_error(dbm, APR_SUCCESS);
227 }
228
229 static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
230 {
231     APR_DBM_FREEDPTR(data.dptr);
232 }
233
234 static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
235                               const char **used1, const char **used2)
236 {
237     char *work;
238
239     /* ### this could be optimized by computing strlen() once and using
240        ### memcpy and pmemdup instead. but why bother? */
241
242     *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
243     *used2 = work = apr_pstrdup(pool, *used1);
244
245     /* we know the extension is 4 characters */
246     memcpy(&work[strlen(work) - 4], APR_SDBM_PAGFEXT, 4);
247 }
248
249
250 APU_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
251     "sdbm",
252
253     vt_sdbm_open,
254     vt_sdbm_close,
255     vt_sdbm_fetch,
256     vt_sdbm_store,
257     vt_sdbm_del,
258     vt_sdbm_exists,
259     vt_sdbm_firstkey,
260     vt_sdbm_nextkey,
261     vt_sdbm_freedatum,
262     vt_sdbm_usednames
263 };
264
265 #endif /* APU_HAVE_SDBM */