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