Apache HTTPD
apr_dbm_ndbm.c
Go to the documentation of this file.
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
19#if APR_HAVE_STDLIB_H
20#include <stdlib.h> /* for free() */
21#endif
22
23#include "apu_config.h"
24#include "apu.h"
25
26#if APU_HAVE_NDBM
27#include "apr_dbm_private.h"
28
29#include <ndbm.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33
34#define APR_DBM_DBMODE_RO O_RDONLY
35#define APR_DBM_DBMODE_RW O_RDWR
36#define APR_DBM_DBMODE_RWCREATE (O_RDWR|O_CREAT)
37#define APR_DBM_DBMODE_RWTRUNC (O_RDWR|O_CREAT|O_TRUNC)
38
39/* map a NDBM error to an apr_status_t */
40static apr_status_t ndbm2s(int ndbmerr)
41{
42 if (ndbmerr == -1) {
43 /* ### need to fix this */
44 return APR_EGENERAL;
45 }
46
47 return APR_SUCCESS;
48}
49
51{
53
54 /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */
55
56 dbm->errmsg = NULL;
57 if (dbm_error((DBM*)dbm->file)) {
58 dbm->errmsg = NULL;
59 rv = APR_EGENERAL; /* ### need something better */
60 }
61
62 /* captured it. clear it now. */
63 dbm_clearerr((DBM*)dbm->file);
64
65 return rv;
66}
67
68/* --------------------------------------------------------------------------
69**
70** DEFINE THE VTABLE FUNCTIONS FOR NDBM
71*/
72
73static apr_status_t vt_ndbm_open(apr_dbm_t **pdb, const char *pathname,
76{
77 DBM *file;
78 int dbmode;
79
80 *pdb = NULL;
81
82 switch (mode) {
85 break;
88 break;
91 break;
92 case APR_DBM_RWTRUNC:
94 break;
95 default:
96 return APR_EINVAL;
97 }
98
99 {
101 if (file == NULL)
102 return APR_EGENERAL; /* ### need a better error */
103 }
104
105 /* we have an open database... return it */
106 *pdb = apr_pcalloc(pool, sizeof(**pdb));
107 (*pdb)->pool = pool;
108 (*pdb)->type = &apr_dbm_type_ndbm;
109 (*pdb)->file = file;
110
111 /* ### register a cleanup to close the DBM? */
112
113 return APR_SUCCESS;
114}
115
116static void vt_ndbm_close(apr_dbm_t *dbm)
117{
118 dbm_close(dbm->file);
119}
120
123{
124 datum kd, rd;
125
126 kd.dptr = key.dptr;
127 kd.dsize = key.dsize;
128
129 rd = dbm_fetch(dbm->file, kd);
130
131 pvalue->dptr = rd.dptr;
132 pvalue->dsize = rd.dsize;
133
134 /* store the error info into DBM, and return a status code. Also, note
135 that *pvalue should have been cleared on error. */
136 return set_error(dbm, APR_SUCCESS);
137}
138
141{
142 int rc;
143 datum kd, vd;
144
145 kd.dptr = key.dptr;
146 kd.dsize = key.dsize;
147
148 vd.dptr = value.dptr;
149 vd.dsize = value.dsize;
150
151 rc = dbm_store(dbm->file, kd, vd, DBM_REPLACE);
152
153 /* store any error info into DBM, and return a status code. */
154 return set_error(dbm, ndbm2s(rc));
155}
156
158{
159 int rc;
160 datum kd;
161
162 kd.dptr = key.dptr;
163 kd.dsize = key.dsize;
164
165 rc = dbm_delete(dbm->file, kd);
166
167 /* store any error info into DBM, and return a status code. */
168 return set_error(dbm, ndbm2s(rc));
169}
170
172{
173 datum kd, rd;
174
175 kd.dptr = key.dptr;
176 kd.dsize = key.dsize;
177
178 rd = dbm_fetch(dbm->file, kd);
179
180 return rd.dptr != NULL;
181}
182
184{
185 datum rd;
186
187 rd = dbm_firstkey(dbm->file);
188
189 pkey->dptr = rd.dptr;
190 pkey->dsize = rd.dsize;
191
192 /* store any error info into DBM, and return a status code. */
193 return set_error(dbm, APR_SUCCESS);
194}
195
197{
198 datum kd, rd;
199
200 kd.dptr = pkey->dptr;
201 kd.dsize = pkey->dsize;
202
203 rd = dbm_nextkey(dbm->file);
204
205 pkey->dptr = rd.dptr;
206 pkey->dsize = rd.dsize;
207
208 /* store any error info into DBM, and return a status code. */
209 return set_error(dbm, APR_SUCCESS);
210}
211
213{
214 /* nothing to do */
215}
216
217static void vt_ndbm_usednames(apr_pool_t *pool, const char *pathname,
218 const char **used1, const char **used2)
219{
221 *used2 = NULL;
222}
223
225 "ndbm",
236};
237
238#endif /* APU_HAVE_NDBM */
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_ndbm
APR Strings library.
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_EINVAL
Definition apr_errno.h:711
apr_dbd_transaction_t int mode
Definition apr_dbd.h:261
apr_datum_t * pkey
Definition apr_dbm.h:158
apr_datum_t apr_datum_t * pvalue
Definition apr_dbm.h:128
#define APR_DBM_RWTRUNC
Definition apr_dbm.h:59
#define APR_DBM_RWCREATE
Definition apr_dbm.h:58
#define APR_DBM_READONLY
Definition apr_dbm.h:56
const char const char const char const char ** used2
Definition apr_dbm.h:203
const char const char const char ** used1
Definition apr_dbm.h:202
#define APR_DBM_READWRITE
Definition apr_dbm.h:57
const char const char * pathname
Definition apr_dbm.h:201
apr_redis_t * rc
Definition apr_redis.h:173
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
const char * value
Definition apr_env.h:51
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_int32_t apr_fileperms_t
const char * key
void * data
const char apr_file_t * file
const char apr_int32_t apr_fileperms_t perm
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
return NULL
Definition mod_so.c:359
apr_size_t dsize
Definition apr_dbm.h:52
char * dptr
Definition apr_dbm.h:50