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