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