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