verify.c revision 1.45 1 /* $NetBSD: verify.c,v 1.45 2015/01/23 02:27:01 christos 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
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.45 2015/01/23 02:27:01 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #if ! HAVE_NBTOOL_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(FILE *fi)
68 {
69 int rval;
70
71 root = spec(fi);
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 *argv[2];
85 char dot[] = ".";
86 argv[0] = dot;
87 argv[1] = NULL;
88
89 if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
90 mtree_err("fts_open: %s", strerror(errno));
91 level = root;
92 specdepth = rval = 0;
93 while ((p = fts_read(t)) != NULL) {
94 if (check_excludes(p->fts_name, p->fts_path)) {
95 fts_set(t, p, FTS_SKIP);
96 continue;
97 }
98 if (!find_only(p->fts_path)) {
99 fts_set(t, p, FTS_SKIP);
100 continue;
101 }
102 switch(p->fts_info) {
103 case FTS_D:
104 case FTS_SL:
105 break;
106 case FTS_DP:
107 if (specdepth > p->fts_level) {
108 for (level = level->parent; level->prev;
109 level = level->prev)
110 continue;
111 --specdepth;
112 }
113 continue;
114 case FTS_DNR:
115 case FTS_ERR:
116 case FTS_NS:
117 warnx("%s: %s", RP(p), strerror(p->fts_errno));
118 continue;
119 default:
120 if (dflag)
121 continue;
122 }
123
124 if (specdepth != p->fts_level)
125 goto extra;
126 for (ep = level; ep; ep = ep->next)
127 if ((ep->flags & F_MAGIC &&
128 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
129 !strcmp(ep->name, p->fts_name)) {
130 ep->flags |= F_VISIT;
131 if ((ep->flags & F_NOCHANGE) == 0 &&
132 compare(ep, p))
133 rval = MISMATCHEXIT;
134 if (!(ep->flags & F_IGN) &&
135 ep->type == F_DIR &&
136 p->fts_info == FTS_D) {
137 if (ep->child) {
138 level = ep->child;
139 ++specdepth;
140 }
141 } else
142 fts_set(t, p, FTS_SKIP);
143 break;
144 }
145
146 if (ep)
147 continue;
148 extra:
149 if (!eflag && !(dflag && p->fts_info == FTS_SL)) {
150 printf("extra: %s", RP(p));
151 if (rflag) {
152 if (rflag > 1 &&
153 lchflags(p->fts_accpath, 0) == -1)
154 printf(" (chflags %s)",
155 strerror(errno));
156 if ((S_ISDIR(p->fts_statp->st_mode)
157 ? rmdir : unlink)(p->fts_accpath)) {
158 printf(", not removed: %s",
159 strerror(errno));
160 } else
161 printf(", removed");
162 }
163 putchar('\n');
164 }
165 fts_set(t, p, FTS_SKIP);
166 }
167 fts_close(t);
168 if (sflag)
169 warnx("%s checksum: %u", fullpath, crc_total);
170 return (rval);
171 }
172
173 static void
174 miss(NODE *p, char *tail)
175 {
176 int create;
177 char *tp;
178 const char *type;
179 u_int32_t flags;
180
181 for (; p; p = p->next) {
182 if (p->flags & F_OPT && !(p->flags & F_VISIT))
183 continue;
184 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
185 continue;
186 strcpy(tail, p->name);
187 if (!(p->flags & F_VISIT)) {
188 /* Don't print missing message if file exists as a
189 symbolic link and the -q flag is set. */
190 struct stat statbuf;
191
192 if (qflag && stat(path, &statbuf) == 0 &&
193 S_ISDIR(statbuf.st_mode))
194 p->flags |= F_VISIT;
195 else
196 (void)printf("%s missing", path);
197 }
198 switch (p->type) {
199 case F_BLOCK:
200 case F_CHAR:
201 type = "device";
202 break;
203 case F_DIR:
204 type = "directory";
205 break;
206 case F_LINK:
207 type = "symlink";
208 break;
209 default:
210 putchar('\n');
211 continue;
212 }
213
214 create = 0;
215 if (!(p->flags & F_VISIT) && uflag) {
216 if (mtree_Wflag || p->type == F_LINK)
217 goto createit;
218 if (!(p->flags & (F_UID | F_UNAME)))
219 printf(
220 " (%s not created: user not specified)", type);
221 else if (!(p->flags & (F_GID | F_GNAME)))
222 printf(
223 " (%s not created: group not specified)", type);
224 else if (!(p->flags & F_MODE))
225 printf(
226 " (%s not created: mode not specified)", type);
227 else
228 createit:
229 switch (p->type) {
230 case F_BLOCK:
231 case F_CHAR:
232 if (mtree_Wflag)
233 continue;
234 if (!(p->flags & F_DEV))
235 printf(
236 " (%s not created: device not specified)",
237 type);
238 else if (mknod(path,
239 p->st_mode | nodetoino(p->type),
240 p->st_rdev) == -1)
241 printf(" (%s not created: %s)\n",
242 type, strerror(errno));
243 else
244 create = 1;
245 break;
246 case F_LINK:
247 if (!(p->flags & F_SLINK))
248 printf(
249 " (%s not created: link not specified)\n",
250 type);
251 else if (symlink(p->slink, path))
252 printf(
253 " (%s not created: %s)\n",
254 type, strerror(errno));
255 else
256 create = 1;
257 break;
258 case F_DIR:
259 if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO))
260 printf(" (not created: %s)",
261 strerror(errno));
262 else
263 create = 1;
264 break;
265 default:
266 mtree_err("can't create create %s",
267 nodetype(p->type));
268 }
269 }
270 if (create)
271 printf(" (created)");
272 if (p->type == F_DIR) {
273 if (!(p->flags & F_VISIT))
274 putchar('\n');
275 for (tp = tail; *tp; ++tp)
276 continue;
277 *tp = '/';
278 miss(p->child, tp + 1);
279 *tp = '\0';
280 } else
281 putchar('\n');
282
283 if (!create || mtree_Wflag)
284 continue;
285 if ((p->flags & (F_UID | F_UNAME)) &&
286 (p->flags & (F_GID | F_GNAME)) &&
287 (lchown(path, p->st_uid, p->st_gid))) {
288 printf("%s: user/group/mode not modified: %s\n",
289 path, strerror(errno));
290 printf("%s: warning: file mode %snot set\n", path,
291 (p->flags & F_FLAGS) ? "and file flags " : "");
292 continue;
293 }
294 if (p->flags & F_MODE) {
295 if (lchmod(path, p->st_mode))
296 printf("%s: permissions not set: %s\n",
297 path, strerror(errno));
298 }
299 #if HAVE_STRUCT_STAT_ST_FLAGS
300 if ((p->flags & F_FLAGS) && p->st_flags) {
301 if (iflag)
302 flags = p->st_flags;
303 else
304 flags = p->st_flags & ~SP_FLGS;
305 if (lchflags(path, flags))
306 printf("%s: file flags not set: %s\n",
307 path, strerror(errno));
308 }
309 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
310 }
311 }
312