Home | History | Annotate | Line # | Download | only in common
ypdb.c revision 1.10
      1  1.10    lukem /*	$NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 1990, 1993
      5   1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6   1.1  thorpej  * All rights reserved.
      7   1.1  thorpej  *
      8   1.1  thorpej  * This code is derived from software contributed to Berkeley by
      9   1.1  thorpej  * Margo Seltzer.
     10   1.1  thorpej  *
     11   1.1  thorpej  * This code is derived from ndbm module of BSD4.4 db (hash) by
     12   1.1  thorpej  * Mats O Jansson
     13   1.1  thorpej  *
     14   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15   1.1  thorpej  * modification, are permitted provided that the following conditions
     16   1.1  thorpej  * are met:
     17   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22   1.9      agc  * 3. Neither the name of the University nor the names of its contributors
     23   1.1  thorpej  *    may be used to endorse or promote products derived from this software
     24   1.1  thorpej  *    without specific prior written permission.
     25   1.1  thorpej  *
     26   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     27   1.1  thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     28   1.1  thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     30   1.1  thorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1  thorpej  * SUCH DAMAGE.
     37   1.1  thorpej  */
     38   1.1  thorpej 
     39   1.4    lukem #include <sys/cdefs.h>
     40   1.4    lukem #ifndef lint
     41  1.10    lukem __RCSID("$NetBSD: ypdb.c,v 1.10 2005/06/20 00:29:42 lukem Exp $");
     42   1.4    lukem #endif
     43   1.4    lukem 
     44   1.1  thorpej #include <sys/param.h>
     45   1.1  thorpej #include <sys/types.h>
     46   1.4    lukem 
     47   1.4    lukem #include <db.h>
     48  1.10    lukem #include <err.h>
     49   1.4    lukem #include <errno.h>
     50   1.1  thorpej #include <stdio.h>
     51   1.1  thorpej #include <string.h>
     52   1.1  thorpej 
     53   1.5    lukem #include <rpcsvc/yp.h>
     54   1.5    lukem 
     55   1.1  thorpej #include "ypdb.h"
     56   1.1  thorpej 
     57   1.1  thorpej /*
     58  1.10    lukem  * ypdb_open --
     59  1.10    lukem  *	dbopen(3) file with the flags & mode.
     60  1.10    lukem  *	First ensure that file has a suffix of YPDB_SUFFIX.
     61  1.10    lukem  *	Try opening as a DB_BTREE first, then DB_HASH.
     62  1.10    lukem  *
     63   1.1  thorpej  * Returns:
     64   1.1  thorpej  * 	*DBM on success
     65   1.1  thorpej  *	 NULL on failure
     66   1.1  thorpej  */
     67   1.1  thorpej 
     68   1.3    lukem DBM *
     69   1.8      wiz ypdb_open(const char *file, int flags, int mode)
     70   1.1  thorpej {
     71  1.10    lukem 	char path[MAXPATHLEN];
     72  1.10    lukem 	const char *cp, *suffix;
     73   1.4    lukem 	DBM *db;
     74   1.1  thorpej 	BTREEINFO info;
     75   1.1  thorpej 
     76   1.4    lukem 	cp = strrchr(file, '.');
     77  1.10    lukem 	if (cp != NULL && strcmp(cp, YPDB_SUFFIX) == 0)
     78  1.10    lukem 		suffix = "";
     79  1.10    lukem 	else
     80  1.10    lukem 		suffix = YPDB_SUFFIX;
     81  1.10    lukem 	if (strlen(file) + strlen(suffix) > (sizeof(path) - 1)) {
     82  1.10    lukem 		warnx("File name `%s' is too long", file);
     83  1.10    lukem 		return (NULL);
     84  1.10    lukem 	}
     85  1.10    lukem 	snprintf(path, sizeof(path), "%s%s", file, suffix);
     86   1.4    lukem 
     87   1.4    lukem 		/* try our btree format first */
     88   1.1  thorpej 	info.flags = 0;
     89   1.1  thorpej 	info.cachesize = 0;
     90   1.1  thorpej 	info.maxkeypage = 0;
     91   1.1  thorpej 	info.minkeypage = 0;
     92   1.1  thorpej 	info.psize = 0;
     93   1.1  thorpej 	info.compare = NULL;
     94   1.1  thorpej 	info.prefix = NULL;
     95   1.1  thorpej 	info.lorder = 0;
     96   1.4    lukem 	db = (DBM *)dbopen(path, flags, mode, DB_BTREE, (void *)&info);
     97   1.4    lukem 	if (db != NULL || errno != EFTYPE)
     98   1.4    lukem 		return (db);
     99   1.4    lukem 
    100   1.4    lukem 		/* fallback to standard hash (for sendmail's aliases.db) */
    101   1.4    lukem 	db = (DBM *)dbopen(path, flags, mode, DB_HASH, NULL);
    102   1.4    lukem 	return (db);
    103   1.1  thorpej }
    104   1.1  thorpej 
    105   1.3    lukem void
    106   1.8      wiz ypdb_close(DBM *db)
    107   1.1  thorpej {
    108   1.1  thorpej 	(void)(db->close)(db);
    109   1.1  thorpej }
    110   1.1  thorpej 
    111   1.1  thorpej /*
    112   1.1  thorpej  * Returns:
    113   1.1  thorpej  *	DATUM on success
    114   1.1  thorpej  *	NULL on failure
    115   1.1  thorpej  */
    116   1.1  thorpej 
    117   1.3    lukem datum
    118   1.8      wiz ypdb_fetch(DBM *db, datum key)
    119   1.1  thorpej {
    120   1.6    lukem 	datum retkey;
    121   1.6    lukem 	DBT nk, nd;
    122   1.1  thorpej 	int status;
    123   1.1  thorpej 
    124   1.6    lukem 	nk.data = key.dptr;
    125   1.7    lukem 	nk.size = key.dsize;
    126   1.6    lukem 	status = (db->get)(db, &nk, &nd, 0);
    127   1.1  thorpej 	if (status) {
    128   1.6    lukem 		retkey.dptr = NULL;
    129   1.6    lukem 		retkey.dsize = 0;
    130   1.6    lukem 	} else {
    131   1.6    lukem 		retkey.dptr = nd.data;
    132   1.7    lukem 		retkey.dsize = nd.size;
    133   1.1  thorpej 	}
    134   1.6    lukem 	return (retkey);
    135   1.1  thorpej }
    136   1.1  thorpej 
    137   1.1  thorpej /*
    138   1.1  thorpej  * Returns:
    139   1.1  thorpej  *	DATUM on success
    140   1.1  thorpej  *	NULL on failure
    141   1.1  thorpej  */
    142   1.1  thorpej 
    143   1.3    lukem datum
    144   1.8      wiz ypdb_firstkey(DBM *db)
    145   1.1  thorpej {
    146   1.1  thorpej 	int status;
    147   1.6    lukem 	datum retkey;
    148   1.6    lukem 	DBT nk, nd;
    149   1.1  thorpej 
    150   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_FIRST);
    151   1.6    lukem 	if (status) {
    152   1.1  thorpej 		retkey.dptr = NULL;
    153   1.6    lukem 		retkey.dsize = 0;
    154   1.6    lukem 	} else {
    155   1.6    lukem 		retkey.dptr = nk.data;
    156   1.7    lukem 		retkey.dsize = nk.size;
    157   1.6    lukem 	}
    158   1.1  thorpej 	return (retkey);
    159   1.1  thorpej }
    160   1.1  thorpej 
    161   1.1  thorpej /*
    162   1.1  thorpej  * Returns:
    163   1.1  thorpej  *	DATUM on success
    164   1.1  thorpej  *	NULL on failure
    165   1.1  thorpej  */
    166   1.1  thorpej 
    167   1.3    lukem datum
    168   1.8      wiz ypdb_nextkey(DBM *db)
    169   1.1  thorpej {
    170   1.1  thorpej 	int status;
    171   1.6    lukem 	datum retkey;
    172   1.6    lukem 	DBT nk, nd;
    173   1.1  thorpej 
    174   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_NEXT);
    175   1.6    lukem 	if (status) {
    176   1.1  thorpej 		retkey.dptr = NULL;
    177   1.6    lukem 		retkey.dsize = 0;
    178   1.6    lukem 	} else {
    179   1.6    lukem 		retkey.dptr = nk.data;
    180   1.7    lukem 		retkey.dsize = nk.size;
    181   1.6    lukem 	}
    182   1.1  thorpej 	return (retkey);
    183   1.1  thorpej }
    184   1.1  thorpej 
    185   1.1  thorpej /*
    186   1.1  thorpej  * Returns:
    187   1.1  thorpej  *	DATUM on success
    188   1.1  thorpej  *	NULL on failure
    189   1.1  thorpej  */
    190   1.1  thorpej 
    191   1.3    lukem datum
    192   1.8      wiz ypdb_setkey(DBM *db, datum key)
    193   1.1  thorpej {
    194   1.1  thorpej 	int status;
    195   1.6    lukem 	DBT nk, nd;
    196   1.6    lukem 
    197   1.6    lukem 	nk.data = key.dptr;
    198   1.7    lukem 	nk.size = key.dsize;
    199   1.6    lukem 	status = (db->seq)(db, &nk, &nd, R_CURSOR);
    200   1.6    lukem 	if (status) {
    201   1.1  thorpej 		key.dptr = NULL;
    202   1.6    lukem 		key.dsize = 0;
    203   1.6    lukem 	}
    204   1.1  thorpej 	return (key);
    205   1.1  thorpej }
    206   1.1  thorpej 
    207   1.1  thorpej /*
    208   1.1  thorpej  * Returns:
    209   1.1  thorpej  *	 0 on success
    210   1.1  thorpej  *	<0 failure
    211   1.1  thorpej  */
    212   1.1  thorpej 
    213   1.1  thorpej int
    214   1.8      wiz ypdb_delete(DBM *db, datum key)
    215   1.1  thorpej {
    216   1.1  thorpej 	int status;
    217   1.6    lukem 	DBT nk;
    218   1.1  thorpej 
    219   1.6    lukem 	nk.data = key.dptr;
    220   1.7    lukem 	nk.size = key.dsize;
    221   1.6    lukem 	status = (db->del)(db, &nk, 0);
    222   1.1  thorpej 	if (status)
    223   1.1  thorpej 		return (-1);
    224   1.1  thorpej 	else
    225   1.1  thorpej 		return (0);
    226   1.1  thorpej }
    227   1.1  thorpej 
    228   1.1  thorpej /*
    229   1.1  thorpej  * Returns:
    230   1.1  thorpej  *	 0 on success
    231   1.1  thorpej  *	<0 failure
    232   1.1  thorpej  *	 1 if YPDB_INSERT and entry exists
    233   1.1  thorpej  */
    234   1.1  thorpej 
    235   1.1  thorpej int
    236   1.8      wiz ypdb_store(DBM *db, datum key, datum content, int flags)
    237   1.1  thorpej {
    238   1.6    lukem 	DBT nk, nd;
    239   1.6    lukem 
    240   1.5    lukem 	if (key.dsize > YPMAXRECORD || content.dsize > YPMAXRECORD)
    241   1.5    lukem 		return -1;
    242   1.6    lukem 	nk.data = key.dptr;
    243   1.7    lukem 	nk.size = key.dsize;
    244   1.6    lukem 	nd.data = content.dptr;
    245   1.7    lukem 	nd.size = content.dsize;
    246   1.6    lukem 	return ((db->put)(db, &nk, &nd,
    247   1.1  thorpej 	    (flags == YPDB_INSERT) ? R_NOOVERWRITE : 0));
    248   1.1  thorpej }
    249