xref: /netbsd/usr.sbin/mtree/verify.c (revision bf9ec67e)
1 /*	$NetBSD: verify.c,v 1.30 2002/02/08 18:15:12 tv 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 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)verify.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: verify.c,v 1.30 2002/02/08 18:15:12 tv Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #if !HAVE_CONFIG_H
49 #include <dirent.h>
50 #endif
51 
52 #include <errno.h>
53 #include <fnmatch.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "extern.h"
59 
60 static NODE *root;
61 static char path[MAXPATHLEN];
62 
63 static void	miss(NODE *, char *);
64 static int	vwalk(void);
65 
66 int
67 verify(void)
68 {
69 	int rval;
70 
71 	root = spec(stdin);
72 	rval = vwalk();
73 	miss(root, path);
74 	return (rval);
75 }
76 
77 static int
78 vwalk(void)
79 {
80 	FTS *t;
81 	FTSENT *p;
82 	NODE *ep, *level;
83 	int specdepth, rval;
84 	char  dot[] = ".";		/* XXX: work around gcc warning */
85 	char *argv[] = { dot, NULL };
86 
87 	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
88 		mtree_err("fts_open: %s", strerror(errno));
89 	level = root;
90 	specdepth = rval = 0;
91 	while ((p = fts_read(t)) != NULL) {
92 		if (check_excludes(p->fts_name, p->fts_path)) {
93 			fts_set(t, p, FTS_SKIP);
94 			continue;
95 		}
96 		switch(p->fts_info) {
97 		case FTS_D:
98 		case FTS_SL:
99 			break;
100 		case FTS_DP:
101 			if (specdepth > p->fts_level) {
102 				for (level = level->parent; level->prev;
103 				    level = level->prev)
104 					continue;
105 				--specdepth;
106 			}
107 			continue;
108 		case FTS_DNR:
109 		case FTS_ERR:
110 		case FTS_NS:
111 			warnx("%s: %s", RP(p), strerror(p->fts_errno));
112 			continue;
113 		default:
114 			if (dflag)
115 				continue;
116 		}
117 
118 		if (specdepth != p->fts_level)
119 			goto extra;
120 		for (ep = level; ep; ep = ep->next)
121 			if ((ep->flags & F_MAGIC &&
122 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
123 			    !strcmp(ep->name, p->fts_name)) {
124 				ep->flags |= F_VISIT;
125 				if (compare(ep, p))
126 					rval = MISMATCHEXIT;
127 				if (!(ep->flags & F_IGN) &&
128 				    ep->child && ep->type == F_DIR &&
129 				    p->fts_info == FTS_D) {
130 					level = ep->child;
131 					++specdepth;
132 				} else
133 					fts_set(t, p, FTS_SKIP);
134 				break;
135 			}
136 
137 		if (ep)
138 			continue;
139  extra:
140 		if (!eflag) {
141 			printf("extra: %s", RP(p));
142 			if (rflag) {
143 				if ((S_ISDIR(p->fts_statp->st_mode)
144 				    ? rmdir : unlink)(p->fts_accpath)) {
145 					printf(", not removed: %s",
146 					    strerror(errno));
147 				} else
148 					printf(", removed");
149 			}
150 			putchar('\n');
151 		}
152 		fts_set(t, p, FTS_SKIP);
153 	}
154 	fts_close(t);
155 	if (sflag)
156 		warnx("%s checksum: %u", fullpath, crc_total);
157 	return (rval);
158 }
159 
160 static void
161 miss(NODE *p, char *tail)
162 {
163 	int create;
164 	char *tp;
165 	const char *type;
166 	u_int32_t flags;
167 
168 	for (; p; p = p->next) {
169 		if (p->flags & F_OPT && !(p->flags & F_VISIT))
170 			continue;
171 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
172 			continue;
173 		strcpy(tail, p->name);
174 		if (!(p->flags & F_VISIT))
175 			printf("missing: %s", path);
176 		switch (p->type) {
177 		case F_BLOCK:
178 		case F_CHAR:
179 			type = "device";
180 			break;
181 		case F_DIR:
182 			type = "directory";
183 			break;
184 		case F_LINK:
185 			type = "symlink";
186 			break;
187 		default:
188 			putchar('\n');
189 			continue;
190 		}
191 
192 		create = 0;
193 		if (!(p->flags & F_VISIT) && uflag) {
194 			if (Wflag || p->type == F_LINK)
195 				goto createit;
196 			if (!(p->flags & (F_UID | F_UNAME)))
197 			    printf(
198 				" (%s not created: user not specified)", type);
199 			else if (!(p->flags & (F_GID | F_GNAME)))
200 			    printf(
201 				" (%s not created: group not specified)", type);
202 			else if (!(p->flags & F_MODE))
203 			    printf(
204 				" (%s not created: mode not specified)", type);
205 			else
206  createit:
207 			switch (p->type) {
208 			case F_BLOCK:
209 			case F_CHAR:
210 				if (Wflag)
211 					continue;
212 				if (!(p->flags & F_DEV))
213 					printf(
214 				    " (%s not created: device not specified)",
215 					    type);
216 				else if (mknod(path,
217 				    p->st_mode | nodetoino(p->type),
218 				    p->st_rdev) == -1)
219 					printf(" (%s not created: %s)\n",
220 					    type, strerror(errno));
221 				else
222 					create = 1;
223 				break;
224 			case F_LINK:
225 				if (!(p->flags & F_SLINK))
226 					printf(
227 				    " (%s not created: link not specified)\n",
228 					    type);
229 				else if (symlink(p->slink, path))
230 					printf(
231 					    " (%s not created: %s)\n",
232 					    type, strerror(errno));
233 				else
234 					create = 1;
235 				break;
236 			case F_DIR:
237 				if (mkdir(path, S_IRWXU))
238 					printf(" (not created: %s)",
239 					    strerror(errno));
240 				else
241 					create = 1;
242 				break;
243 			default:
244 				mtree_err("can't create create %s",
245 				    nodetype(p->type));
246 			}
247 		}
248 		if (create)
249 			printf(" (created)");
250 		if (p->type == F_DIR) {
251 			if (!(p->flags & F_VISIT))
252 				putchar('\n');
253 			for (tp = tail; *tp; ++tp)
254 				continue;
255 			*tp = '/';
256 			miss(p->child, tp + 1);
257 			*tp = '\0';
258 		} else
259 			putchar('\n');
260 
261 		if (!create || Wflag)
262 			continue;
263 		if ((p->flags & (F_UID | F_UNAME)) &&
264 		    (p->flags & (F_GID | F_GNAME)) &&
265 		    (lchown(path, p->st_uid, p->st_gid))) {
266 			printf("%s: user/group/mode not modified: %s\n",
267 			    path, strerror(errno));
268 			printf("%s: warning: file mode %snot set\n", path,
269 			    (p->flags & F_FLAGS) ? "and file flags " : "");
270 			continue;
271 		}
272 		if (p->flags & F_MODE) {
273 #if HAVE_LCHMOD
274 			if (lchmod(path, p->st_mode))
275 #else
276 			if ((p->type != F_LINK) && chmod(path, p->st_mode))
277 #endif
278 				printf("%s: permissions not set: %s\n",
279 				    path, strerror(errno));
280 		}
281 #if HAVE_STRUCT_STAT_ST_FLAGS
282 		if ((p->flags & F_FLAGS) && p->st_flags) {
283 			if (iflag)
284 				flags = p->st_flags;
285 			else
286 				flags = p->st_flags & ~SP_FLGS;
287 			if (lchflags(path, flags))
288 				printf("%s: file flags not set: %s\n",
289 				    path, strerror(errno));
290 		}
291 #endif
292 	}
293 }
294