xref: /freebsd/crypto/heimdal/lib/roken/ndbm_wrap.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
18373020dSJacques Vidrine /*
2*ae771770SStanislav Sedov  * Copyright (c) 2002 Kungliga Tekniska Högskolan
38373020dSJacques Vidrine  * (Royal Institute of Technology, Stockholm, Sweden).
48373020dSJacques Vidrine  * All rights reserved.
58373020dSJacques Vidrine  *
68373020dSJacques Vidrine  * Redistribution and use in source and binary forms, with or without
78373020dSJacques Vidrine  * modification, are permitted provided that the following conditions
88373020dSJacques Vidrine  * are met:
98373020dSJacques Vidrine  *
108373020dSJacques Vidrine  * 1. Redistributions of source code must retain the above copyright
118373020dSJacques Vidrine  *    notice, this list of conditions and the following disclaimer.
128373020dSJacques Vidrine  *
138373020dSJacques Vidrine  * 2. Redistributions in binary form must reproduce the above copyright
148373020dSJacques Vidrine  *    notice, this list of conditions and the following disclaimer in the
158373020dSJacques Vidrine  *    documentation and/or other materials provided with the distribution.
168373020dSJacques Vidrine  *
178373020dSJacques Vidrine  * 3. Neither the name of the Institute nor the names of its contributors
188373020dSJacques Vidrine  *    may be used to endorse or promote products derived from this software
198373020dSJacques Vidrine  *    without specific prior written permission.
208373020dSJacques Vidrine  *
218373020dSJacques Vidrine  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
228373020dSJacques Vidrine  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238373020dSJacques Vidrine  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248373020dSJacques Vidrine  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
258373020dSJacques Vidrine  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268373020dSJacques Vidrine  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278373020dSJacques Vidrine  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288373020dSJacques Vidrine  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298373020dSJacques Vidrine  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308373020dSJacques Vidrine  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318373020dSJacques Vidrine  * SUCH DAMAGE.
328373020dSJacques Vidrine  */
338373020dSJacques Vidrine 
348373020dSJacques Vidrine #include <config.h>
358373020dSJacques Vidrine 
368373020dSJacques Vidrine #include "ndbm_wrap.h"
37*ae771770SStanislav Sedov #if defined(HAVE_DBHEADER)
38*ae771770SStanislav Sedov #include <db.h>
39*ae771770SStanislav Sedov #elif defined(HAVE_DB5_DB_H)
40*ae771770SStanislav Sedov #include <db5/db.h>
41*ae771770SStanislav Sedov #elif defined(HAVE_DB4_DB_H)
428373020dSJacques Vidrine #include <db4/db.h>
438373020dSJacques Vidrine #elif defined(HAVE_DB3_DB_H)
448373020dSJacques Vidrine #include <db3/db.h>
458373020dSJacques Vidrine #else
468373020dSJacques Vidrine #include <db.h>
478373020dSJacques Vidrine #endif
488373020dSJacques Vidrine 
498373020dSJacques Vidrine #include <stdio.h>
508373020dSJacques Vidrine #include <stdlib.h>
518373020dSJacques Vidrine #include <string.h>
528373020dSJacques Vidrine #include <fcntl.h>
538373020dSJacques Vidrine 
54c19800e8SDoug Rabson /* XXX undefine open so this works on Solaris with large file support */
55c19800e8SDoug Rabson #undef open
568373020dSJacques Vidrine 
578373020dSJacques Vidrine #define DBT2DATUM(DBT, DATUM) do { (DATUM)->dptr = (DBT)->data; (DATUM)->dsize = (DBT)->size; } while(0)
588373020dSJacques Vidrine #define DATUM2DBT(DATUM, DBT) do { (DBT)->data = (DATUM)->dptr; (DBT)->size = (DATUM)->dsize; } while(0)
598373020dSJacques Vidrine #define RETURN(X) return ((X) == 0) ? 0 : -1
608373020dSJacques Vidrine 
618373020dSJacques Vidrine #ifdef HAVE_DB3
628373020dSJacques Vidrine static DBC *cursor;
638373020dSJacques Vidrine #endif
648373020dSJacques Vidrine 
658373020dSJacques Vidrine #define D(X) ((DB*)(X))
668373020dSJacques Vidrine 
67*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
dbm_close(DBM * db)688373020dSJacques Vidrine dbm_close (DBM *db)
698373020dSJacques Vidrine {
708373020dSJacques Vidrine #ifdef HAVE_DB3
718373020dSJacques Vidrine     D(db)->close(D(db), 0);
728373020dSJacques Vidrine     cursor = NULL;
738373020dSJacques Vidrine #else
748373020dSJacques Vidrine     D(db)->close(D(db));
758373020dSJacques Vidrine #endif
768373020dSJacques Vidrine }
778373020dSJacques Vidrine 
78*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
dbm_delete(DBM * db,datum dkey)798373020dSJacques Vidrine dbm_delete (DBM *db, datum dkey)
808373020dSJacques Vidrine {
818373020dSJacques Vidrine     DBT key;
828373020dSJacques Vidrine     DATUM2DBT(&dkey, &key);
838373020dSJacques Vidrine #ifdef HAVE_DB3
848373020dSJacques Vidrine     RETURN(D(db)->del(D(db), NULL, &key, 0));
858373020dSJacques Vidrine #else
868373020dSJacques Vidrine     RETURN(D(db)->del(D(db), &key, 0));
878373020dSJacques Vidrine #endif
888373020dSJacques Vidrine }
898373020dSJacques Vidrine 
908373020dSJacques Vidrine datum
dbm_fetch(DBM * db,datum dkey)918373020dSJacques Vidrine dbm_fetch (DBM *db, datum dkey)
928373020dSJacques Vidrine {
938373020dSJacques Vidrine     datum dvalue;
948373020dSJacques Vidrine     DBT key, value;
958373020dSJacques Vidrine     DATUM2DBT(&dkey, &key);
968373020dSJacques Vidrine     if(D(db)->get(D(db),
978373020dSJacques Vidrine #ifdef HAVE_DB3
988373020dSJacques Vidrine 	       NULL,
998373020dSJacques Vidrine #endif
100c19800e8SDoug Rabson 	       &key, &value, 0) != 0) {
1018373020dSJacques Vidrine 	dvalue.dptr = NULL;
102c19800e8SDoug Rabson 	dvalue.dsize = 0;
103c19800e8SDoug Rabson     }
1048373020dSJacques Vidrine     else
1058373020dSJacques Vidrine 	DBT2DATUM(&value, &dvalue);
1068373020dSJacques Vidrine 
1078373020dSJacques Vidrine     return dvalue;
1088373020dSJacques Vidrine }
1098373020dSJacques Vidrine 
1108373020dSJacques Vidrine static datum
dbm_get(DB * db,int flags)1118373020dSJacques Vidrine dbm_get (DB *db, int flags)
1128373020dSJacques Vidrine {
1138373020dSJacques Vidrine     DBT key, value;
1148373020dSJacques Vidrine     datum datum;
1158373020dSJacques Vidrine #ifdef HAVE_DB3
1168373020dSJacques Vidrine     if(cursor == NULL)
1178373020dSJacques Vidrine 	db->cursor(db, NULL, &cursor, 0);
118c19800e8SDoug Rabson     if(cursor->c_get(cursor, &key, &value, flags) != 0) {
1198373020dSJacques Vidrine 	datum.dptr = NULL;
120c19800e8SDoug Rabson 	datum.dsize = 0;
121c19800e8SDoug Rabson     } else
1228373020dSJacques Vidrine 	DBT2DATUM(&value, &datum);
1238373020dSJacques Vidrine #else
1248373020dSJacques Vidrine     db->seq(db, &key, &value, flags);
125*ae771770SStanislav Sedov     DBT2DATUM(&value, &datum);
1268373020dSJacques Vidrine #endif
1278373020dSJacques Vidrine     return datum;
1288373020dSJacques Vidrine }
1298373020dSJacques Vidrine 
1308373020dSJacques Vidrine #ifndef DB_FIRST
1318373020dSJacques Vidrine #define DB_FIRST	R_FIRST
1328373020dSJacques Vidrine #define DB_NEXT		R_NEXT
1338373020dSJacques Vidrine #define DB_NOOVERWRITE	R_NOOVERWRITE
1348373020dSJacques Vidrine #define DB_KEYEXIST	1
1358373020dSJacques Vidrine #endif
1368373020dSJacques Vidrine 
137*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
dbm_firstkey(DBM * db)1388373020dSJacques Vidrine dbm_firstkey (DBM *db)
1398373020dSJacques Vidrine {
1408373020dSJacques Vidrine     return dbm_get(D(db), DB_FIRST);
1418373020dSJacques Vidrine }
1428373020dSJacques Vidrine 
143*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION datum ROKEN_LIB_CALL
dbm_nextkey(DBM * db)1448373020dSJacques Vidrine dbm_nextkey (DBM *db)
1458373020dSJacques Vidrine {
1468373020dSJacques Vidrine     return dbm_get(D(db), DB_NEXT);
1478373020dSJacques Vidrine }
1488373020dSJacques Vidrine 
149*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION DBM* ROKEN_LIB_CALL
dbm_open(const char * file,int flags,mode_t mode)1508373020dSJacques Vidrine dbm_open (const char *file, int flags, mode_t mode)
1518373020dSJacques Vidrine {
152*ae771770SStanislav Sedov #ifdef HAVE_DB3
1538373020dSJacques Vidrine     int myflags = 0;
154*ae771770SStanislav Sedov #endif
155*ae771770SStanislav Sedov     DB *db;
1568373020dSJacques Vidrine     char *fn = malloc(strlen(file) + 4);
1578373020dSJacques Vidrine     if(fn == NULL)
1588373020dSJacques Vidrine 	return NULL;
1598373020dSJacques Vidrine     strcpy(fn, file);
1608373020dSJacques Vidrine     strcat(fn, ".db");
1618373020dSJacques Vidrine #ifdef HAVE_DB3
1628373020dSJacques Vidrine     if (flags & O_CREAT)
1638373020dSJacques Vidrine 	myflags |= DB_CREATE;
1648373020dSJacques Vidrine 
1658373020dSJacques Vidrine     if (flags & O_EXCL)
1668373020dSJacques Vidrine 	myflags |= DB_EXCL;
1678373020dSJacques Vidrine 
1688373020dSJacques Vidrine     if (flags & O_RDONLY)
1698373020dSJacques Vidrine 	myflags |= DB_RDONLY;
1708373020dSJacques Vidrine 
1718373020dSJacques Vidrine     if (flags & O_TRUNC)
1728373020dSJacques Vidrine 	myflags |= DB_TRUNCATE;
1738373020dSJacques Vidrine     if(db_create(&db, NULL, 0) != 0) {
1748373020dSJacques Vidrine 	free(fn);
1758373020dSJacques Vidrine 	return NULL;
1768373020dSJacques Vidrine     }
1771c43270aSJacques Vidrine 
1781c43270aSJacques Vidrine #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
1791c43270aSJacques Vidrine     if(db->open(db, NULL, fn, NULL, DB_BTREE, myflags, mode) != 0) {
1801c43270aSJacques Vidrine #else
1818373020dSJacques Vidrine     if(db->open(db, fn, NULL, DB_BTREE, myflags, mode) != 0) {
1821c43270aSJacques Vidrine #endif
1838373020dSJacques Vidrine 	free(fn);
1848373020dSJacques Vidrine 	db->close(db, 0);
1858373020dSJacques Vidrine 	return NULL;
1868373020dSJacques Vidrine     }
1878373020dSJacques Vidrine #else
1888373020dSJacques Vidrine     db = dbopen(fn, flags, mode, DB_BTREE, NULL);
1898373020dSJacques Vidrine #endif
1908373020dSJacques Vidrine     free(fn);
1918373020dSJacques Vidrine     return (DBM*)db;
1928373020dSJacques Vidrine }
1938373020dSJacques Vidrine 
194*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
1958373020dSJacques Vidrine dbm_store (DBM *db, datum dkey, datum dvalue, int flags)
1968373020dSJacques Vidrine {
1978373020dSJacques Vidrine     int ret;
1988373020dSJacques Vidrine     DBT key, value;
1998373020dSJacques Vidrine     int myflags = 0;
2008373020dSJacques Vidrine     if((flags & DBM_REPLACE) == 0)
2018373020dSJacques Vidrine 	myflags |= DB_NOOVERWRITE;
2028373020dSJacques Vidrine     DATUM2DBT(&dkey, &key);
2038373020dSJacques Vidrine     DATUM2DBT(&dvalue, &value);
2048373020dSJacques Vidrine     ret = D(db)->put(D(db),
2058373020dSJacques Vidrine #ifdef HAVE_DB3
2068373020dSJacques Vidrine 		     NULL,
2078373020dSJacques Vidrine #endif
2088373020dSJacques Vidrine &key, &value, myflags);
2098373020dSJacques Vidrine     if(ret == DB_KEYEXIST)
2108373020dSJacques Vidrine 	return 1;
2118373020dSJacques Vidrine     RETURN(ret);
2128373020dSJacques Vidrine }
2138373020dSJacques Vidrine 
214*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
2158373020dSJacques Vidrine dbm_error (DBM *db)
2168373020dSJacques Vidrine {
2178373020dSJacques Vidrine     return 0;
2188373020dSJacques Vidrine }
2198373020dSJacques Vidrine 
220*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
2218373020dSJacques Vidrine dbm_clearerr (DBM *db)
2228373020dSJacques Vidrine {
2238373020dSJacques Vidrine     return 0;
2248373020dSJacques Vidrine }
2258373020dSJacques Vidrine 
226