xref: /freebsd/contrib/mtree/mtree.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*	$NetBSD: mtree.c,v 1.49 2014/04/24 17:22:41 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1990, 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 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__COPYRIGHT) && !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\
39  The Regents of the University of California.  All rights reserved.");
40 #endif /* not lint */
41 
42 #if defined(__RCSID) && !defined(lint)
43 #if 0
44 static char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
45 #else
46 __RCSID("$NetBSD: mtree.c,v 1.49 2014/04/24 17:22:41 christos Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "extern.h"
60 
61 int	ftsoptions = FTS_PHYSICAL;
62 int	bflag, dflag, eflag, iflag, jflag, lflag, mflag, nflag, qflag, rflag,
63 	sflag, tflag, uflag;
64 char	fullpath[MAXPATHLEN];
65 
66 static struct {
67 	enum flavor flavor;
68 	const char name[9];
69 } flavors[] = {
70 	{F_MTREE, "mtree"},
71 	{F_FREEBSD9, "freebsd9"},
72 	{F_NETBSD6, "netbsd6"},
73 };
74 
75 __dead static	void	usage(void);
76 
77 int
78 main(int argc, char **argv)
79 {
80 	int	ch, status;
81 	unsigned int	i;
82 	int	cflag, Cflag, Dflag, Uflag, wflag;
83 	char	*dir, *p;
84 	FILE	*spec1, *spec2;
85 
86 	setprogname(argv[0]);
87 
88 	cflag = Cflag = Dflag = Uflag = wflag = 0;
89 	dir = NULL;
90 	init_excludes();
91 	spec1 = stdin;
92 	spec2 = NULL;
93 
94 	while ((ch = getopt(argc, argv,
95 	    "bcCdDeE:f:F:I:ijk:K:lLmMnN:O:p:PqrR:s:StuUwWxX:"))
96 	    != -1) {
97 		switch((char)ch) {
98 		case 'b':
99 			bflag = 1;
100 			break;
101 		case 'c':
102 			cflag = 1;
103 			break;
104 		case 'C':
105 			Cflag = 1;
106 			break;
107 		case 'd':
108 			dflag = 1;
109 			break;
110 		case 'D':
111 			Dflag = 1;
112 			break;
113 		case 'E':
114 			parsetags(&excludetags, optarg);
115 			break;
116 		case 'e':
117 			eflag = 1;
118 			break;
119 		case 'f':
120 			if (spec1 == stdin) {
121 				spec1 = fopen(optarg, "r");
122 				if (spec1 == NULL)
123 					mtree_err("%s: %s", optarg,
124 					    strerror(errno));
125 			} else if (spec2 == NULL) {
126 				spec2 = fopen(optarg, "r");
127 				if (spec2 == NULL)
128 					mtree_err("%s: %s", optarg,
129 					    strerror(errno));
130 			} else
131 				usage();
132 			break;
133 		case 'F':
134 			for (i = 0; i < __arraycount(flavors); i++)
135 				if (strcmp(optarg, flavors[i].name) == 0) {
136 					flavor = flavors[i].flavor;
137 					break;
138 				}
139 			if (i == __arraycount(flavors))
140 				usage();
141 			break;
142 		case 'i':
143 			iflag = 1;
144 			break;
145 		case 'I':
146 			parsetags(&includetags, optarg);
147 			break;
148 		case 'j':
149 			jflag = 1;
150 			break;
151 		case 'k':
152 			keys = F_TYPE;
153 			while ((p = strsep(&optarg, " \t,")) != NULL)
154 				if (*p != '\0')
155 					keys |= parsekey(p, NULL);
156 			break;
157 		case 'K':
158 			while ((p = strsep(&optarg, " \t,")) != NULL)
159 				if (*p != '\0')
160 					keys |= parsekey(p, NULL);
161 			break;
162 		case 'l':
163 			lflag = 1;
164 			break;
165 		case 'L':
166 			ftsoptions &= ~FTS_PHYSICAL;
167 			ftsoptions |= FTS_LOGICAL;
168 			break;
169 		case 'm':
170 			mflag = 1;
171 			break;
172 		case 'M':
173 			mtree_Mflag = 1;
174 			break;
175 		case 'n':
176 			nflag = 1;
177 			break;
178 		case 'N':
179 			if (! setup_getid(optarg))
180 				mtree_err(
181 			    "Unable to use user and group databases in `%s'",
182 				    optarg);
183 			break;
184 		case 'O':
185 			load_only(optarg);
186 			break;
187 		case 'p':
188 			dir = optarg;
189 			break;
190 		case 'P':
191 			ftsoptions &= ~FTS_LOGICAL;
192 			ftsoptions |= FTS_PHYSICAL;
193 			break;
194 		case 'q':
195 			qflag = 1;
196 			break;
197 		case 'r':
198 			rflag = 1;
199 			break;
200 		case 'R':
201 			while ((p = strsep(&optarg, " \t,")) != NULL)
202 				if (*p != '\0')
203 					keys &= ~parsekey(p, NULL);
204 			break;
205 		case 's':
206 			sflag = 1;
207 			crc_total = ~strtol(optarg, &p, 0);
208 			if (*p)
209 				mtree_err("illegal seed value -- %s", optarg);
210 			break;
211 		case 'S':
212 			mtree_Sflag = 1;
213 			break;
214 		case 't':
215 			tflag = 1;
216 			break;
217 		case 'u':
218 			uflag = 1;
219 			break;
220 		case 'U':
221 			Uflag = uflag = 1;
222 			break;
223 		case 'w':
224 			wflag = 1;
225 			break;
226 		case 'W':
227 			mtree_Wflag = 1;
228 			break;
229 		case 'x':
230 			ftsoptions |= FTS_XDEV;
231 			break;
232 		case 'X':
233 			read_excludes_file(optarg);
234 			break;
235 		case '?':
236 		default:
237 			usage();
238 		}
239 	}
240 	argc -= optind;
241 	argv += optind;
242 
243 	if (argc)
244 		usage();
245 
246 	switch (flavor) {
247 	case F_FREEBSD9:
248 		if (cflag && iflag) {
249 			warnx("-c and -i passed, replacing -i with -j for "
250 			    "FreeBSD compatibility");
251 			iflag = 0;
252 			jflag = 1;
253 		}
254 		if (dflag && !bflag) {
255 			warnx("Adding -b to -d for FreeBSD compatibility");
256 			bflag = 1;
257 		}
258 		if (uflag && !iflag) {
259 			warnx("Adding -i to -%c for FreeBSD compatibility",
260 			    Uflag ? 'U' : 'u');
261 			iflag = 1;
262 		}
263 		if (uflag && !tflag) {
264 			warnx("Adding -t to -%c for FreeBSD compatibility",
265 			    Uflag ? 'U' : 'u');
266 			tflag = 1;
267 		}
268 		if (wflag)
269 			warnx("The -w flag is a no-op");
270 		break;
271 	default:
272 		if (wflag)
273 			usage();
274 	}
275 
276 	if (spec2 && (cflag || Cflag || Dflag))
277 		mtree_err("Double -f, -c, -C and -D flags are mutually "
278 		    "exclusive");
279 
280 	if (dir && spec2)
281 		mtree_err("Double -f and -p flags are mutually exclusive");
282 
283 	if (dir && chdir(dir))
284 		mtree_err("%s: %s", dir, strerror(errno));
285 
286 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
287 		mtree_err("%s", strerror(errno));
288 
289 	if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
290 		mtree_err("-c, -C and -D flags are mutually exclusive");
291 
292 	if (iflag && mflag)
293 		mtree_err("-i and -m flags are mutually exclusive");
294 
295 	if (lflag && uflag)
296 		mtree_err("-l and -u flags are mutually exclusive");
297 
298 	if (cflag) {
299 		cwalk(stdout);
300 		exit(0);
301 	}
302 	if (Cflag || Dflag) {
303 		dump_nodes(stdout, "", spec(spec1), Dflag);
304 		exit(0);
305 	}
306 	if (spec2 != NULL)
307 		status = mtree_specspec(spec1, spec2);
308 	else
309 		status = verify(spec1);
310 	if (Uflag && (status == MISMATCHEXIT))
311 		status = 0;
312 	exit(status);
313 }
314 
315 static void
316 usage(void)
317 {
318 	unsigned int i;
319 
320 	fprintf(stderr,
321 	    "usage: %s [-bCcDdejLlMnPqrStUuWx] [-i|-m] [-E tags]\n"
322 	    "\t\t[-f spec] [-f spec]\n"
323 	    "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
324 	    "\t\t[-R keywords] [-s seed] [-X exclude-file]\n"
325 	    "\t\t[-F flavor]\n",
326 	    getprogname());
327 	fprintf(stderr, "\nflavors:");
328 	for (i = 0; i < __arraycount(flavors); i++)
329 		fprintf(stderr, " %s", flavors[i].name);
330 	fprintf(stderr, "\n");
331 	exit(1);
332 }
333