file_cmds-264.1.1.tar.gz
[apple/file_cmds.git] / mtree / verify.c
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
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/mtree/verify.c,v 1.24 2005/08/11 15:43:55 brian Exp $");
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 <stdint.h>
47 #include <unistd.h>
48 #include "mtree.h"
49 #include "extern.h"
50
51 static NODE *root;
52 static char path[MAXPATHLEN];
53
54 static int miss(NODE *, char *);
55 static int vwalk(void);
56
57 int
58 mtree_verifyspec(FILE *fi)
59 {
60 int rval, mval;
61
62 root = mtree_readspec(fi);
63 rval = vwalk();
64 mval = miss(root, path);
65
66 if (rval != 0)
67 return rval;
68 else
69 return mval;
70 }
71
72 static int
73 vwalk(void)
74 {
75 FTS *t;
76 FTSENT *p;
77 NODE *ep, *level;
78 int specdepth, rval;
79 char *argv[2];
80 char dot[] = ".";
81
82 argv[0] = dot;
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 (level == NULL) {
99 errx(1 , "invalid root in vwalk");
100 }
101 if (specdepth > p->fts_level) {
102 for (level = level->parent; level->prev;
103 level = level->prev);
104 --specdepth;
105 }
106 continue;
107 case FTS_DNR:
108 case FTS_ERR:
109 case FTS_NS:
110 warnx("%s: %s", RP(p), strerror(p->fts_errno));
111 continue;
112 default:
113 if (dflag)
114 continue;
115 }
116
117 if (specdepth != p->fts_level)
118 goto extra;
119 for (ep = level; ep; ep = ep->next)
120 if ((ep->flags & F_MAGIC &&
121 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
122 !strcmp(ep->name, p->fts_name)) {
123 ep->flags |= F_VISIT;
124 if ((ep->flags & F_NOCHANGE) == 0 &&
125 compare(ep->name, ep, p))
126 rval = MISMATCHEXIT;
127 if (ep->flags & F_IGN)
128 (void)fts_set(t, p, FTS_SKIP);
129 else if (ep->child && ep->type == F_DIR &&
130 p->fts_info == FTS_D) {
131 level = ep->child;
132 ++specdepth;
133 }
134 break;
135 }
136
137 if (ep)
138 continue;
139 extra:
140 if (!eflag) {
141 (void)printf("%s extra", RP(p));
142 if (rflag) {
143 if ((S_ISDIR(p->fts_statp->st_mode)
144 ? rmdir : unlink)(p->fts_accpath)) {
145 (void)printf(", not removed: %s",
146 strerror(errno));
147 } else
148 (void)printf(", removed");
149 }
150 (void)putchar('\n');
151 }
152 (void)fts_set(t, p, FTS_SKIP);
153 }
154 (void)fts_close(t);
155 if (sflag)
156 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
157 return (rval);
158 }
159
160 static int
161 miss(NODE *p, char *tail)
162 {
163 int create;
164 char *tp;
165 const char *type, *what;
166 int serr;
167 int rval = 0;
168 int rrval = 0;
169
170 for (; p; p = p->next) {
171 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
172 continue;
173 (void)strcpy(tail, p->name);
174 if (!(p->flags & F_VISIT)) {
175 /* Don't print missing message if file exists as a
176 symbolic link and the -q flag is set. */
177 struct stat statbuf;
178
179 if (qflag && stat(path, &statbuf) == 0) {
180 p->flags |= F_VISIT;
181 } else {
182 (void)printf("%s missing", path);
183 rval = MISMATCHEXIT;
184 }
185 }
186 if (p->type != F_DIR && p->type != F_LINK) {
187 putchar('\n');
188 continue;
189 }
190
191 create = 0;
192 if (p->type == F_LINK)
193 type = "symlink";
194 else
195 type = "directory";
196 if (!(p->flags & F_VISIT) && uflag) {
197 if (!(p->flags & (F_UID | F_UNAME)))
198 (void)printf(" (%s not created: user not specified)", type);
199 else if (!(p->flags & (F_GID | F_GNAME)))
200 (void)printf(" (%s not created: group not specified)", type);
201 else if (p->type == F_LINK) {
202 if (symlink(p->slink, path))
203 (void)printf(" (symlink not created: %s)\n",
204 strerror(errno));
205 else
206 (void)printf(" (created)\n");
207 if (lchown(path, p->st_uid, p->st_gid) == -1) {
208 serr = errno;
209 if (p->st_uid == (uid_t)-1)
210 what = "group";
211 else if (lchown(path, (uid_t)-1,
212 p->st_gid) == -1)
213 what = "user & group";
214 else {
215 what = "user";
216 errno = serr;
217 }
218 (void)printf("%s: %s not modified: %s"
219 "\n", path, what, strerror(errno));
220 }
221 continue;
222 } else if (!(p->flags & F_MODE))
223 (void)printf(" (directory not created: mode not specified)");
224 else if (mkdir(path, S_IRWXU))
225 (void)printf(" (directory not created: %s)",
226 strerror(errno));
227 else {
228 create = 1;
229 (void)printf(" (created)");
230 }
231 }
232 if (!(p->flags & F_VISIT))
233 (void)putchar('\n');
234
235 for (tp = tail; *tp; ++tp);
236 *tp = '/';
237 rrval = miss(p->child, tp + 1);
238 if (rrval != 0)
239 rval = rrval;
240 *tp = '\0';
241
242 if (!create)
243 continue;
244 if (chown(path, p->st_uid, p->st_gid) == -1) {
245 serr = errno;
246 if (p->st_uid == (uid_t)-1)
247 what = "group";
248 else if (chown(path, (uid_t)-1, p->st_gid) == -1)
249 what = "user & group";
250 else {
251 what = "user";
252 errno = serr;
253 }
254 (void)printf("%s: %s not modified: %s\n",
255 path, what, strerror(errno));
256 }
257 if (chmod(path, p->st_mode))
258 (void)printf("%s: permissions not set: %s\n",
259 path, strerror(errno));
260 if ((p->flags & F_FLAGS) && p->st_flags &&
261 chflags(path, (u_int)p->st_flags))
262 (void)printf("%s: file flags not set: %s\n",
263 path, strerror(errno));
264 }
265 return rval;
266 }