ac(8), from NetBSD, FreeBSD smoothing by Michael Reifenberger
[unix-history] / usr.sbin / mtree / verify.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * 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
34#ifndef lint
35static char sccsid[] = "@(#)verify.c 5.9 (Berkeley) 3/12/91";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/stat.h>
40#include <dirent.h>
41#include <fts.h>
42#include <unistd.h>
43#include <errno.h>
44#include <stdio.h>
45#include "mtree.h"
46
47extern NODE *root;
48
49static char path[MAXPATHLEN];
50
51verify()
52{
53 vwalk();
54 miss(root, path);
55}
56
57vwalk()
58{
59 extern int ftsoptions, dflag, eflag, rflag;
60 register FTS *t;
61 register FTSENT *p;
62 register NODE *ep, *level;
63 char *argv[2];
64 int ftsdepth = 0, specdepth = 0;
65
66 argv[0] = ".";
67 argv[1] = (char *)NULL;
68 if (!(t = fts_open(argv, ftsoptions, (int (*)())NULL))) {
69 (void)fprintf(stderr,
70 "mtree: fts_open: %s.\n", strerror(errno));
71 exit(1);
72 }
73 level = root;
74 while (p = fts_read(t)) {
75 switch(p->fts_info) {
76 case FTS_D:
77 if (!strcmp(p->fts_name, "."))
78 continue;
79 ftsdepth++;
80 break;
81 case FTS_DP:
82 ftsdepth--;
83 if (specdepth > ftsdepth) {
84 for (level = level->parent; level->prev;
85 level = level->prev);
86 specdepth--;
87 }
88 continue;
89 case FTS_DNR:
90 case FTS_ERR:
91 case FTS_NS:
92 (void)fprintf(stderr, "mtree: %s: %s.\n",
93 RP(p), strerror(errno));
94 continue;
95 default:
96 if (dflag)
97 continue;
98 }
99
100 for (ep = level; ep; ep = ep->next)
101 if (ep->flags & F_MAGIC && fnmatch(ep->name,
102 p->fts_name, FNM_PATHNAME|FNM_QUOTE) ||
103 !strcmp(ep->name, p->fts_name)) {
104 ep->flags |= F_VISIT;
105 if (ep->flags & F_IGN) {
106 (void)fts_set(t, p, FTS_SKIP);
107 continue;
108 }
109 compare(ep->name, ep, p);
110 if (ep->child && ep->type == F_DIR &&
111 p->fts_info == FTS_D) {
112 level = ep->child;
113 specdepth++;
114 }
115 break;
116 }
117
118 if (ep)
119 continue;
120 if (!eflag) {
121 (void)printf("extra: %s", RP(p));
122 if (rflag) {
123 if (unlink(p->fts_accpath)) {
124 (void)printf(", not removed: %s",
125 strerror(errno));
126 } else
127 (void)printf(", removed");
128 }
129 (void)putchar('\n');
130 }
131 (void)fts_set(t, p, FTS_SKIP);
132 }
133 (void)fts_close(t);
134}
135
136miss(p, tail)
137 register NODE *p;
138 register char *tail;
139{
140 extern int dflag, uflag;
141 register int create;
142 register char *tp;
143
144 for (; p; p = p->next) {
145 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
146 continue;
147 (void)strcpy(tail, p->name);
148 if (!(p->flags & F_VISIT))
149 (void)printf("missing: %s", path);
150 if (p->type != F_DIR) {
151 putchar('\n');
152 continue;
153 }
154
155 create = 0;
156 if (!(p->flags & F_VISIT) && uflag)
157#define MINBITS (F_GROUP|F_MODE|F_OWNER)
158 if ((p->flags & MINBITS) != MINBITS)
159 (void)printf(" (not created -- group, mode or owner not specified)");
160 else if (mkdir(path, S_IRWXU))
161 (void)printf(" (not created: %s)",
162 strerror(errno));
163 else {
164 create = 1;
165 (void)printf(" (created)");
166 }
167
168 if (!(p->flags & F_VISIT))
169 (void)putchar('\n');
170
171 for (tp = tail; *tp; ++tp);
172 *tp = '/';
173 miss(p->child, tp + 1);
174 *tp = '\0';
175
176 if (!create)
177 continue;
178 if (chown(path, p->st_uid, p->st_gid)) {
179 (void)printf("%s: owner/group/mode not modified: %s\n",
180 path, strerror(errno));
181 continue;
182 }
183 if (chmod(path, p->st_mode))
184 (void)printf("%s: permissions not set: %s\n",
185 path, strerror(errno));
186 }
187}