yp_dblookup.c (1fbdac93d98e19ba0c459e3a2c782dcf764ef05a) yp_dblookup.c (b3e932340dfea2508fbbc4f8fa226b922970c073)
1/*
2 * Copyright (c) 1995
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
1/*
2 * Copyright (c) 1995
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: yp_dblookup.c,v 1.9 1996/05/01 02:33:52 wpaul Exp $
32 * $Id: yp_dblookup.c,v 1.13 1996/06/04 04:08:21 wpaul Exp $
33 *
34 */
35#include <stdio.h>
36#include <stdlib.h>
37#include <fcntl.h>
38#include <string.h>
39#include <limits.h>
40#include <unistd.h>
41#include <db.h>
42#include <sys/stat.h>
43#include <sys/param.h>
44#include <errno.h>
45#include <paths.h>
46#include <rpcsvc/yp.h>
47#include "yp_extern.h"
48
49#ifndef lint
33 *
34 */
35#include <stdio.h>
36#include <stdlib.h>
37#include <fcntl.h>
38#include <string.h>
39#include <limits.h>
40#include <unistd.h>
41#include <db.h>
42#include <sys/stat.h>
43#include <sys/param.h>
44#include <errno.h>
45#include <paths.h>
46#include <rpcsvc/yp.h>
47#include "yp_extern.h"
48
49#ifndef lint
50static const char rcsid[] = "$Id: yp_dblookup.c,v 1.9 1996/05/01 02:33:52 wpaul Exp $";
50static const char rcsid[] = "$Id: yp_dblookup.c,v 1.13 1996/06/04 04:08:21 wpaul Exp $";
51#endif
52
53int ypdb_debug = 0;
54int yp_errno = YP_TRUE;
55
56#define PERM_SECURE (S_IRUSR|S_IWUSR)
57HASHINFO openinfo = {
58 4096, /* bsize */
59 32, /* ffactor */
51#endif
52
53int ypdb_debug = 0;
54int yp_errno = YP_TRUE;
55
56#define PERM_SECURE (S_IRUSR|S_IWUSR)
57HASHINFO openinfo = {
58 4096, /* bsize */
59 32, /* ffactor */
60 512, /* nelem */
60 256, /* nelem */
61 2048 * 512, /* cachesize */
62 NULL, /* hash */
63 0, /* lorder */
64};
65
66#ifdef DB_CACHE
67#define MAXDBS 20
68#define LASTDB (MAXDBS - 1)

--- 19 unchanged lines hidden (view full) ---

88 dbs[i] = NULL;
89 return;
90}
91
92/*
93 * Zorch a single entry in the dbent table and release
94 * all its resources.
95 */
61 2048 * 512, /* cachesize */
62 NULL, /* hash */
63 0, /* lorder */
64};
65
66#ifdef DB_CACHE
67#define MAXDBS 20
68#define LASTDB (MAXDBS - 1)

--- 19 unchanged lines hidden (view full) ---

88 dbs[i] = NULL;
89 return;
90}
91
92/*
93 * Zorch a single entry in the dbent table and release
94 * all its resources.
95 */
96static __inline void yp_flush(i)
96static void yp_flush(i)
97 register int i;
98{
99 (void)(dbs[i]->dbp->close)(dbs[i]->dbp);
100 dbs[i]->dbp = NULL;
101 free(dbs[i]->name);
102 dbs[i]->name = NULL;
103 dbs[i]->key = NULL;
104 dbs[i]->size = 0;

--- 18 unchanged lines hidden (view full) ---

123
124
125/*
126 * Add a DB handle and database name to the cache. We only maintain
127 * fixed number of entries in the cache, so if we're asked to store
128 * a new entry when all our slots are already filled, we have to kick
129 * out the entry in the last slot to make room.
130 */
97 register int i;
98{
99 (void)(dbs[i]->dbp->close)(dbs[i]->dbp);
100 dbs[i]->dbp = NULL;
101 free(dbs[i]->name);
102 dbs[i]->name = NULL;
103 dbs[i]->key = NULL;
104 dbs[i]->size = 0;

--- 18 unchanged lines hidden (view full) ---

123
124
125/*
126 * Add a DB handle and database name to the cache. We only maintain
127 * fixed number of entries in the cache, so if we're asked to store
128 * a new entry when all our slots are already filled, we have to kick
129 * out the entry in the last slot to make room.
130 */
131static __inline void yp_add_db(dbp, name, size)
131static void yp_add_db(dbp, name, size)
132 DB *dbp;
133 char *name;
134 int size;
135{
136 register int i;
137 register struct dbent *tmp;
138
139 tmp = dbs[LASTDB];

--- 45 unchanged lines hidden (view full) ---

185 * matches to see if their saved key indexes and lengths also match.
186 * This lets us return a DB handle that's already positioned at the
187 * correct location within a database.
188 *
189 * - Once we have a match, it gets migrated to the top of the list
190 * array so that it will be easier to find if another request for
191 * the same database comes in later.
192 */
132 DB *dbp;
133 char *name;
134 int size;
135{
136 register int i;
137 register struct dbent *tmp;
138
139 tmp = dbs[LASTDB];

--- 45 unchanged lines hidden (view full) ---

185 * matches to see if their saved key indexes and lengths also match.
186 * This lets us return a DB handle that's already positioned at the
187 * correct location within a database.
188 *
189 * - Once we have a match, it gets migrated to the top of the list
190 * array so that it will be easier to find if another request for
191 * the same database comes in later.
192 */
193static __inline DB *yp_find_db(name, key, size)
193static DB *yp_find_db(name, key, size)
194 char *name;
195 char *key;
196 int size;
197{
198 register int i, j;
199 register struct dbent *tmp;
200
201 for (i = 0; i < numdbs; i++) {
202 if (dbs[i]->name != NULL && !strcmp(dbs[i]->name, name)) {
203 if (size) {
204 if (size != dbs[i]->size ||
194 char *name;
195 char *key;
196 int size;
197{
198 register int i, j;
199 register struct dbent *tmp;
200
201 for (i = 0; i < numdbs; i++) {
202 if (dbs[i]->name != NULL && !strcmp(dbs[i]->name, name)) {
203 if (size) {
204 if (size != dbs[i]->size ||
205 strncmp(dbs[i]->key, key, size))
205 strncmp(dbs[i]->key, key, size))
206 continue;
207 } else {
206 continue;
207 } else {
208 if (dbs[i]->size) {
208 if (dbs[i]->size)
209 continue;
209 continue;
210 }
211 }
212 if (i > 0) {
213 tmp = dbs[i];
214 for (j = i; j > 0; j--)
215 dbs[j] = dbs[j - 1];
216 dbs[0] = tmp;
217 }
218 return(dbs[0]->dbp);
219 }
220 }
210 }
211 if (i > 0) {
212 tmp = dbs[i];
213 for (j = i; j > 0; j--)
214 dbs[j] = dbs[j - 1];
215 dbs[0] = tmp;
216 }
217 return(dbs[0]->dbp);
218 }
219 }
220
221 return(NULL);
222}
223
224/*
225 * Open a DB database and cache the handle for later use. We first
226 * check the cache to see if the required database is already open.
227 * If so, we fetch the handle from the cache. If not, we try to open
228 * the database and save the handle in the cache for later use.
229 */
230DB *yp_open_db_cache(domain, map, key, size)
231 const char *domain;
232 const char *map;
233 const char *key;
234 const int size;
235{
236 DB *dbp = NULL;
237 char buf[MAXPATHLEN + 2];
221 return(NULL);
222}
223
224/*
225 * Open a DB database and cache the handle for later use. We first
226 * check the cache to see if the required database is already open.
227 * If so, we fetch the handle from the cache. If not, we try to open
228 * the database and save the handle in the cache for later use.
229 */
230DB *yp_open_db_cache(domain, map, key, size)
231 const char *domain;
232 const char *map;
233 const char *key;
234 const int size;
235{
236 DB *dbp = NULL;
237 char buf[MAXPATHLEN + 2];
238/*
239 snprintf(buf, sizeof(buf), "%s/%s", domain, map);
240*/
238
239 strcpy(buf, domain);
240 strcat(buf, "/");
241 strcat(buf, map);
242
243 if ((dbp = yp_find_db((char *)&buf, key, size)) != NULL) {
244 return(dbp);
245 } else {

--- 200 unchanged lines hidden (view full) ---

446 return(rval);
447 }
448
449 if (ypdb_debug)
450 yp_error("Retreiving next key, previous was: [%.*s]",
451 key->size, key->data);
452
453 if (!all) {
241
242 strcpy(buf, domain);
243 strcat(buf, "/");
244 strcat(buf, map);
245
246 if ((dbp = yp_find_db((char *)&buf, key, size)) != NULL) {
247 return(dbp);
248 } else {

--- 200 unchanged lines hidden (view full) ---

449 return(rval);
450 }
451
452 if (ypdb_debug)
453 yp_error("Retreiving next key, previous was: [%.*s]",
454 key->size, key->data);
455
456 if (!all) {
454#ifndef DB_CACHE
455 if (key->size != lkey.size ||
456 strncmp(key->data, lkey.data, key->size)) {
457#else
458 if (!dbs[0]->size) {
457#ifdef DB_CACHE
458 if (!dbs[0]->key) {
459#endif
460 (dbp->seq)(dbp,&lkey,&ldata,R_FIRST);
461 while(strncmp((char *)key->data,lkey.data,
462 (int)key->size) || key->size != lkey.size)
459#endif
460 (dbp->seq)(dbp,&lkey,&ldata,R_FIRST);
461 while(strncmp((char *)key->data,lkey.data,
462 (int)key->size) || key->size != lkey.size)
463 (dbp->seq)(dbp,&lkey,&ldata,R_NEXT);
463 if ((dbp->seq)(dbp,&lkey,&ldata,R_NEXT)) {
464#ifdef DB_CACHE
465 dbs[0]->size = 0;
466#endif
467 return(YP_NOKEY);
468 }
469
470#ifdef DB_CACHE
464 }
471 }
472#endif
465 }
466
467 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
468#ifdef DB_CACHE
469 dbs[0]->size = 0;
470#endif
471 return(YP_NOMORE);
472 }

--- 26 unchanged lines hidden ---
473 }
474
475 if ((dbp->seq)(dbp,key,data,R_NEXT)) {
476#ifdef DB_CACHE
477 dbs[0]->size = 0;
478#endif
479 return(YP_NOMORE);
480 }

--- 26 unchanged lines hidden ---