xref: /dragonfly/usr.sbin/mtree/mtree.c (revision 4942b0125f83d8ab6d88e28fc063efb8978bbfd3)
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
main(int argc,char ** argv)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 #ifdef BOOTSTRAPPING
163                               mtree_err("The -N is disabled in btools");
164 #else
165                               if (! setup_getid(optarg))
166                                         mtree_err(
167                                   "Unable to use user and group databases in `%s'",
168                                             optarg);
169 #endif
170                               break;
171                     case 'O':
172                               load_only(optarg);
173                               break;
174                     case 'p':
175                               dir = optarg;
176                               break;
177                     case 'P':
178                               ftsoptions &= ~FTS_LOGICAL;
179                               ftsoptions |= FTS_PHYSICAL;
180                               break;
181                     case 'q':
182                               qflag = 1;
183                               break;
184                     case 'r':
185                               rflag++;
186                               break;
187                     case 'R':
188                               while ((p = strsep(&optarg, " \t,")) != NULL)
189                                         if (*p != '\0')
190                                                   keys &= ~parsekey(p, NULL);
191                               break;
192                     case 's':
193                               sflag = 1;
194                               crc_total = ~strtol(optarg, &p, 0);
195                               if (*p)
196                                         mtree_err("illegal seed value -- %s", optarg);
197                               break;
198                     case 'S':
199                               mtree_Sflag = 1;
200                               break;
201                     case 't':
202                               tflag = 1;
203                               break;
204                     case 'u':
205                               uflag = 1;
206                               break;
207                     case 'U':
208                               Uflag = uflag = 1;
209                               break;
210                     case 'w':
211                               wflag = 1;
212                               break;
213                     case 'W':
214                               mtree_Wflag = 1;
215                               break;
216                     case 'x':
217                               ftsoptions |= FTS_XDEV;
218                               break;
219                     case 'X':
220                               read_excludes_file(optarg);
221                               break;
222                     case '?':
223                     default:
224                               usage();
225                     }
226           }
227           argc -= optind;
228           argv += optind;
229 
230           if (argc)
231                     usage();
232 
233           switch (flavor) {
234           case F_FREEBSD9:
235                     if (cflag && iflag) {
236                               warnx("-c and -i passed, replacing -i with -j for "
237                                   "FreeBSD compatibility");
238                               iflag = 0;
239                               jflag = 1;
240                     }
241                     if (dflag && !bflag) {
242                               warnx("Adding -b to -d for FreeBSD compatibility");
243                               bflag = 1;
244                     }
245                     if (uflag && !iflag) {
246                               warnx("Adding -i to -%c for FreeBSD compatibility",
247                                   Uflag ? 'U' : 'u');
248                               iflag = 1;
249                     }
250                     if (uflag && !tflag) {
251                               warnx("Adding -t to -%c for FreeBSD compatibility",
252                                   Uflag ? 'U' : 'u');
253                               tflag = 1;
254                     }
255                     if (wflag)
256                               warnx("The -w flag is a no-op");
257                     break;
258           default:
259                     if (wflag)
260                               usage();
261           }
262 
263           if (spec2 && (cflag || Cflag || Dflag))
264                     mtree_err("Double -f, -c, -C and -D flags are mutually "
265                         "exclusive");
266 
267           if (dir && spec2)
268                     mtree_err("Double -f and -p flags are mutually exclusive");
269 
270           if (dir && chdir(dir))
271                     mtree_err("%s: %s", dir, strerror(errno));
272 
273           if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
274                     mtree_err("%s", strerror(errno));
275 
276           if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
277                     mtree_err("-c, -C and -D flags are mutually exclusive");
278 
279           if (iflag && mflag)
280                     mtree_err("-i and -m flags are mutually exclusive");
281 
282           if (lflag && uflag)
283                     mtree_err("-l and -u flags are mutually exclusive");
284 
285           if (cflag) {
286                     cwalk(stdout);
287                     exit(0);
288           }
289           if (Cflag || Dflag) {
290                     dump_nodes(stdout, "", spec(spec1), Dflag);
291                     exit(0);
292           }
293           if (spec2 != NULL)
294                     status = mtree_specspec(spec1, spec2);
295           else
296                     status = verify(spec1);
297           if (Uflag && (status == MISMATCHEXIT))
298                     status = 0;
299           exit(status);
300 }
301 
302 static void
usage(void)303 usage(void)
304 {
305           unsigned int i;
306 
307           fprintf(stderr,
308               "usage: %s [-bCcDdejLlMnPqrStUuWx] [-i|-m] [-E tags]\n"
309               "\t\t[-f spec] [-f spec]\n"
310               "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
311               "\t\t[-R keywords] [-s seed] [-X exclude-file]\n"
312               "\t\t[-F flavor]\n",
313               getprogname());
314           fprintf(stderr, "\nflavors:");
315           for (i = 0; i < NELEM(flavors); i++)
316                     fprintf(stderr, " %s", flavors[i].name);
317           fprintf(stderr, "\n");
318           exit(1);
319 }
320