only.c revision 1.2.4.2 1 1.2.4.2 tls /* $NetBSD: only.c,v 1.2.4.2 2013/02/25 00:30:46 tls Exp $ */
2 1.2.4.2 tls
3 1.2.4.2 tls /*-
4 1.2.4.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.4.2 tls * All rights reserved.
6 1.2.4.2 tls *
7 1.2.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 tls * by Christos Zoulas.
9 1.2.4.2 tls *
10 1.2.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 tls * modification, are permitted provided that the following conditions
12 1.2.4.2 tls * are met:
13 1.2.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.4.2 tls * 3. Neither the name of The NetBSD Foundation nor the names of its
19 1.2.4.2 tls * contributors may be used to endorse or promote products derived
20 1.2.4.2 tls * from this software without specific prior written permission.
21 1.2.4.2 tls *
22 1.2.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.2.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.2.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.2.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.2.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.2.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.2.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.2.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.2.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.2.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.2.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
33 1.2.4.2 tls */
34 1.2.4.2 tls #if HAVE_NBTOOL_CONFIG_H
35 1.2.4.2 tls #include "nbtool_config.h"
36 1.2.4.2 tls #endif
37 1.2.4.2 tls
38 1.2.4.2 tls #include <sys/cdefs.h>
39 1.2.4.2 tls
40 1.2.4.2 tls #if defined(__RCSID) && !defined(lint)
41 1.2.4.2 tls __RCSID("$NetBSD: only.c,v 1.2.4.2 2013/02/25 00:30:46 tls Exp $");
42 1.2.4.2 tls #endif
43 1.2.4.2 tls
44 1.2.4.2 tls #include <sys/param.h>
45 1.2.4.2 tls
46 1.2.4.2 tls #include <stdio.h>
47 1.2.4.2 tls #include <stdlib.h>
48 1.2.4.2 tls #include <string.h>
49 1.2.4.2 tls #include <stdbool.h>
50 1.2.4.2 tls #include <time.h>
51 1.2.4.2 tls #include <err.h>
52 1.2.4.2 tls #include <util.h>
53 1.2.4.2 tls
54 1.2.4.2 tls #include "extern.h"
55 1.2.4.2 tls
56 1.2.4.2 tls struct hentry {
57 1.2.4.2 tls char *str;
58 1.2.4.2 tls uint32_t hash;
59 1.2.4.2 tls struct hentry *next;
60 1.2.4.2 tls };
61 1.2.4.2 tls
62 1.2.4.2 tls static struct hentry *table[1024];
63 1.2.4.2 tls static bool loaded;
64 1.2.4.2 tls
65 1.2.4.2 tls static uint32_t
66 1.2.4.2 tls hash_str(const char *str)
67 1.2.4.2 tls {
68 1.2.4.2 tls const uint8_t *s = (const uint8_t *)str;
69 1.2.4.2 tls uint8_t c;
70 1.2.4.2 tls uint32_t hash = 0;
71 1.2.4.2 tls while ((c = *s++) != '\0')
72 1.2.4.2 tls hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */
73 1.2.4.2 tls return hash + (hash >> 5);
74 1.2.4.2 tls }
75 1.2.4.2 tls
76 1.2.4.2 tls static bool
77 1.2.4.2 tls hash_find(const char *str, uint32_t *h)
78 1.2.4.2 tls {
79 1.2.4.2 tls struct hentry *e;
80 1.2.4.2 tls *h = hash_str(str) % __arraycount(table);
81 1.2.4.2 tls
82 1.2.4.2 tls for (e = table[*h]; e; e = e->next)
83 1.2.4.2 tls if (e->hash == *h && strcmp(e->str, str) == 0)
84 1.2.4.2 tls return true;
85 1.2.4.2 tls return false;
86 1.2.4.2 tls }
87 1.2.4.2 tls
88 1.2.4.2 tls static void
89 1.2.4.2 tls hash_insert(char *str, uint32_t h)
90 1.2.4.2 tls {
91 1.2.4.2 tls struct hentry *e;
92 1.2.4.2 tls
93 1.2.4.2 tls if ((e = malloc(sizeof(*e))) == NULL)
94 1.2.4.2 tls mtree_err("memory allocation error");
95 1.2.4.2 tls
96 1.2.4.2 tls e->str = str;
97 1.2.4.2 tls e->hash = h;
98 1.2.4.2 tls e->next = table[h];
99 1.2.4.2 tls table[h] = e;
100 1.2.4.2 tls }
101 1.2.4.2 tls
102 1.2.4.2 tls static void
103 1.2.4.2 tls fill(char *str)
104 1.2.4.2 tls {
105 1.2.4.2 tls uint32_t h;
106 1.2.4.2 tls char *ptr = strrchr(str, '/');
107 1.2.4.2 tls
108 1.2.4.2 tls if (ptr == NULL)
109 1.2.4.2 tls return;
110 1.2.4.2 tls
111 1.2.4.2 tls *ptr = '\0';
112 1.2.4.2 tls if (!hash_find(str, &h)) {
113 1.2.4.2 tls char *x = strdup(str);
114 1.2.4.2 tls if (x == NULL)
115 1.2.4.2 tls mtree_err("memory allocation error");
116 1.2.4.2 tls hash_insert(x, h);
117 1.2.4.2 tls fill(str);
118 1.2.4.2 tls }
119 1.2.4.2 tls *ptr = '/';
120 1.2.4.2 tls }
121 1.2.4.2 tls
122 1.2.4.2 tls void
123 1.2.4.2 tls load_only(const char *fname)
124 1.2.4.2 tls {
125 1.2.4.2 tls FILE *fp;
126 1.2.4.2 tls char *line;
127 1.2.4.2 tls size_t len, lineno;
128 1.2.4.2 tls
129 1.2.4.2 tls if ((fp = fopen(fname, "r")) == NULL)
130 1.2.4.2 tls err(1, "Cannot open `%s'", fname);
131 1.2.4.2 tls
132 1.2.4.2 tls while ((line = fparseln(fp, &len, &lineno, NULL, FPARSELN_UNESCALL))) {
133 1.2.4.2 tls uint32_t h;
134 1.2.4.2 tls if (hash_find(line, &h))
135 1.2.4.2 tls err(1, "Duplicate entry %s", line);
136 1.2.4.2 tls hash_insert(line, h);
137 1.2.4.2 tls fill(line);
138 1.2.4.2 tls }
139 1.2.4.2 tls
140 1.2.4.2 tls fclose(fp);
141 1.2.4.2 tls loaded = true;
142 1.2.4.2 tls }
143 1.2.4.2 tls
144 1.2.4.2 tls bool
145 1.2.4.2 tls find_only(const char *path)
146 1.2.4.2 tls {
147 1.2.4.2 tls uint32_t h;
148 1.2.4.2 tls
149 1.2.4.2 tls if (!loaded)
150 1.2.4.2 tls return true;
151 1.2.4.2 tls return hash_find(path, &h);
152 1.2.4.2 tls }
153