2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
8
* Copyright (c) 1990, 1993
9
* Margo Seltzer. All rights reserved.
12
* Copyright (c) 1990, 1993
13
* The Regents of the University of California. All rights reserved.
15
* This code is derived from software contributed to Berkeley by
18
* Redistribution and use in source and binary forms, with or without
19
* modification, are permitted provided that the following conditions
21
* 1. Redistributions of source code must retain the above copyright
22
* notice, this list of conditions and the following disclaimer.
23
* 2. Redistributions in binary form must reproduce the above copyright
24
* notice, this list of conditions and the following disclaimer in the
25
* documentation and/or other materials provided with the distribution.
26
* 3. Neither the name of the University nor the names of its contributors
27
* may be used to endorse or promote products derived from this software
28
* without specific prior written permission.
30
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
#include "db_config.h"
46
static const char revid[] = "$Id$";
49
#ifndef NO_SYSTEM_INCLUDES
50
#include <sys/types.h>
56
#define DB_DBM_HSEARCH 1
61
* This package provides dbm and ndbm compatible interfaces to DB.
63
* EXTERN: #if DB_DBM_HSEARCH != 0
65
* EXTERN: int __db_ndbm_clearerr __P((DBM *));
66
* EXTERN: void __db_ndbm_close __P((DBM *));
67
* EXTERN: int __db_ndbm_delete __P((DBM *, datum));
68
* EXTERN: int __db_ndbm_dirfno __P((DBM *));
69
* EXTERN: int __db_ndbm_error __P((DBM *));
70
* EXTERN: datum __db_ndbm_fetch __P((DBM *, datum));
71
* EXTERN: datum __db_ndbm_firstkey __P((DBM *));
72
* EXTERN: datum __db_ndbm_nextkey __P((DBM *));
73
* EXTERN: DBM *__db_ndbm_open __P((const char *, int, int));
74
* EXTERN: int __db_ndbm_pagfno __P((DBM *));
75
* EXTERN: int __db_ndbm_rdonly __P((DBM *));
76
* EXTERN: int __db_ndbm_store __P((DBM *, datum, datum, int));
78
* EXTERN: int __db_dbm_close __P((void));
79
* EXTERN: int __db_dbm_dbrdonly __P((void));
80
* EXTERN: int __db_dbm_delete __P((datum));
81
* EXTERN: int __db_dbm_dirf __P((void));
82
* EXTERN: datum __db_dbm_fetch __P((datum));
83
* EXTERN: datum __db_dbm_firstkey __P((void));
84
* EXTERN: int __db_dbm_init __P((char *));
85
* EXTERN: datum __db_dbm_nextkey __P((datum));
86
* EXTERN: int __db_dbm_pagf __P((void));
87
* EXTERN: int __db_dbm_store __P((datum, datum));
93
* The DBM routines, which call the NDBM routines.
97
static void __db_no_open __P((void));
103
if (__cur_db != NULL)
104
(void)dbm_close(__cur_db);
106
dbm_open(file, O_CREAT | O_RDWR, __db_omode("rw----"))) != NULL)
108
if ((__cur_db = dbm_open(file, O_RDONLY, 0)) != NULL)
116
if (__cur_db != NULL) {
129
if (__cur_db == NULL) {
135
return (dbm_fetch(__cur_db, key));
143
if (__cur_db == NULL) {
149
return (dbm_firstkey(__cur_db));
153
__db_dbm_nextkey(key)
158
COMPQUIET(key.dsize, 0);
160
if (__cur_db == NULL) {
166
return (dbm_nextkey(__cur_db));
173
if (__cur_db == NULL) {
177
return (dbm_delete(__cur_db, key));
181
__db_dbm_store(key, dat)
184
if (__cur_db == NULL) {
188
return (dbm_store(__cur_db, key, dat, DBM_REPLACE));
194
(void)fprintf(stderr, "dbm: no open database.\n");
198
* This package provides dbm and ndbm compatible interfaces to DB.
200
* The NDBM routines, which call the DB routines.
208
__db_ndbm_open(file, oflags, mode)
215
char path[MAXPATHLEN];
219
* Don't use sprintf(3)/snprintf(3) -- the former is dangerous, and
220
* the latter isn't standard, and we're manipulating strings handed
221
* us by the application.
223
if (strlen(file) + strlen(DBM_SUFFIX) + 1 > sizeof(path)) {
224
__os_set_errno(ENAMETOOLONG);
227
(void)strcpy(path, file);
228
(void)strcat(path, DBM_SUFFIX);
229
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
236
* The historic ndbm library corrected for opening O_WRONLY.
238
if (oflags & O_WRONLY) {
243
if ((ret = dbp->set_pagesize(dbp, 4096)) != 0 ||
244
(ret = dbp->set_h_ffactor(dbp, 40)) != 0 ||
245
(ret = dbp->set_h_nelem(dbp, 1)) != 0 ||
246
(ret = (*dbp->open)(dbp, NULL,
247
path, NULL, DB_HASH, __db_oflags(oflags), mode)) != 0) {
252
if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0) {
253
(void)dbp->close(dbp, 0);
273
(void)dbc->dbp->close(dbc->dbp, 0);
282
__db_ndbm_fetch(dbm, key)
293
memset(&_key, 0, sizeof(DBT));
294
memset(&_data, 0, sizeof(DBT));
295
_key.size = key.dsize;
296
_key.data = key.dptr;
299
* Note that we can't simply use the dbc we have to do a c_get/SET,
300
* because that cursor is the one used for sequential iteration and
301
* it has to remain stable in the face of intervening gets and puts.
303
if ((ret = dbc->dbp->get(dbc->dbp, NULL, &_key, &_data, 0)) == 0) {
304
data.dptr = _data.data;
305
data.dsize = _data.size;
309
if (ret == DB_NOTFOUND)
310
__os_set_errno(ENOENT);
313
F_SET(dbc->dbp, DB_AM_DBM_ERROR);
325
__db_ndbm_firstkey(dbm)
335
memset(&_key, 0, sizeof(DBT));
336
memset(&_data, 0, sizeof(DBT));
338
if ((ret = dbc->c_get(dbc, &_key, &_data, DB_FIRST)) == 0) {
339
key.dptr = _key.data;
340
key.dsize = _key.size;
344
if (ret == DB_NOTFOUND)
345
__os_set_errno(ENOENT);
348
F_SET(dbc->dbp, DB_AM_DBM_ERROR);
360
__db_ndbm_nextkey(dbm)
370
memset(&_key, 0, sizeof(DBT));
371
memset(&_data, 0, sizeof(DBT));
373
if ((ret = dbc->c_get(dbc, &_key, &_data, DB_NEXT)) == 0) {
374
key.dptr = _key.data;
375
key.dsize = _key.size;
379
if (ret == DB_NOTFOUND)
380
__os_set_errno(ENOENT);
383
F_SET(dbc->dbp, DB_AM_DBM_ERROR);
395
__db_ndbm_delete(dbm, key)
405
memset(&_key, 0, sizeof(DBT));
406
_key.data = key.dptr;
407
_key.size = key.dsize;
409
if ((ret = dbc->dbp->del(dbc->dbp, NULL, &_key, 0)) == 0)
412
if (ret == DB_NOTFOUND)
413
__os_set_errno(ENOENT);
416
F_SET(dbc->dbp, DB_AM_DBM_ERROR);
425
* 1 if DBM_INSERT and entry exists
428
__db_ndbm_store(dbm, key, data, flags)
439
memset(&_key, 0, sizeof(DBT));
440
_key.data = key.dptr;
441
_key.size = key.dsize;
443
memset(&_data, 0, sizeof(DBT));
444
_data.data = data.dptr;
445
_data.size = data.dsize;
447
if ((ret = dbc->dbp->put(dbc->dbp, NULL,
448
&_key, &_data, flags == DBM_INSERT ? DB_NOOVERWRITE : 0)) == 0)
451
if (ret == DB_KEYEXIST)
455
F_SET(dbc->dbp, DB_AM_DBM_ERROR);
467
return (F_ISSET(dbc->dbp, DB_AM_DBM_ERROR));
471
__db_ndbm_clearerr(dbm)
478
F_CLR(dbc->dbp, DB_AM_DBM_ERROR);
488
__db_ndbm_rdonly(dbm)
495
return (F_ISSET(dbc->dbp, DB_AM_RDONLY) ? 1 : 0);
500
* We only have a single file descriptor that we can return, not two. Return
501
* the same one for both files. Hopefully, the user is using it for locking
502
* and picked one to use at random.
505
__db_ndbm_dirfno(dbm)
508
return (dbm_pagfno(dbm));
512
__db_ndbm_pagfno(dbm)
520
(void)dbc->dbp->fd(dbc->dbp, &fd);