berkeley.c
8.15 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2016 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <mailutils/types.h>
#include <mailutils/dbm.h>
#include <mailutils/util.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/stream.h>
#include <mailutils/locker.h>
#include "mudbm.h"
#ifndef O_EXCL
# define O_EXCL 0
#endif
#if defined(WITH_BDB)
#include <db.h>
struct bdb_file
{
DB *db;
DBC *dbc;
mu_locker_t locker;
};
static int
_bdb_file_safety (mu_dbm_file_t db, int mode, uid_t owner)
{
return mu_file_safety_check (db->db_name, mode, owner, NULL);
}
static int
_bdb_get_fd (mu_dbm_file_t db, int *pag, int *dir)
{
struct bdb_file *bdb_file = db->db_descr;
int rc = bdb_file->db->fd (bdb_file->db, pag);
if (rc)
{
db->db_errno.n = rc;
return MU_ERR_FAILURE;
}
if (dir)
*dir = *pag;
return 0;
}
static int
do_bdb_open (mu_dbm_file_t mdb, int flags, int mode)
{
struct bdb_file *bdb_file = mdb->db_descr;
int f, rc, locker_flags;
enum mu_locker_mode locker_mode;
int tfd = -1;
switch (flags)
{
case MU_STREAM_CREAT:
f = DB_CREATE|DB_TRUNCATE;
locker_mode = mu_lck_exc;
break;
case MU_STREAM_READ:
f = DB_RDONLY;
locker_mode = mu_lck_shr;
break;
case MU_STREAM_RDWR:
f = DB_CREATE;
locker_mode = mu_lck_exc;
break;
default:
return EINVAL;
}
#ifdef DB_FCNTL_LOCKING
f |= DB_FCNTL_LOCKING;
locker_flags = MU_LOCKER_KERNEL;
#else
locker_flags = 0;
#endif
rc = mu_locker_create (&bdb_file->locker, mdb->db_name,
locker_flags|MU_LOCKER_RETRY);
if (rc)
return rc;
if (access (mdb->db_name, R_OK) && errno == ENOENT)
{
tfd = open (mdb->db_name, O_CREAT|O_RDONLY|O_EXCL,
mu_safety_criteria_to_file_mode (mdb->db_safety_flags));
if (tfd == -1)
{
mu_locker_destroy (&bdb_file->locker);
return errno;
}
}
rc = mu_locker_lock_mode (bdb_file->locker, locker_mode);
if (tfd != -1)
close (tfd);
switch (rc)
{
case 0:
break;
case EACCES:
mu_locker_destroy (&bdb_file->locker);
break;
default:
return rc;
}
rc = db_create (&bdb_file->db, NULL, 0);
if (rc != 0 || bdb_file->db == NULL)
return MU_ERR_FAILURE;
#if DB_VERSION_MAJOR == 3
rc = bdb_file->db->open (bdb_file->db, mdb->db_name, NULL, DB_HASH, f, mode);
#else
rc = bdb_file->db->open (bdb_file->db, NULL, mdb->db_name, NULL, DB_HASH,
f, mode);
#endif
if (rc)
return MU_ERR_FAILURE;
return 0;
}
static int
_bdb_close (mu_dbm_file_t db)
{
if (db->db_descr)
{
struct bdb_file *bdb_file = db->db_descr;
if (bdb_file->db)
bdb_file->db->close (bdb_file->db, 0);
if (bdb_file->locker)
{
mu_locker_unlock (bdb_file->locker);
mu_locker_destroy (&bdb_file->locker);
}
free (bdb_file);
db->db_descr = NULL;
}
return 0;
}
static int
_bdb_open (mu_dbm_file_t mdb, int flags, int mode)
{
int rc;
struct bdb_file *bdb_file;
bdb_file = calloc (1, sizeof *bdb_file);
if (!bdb_file)
return ENOMEM;
mdb->db_descr = bdb_file;
rc = do_bdb_open (mdb, flags, mode);
if (rc)
_bdb_close (mdb);
return rc;
}
static int
_bdb_fetch (mu_dbm_file_t db, struct mu_dbm_datum const *key,
struct mu_dbm_datum *ret)
{
DBT keydat, content;
struct bdb_file *bdb_file = db->db_descr;
int rc;
memset (&keydat, 0, sizeof keydat);
keydat.data = key->mu_dptr;
keydat.size = key->mu_dsize;
memset (&content, 0, sizeof content);
content.flags = DB_DBT_MALLOC;
rc = bdb_file->db->get (bdb_file->db, NULL, &keydat, &content, 0);
mu_dbm_datum_free (ret);
switch (rc)
{
case 0:
ret->mu_dptr = content.data;
ret->mu_dsize = content.size;
ret->mu_sys = db->db_sys;
break;
case DB_NOTFOUND:
db->db_errno.n = rc;
rc = MU_ERR_NOENT;
break;
default:
db->db_errno.n = rc;
rc = MU_ERR_FAILURE;
}
return rc;
}
static int
_bdb_store (mu_dbm_file_t db,
struct mu_dbm_datum const *key,
struct mu_dbm_datum const *contents,
int replace)
{
struct bdb_file *bdb_file = db->db_descr;
int rc;
DBT keydat, condat;
memset (&keydat, 0, sizeof keydat);
keydat.data = key->mu_dptr;
keydat.size = key->mu_dsize;
memset (&condat, 0, sizeof condat);
condat.data = contents->mu_dptr;
condat.size = contents->mu_dsize;
rc = bdb_file->db->put (bdb_file->db, NULL, &keydat, &condat,
replace ? 0 : DB_NOOVERWRITE);
db->db_errno.n = rc;
switch (rc)
{
case 0:
break;
case DB_KEYEXIST:
rc = MU_ERR_EXISTS;
break;
default:
rc = MU_ERR_FAILURE;
break;
}
return rc;
}
static int
_bdb_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
{
DBT keydat;
struct bdb_file *bdb_file = db->db_descr;
int rc;
memset (&keydat, 0, sizeof keydat);
keydat.data = key->mu_dptr;
keydat.size = key->mu_dsize;
rc = bdb_file->db->del (bdb_file->db, NULL, &keydat, 0);
switch (rc)
{
case 0:
break;
case DB_NOTFOUND:
db->db_errno.n = rc;
rc = MU_ERR_NOENT;
break;
default:
db->db_errno.n = rc;
rc = MU_ERR_FAILURE;
}
return rc;
}
static int
_bdb_firstkey (mu_dbm_file_t db, struct mu_dbm_datum *ret)
{
struct bdb_file *bdb_file = db->db_descr;
int rc;
DBT key, dbt;
if (!bdb_file->dbc)
{
rc = bdb_file->db->cursor (bdb_file->db, NULL, &bdb_file->dbc
BDB2_CURSOR_LASTARG);
if (rc)
{
db->db_errno.n = rc;
return MU_ERR_FAILURE;
}
}
memset (&key, 0, sizeof key);
key.flags = DB_DBT_MALLOC;
memset (&dbt, 0, sizeof dbt);
dbt.flags = DB_DBT_MALLOC;
rc = bdb_file->dbc->c_get (bdb_file->dbc, &key, &dbt, DB_FIRST);
mu_dbm_datum_free (ret);
switch (rc)
{
case 0:
free (dbt.data); /* FIXME: cache it for the eventual fetch that can
follow */
ret->mu_dptr = key.data;
ret->mu_dsize = key.size;
ret->mu_sys = db->db_sys;
break;
case DB_NOTFOUND:
db->db_errno.n = rc;
rc = MU_ERR_NOENT;
break;
default:
db->db_errno.n = rc;
rc = MU_ERR_FAILURE;
}
return rc;
}
static int
_bdb_nextkey (mu_dbm_file_t db, struct mu_dbm_datum *ret)
{
struct bdb_file *bdb_file = db->db_descr;
int rc;
DBT key, dbt;
if (!bdb_file->dbc)
return MU_ERR_SEQ;
memset (&key, 0, sizeof key);
key.flags = DB_DBT_MALLOC;
memset (&dbt, 0, sizeof dbt);
dbt.flags = DB_DBT_MALLOC;
rc = bdb_file->dbc->c_get (bdb_file->dbc, &key, &dbt, DB_NEXT);
mu_dbm_datum_free (ret);
switch (rc)
{
case 0:
free (dbt.data); /* FIXME: cache it for the eventual fetch that can
follow */
ret->mu_dptr = key.data;
ret->mu_dsize = key.size;
ret->mu_sys = db->db_sys;
break;
case DB_NOTFOUND:
db->db_errno.n = rc;
rc = MU_ERR_NOENT;
break;
default:
db->db_errno.n = rc;
rc = MU_ERR_FAILURE;
}
return rc;
}
static void
_bdb_datum_free (struct mu_dbm_datum *datum)
{
free (datum->mu_dptr);
}
static char const *
_bdb_strerror (mu_dbm_file_t db)
{
return db_strerror (db->db_errno.n);
}
struct mu_dbm_impl _mu_dbm_bdb = {
"bdb",
_bdb_file_safety,
_bdb_get_fd,
_bdb_open,
_bdb_close,
_bdb_fetch,
_bdb_store,
_bdb_delete,
_bdb_firstkey,
_bdb_nextkey,
_bdb_datum_free,
_bdb_strerror
};
#endif