xref: /netbsd-src/usr.sbin/mtree/verify.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: verify.c,v 1.10 1995/03/07 21:26:28 cgd Exp $	*/
2 
3 /*-
4  * Copyright (c) 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)verify.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: verify.c,v 1.10 1995/03/07 21:26:28 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <dirent.h>
47 #include <fts.h>
48 #include <fnmatch.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include "mtree.h"
53 #include "extern.h"
54 
55 extern int crc_total, ftsoptions;
56 extern int dflag, eflag, rflag, sflag, uflag;
57 extern char fullpath[MAXPATHLEN];
58 
59 static NODE *root;
60 static char path[MAXPATHLEN];
61 
62 static void	miss __P((NODE *, char *));
63 static int	vwalk __P((void));
64 
65 int
66 verify()
67 {
68 	int rval;
69 
70 	root = spec();
71 	rval = vwalk();
72 	miss(root, path);
73 	return (rval);
74 }
75 
76 static int
77 vwalk()
78 {
79 	register FTS *t;
80 	register FTSENT *p;
81 	register NODE *ep, *level;
82 	int ftsdepth, specdepth, rval;
83 	char *argv[2];
84 
85 	argv[0] = ".";
86 	argv[1] = NULL;
87 	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
88 		err("fts_open: %s", strerror(errno));
89 	level = root;
90 	ftsdepth = specdepth = rval = 0;
91 	while (p = fts_read(t)) {
92 		switch(p->fts_info) {
93 		case FTS_D:
94 			++ftsdepth;
95 			break;
96 		case FTS_DP:
97 			--ftsdepth;
98 			if (specdepth > ftsdepth) {
99 				for (level = level->parent; level->prev;
100 				      level = level->prev);
101 				--specdepth;
102 			}
103 			continue;
104 		case FTS_DNR:
105 		case FTS_ERR:
106 		case FTS_NS:
107 			(void)fprintf(stderr, "mtree: %s: %s\n",
108 			    RP(p), strerror(errno));
109 			continue;
110 		default:
111 			if (dflag)
112 				continue;
113 		}
114 
115 		for (ep = level; ep; ep = ep->next)
116 			if (ep->flags & F_MAGIC &&
117 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME) ||
118 			    !strcmp(ep->name, p->fts_name)) {
119 				ep->flags |= F_VISIT;
120 				if (compare(ep->name, ep, p))
121 					rval = MISMATCHEXIT;
122 				if (ep->flags & F_IGN)
123 					(void)fts_set(t, p, FTS_SKIP);
124 				else if (ep->child && ep->type == F_DIR &&
125 				    p->fts_info == FTS_D) {
126 					level = ep->child;
127 					++specdepth;
128 				}
129 				break;
130 			}
131 
132 		if (ep)
133 			continue;
134 		if (!eflag) {
135 			(void)printf("extra: %s", RP(p));
136 			if (rflag) {
137 				if (unlink(p->fts_accpath)) {
138 					(void)printf(", not removed: %s",
139 					    strerror(errno));
140 				} else
141 					(void)printf(", removed");
142 			}
143 			(void)putchar('\n');
144 		}
145 		(void)fts_set(t, p, FTS_SKIP);
146 	}
147 	(void)fts_close(t);
148 	if (sflag)
149 		(void)fprintf(stderr,
150 		    "mtree: %s checksum: %lu\n", fullpath, crc_total);
151 	return (rval);
152 }
153 
154 static void
155 miss(p, tail)
156 	register NODE *p;
157 	register char *tail;
158 {
159 	register int create;
160 	register char *tp;
161 
162 	for (; p; p = p->next) {
163 		if (p->flags & F_OPT && !(p->flags & F_VISIT))
164 			continue;
165 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
166 			continue;
167 		(void)strcpy(tail, p->name);
168 		if (!(p->flags & F_VISIT))
169 			(void)printf("missing: %s", path);
170 		if (p->type != F_DIR) {
171 			putchar('\n');
172 			continue;
173 		}
174 
175 		create = 0;
176 		if (!(p->flags & F_VISIT) && uflag)
177 			if (!(p->flags & (F_UID | F_UNAME)))
178 			    (void)printf(" (not created: user not specified)");
179 			else if (!(p->flags & (F_GID | F_GNAME)))
180 			    (void)printf(" (not created: group not specified)");
181 			else if (!(p->flags & F_MODE))
182 			    (void)printf(" (not created: mode not specified)");
183 			else if (mkdir(path, S_IRWXU))
184 				(void)printf(" (not created: %s)",
185 				    strerror(errno));
186 			else {
187 				create = 1;
188 				(void)printf(" (created)");
189 			}
190 
191 		if (!(p->flags & F_VISIT))
192 			(void)putchar('\n');
193 
194 		for (tp = tail; *tp; ++tp);
195 		*tp = '/';
196 		miss(p->child, tp + 1);
197 		*tp = '\0';
198 
199 		if (!create)
200 			continue;
201 		if (chown(path, p->st_uid, p->st_gid)) {
202 			(void)printf("%s: user/group/mode not modified: %s\n",
203 			    path, strerror(errno));
204 			continue;
205 		}
206 		if (chmod(path, p->st_mode))
207 			(void)printf("%s: permissions not set: %s\n",
208 			    path, strerror(errno));
209 	}
210 }
211