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