xref: /minix3/usr.sbin/mtree/misc.c (revision 0b98e8aad89f2bd4ba80b523d73cf29e9dd82ce1)
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 #if 1 /* defined(__minix): LSC: our mkfs doesn't understand escaped names,
54 	 so always default to netbsd6 escaping mode. */
55 enum flavor	flavor = F_NETBSD6;
56 #else
57 enum flavor	flavor = F_MTREE;
58 #endif /* defined(__minix) */
59 typedef struct _key {
60 	const char	*name;		/* key name */
61 	u_int		val;		/* value */
62 
63 #define	NEEDVALUE	0x01
64 	u_int		flags;
65 } KEY;
66 
67 /* NB: the following tables must be sorted lexically. */
68 static KEY keylist[] = {
69 	{"cksum",	F_CKSUM,	NEEDVALUE},
70 	{"device",	F_DEV,		NEEDVALUE},
71 	{"flags",	F_FLAGS,	NEEDVALUE},
72 	{"gid",		F_GID,		NEEDVALUE},
73 	{"gname",	F_GNAME,	NEEDVALUE},
74 	{"ignore",	F_IGN,		0},
75 	{"link",	F_SLINK,	NEEDVALUE},
76 	{"md5",		F_MD5,		NEEDVALUE},
77 	{"md5digest",	F_MD5,		NEEDVALUE},
78 	{"mode",	F_MODE,		NEEDVALUE},
79 	{"nlink",	F_NLINK,	NEEDVALUE},
80 	{"nochange",	F_NOCHANGE,	0},
81 	{"optional",	F_OPT,		0},
82 	{"ripemd160digest", F_RMD160,	NEEDVALUE},
83 	{"rmd160",	F_RMD160,	NEEDVALUE},
84 	{"rmd160digest",F_RMD160,	NEEDVALUE},
85 	{"sha1",	F_SHA1,		NEEDVALUE},
86 	{"sha1digest",	F_SHA1,		NEEDVALUE},
87 	{"sha256",	F_SHA256,	NEEDVALUE},
88 	{"sha256digest",F_SHA256,	NEEDVALUE},
89 	{"sha384",	F_SHA384,	NEEDVALUE},
90 	{"sha384digest",F_SHA384,	NEEDVALUE},
91 	{"sha512",	F_SHA512,	NEEDVALUE},
92 	{"sha512digest",F_SHA512,	NEEDVALUE},
93 	{"size",	F_SIZE,		NEEDVALUE},
94 	{"tags",	F_TAGS,		NEEDVALUE},
95 	{"time",	F_TIME,		NEEDVALUE},
96 	{"type",	F_TYPE,		NEEDVALUE},
97 	{"uid",		F_UID,		NEEDVALUE},
98 	{"uname",	F_UNAME,	NEEDVALUE}
99 };
100 
101 static KEY typelist[] = {
102 	{"block",	F_BLOCK,	0},
103 	{"char",	F_CHAR,		0},
104 	{"dir",		F_DIR,		0},
105 #ifdef S_IFDOOR
106 	{"door",	F_DOOR,		0},
107 #endif
108 	{"fifo",	F_FIFO,		0},
109 	{"file",	F_FILE,		0},
110 	{"link",	F_LINK,		0},
111 	{"socket",	F_SOCK,		0},
112 };
113 
114 slist_t	excludetags, includetags;
115 int	keys = KEYDEFAULT;
116 
117 
118 int keycompare(const void *, const void *);
119 
120 u_int
121 parsekey(const char *name, int *needvaluep)
122 {
123 	static int allbits;
124 	KEY *k, tmp;
125 
126 	if (allbits == 0) {
127 		size_t i;
128 
129 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
130 			allbits |= keylist[i].val;
131 	}
132 	tmp.name = name;
133 	if (strcmp(name, "all") == 0)
134 		return (allbits);
135 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
136 	    sizeof(KEY), keycompare);
137 	if (k == NULL)
138 		mtree_err("unknown keyword `%s'", name);
139 
140 	if (needvaluep)
141 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
142 
143 	return (k->val);
144 }
145 
146 u_int
147 parsetype(const char *name)
148 {
149 	KEY *k, tmp;
150 
151 	tmp.name = name;
152 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
153 	    sizeof(KEY), keycompare);
154 	if (k == NULL)
155 		mtree_err("unknown file type `%s'", name);
156 
157 	return (k->val);
158 }
159 
160 int
161 keycompare(const void *a, const void *b)
162 {
163 
164 	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
165 }
166 
167 void
168 mtree_err(const char *fmt, ...)
169 {
170 	va_list ap;
171 
172 	va_start(ap, fmt);
173 	vwarnx(fmt, ap);
174 	va_end(ap);
175 	if (mtree_lineno)
176 		warnx("failed at line %lu of the specification",
177 		    (u_long) mtree_lineno);
178 	exit(1);
179 	/* NOTREACHED */
180 }
181 
182 void
183 addtag(slist_t *list, char *elem)
184 {
185 
186 #define	TAG_CHUNK 20
187 
188 	if ((list->count % TAG_CHUNK) == 0) {
189 		char **new;
190 
191 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
192 		    * sizeof(char *));
193 		if (new == NULL)
194 			mtree_err("memory allocation error");
195 		list->list = new;
196 	}
197 	list->list[list->count] = elem;
198 	list->count++;
199 }
200 
201 void
202 parsetags(slist_t *list, char *args)
203 {
204 	char	*p, *e;
205 	int	len;
206 
207 	if (args == NULL) {
208 		addtag(list, NULL);
209 		return;
210 	}
211 	while ((p = strsep(&args, ",")) != NULL) {
212 		if (*p == '\0')
213 			continue;
214 		len = strlen(p) + 3;	/* "," + p + ",\0" */
215 		if ((e = malloc(len)) == NULL)
216 			mtree_err("memory allocation error");
217 		snprintf(e, len, ",%s,", p);
218 		addtag(list, e);
219 	}
220 }
221 
222 /*
223  * matchtags
224  *	returns 0 if there's a match from the exclude list in the node's tags,
225  *	or there's an include list and no match.
226  *	return 1 otherwise.
227  */
228 int
229 matchtags(NODE *node)
230 {
231 	int	i;
232 
233 	if (node->tags) {
234 		for (i = 0; i < excludetags.count; i++)
235 			if (strstr(node->tags, excludetags.list[i]))
236 				break;
237 		if (i < excludetags.count)
238 			return (0);
239 
240 		for (i = 0; i < includetags.count; i++)
241 			if (strstr(node->tags, includetags.list[i]))
242 				break;
243 		if (i > 0 && i == includetags.count)
244 			return (0);
245 	} else if (includetags.count > 0) {
246 		return (0);
247 	}
248 	return (1);
249 }
250 
251 u_int
252 nodetoino(u_int type)
253 {
254 
255 	switch (type) {
256 	case F_BLOCK:
257 		return S_IFBLK;
258 	case F_CHAR:
259 		return S_IFCHR;
260 	case F_DIR:
261 		return S_IFDIR;
262 	case F_FIFO:
263 		return S_IFIFO;
264 	case F_FILE:
265 		return S_IFREG;
266 	case F_LINK:
267 		return S_IFLNK;
268 #ifdef S_IFSOCK
269 	case F_SOCK:
270 		return S_IFSOCK;
271 #endif
272 	default:
273 		printf("unknown type %d", type);
274 		abort();
275 	}
276 	/* NOTREACHED */
277 }
278 
279 const char *
280 nodetype(u_int type)
281 {
282 
283 	return (inotype(nodetoino(type)));
284 }
285 
286 
287 const char *
288 inotype(u_int type)
289 {
290 
291 	switch (type & S_IFMT) {
292 	case S_IFBLK:
293 		return ("block");
294 	case S_IFCHR:
295 		return ("char");
296 	case S_IFDIR:
297 		return ("dir");
298 	case S_IFIFO:
299 		return ("fifo");
300 	case S_IFREG:
301 		return ("file");
302 	case S_IFLNK:
303 		return ("link");
304 #ifdef S_IFSOCK
305 	case S_IFSOCK:
306 		return ("socket");
307 #endif
308 #ifdef S_IFDOOR
309 	case S_IFDOOR:
310 		return ("door");
311 #endif
312 	default:
313 		return ("unknown");
314 	}
315 	/* NOTREACHED */
316 }
317