File: bdbobj.c

package info (click to toggle)
maildrop 2.9.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,104 kB
  • sloc: ansic: 26,822; cpp: 9,085; sh: 4,868; makefile: 753; perl: 94
file content (294 lines) | stat: -rw-r--r-- 4,857 bytes parent folder | download | duplicates (34)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
** Copyright 1998 - 2003 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif

#include	<fcntl.h>
#include	<string.h>
#include	<stdlib.h>
#if	HAVE_FCNTL_H
#include	<fcntl.h>
#endif
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"bdbobj.h"

void bdbobj_init(struct bdbobj *obj)
{
	obj->has_dbf=0;

#if	DB_VERSION_MAJOR >= 2
	obj->has_dbc=0;
#endif
}

void bdbobj_close(struct bdbobj *obj)
{
#if	DB_VERSION_MAJOR >= 2
	if (obj->has_dbc)
	{
		(*obj->dbc->c_close)(obj->dbc);
		obj->has_dbc=0;
	}
#endif
	if ( obj->has_dbf )
	{
#if	DB_VERSION_MAJOR < 2
		(*obj->dbf->close)(obj->dbf);
#else
		(*obj->dbf->close)(obj->dbf, 0);
#endif
		obj->has_dbf=0;
	}
}

int bdbobj_open(struct bdbobj *obj, const char *filename, const char *modestr)
{
#if	DB_VERSION_MAJOR < 2

int	flags=O_RDONLY;

#else

int	flags=DB_RDONLY;

#endif

DBTYPE	dbtype=DB_HASH;

	for ( ; *modestr; modestr++)
		switch (*modestr)	{
		case 'c':
		case 'C':
#if	DB_VERSION_MAJOR < 2
			flags=O_RDWR|O_CREAT;
#else
			flags=DB_CREATE;
#endif
			break;
		case 'w':
		case 'W':
#if	DB_VERSION_MAJOR < 2
			flags=O_RDWR;
#else
			flags=0;
#endif
			break;
		case 'n':
		case 'N':
#if	DB_VERSION_MAJOR < 2
			flags=O_RDWR|O_CREAT|O_TRUNC;
#else
			flags=DB_CREATE|DB_TRUNCATE;
#endif

			break;

		case 'b':
		case 'B':
			dbtype=DB_BTREE;
			break;

		case 'e':
		case 'E':
			dbtype=DB_RECNO;
			break;
		}

	bdbobj_close(obj);

#if DB_VERSION_MAJOR < 3
#if DB_VERSION_MAJOR < 2
	if ( (obj->dbf=dbopen(filename, flags, 0664, dbtype, 0)) != 0)
#else
	if ( db_open(filename, dbtype, flags, 0664, 0, 0, &obj->dbf) == 0)
#endif
#else
	obj->dbf=0;

#define DB_40 0

#if DB_VERSION_MAJOR == 4
#if DB_VERSION_MINOR == 0

#undef DB_40
#define DB_40 1

#endif
#endif

#if DB_VERSION_MAJOR == 3
#undef DB_40
#define DB_40 1
#endif

	if (db_create(&obj->dbf, NULL, 0) == 0)
	{
		if ( (*obj->dbf->open)(obj->dbf,

#if DB_40

#else
				       NULL,
#endif

				       filename, NULL,
				       dbtype, flags, 0664))
		{
			(*obj->dbf->close)(obj->dbf, DB_NOSYNC);
			obj->dbf=0;
		}
	}

	if (obj->dbf)
#endif
	{
#ifdef  FD_CLOEXEC

#if DB_VERSION_MAJOR < 2
        int     fd=(*obj->dbf->fd)(obj->dbf);
#else
	int	fd;

		if ((*obj->dbf->fd)(obj->dbf, &fd))
			fd= -1;
#endif

                if (fd >= 0)    fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif


		obj->has_dbf=1;
		return (0);
	}
	return (-1);
}

int bdbobj_store(struct bdbobj *obj, const char *key, size_t keylen,
		const char *data,
		size_t	datalen,
		const	char *mode)
{
DBT dkey, dval;

	memset(&dkey, 0, sizeof(dkey));
	memset(&dval, 0, sizeof(dval));

	dkey.data=(void *)key;
	dkey.size=keylen;
	dval.data=(void *)data;
	dval.size=datalen;

#if	DB_VERSION_MAJOR < 2
	return (obj->has_dbf ? (*obj->dbf->put)(obj->dbf, &dkey, &dval, (
			*mode == 'i' || *mode == 'I' ?  R_NOOVERWRITE:0)):-1);
#else
	return (obj->has_dbf ? (*obj->dbf->put)(obj->dbf, 0, &dkey, &dval, (
			*mode == 'i' || *mode == 'I' ? DB_NOOVERWRITE:0)):-1);
#endif
}

static char *doquery(struct bdbobj *obj,
	const char *, size_t, size_t *, const char *);

char	*bdbobj_fetch(struct bdbobj *obj, const char *key, size_t keylen,
		size_t *datalen, const char *options)
{
char	*p=doquery(obj, key, keylen, datalen, options);
char	*q;

	if (!p)	return (0);

	q=(char *)malloc(*datalen);

	if (!q)	return (0);

	memcpy(q, p, *datalen);
	return (q);
}

char    *dofetch(struct bdbobj *, const char *, size_t, size_t *);

static char *doquery(struct bdbobj *obj, const char *key, size_t keylen,
	size_t *datalen, const char *options)
{
char	*p;

	for (;;)
	{
		if ((p=dofetch(obj, key, keylen, datalen)) != 0)
			return (p);
		if (!options)	break;
		if (*options == 'I')
		{
			while (keylen && key[--keylen] != '.')
				;
			if (!keylen)	break;
			continue;
		}
		if (*options == 'D')
		{
		size_t	i;

			for (i=0; i<keylen; i++)
				if (key[i] == '@') { ++i; break; }
			if (i < keylen)
			{
				if ((p=dofetch(obj, key, i, datalen)) != 0)
					return (p);
				key += i;
				keylen -= i;
				continue;
			}

			for (i=0; i<keylen; i++)
				if (key[i] == '.') { ++i; break; }
			if (i < keylen)
			{
				key += i;
				keylen -= i;
				continue;
			}
			break;
		}
		break;
	}
	return (0);
}

char	*dofetch(struct bdbobj *obj, const char *key, size_t keylen,
	size_t *datalen)
{
DBT	dkey, val;

	if (!obj->has_dbf)	return (0);

	memset(&dkey, 0, sizeof(dkey));
	memset(&val, 0, sizeof(val));

	dkey.data=(void *)key;
	dkey.size=keylen;

#if	DB_VERSION_MAJOR < 2
	if ( (*obj->dbf->get)(obj->dbf, &dkey, &val, 0))	return (0);
#else
	if ( (*obj->dbf->get)(obj->dbf, 0, &dkey, &val, 0)) return (0);
#endif

	*datalen=val.size;
	return ((char *)val.data);
}

int	bdbobj_exists(struct bdbobj *obj, const char *key, size_t keylen)
{
size_t	datalen;
char	*p=doquery(obj, key, keylen, &datalen, 0);

	return (p ? 1:0);
}