ndbm_wrap.c revision 1.1
1/*	$NetBSD: ndbm_wrap.c,v 1.1 2011/04/13 18:15:42 elric Exp $	*/
2
3/*
4 * Copyright (c) 2002 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <config.h>
37
38#include "ndbm_wrap.h"
39#if defined(HAVE_DBHEADER)
40#include <db.h>
41#elif defined(HAVE_DB5_DB_H)
42#include <db5/db.h>
43#elif defined(HAVE_DB4_DB_H)
44#include <db4/db.h>
45#elif defined(HAVE_DB3_DB_H)
46#include <db3/db.h>
47#else
48#include <db.h>
49#endif
50
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <fcntl.h>
55
56/* XXX undefine open so this works on Solaris with large file support */
57#undef open
58
59#define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
60#define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
61#define RETURN(X) return ((X) == 0) ? 0 : -1
62
63#ifdef HAVE_DB3
64static DBC *cursor;
65#endif
66
67#define D(X) ((DB*)(X))
68
69ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
70dbm_close (DBM *db)
71{
72#ifdef HAVE_DB3
73    D(db)->close(D(db), 0);
74    cursor = NULL;
75#else
76    D(db)->close(D(db));
77#endif
78}
79
80ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
81dbm_delete (DBM *db, datum dkey)
82{
83    DBT key;
84    DATUM2DBT(&dkey, &key);
85#ifdef HAVE_DB3
86    RETURN(D(db)->del(D(db), NULL, &key, 0));
87#else
88    RETURN(D(db)->del(D(db), &key, 0));
89#endif
90}
91
92datum
93dbm_fetch (DBM *db, datum dkey)
94{
95    datum dvalue;
96    DBT key, value;
97    DATUM2DBT(&dkey, &key);
98    if(D(db)->get(D(db),
99#ifdef HAVE_DB3
100	       NULL,
101#endif
102	       &key, &value, 0) != 0) {
103	dvalue.dptr = NULL;
104	dvalue.dsize = 0;
105    }
106    else
107	DBT2DATUM(&value, &dvalue);
108
109    return dvalue;
110}
111
112static datum
113dbm_get (DB *db, int flags)
114{
115    DBT key, value;
116    datum datum;
117#ifdef HAVE_DB3
118    if(cursor == NULL)
119	db->cursor(db, NULL, &cursor, 0);
120    if(cursor->c_get(cursor, &key, &value, flags) != 0) {
121	datum.dptr = NULL;
122	datum.dsize = 0;
123    } else
124	DBT2DATUM(&value, &datum);
125#else
126    db->seq(db, &key, &value, flags);
127#endif
128    return datum;
129}
130
131#ifndef DB_FIRST
132#define DB_FIRST	R_FIRST
133#define DB_NEXT		R_NEXT
134#define DB_NOOVERWRITE	R_NOOVERWRITE
135#define DB_KEYEXIST	1
136#endif
137
138ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
139dbm_firstkey (DBM *db)
140{
141    return dbm_get(D(db), DB_FIRST);
142}
143
144ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
145dbm_nextkey (DBM *db)
146{
147    return dbm_get(D(db), DB_NEXT);
148}
149
150ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL
151dbm_open (const char *file, int flags, mode_t mode)
152{
153    DB *db;
154    int myflags = 0;
155    char *fn = malloc(strlen(file) + 4);
156    if(fn == NULL)
157	return NULL;
158    strcpy(fn, file);
159    strcat(fn, ".db");
160#ifdef HAVE_DB3
161    if (flags & O_CREAT)
162	myflags |= DB_CREATE;
163
164    if (flags & O_EXCL)
165	myflags |= DB_EXCL;
166
167    if (flags & O_RDONLY)
168	myflags |= DB_RDONLY;
169
170    if (flags & O_TRUNC)
171	myflags |= DB_TRUNCATE;
172    if(db_create(&db, NULL, 0) != 0) {
173	free(fn);
174	return NULL;
175    }
176
177#if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
178    if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) {
179#else
180    if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) {
181#endif
182	free(fn);
183	db->close(db, 0);
184	return NULL;
185    }
186#else
187    db = dbopen(fn, flags, mode, DB_BTREE, NULL);
188#endif
189    free(fn);
190    return (DBM*)db;
191}
192
193ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
194dbm_store (DBM *db, datum dkey, datum dvalue, int flags)
195{
196    int ret;
197    DBT key, value;
198    int myflags = 0;
199    if((flags & DBM_REPLACE) == 0)
200	myflags |= DB_NOOVERWRITE;
201    DATUM2DBT(&dkey, &key);
202    DATUM2DBT(&dvalue, &value);
203    ret = D(db)->put(D(db),
204#ifdef HAVE_DB3
205		     NULL,
206#endif
207&key, &value, myflags);
208    if(ret == DB_KEYEXIST)
209	return 1;
210    RETURN(ret);
211}
212
213ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
214dbm_error (DBM *db)
215{
216    return 0;
217}
218
219ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
220dbm_clearerr (DBM *db)
221{
222    return 0;
223}
224
225