xref: /dragonfly/usr.sbin/mtree/verify.c (revision 9f3fc534)
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  * @(#)verify.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/mtree/verify.c,v 1.10.2.2 2001/01/12 19:17:18 phk Exp $
35  * $DragonFly: src/usr.sbin/mtree/verify.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fts.h>
44 #include <fnmatch.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include "mtree.h"
48 #include "extern.h"
49 
50 static NODE *root;
51 static char path[MAXPATHLEN];
52 
53 static void	miss(NODE *, char *);
54 static int	vwalk(void);
55 
56 int
57 verify(void)
58 {
59 	int rval;
60 
61 	root = spec();
62 	rval = vwalk();
63 	miss(root, path);
64 	return (rval);
65 }
66 
67 static int
68 vwalk(void)
69 {
70 	FTS *t;
71 	FTSENT *p;
72 	NODE *ep, *level;
73 	int specdepth, rval;
74 	char *argv[2], dot[] = ".";
75 
76 	argv[0] = dot;
77 	argv[1] = NULL;
78 	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
79 		err(1, "line %d: fts_open", lineno);
80 	level = root;
81 	specdepth = rval = 0;
82 	while ((p = fts_read(t))) {
83 		if (check_excludes(p->fts_name, p->fts_path)) {
84 			fts_set(t, p, FTS_SKIP);
85 			continue;
86 		}
87 		switch(p->fts_info) {
88 		case FTS_D:
89 		case FTS_SL:
90 			break;
91 		case FTS_DP:
92 			if (specdepth > p->fts_level) {
93 				for (level = level->parent; level->prev;
94 				      level = level->prev);
95 				--specdepth;
96 			}
97 			continue;
98 		case FTS_DNR:
99 		case FTS_ERR:
100 		case FTS_NS:
101 			warnx("%s: %s", RP(p), strerror(p->fts_errno));
102 			continue;
103 		default:
104 			if (dflag)
105 				continue;
106 		}
107 
108 		if (specdepth != p->fts_level)
109 			goto extra;
110 		for (ep = level; ep; ep = ep->next)
111 			if ((ep->flags & F_MAGIC &&
112 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
113 			    !strcmp(ep->name, p->fts_name)) {
114 				ep->flags |= F_VISIT;
115 				if ((ep->flags & F_NOCHANGE) == 0 &&
116 				    compare(ep, p))
117 					rval = MISMATCHEXIT;
118 				if (ep->flags & F_IGN)
119 					fts_set(t, p, FTS_SKIP);
120 				else if (ep->child && ep->type == F_DIR &&
121 				    p->fts_info == FTS_D) {
122 					level = ep->child;
123 					++specdepth;
124 				}
125 				break;
126 			}
127 
128 		if (ep)
129 			continue;
130 extra:
131 		if (!eflag) {
132 			printf("%s extra", RP(p));
133 			if (rflag) {
134 				if ((S_ISDIR(p->fts_statp->st_mode)
135 				    ? rmdir : unlink)(p->fts_accpath)) {
136 					printf(", not removed: %s",
137 					    strerror(errno));
138 				} else
139 					printf(", removed");
140 			}
141 			putchar('\n');
142 		}
143 		fts_set(t, p, FTS_SKIP);
144 	}
145 	fts_close(t);
146 	if (sflag)
147 		warnx("%s checksum: %lu", fullpath, crc_total);
148 	return (rval);
149 }
150 
151 static void
152 miss(NODE *p, char *tail)
153 {
154 	int create;
155 	char *tp;
156 	const char *type;
157 
158 	for (; p; p = p->next) {
159 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
160 			continue;
161 		strcpy(tail, p->name);
162 		if (!(p->flags & F_VISIT)) {
163 			/* Don't print missing message if file exists as a
164 			   symbolic link and the -q flag is set. */
165 			struct stat statbuf;
166 
167 			if (qflag && stat(path, &statbuf) == 0)
168 				p->flags |= F_VISIT;
169 			else
170 				printf("%s missing", path);
171 		}
172 		if (p->type != F_DIR && p->type != F_LINK) {
173 			putchar('\n');
174 			continue;
175 		}
176 
177 		create = 0;
178 		if (p->type == F_LINK)
179 			type = "symlink";
180 		else
181 			type = "directory";
182 		if (!(p->flags & F_VISIT) && uflag) {
183 			if (!(p->flags & (F_UID | F_UNAME)))
184 				printf(" (%s not created: user not specified)", type);
185 			else if (!(p->flags & (F_GID | F_GNAME)))
186 				printf(" (%s not created: group not specified)", type);
187 			else if (p->type == F_LINK) {
188 				if (symlink(p->slink, path))
189 					printf(" (symlink not created: %s)\n",
190 					    strerror(errno));
191 				else
192 					printf(" (created)\n");
193 				if (lchown(path, p->st_uid, p->st_gid))
194 					printf("%s: user/group not modified: %s\n",
195 					    path, strerror(errno));
196 				continue;
197 			} else if (!(p->flags & F_MODE))
198 			    printf(" (directory not created: mode not specified)");
199 			else if (mkdir(path, S_IRWXU))
200 				printf(" (directory not created: %s)",
201 				    strerror(errno));
202 			else {
203 				create = 1;
204 				printf(" (created)");
205 			}
206 		}
207 		if (!(p->flags & F_VISIT))
208 			putchar('\n');
209 
210 		for (tp = tail; *tp; ++tp);
211 		*tp = '/';
212 		miss(p->child, tp + 1);
213 		*tp = '\0';
214 
215 		if (!create)
216 			continue;
217 		if (chown(path, p->st_uid, p->st_gid)) {
218 			printf("%s: user/group/mode not modified: %s\n",
219 			    path, strerror(errno));
220 			printf("%s: warning: file mode %snot set\n", path,
221 			    (p->flags & F_FLAGS) ? "and file flags " : "");
222 			continue;
223 		}
224 		if (chmod(path, p->st_mode))
225 			printf("%s: permissions not set: %s\n",
226 			    path, strerror(errno));
227 		if ((p->flags & F_FLAGS) && p->st_flags &&
228 		    chflags(path, p->st_flags))
229 			printf("%s: file flags not set: %s\n",
230 			    path, strerror(errno));
231 	}
232 }
233