gdb - Local mods (compile)
[dragonfly.git] / usr.sbin / mtree / verify.c
CommitLineData
984263bc
MD
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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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.
1de703da
MD
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 $
4a5947c3 31 * $DragonFly: src/usr.sbin/mtree/verify.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
984263bc
MD
32 */
33
984263bc
MD
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
984263bc
MD
46static NODE *root;
47static char path[MAXPATHLEN];
48
2d8a3be7
EN
49static void miss(NODE *, char *);
50static int vwalk(void);
984263bc
MD
51
52int
6ffd281a 53verify(void)
984263bc
MD
54{
55 int rval;
56
57 root = spec();
58 rval = vwalk();
59 miss(root, path);
60 return (rval);
61}
62
63static int
6ffd281a 64vwalk(void)
984263bc 65{
4a5947c3
MD
66 FTS *t;
67 FTSENT *p;
68 NODE *ep, *level;
984263bc 69 int specdepth, rval;
b4b8669d 70 char *argv[2], dot[] = ".";
984263bc 71
b4b8669d 72 argv[0] = dot;
984263bc
MD
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 &&
b4b8669d 112 compare(ep, p))
984263bc
MD
113 rval = MISMATCHEXIT;
114 if (ep->flags & F_IGN)
4a5947c3 115 fts_set(t, p, FTS_SKIP);
984263bc
MD
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;
126extra:
127 if (!eflag) {
4a5947c3 128 printf("%s extra", RP(p));
984263bc
MD
129 if (rflag) {
130 if ((S_ISDIR(p->fts_statp->st_mode)
131 ? rmdir : unlink)(p->fts_accpath)) {
4a5947c3 132 printf(", not removed: %s",
984263bc
MD
133 strerror(errno));
134 } else
4a5947c3 135 printf(", removed");
984263bc 136 }
4a5947c3 137 putchar('\n');
984263bc 138 }
4a5947c3 139 fts_set(t, p, FTS_SKIP);
984263bc 140 }
4a5947c3 141 fts_close(t);
984263bc
MD
142 if (sflag)
143 warnx("%s checksum: %lu", fullpath, crc_total);
144 return (rval);
145}
146
147static void
4a5947c3 148miss(NODE *p, char *tail)
984263bc 149{
4a5947c3
MD
150 int create;
151 char *tp;
984263bc
MD
152 const char *type;
153
154 for (; p; p = p->next) {
155 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
156 continue;
4a5947c3 157 strcpy(tail, p->name);
984263bc
MD
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
4a5947c3 166 printf("%s missing", path);
984263bc
MD
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)))
4a5947c3 180 printf(" (%s not created: user not specified)", type);
984263bc 181 else if (!(p->flags & (F_GID | F_GNAME)))
4a5947c3 182 printf(" (%s not created: group not specified)", type);
984263bc
MD
183 else if (p->type == F_LINK) {
184 if (symlink(p->slink, path))
4a5947c3 185 printf(" (symlink not created: %s)\n",
984263bc
MD
186 strerror(errno));
187 else
4a5947c3 188 printf(" (created)\n");
984263bc 189 if (lchown(path, p->st_uid, p->st_gid))
4a5947c3 190 printf("%s: user/group not modified: %s\n",
984263bc
MD
191 path, strerror(errno));
192 continue;
193 } else if (!(p->flags & F_MODE))
4a5947c3 194 printf(" (directory not created: mode not specified)");
984263bc 195 else if (mkdir(path, S_IRWXU))
4a5947c3 196 printf(" (directory not created: %s)",
984263bc
MD
197 strerror(errno));
198 else {
199 create = 1;
4a5947c3 200 printf(" (created)");
984263bc
MD
201 }
202 }
203 if (!(p->flags & F_VISIT))
4a5947c3 204 putchar('\n');
984263bc
MD
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)) {
4a5947c3 214 printf("%s: user/group/mode not modified: %s\n",
984263bc 215 path, strerror(errno));
4a5947c3 216 printf("%s: warning: file mode %snot set\n", path,
984263bc
MD
217 (p->flags & F_FLAGS) ? "and file flags " : "");
218 continue;
219 }
220 if (chmod(path, p->st_mode))
4a5947c3 221 printf("%s: permissions not set: %s\n",
984263bc
MD
222 path, strerror(errno));
223 if ((p->flags & F_FLAGS) && p->st_flags &&
224 chflags(path, p->st_flags))
4a5947c3 225 printf("%s: file flags not set: %s\n",
984263bc
MD
226 path, strerror(errno));
227 }
228}