citrus_lookup.c revision 1.2 1 /* $NetBSD: citrus_lookup.c,v 1.2 2003/06/26 12:05:04 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.2 2003/06/26 12:05:04 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 _bcs_convert_to_lower(cl->cl_key);
124 cl->cl_keylen = strlen(cl->cl_key);
125 _db_locator_init(&cl->cl_dblocator);
126 return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, &cl->cl_dblocator);
127 }
128
129 static void
130 seq_close_db(struct _citrus_lookup *cl)
131 {
132 _db_close(cl->cl_db);
133 _unmap_file(&cl->cl_dbfile);
134 }
135
136 static int
137 seq_open_db(struct _citrus_lookup *cl, const char *name)
138 {
139 int ret;
140 struct _region r;
141 char path[PATH_MAX];
142
143 snprintf(path, sizeof(path), "%s.db", name);
144 ret = _map_file(&r, path);
145 if (ret)
146 return ret;
147
148 ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
149 _db_hash_std, NULL);
150 if (ret) {
151 _unmap_file(&r);
152 return ret;
153 }
154
155 cl->cl_dbfile = r;
156 cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
157 cl->cl_dbidx = 0;
158 cl->cl_rewind = 1;
159 cl->cl_lookup = &seq_lookup_db;
160 cl->cl_next = &seq_next_db;
161 cl->cl_num_entries = &seq_get_num_entries_db;
162 cl->cl_close = &seq_close_db;
163
164 return 0;
165 }
166
167 #define T_COMM '#'
168 static int
169 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
170 struct _region *data)
171 {
172 const char *p, *q;
173 size_t len;
174
175 if (cl->cl_rewind)
176 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
177 cl->cl_rewind = 0;
178
179 retry:
180 p = _memstream_getln(&cl->cl_plainms, &len);
181 if (p == NULL)
182 return ENOENT;
183 /* ignore comment */
184 q = memchr(p, T_COMM, len);
185 if (q) {
186 len = q-p;
187 }
188 /* ignore trailing spaces */
189 _bcs_trunc_rws_len(p, &len);
190 p = _bcs_skip_ws_len(p, &len);
191 q = _bcs_skip_nonws_len(p, &len);
192 if (p==q)
193 goto retry;
194 if (cl->cl_key && (q-p != cl->cl_keylen ||
195 memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
196 goto retry;
197
198 /* found a entry */
199 if (key)
200 /* LINTED: discard const */
201 _region_init(key, (char *)p, q-p);
202 p = _bcs_skip_ws_len(q, &len);
203 if (data)
204 /* LINTED: discard const */
205 _region_init(data, len ? (char *)p : NULL, len);
206
207 return 0;
208 }
209
210 static int
211 seq_get_num_entries_plain(struct _citrus_lookup *cl)
212 {
213 int num;
214
215 num = 0;
216 while (seq_next_plain(cl, NULL, NULL) == 0)
217 num++;
218
219 return num;
220 }
221
222 static int
223 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
224 struct _region *data)
225 {
226 size_t len;
227 const char *p;
228
229 cl->cl_rewind = 0;
230 free(cl->cl_key);
231 cl->cl_key = strdup(key);
232 _bcs_convert_to_lower(cl->cl_key);
233 cl->cl_keylen = strlen(cl->cl_key);
234 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
235 p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
236 if (p == NULL)
237 return ENOENT;
238 if (data)
239 /* LINTED: discard const */
240 _region_init(data, (char *)p, len);
241
242 return 0;
243 }
244
245 static void
246 seq_close_plain(struct _citrus_lookup *cl)
247 {
248 _unmap_file(&cl->cl_plainr);
249 }
250
251 static int
252 seq_open_plain(struct _citrus_lookup *cl, const char *name)
253 {
254 int ret;
255
256 /* open read stream */
257 ret = _map_file(&cl->cl_plainr, name);
258 if (ret)
259 return ret;
260
261 cl->cl_rewind = 1;
262 cl->cl_next = &seq_next_plain;
263 cl->cl_lookup = &seq_lookup_plain;
264 cl->cl_num_entries = &seq_get_num_entries_plain;
265 cl->cl_close = &seq_close_plain;
266
267 return 0;
268 }
269
270 int
271 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name)
272 {
273 int ret;
274 struct _citrus_lookup *cl;
275
276 cl = malloc(sizeof(*cl));
277 if (cl == NULL)
278 return errno;
279
280 cl->cl_key = NULL;
281 cl->cl_keylen = 0;
282 ret = seq_open_db(cl, name);
283 if (ret == ENOENT)
284 ret = seq_open_plain(cl, name);
285 if (!ret)
286 *rcl = cl;
287 else
288 free(cl);
289
290 return ret;
291 }
292
293 void
294 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
295 {
296 cl->cl_rewind = 1;
297 free(cl->cl_key);
298 cl->cl_key = NULL;
299 cl->cl_keylen = 0;
300 }
301
302 int
303 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
304 struct _region *key, struct _region *data)
305 {
306 return (*cl->cl_next)(cl, key, data);
307 }
308
309 int
310 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
311 struct _region *data)
312 {
313 return (*cl->cl_lookup)(cl, key, data);
314 }
315
316 int
317 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
318 {
319 return (*cl->cl_num_entries)(cl);
320 }
321
322 void
323 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
324 {
325 free(cl->cl_key);
326 (*cl->cl_close)(cl);
327 }
328
329 char *
330 _citrus_lookup_simple(const char *name, const char *key,
331 char *linebuf, size_t linebufsize)
332 {
333 int ret;
334 struct _citrus_lookup *cl;
335 struct _region data;
336
337 ret = _citrus_lookup_seq_open(&cl, name);
338 if (ret)
339 return NULL;
340
341 ret = _citrus_lookup_seq_lookup(cl, key, &data);
342 if (ret) {
343 _citrus_lookup_seq_close(cl);
344 return NULL;
345 }
346
347 snprintf(linebuf, linebufsize, "%.*s",
348 (int)_region_size(&data), (const char *)_region_head(&data));
349
350 _citrus_lookup_seq_close(cl);
351
352 return linebuf;
353 }
354