Home | History | Annotate | Line # | Download | only in citrus
citrus_lookup.c revision 1.1
      1 /*	$NetBSD: citrus_lookup.c,v 1.1 2003/06/25 09:51:34 tshiozak Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2003 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 __RCSID("$NetBSD: citrus_lookup.c,v 1.1 2003/06/25 09:51:34 tshiozak Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include "namespace.h"
     35 #include <assert.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <errno.h>
     40 #include <limits.h>
     41 #include <unistd.h>
     42 #include <fcntl.h>
     43 #include <paths.h>
     44 #include <dirent.h>
     45 #include <sys/types.h>
     46 
     47 #include "citrus_namespace.h"
     48 #include "citrus_bcs.h"
     49 #include "citrus_region.h"
     50 #include "citrus_memstream.h"
     51 #include "citrus_mmap.h"
     52 #include "citrus_db.h"
     53 #include "citrus_db_hash.h"
     54 #include "citrus_lookup.h"
     55 #include "citrus_lookup_file.h"
     56 
     57 struct _citrus_lookup {
     58 	union {
     59 		struct {
     60 			struct _citrus_db *db;
     61 			struct _citrus_region file;
     62 			int num, idx;
     63 			struct _db_locator locator;
     64 		} db;
     65 		struct {
     66 			struct _region r;
     67 			struct _memstream ms;
     68 		} plain;
     69 	} u;
     70 #define cl_db		u.db.db
     71 #define cl_dbidx	u.db.idx
     72 #define cl_dbfile	u.db.file
     73 #define cl_dbnum	u.db.num
     74 #define cl_dblocator	u.db.locator
     75 #define cl_plainr	u.plain.r
     76 #define cl_plainms	u.plain.ms
     77 	int cl_rewind;
     78 	char *cl_key;
     79 	size_t cl_keylen;
     80 	int (*cl_next)(struct _citrus_lookup *, struct _region *,
     81 		       struct _region *);
     82 	int (*cl_lookup)(struct _citrus_lookup *, const char *,
     83 			 struct _region *);
     84 	int (*cl_num_entries)(struct _citrus_lookup *);
     85 	void (*cl_close)(struct _citrus_lookup *);
     86 };
     87 
     88 static int
     89 seq_get_num_entries_db(struct _citrus_lookup *cl)
     90 {
     91 	return cl->cl_dbnum;
     92 }
     93 
     94 static int
     95 seq_next_db(struct _citrus_lookup *cl,
     96 	    struct _region *key, struct _region *data)
     97 {
     98 
     99 	if (cl->cl_key) {
    100 		if (key)
    101 			_region_init(key, cl->cl_key, cl->cl_keylen);
    102 		return _db_lookup_by_s(cl->cl_db, cl->cl_key, data,
    103 				       &cl->cl_dblocator);
    104 	}
    105 
    106 	if (cl->cl_rewind) {
    107 		cl->cl_dbidx = 0;
    108 	}
    109 	cl->cl_rewind = 0;
    110 	if (cl->cl_dbidx >= cl->cl_dbnum)
    111 		return ENOENT;
    112 
    113 	return _db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data);
    114 }
    115 
    116 static int
    117 seq_lookup_db(struct _citrus_lookup *cl, const char *key,
    118 	      struct _region *data)
    119 {
    120 	cl->cl_rewind = 0;
    121 	free(cl->cl_key);
    122 	cl->cl_key = strdup(key);
    123 	cl->cl_keylen = strlen(key);
    124 	_db_locator_init(&cl->cl_dblocator);
    125 	return _db_lookup_by_s(cl->cl_db, key, data, &cl->cl_dblocator);
    126 }
    127 
    128 static void
    129 seq_close_db(struct _citrus_lookup *cl)
    130 {
    131 	_db_close(cl->cl_db);
    132 	_unmap_file(&cl->cl_dbfile);
    133 }
    134 
    135 static int
    136 seq_open_db(struct _citrus_lookup *cl, const char *name)
    137 {
    138 	int ret;
    139 	struct _region r;
    140 	char path[PATH_MAX];
    141 
    142 	snprintf(path, sizeof(path), "%s.db", name);
    143 	ret = _map_file(&r, path);
    144 	if (ret)
    145 		return ret;
    146 
    147 	ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
    148 		       _db_hash_std, NULL);
    149 	if (ret) {
    150 		_unmap_file(&r);
    151 		return ret;
    152 	}
    153 
    154 	cl->cl_dbfile = r;
    155 	cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
    156 	cl->cl_dbidx = 0;
    157 	cl->cl_rewind = 1;
    158 	cl->cl_lookup = &seq_lookup_db;
    159 	cl->cl_next = &seq_next_db;
    160 	cl->cl_num_entries = &seq_get_num_entries_db;
    161 	cl->cl_close = &seq_close_db;
    162 
    163 	return 0;
    164 }
    165 
    166 #define T_COMM '#'
    167 static int
    168 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
    169 	       struct _region *data)
    170 {
    171 	const char *p, *q;
    172 	size_t len;
    173 
    174 	if (cl->cl_rewind)
    175 		_memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
    176 	cl->cl_rewind = 0;
    177 
    178 retry:
    179 	p = _memstream_getln(&cl->cl_plainms, &len);
    180 	if (p == NULL)
    181 		return ENOENT;
    182 	/* ignore comment */
    183 	q = memchr(p, T_COMM, len);
    184 	if (q) {
    185 		len = q-p;
    186 	}
    187 	/* ignore trailing spaces */
    188 	_bcs_trunc_rws_len(p, &len);
    189 	p = _bcs_skip_ws_len(p, &len);
    190 	q = _bcs_skip_nonws_len(p, &len);
    191 	if (p==q)
    192 		goto retry;
    193 	if (cl->cl_key && (q-p != cl->cl_keylen ||
    194 			   memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
    195 		goto retry;
    196 
    197 	/* found a entry */
    198 	if (key)
    199 		/* LINTED: discard const */
    200 		_region_init(key, (char *)p, q-p);
    201 	p = _bcs_skip_ws_len(q, &len);
    202 	if (data)
    203 		/* LINTED: discard const */
    204 		_region_init(data, len ? (char *)p : NULL, len);
    205 
    206 	return 0;
    207 }
    208 
    209 static int
    210 seq_get_num_entries_plain(struct _citrus_lookup *cl)
    211 {
    212 	int num;
    213 
    214 	num = 0;
    215 	while (seq_next_plain(cl, NULL, NULL) == 0)
    216 		num++;
    217 
    218 	return num;
    219 }
    220 
    221 static int
    222 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
    223 		 struct _region *data)
    224 {
    225 	size_t len;
    226 	const char *p;
    227 
    228 	cl->cl_rewind = 0;
    229 	_memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
    230 	p = _memstream_matchline(&cl->cl_plainms, key, &len, 0);
    231 	if (p == NULL)
    232 		return ENOENT;
    233 	free(cl->cl_key);
    234 	cl->cl_key = strdup(key);
    235 	cl->cl_keylen = strlen(key);
    236 	if (data)
    237 		/* LINTED: discard const */
    238 		_region_init(data, (char *)p, len);
    239 
    240 	return 0;
    241 }
    242 
    243 static void
    244 seq_close_plain(struct _citrus_lookup *cl)
    245 {
    246 	_unmap_file(&cl->cl_plainr);
    247 }
    248 
    249 static int
    250 seq_open_plain(struct _citrus_lookup *cl, const char *name)
    251 {
    252 	int ret;
    253 
    254 	/* open read stream */
    255 	ret = _map_file(&cl->cl_plainr, name);
    256 	if (ret)
    257 		return ret;
    258 
    259 	cl->cl_rewind = 1;
    260 	cl->cl_next = &seq_next_plain;
    261 	cl->cl_lookup = &seq_lookup_plain;
    262 	cl->cl_num_entries = &seq_get_num_entries_plain;
    263 	cl->cl_close = &seq_close_plain;
    264 
    265 	return 0;
    266 }
    267 
    268 int
    269 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name)
    270 {
    271 	int ret;
    272 	struct _citrus_lookup *cl;
    273 
    274 	cl = malloc(sizeof(*cl));
    275 	if (cl == NULL)
    276 		return errno;
    277 
    278 	cl->cl_key = NULL;
    279 	cl->cl_keylen = 0;
    280 	ret = seq_open_db(cl, name);
    281 	if (ret == ENOENT)
    282 		ret = seq_open_plain(cl, name);
    283 	if (!ret)
    284 		*rcl = cl;
    285 	else
    286 		free(cl);
    287 
    288 	return ret;
    289 }
    290 
    291 void
    292 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
    293 {
    294 	cl->cl_rewind = 1;
    295 	free(cl->cl_key);
    296 	cl->cl_key = NULL;
    297 	cl->cl_keylen = 0;
    298 }
    299 
    300 int
    301 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
    302 			struct _region *key, struct _region *data)
    303 {
    304 	return (*cl->cl_next)(cl, key, data);
    305 }
    306 
    307 int
    308 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
    309 			  struct _region *data)
    310 {
    311 	return (*cl->cl_lookup)(cl, key, data);
    312 }
    313 
    314 int
    315 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
    316 {
    317 	return (*cl->cl_num_entries)(cl);
    318 }
    319 
    320 void
    321 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
    322 {
    323 	free(cl->cl_key);
    324 	(*cl->cl_close)(cl);
    325 }
    326 
    327 char *
    328 _citrus_lookup_simple(const char *name, const char *key,
    329 		      char *linebuf, size_t linebufsize)
    330 {
    331 	int ret;
    332 	struct _citrus_lookup *cl;
    333 	struct _region data;
    334 
    335 	ret = _citrus_lookup_seq_open(&cl, name);
    336 	if (ret)
    337 		return NULL;
    338 
    339 	ret = _citrus_lookup_seq_lookup(cl, key, &data);
    340 	if (ret) {
    341 		_citrus_lookup_seq_close(cl);
    342 		return NULL;
    343 	}
    344 
    345 	snprintf(linebuf, linebufsize, "%.*s",
    346 		 (int)_region_size(&data), (const char *)_region_head(&data));
    347 
    348 	_citrus_lookup_seq_close(cl);
    349 
    350 	return linebuf;
    351 }
    352