verify.c revision 51705
1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)verify.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/usr.sbin/mtree/verify.c 51705 1999-09-27 00:36:03Z billf $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/stat.h>
44#include <dirent.h>
45#include <err.h>
46#include <errno.h>
47#include <fts.h>
48#include <fnmatch.h>
49#include <stdio.h>
50#include <unistd.h>
51#include "mtree.h"
52#include "extern.h"
53
54extern long int crc_total;
55extern int ftsoptions;
56extern int dflag, eflag, rflag, sflag, uflag;
57extern char fullpath[MAXPATHLEN];
58extern int lineno;
59
60static NODE *root;
61static char path[MAXPATHLEN];
62
63static void	miss __P((NODE *, char *));
64static int	vwalk __P((void));
65
66int
67verify()
68{
69	int rval;
70
71	root = spec();
72	rval = vwalk();
73	miss(root, path);
74	return (rval);
75}
76
77static int
78vwalk()
79{
80	register FTS *t;
81	register FTSENT *p;
82	register NODE *ep, *level;
83	int specdepth, rval;
84	char *argv[2];
85
86	argv[0] = ".";
87	argv[1] = NULL;
88	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
89		err(1, "line %d: fts_open", lineno);
90	level = root;
91	specdepth = rval = 0;
92	while ((p = fts_read(t))) {
93		switch(p->fts_info) {
94		case FTS_D:
95			break;
96		case FTS_DP:
97			if (specdepth > p->fts_level) {
98				for (level = level->parent; level->prev;
99				      level = level->prev);
100				--specdepth;
101			}
102			continue;
103		case FTS_DNR:
104		case FTS_ERR:
105		case FTS_NS:
106			warnx("%s: %s", RP(p), strerror(p->fts_errno));
107			continue;
108		default:
109			if (dflag)
110				continue;
111		}
112
113		if (specdepth != p->fts_level)
114			goto extra;
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 ((ep->flags & F_NOCHANGE) == 0 &&
121				    compare(ep->name, ep, p))
122					rval = MISMATCHEXIT;
123				if (ep->flags & F_IGN)
124					(void)fts_set(t, p, FTS_SKIP);
125				else if (ep->child && ep->type == F_DIR &&
126				    p->fts_info == FTS_D) {
127					level = ep->child;
128					++specdepth;
129				}
130				break;
131			}
132
133		if (ep)
134			continue;
135extra:
136		if (!eflag) {
137			(void)printf("extra: %s", RP(p));
138			if (rflag) {
139				if ((S_ISDIR(p->fts_statp->st_mode)
140				    ? rmdir : unlink)(p->fts_accpath)) {
141					(void)printf(", not removed: %s",
142					    strerror(errno));
143				} else
144					(void)printf(", removed");
145			}
146			(void)putchar('\n');
147		}
148		(void)fts_set(t, p, FTS_SKIP);
149	}
150	(void)fts_close(t);
151	if (sflag)
152		warnx("%s checksum: %lu", fullpath, crc_total);
153	return (rval);
154}
155
156static void
157miss(p, tail)
158	register NODE *p;
159	register char *tail;
160{
161	register int create;
162	register char *tp;
163
164	for (; p; p = p->next) {
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(" (directory not created: user not specified)");
179			else if (!(p->flags & (F_GID | F_GNAME)))
180			    (void)printf(" (directory not created: group not specified)");
181			else if (!(p->flags & F_MODE))
182			    (void)printf(" (directory not created: mode not specified)");
183			else if (mkdir(path, S_IRWXU))
184				(void)printf(" (directory 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