xref: /freebsd/contrib/mtree/misc.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*	$NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
32  */
33 
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37 
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $");
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "extern.h"
52 
53 enum flavor	flavor = F_MTREE;
54 
55 typedef struct _key {
56 	const char	*name;		/* key name */
57 	u_int		val;		/* value */
58 
59 #define	NEEDVALUE	0x01
60 	u_int		flags;
61 } KEY;
62 
63 /* NB: the following tables must be sorted lexically. */
64 static KEY keylist[] = {
65 	{"cksum",	F_CKSUM,	NEEDVALUE},
66 	{"device",	F_DEV,		NEEDVALUE},
67 	{"flags",	F_FLAGS,	NEEDVALUE},
68 	{"gid",		F_GID,		NEEDVALUE},
69 	{"gname",	F_GNAME,	NEEDVALUE},
70 	{"ignore",	F_IGN,		0},
71 	{"link",	F_SLINK,	NEEDVALUE},
72 	{"md5",		F_MD5,		NEEDVALUE},
73 	{"md5digest",	F_MD5,		NEEDVALUE},
74 	{"mode",	F_MODE,		NEEDVALUE},
75 	{"nlink",	F_NLINK,	NEEDVALUE},
76 	{"nochange",	F_NOCHANGE,	0},
77 	{"optional",	F_OPT,		0},
78 	{"ripemd160digest", F_RMD160,	NEEDVALUE},
79 	{"rmd160",	F_RMD160,	NEEDVALUE},
80 	{"rmd160digest",F_RMD160,	NEEDVALUE},
81 	{"sha1",	F_SHA1,		NEEDVALUE},
82 	{"sha1digest",	F_SHA1,		NEEDVALUE},
83 	{"sha256",	F_SHA256,	NEEDVALUE},
84 	{"sha256digest",F_SHA256,	NEEDVALUE},
85 	{"sha384",	F_SHA384,	NEEDVALUE},
86 	{"sha384digest",F_SHA384,	NEEDVALUE},
87 	{"sha512",	F_SHA512,	NEEDVALUE},
88 	{"sha512digest",F_SHA512,	NEEDVALUE},
89 	{"size",	F_SIZE,		NEEDVALUE},
90 	{"tags",	F_TAGS,		NEEDVALUE},
91 	{"time",	F_TIME,		NEEDVALUE},
92 	{"type",	F_TYPE,		NEEDVALUE},
93 	{"uid",		F_UID,		NEEDVALUE},
94 	{"uname",	F_UNAME,	NEEDVALUE}
95 };
96 
97 static KEY typelist[] = {
98 	{"block",	F_BLOCK,	0},
99 	{"char",	F_CHAR,		0},
100 	{"dir",		F_DIR,		0},
101 #ifdef S_IFDOOR
102 	{"door",	F_DOOR,		0},
103 #endif
104 	{"fifo",	F_FIFO,		0},
105 	{"file",	F_FILE,		0},
106 	{"link",	F_LINK,		0},
107 	{"socket",	F_SOCK,		0},
108 };
109 
110 slist_t	excludetags, includetags;
111 int	keys = KEYDEFAULT;
112 
113 
114 int keycompare(const void *, const void *);
115 
116 u_int
117 parsekey(const char *name, int *needvaluep)
118 {
119 	static int allbits;
120 	KEY *k, tmp;
121 
122 	if (allbits == 0) {
123 		size_t i;
124 
125 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
126 			allbits |= keylist[i].val;
127 	}
128 	tmp.name = name;
129 	if (strcmp(name, "all") == 0)
130 		return (allbits);
131 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
132 	    sizeof(KEY), keycompare);
133 	if (k == NULL)
134 		mtree_err("unknown keyword `%s'", name);
135 
136 	if (needvaluep)
137 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
138 
139 	return (k->val);
140 }
141 
142 u_int
143 parsetype(const char *name)
144 {
145 	KEY *k, tmp;
146 
147 	tmp.name = name;
148 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
149 	    sizeof(KEY), keycompare);
150 	if (k == NULL)
151 		mtree_err("unknown file type `%s'", name);
152 
153 	return (k->val);
154 }
155 
156 int
157 keycompare(const void *a, const void *b)
158 {
159 
160 	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
161 }
162 
163 void
164 mtree_err(const char *fmt, ...)
165 {
166 	va_list ap;
167 
168 	va_start(ap, fmt);
169 	vwarnx(fmt, ap);
170 	va_end(ap);
171 	if (mtree_lineno)
172 		warnx("failed at line %lu of the specification",
173 		    (u_long) mtree_lineno);
174 	exit(1);
175 	/* NOTREACHED */
176 }
177 
178 void
179 addtag(slist_t *list, char *elem)
180 {
181 
182 #define	TAG_CHUNK 20
183 
184 	if ((list->count % TAG_CHUNK) == 0) {
185 		char **new;
186 
187 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
188 		    * sizeof(char *));
189 		if (new == NULL)
190 			mtree_err("memory allocation error");
191 		list->list = new;
192 	}
193 	list->list[list->count] = elem;
194 	list->count++;
195 }
196 
197 void
198 parsetags(slist_t *list, char *args)
199 {
200 	char	*p, *e;
201 	int	len;
202 
203 	if (args == NULL) {
204 		addtag(list, NULL);
205 		return;
206 	}
207 	while ((p = strsep(&args, ",")) != NULL) {
208 		if (*p == '\0')
209 			continue;
210 		len = strlen(p) + 3;	/* "," + p + ",\0" */
211 		if ((e = malloc(len)) == NULL)
212 			mtree_err("memory allocation error");
213 		snprintf(e, len, ",%s,", p);
214 		addtag(list, e);
215 	}
216 }
217 
218 /*
219  * matchtags
220  *	returns 0 if there's a match from the exclude list in the node's tags,
221  *	or there's an include list and no match.
222  *	return 1 otherwise.
223  */
224 int
225 matchtags(NODE *node)
226 {
227 	int	i;
228 
229 	if (node->tags) {
230 		for (i = 0; i < excludetags.count; i++)
231 			if (strstr(node->tags, excludetags.list[i]))
232 				break;
233 		if (i < excludetags.count)
234 			return (0);
235 
236 		for (i = 0; i < includetags.count; i++)
237 			if (strstr(node->tags, includetags.list[i]))
238 				break;
239 		if (i > 0 && i == includetags.count)
240 			return (0);
241 	} else if (includetags.count > 0) {
242 		return (0);
243 	}
244 	return (1);
245 }
246 
247 u_int
248 nodetoino(u_int type)
249 {
250 
251 	switch (type) {
252 	case F_BLOCK:
253 		return S_IFBLK;
254 	case F_CHAR:
255 		return S_IFCHR;
256 	case F_DIR:
257 		return S_IFDIR;
258 	case F_FIFO:
259 		return S_IFIFO;
260 	case F_FILE:
261 		return S_IFREG;
262 	case F_LINK:
263 		return S_IFLNK;
264 #ifdef S_IFSOCK
265 	case F_SOCK:
266 		return S_IFSOCK;
267 #endif
268 	default:
269 		printf("unknown type %d", type);
270 		abort();
271 	}
272 	/* NOTREACHED */
273 }
274 
275 const char *
276 nodetype(u_int type)
277 {
278 
279 	return (inotype(nodetoino(type)));
280 }
281 
282 
283 const char *
284 inotype(u_int type)
285 {
286 
287 	switch (type & S_IFMT) {
288 	case S_IFBLK:
289 		return ("block");
290 	case S_IFCHR:
291 		return ("char");
292 	case S_IFDIR:
293 		return ("dir");
294 	case S_IFIFO:
295 		return ("fifo");
296 	case S_IFREG:
297 		return ("file");
298 	case S_IFLNK:
299 		return ("link");
300 #ifdef S_IFSOCK
301 	case S_IFSOCK:
302 		return ("socket");
303 #endif
304 #ifdef S_IFDOOR
305 	case S_IFDOOR:
306 		return ("door");
307 #endif
308 	default:
309 		return ("unknown");
310 	}
311 	/* NOTREACHED */
312 }
313