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