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