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
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_config.h"
23#include "apu.h"
24
25#if APU_HAVE_SDBM
26
27#include "apr_dbm_private.h"
28#include "apr_sdbm.h"
29
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 | \
34                                 APR_FOPEN_TRUNCATE)
35
36static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said)
37{
38    dbm->errcode = dbm_said;
39
40    if (dbm_said != APR_SUCCESS) {
41        dbm->errmsg = apr_psprintf(dbm->pool, "%pm", &dbm_said);
42    } else {
43        dbm->errmsg = NULL;
44    }
45
46    return dbm_said;
47}
48
49/* --------------------------------------------------------------------------
50**
51** DEFINE THE VTABLE FUNCTIONS FOR SDBM
52*/
53
54static apr_status_t vt_sdbm_open(apr_dbm_t **pdb, const char *pathname,
55                                 apr_int32_t mode, apr_fileperms_t perm,
56                                 apr_pool_t *pool)
57{
58    apr_sdbm_t *file;
59    int dbmode;
60
61    *pdb = NULL;
62
63    switch (mode) {
64    case APR_DBM_READONLY:
65        dbmode = APR_DBM_DBMODE_RO;
66        break;
67    case APR_DBM_READWRITE:
68        dbmode = APR_DBM_DBMODE_RW;
69        break;
70    case APR_DBM_RWCREATE:
71        dbmode = APR_DBM_DBMODE_RWCREATE;
72        break;
73    case APR_DBM_RWTRUNC:
74        dbmode = APR_DBM_DBMODE_RWTRUNC;
75        break;
76    default:
77        return APR_EINVAL;
78    }
79
80    {
81        apr_status_t rv;
82
83        rv = apr_sdbm_open(&file, pathname, dbmode, perm, pool);
84        if (rv != APR_SUCCESS)
85            return rv;
86    }
87
88    /* we have an open database... return it */
89    *pdb = apr_pcalloc(pool, sizeof(**pdb));
90    (*pdb)->pool = pool;
91    (*pdb)->type = &apr_dbm_type_sdbm;
92    (*pdb)->file = file;
93
94    /* ### register a cleanup to close the DBM? */
95
96    return APR_SUCCESS;
97}
98
99static void vt_sdbm_close(apr_dbm_t *dbm)
100{
101    apr_sdbm_close(dbm->file);
102}
103
104static apr_status_t vt_sdbm_fetch(apr_dbm_t *dbm, apr_datum_t key,
105                                  apr_datum_t *pvalue)
106{
107    apr_status_t rv;
108    apr_sdbm_datum_t kd, rd;
109
110    kd.dptr = key.dptr;
111    kd.dsize = (int)key.dsize;
112
113    rv = apr_sdbm_fetch(dbm->file, &rd, kd);
114
115    pvalue->dptr = rd.dptr;
116    pvalue->dsize = rd.dsize;
117
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);
121}
122
123static apr_status_t vt_sdbm_store(apr_dbm_t *dbm, apr_datum_t key,
124                                  apr_datum_t value)
125{
126    apr_status_t rv;
127    apr_sdbm_datum_t kd, vd;
128
129    kd.dptr = key.dptr;
130    kd.dsize = (int)key.dsize;
131
132    vd.dptr = value.dptr;
133    vd.dsize = (int)value.dsize;
134
135    rv = apr_sdbm_store(dbm->file, kd, vd, APR_SDBM_REPLACE);
136
137    /* store any error info into DBM, and return a status code. */
138    return set_error(dbm, rv);
139}
140
141static apr_status_t vt_sdbm_del(apr_dbm_t *dbm, apr_datum_t key)
142{
143    apr_status_t rv;
144    apr_sdbm_datum_t kd;
145
146    kd.dptr = key.dptr;
147    kd.dsize = (int)key.dsize;
148
149    rv = apr_sdbm_delete(dbm->file, kd);
150
151    /* store any error info into DBM, and return a status code. */
152    return set_error(dbm, rv);
153}
154
155static int vt_sdbm_exists(apr_dbm_t *dbm, apr_datum_t key)
156{
157    int exists;
158    apr_sdbm_datum_t vd, kd;
159
160    kd.dptr = key.dptr;
161    kd.dsize = (int)key.dsize;
162
163    if (apr_sdbm_fetch(dbm->file, &vd, kd) != APR_SUCCESS)
164        exists = 0;
165    else
166        exists = vd.dptr != NULL;
167
168    return exists;
169}
170
171static apr_status_t vt_sdbm_firstkey(apr_dbm_t *dbm, apr_datum_t *pkey)
172{
173    apr_status_t rv;
174    apr_sdbm_datum_t rd;
175
176    rv = apr_sdbm_firstkey(dbm->file, &rd);
177
178    pkey->dptr = rd.dptr;
179    pkey->dsize = rd.dsize;
180
181    /* store any error info into DBM, and return a status code. */
182    return set_error(dbm, rv);
183}
184
185static apr_status_t vt_sdbm_nextkey(apr_dbm_t *dbm, apr_datum_t *pkey)
186{
187    apr_sdbm_datum_t rd;
188
189    (void)apr_sdbm_nextkey(dbm->file, &rd);
190
191    pkey->dptr = rd.dptr;
192    pkey->dsize = rd.dsize;
193
194    /* store any error info into DBM, and return a status code. */
195    return set_error(dbm, APR_SUCCESS);
196}
197
198static void vt_sdbm_freedatum(apr_dbm_t *dbm, apr_datum_t data)
199{
200}
201
202static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
203                              const char **used1, const char **used2)
204{
205    *used1 = apr_pstrcat(pool, pathname, APR_SDBM_DIRFEXT, NULL);
206    *used2 = apr_pstrcat(pool, pathname, APR_SDBM_PAGFEXT, NULL);
207}
208
209APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
210    "sdbm",
211    vt_sdbm_open,
212    vt_sdbm_close,
213    vt_sdbm_fetch,
214    vt_sdbm_store,
215    vt_sdbm_del,
216    vt_sdbm_exists,
217    vt_sdbm_firstkey,
218    vt_sdbm_nextkey,
219    vt_sdbm_freedatum,
220    vt_sdbm_usednames
221};
222
223#endif /* APU_HAVE_SDBM */
224