xref: /dragonfly/usr.sbin/mtree/mtree.c (revision 8e11cefe)
1 /*	@(#)mtree.c	8.1 (Berkeley) 6/6/93	*/
2 /*	$NetBSD: mtree.c,v 1.50 2015/01/23 02:27:01 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "extern.h"
43 
44 int	ftsoptions = FTS_PHYSICAL;
45 int	bflag, dflag, eflag, iflag, jflag, lflag, mflag, nflag, qflag, rflag,
46 	sflag, tflag, uflag;
47 char	fullpath[MAXPATHLEN];
48 
49 static struct {
50 	enum flavor flavor;
51 	const char name[9];
52 } flavors[] = {
53 	{F_MTREE, "mtree"},
54 	{F_FREEBSD9, "freebsd9"},
55 	{F_NETBSD6, "netbsd6"},
56 };
57 
58 __dead2 static	void	usage(void);
59 
60 int
61 main(int argc, char **argv)
62 {
63 	int	ch, status;
64 	unsigned int	i;
65 	int	cflag, Cflag, Dflag, Uflag, wflag;
66 	char	*dir, *p;
67 	FILE	*spec1, *spec2;
68 
69 	setprogname(argv[0]);
70 
71 	cflag = Cflag = Dflag = Uflag = wflag = 0;
72 	dir = NULL;
73 	init_excludes();
74 	spec1 = stdin;
75 	spec2 = NULL;
76 
77 	while ((ch = getopt(argc, argv,
78 	    "bcCdDeE:f:F:I:ijk:K:lLmMnN:O:p:PqrR:s:StuUwWxX:"))
79 	    != -1) {
80 		switch((char)ch) {
81 		case 'b':
82 			bflag = 1;
83 			break;
84 		case 'c':
85 			cflag = 1;
86 			break;
87 		case 'C':
88 			Cflag = 1;
89 			break;
90 		case 'd':
91 			dflag = 1;
92 			break;
93 		case 'D':
94 			Dflag = 1;
95 			break;
96 		case 'E':
97 			parsetags(&excludetags, optarg);
98 			break;
99 		case 'e':
100 			eflag = 1;
101 			break;
102 		case 'f':
103 			if (spec1 == stdin) {
104 				spec1 = fopen(optarg, "r");
105 				if (spec1 == NULL)
106 					mtree_err("%s: %s", optarg,
107 					    strerror(errno));
108 			} else if (spec2 == NULL) {
109 				spec2 = fopen(optarg, "r");
110 				if (spec2 == NULL)
111 					mtree_err("%s: %s", optarg,
112 					    strerror(errno));
113 			} else
114 				usage();
115 			break;
116 		case 'F':
117 			for (i = 0; i < NELEM(flavors); i++)
118 				if (strcmp(optarg, flavors[i].name) == 0) {
119 					flavor = flavors[i].flavor;
120 					break;
121 				}
122 			if (i == NELEM(flavors))
123 				usage();
124 			break;
125 		case 'i':
126 			iflag = 1;
127 			break;
128 		case 'I':
129 			parsetags(&includetags, optarg);
130 			break;
131 		case 'j':
132 			jflag = 1;
133 			break;
134 		case 'k':
135 			keys = F_TYPE;
136 			while ((p = strsep(&optarg, " \t,")) != NULL)
137 				if (*p != '\0')
138 					keys |= parsekey(p, NULL);
139 			break;
140 		case 'K':
141 			while ((p = strsep(&optarg, " \t,")) != NULL)
142 				if (*p != '\0')
143 					keys |= parsekey(p, NULL);
144 			break;
145 		case 'l':
146 			lflag = 1;
147 			break;
148 		case 'L':
149 			ftsoptions &= ~FTS_PHYSICAL;
150 			ftsoptions |= FTS_LOGICAL;
151 			break;
152 		case 'm':
153 			mflag = 1;
154 			break;
155 		case 'M':
156 			mtree_Mflag = 1;
157 			break;
158 		case 'n':
159 			nflag = 1;
160 			break;
161 		case 'N':
162 			if (! setup_getid(optarg))
163 				mtree_err(
164 			    "Unable to use user and group databases in `%s'",
165 				    optarg);
166 			break;
167 		case 'O':
168 			load_only(optarg);
169 			break;
170 		case 'p':
171 			dir = optarg;
172 			break;
173 		case 'P':
174 			ftsoptions &= ~FTS_LOGICAL;
175 			ftsoptions |= FTS_PHYSICAL;
176 			break;
177 		case 'q':
178 			qflag = 1;
179 			break;
180 		case 'r':
181 			rflag++;
182 			break;
183 		case 'R':
184 			while ((p = strsep(&optarg, " \t,")) != NULL)
185 				if (*p != '\0')
186 					keys &= ~parsekey(p, NULL);
187 			break;
188 		case 's':
189 			sflag = 1;
190 			crc_total = ~strtol(optarg, &p, 0);
191 			if (*p)
192 				mtree_err("illegal seed value -- %s", optarg);
193 			break;
194 		case 'S':
195 			mtree_Sflag = 1;
196 			break;
197 		case 't':
198 			tflag = 1;
199 			break;
200 		case 'u':
201 			uflag = 1;
202 			break;
203 		case 'U':
204 			Uflag = uflag = 1;
205 			break;
206 		case 'w':
207 			wflag = 1;
208 			break;
209 		case 'W':
210 			mtree_Wflag = 1;
211 			break;
212 		case 'x':
213 			ftsoptions |= FTS_XDEV;
214 			break;
215 		case 'X':
216 			read_excludes_file(optarg);
217 			break;
218 		case '?':
219 		default:
220 			usage();
221 		}
222 	}
223 	argc -= optind;
224 	argv += optind;
225 
226 	if (argc)
227 		usage();
228 
229 	switch (flavor) {
230 	case F_FREEBSD9:
231 		if (cflag && iflag) {
232 			warnx("-c and -i passed, replacing -i with -j for "
233 			    "FreeBSD compatibility");
234 			iflag = 0;
235 			jflag = 1;
236 		}
237 		if (dflag && !bflag) {
238 			warnx("Adding -b to -d for FreeBSD compatibility");
239 			bflag = 1;
240 		}
241 		if (uflag && !iflag) {
242 			warnx("Adding -i to -%c for FreeBSD compatibility",
243 			    Uflag ? 'U' : 'u');
244 			iflag = 1;
245 		}
246 		if (uflag && !tflag) {
247 			warnx("Adding -t to -%c for FreeBSD compatibility",
248 			    Uflag ? 'U' : 'u');
249 			tflag = 1;
250 		}
251 		if (wflag)
252 			warnx("The -w flag is a no-op");
253 		break;
254 	default:
255 		if (wflag)
256 			usage();
257 	}
258 
259 	if (spec2 && (cflag || Cflag || Dflag))
260 		mtree_err("Double -f, -c, -C and -D flags are mutually "
261 		    "exclusive");
262 
263 	if (dir && spec2)
264 		mtree_err("Double -f and -p flags are mutually exclusive");
265 
266 	if (dir && chdir(dir))
267 		mtree_err("%s: %s", dir, strerror(errno));
268 
269 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
270 		mtree_err("%s", strerror(errno));
271 
272 	if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
273 		mtree_err("-c, -C and -D flags are mutually exclusive");
274 
275 	if (iflag && mflag)
276 		mtree_err("-i and -m flags are mutually exclusive");
277 
278 	if (lflag && uflag)
279 		mtree_err("-l and -u flags are mutually exclusive");
280 
281 	if (cflag) {
282 		cwalk(stdout);
283 		exit(0);
284 	}
285 	if (Cflag || Dflag) {
286 		dump_nodes(stdout, "", spec(spec1), Dflag);
287 		exit(0);
288 	}
289 	if (spec2 != NULL)
290 		status = mtree_specspec(spec1, spec2);
291 	else
292 		status = verify(spec1);
293 	if (Uflag && (status == MISMATCHEXIT))
294 		status = 0;
295 	exit(status);
296 }
297 
298 static void
299 usage(void)
300 {
301 	unsigned int i;
302 
303 	fprintf(stderr,
304 	    "usage: %s [-bCcDdejLlMnPqrStUuWx] [-i|-m] [-E tags]\n"
305 	    "\t\t[-f spec] [-f spec]\n"
306 	    "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
307 	    "\t\t[-R keywords] [-s seed] [-X exclude-file]\n"
308 	    "\t\t[-F flavor]\n",
309 	    getprogname());
310 	fprintf(stderr, "\nflavors:");
311 	for (i = 0; i < NELEM(flavors); i++)
312 		fprintf(stderr, " %s", flavors[i].name);
313 	fprintf(stderr, "\n");
314 	exit(1);
315 }
316