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