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