verify.c revision 2860
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. 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	8.1 (Berkeley) 6/6/93";
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 <fnmatch.h>
43#include <unistd.h>
44#include <errno.h>
45#include <stdio.h>
46#include "mtree.h"
47#include "extern.h"
48
49extern long int crc_total;
50extern int ftsoptions;
51extern int dflag, eflag, rflag, sflag, uflag;
52extern char fullpath[MAXPATHLEN];
53
54static NODE *root;
55static char path[MAXPATHLEN];
56
57static void	miss __P((NODE *, char *));
58static int	vwalk __P((void));
59
60int
61verify()
62{
63	int rval;
64
65	root = spec();
66	rval = vwalk();
67	miss(root, path);
68	return (rval);
69}
70
71static int
72vwalk()
73{
74	register FTS *t;
75	register FTSENT *p;
76	register NODE *ep, *level;
77	int ftsdepth, specdepth, rval;
78	char *argv[2];
79
80	argv[0] = ".";
81	argv[1] = NULL;
82	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
83		err("fts_open: %s", strerror(errno));
84	level = root;
85	ftsdepth = specdepth = rval = 0;
86	while ((p = fts_read(t))) {
87		switch(p->fts_info) {
88		case FTS_D:
89			++ftsdepth;
90			break;
91		case FTS_DP:
92			--ftsdepth;
93			if (specdepth > ftsdepth) {
94				for (level = level->parent; level->prev;
95				      level = level->prev);
96				--specdepth;
97			}
98			continue;
99		case FTS_DNR:
100		case FTS_ERR:
101		case FTS_NS:
102			(void)fprintf(stderr, "mtree: %s: %s\n",
103			    RP(p), strerror(errno));
104			continue;
105		default:
106			if (dflag)
107				continue;
108		}
109
110		for (ep = level; ep; ep = ep->next)
111			if ((ep->flags & F_MAGIC &&
112			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
113			    !strcmp(ep->name, p->fts_name)) {
114				ep->flags |= F_VISIT;
115				if (compare(ep->name, ep, p))
116					rval = MISMATCHEXIT;
117				if (ep->flags & F_IGN)
118					(void)fts_set(t, p, FTS_SKIP);
119				else if (ep->child && ep->type == F_DIR &&
120				    p->fts_info == FTS_D) {
121					level = ep->child;
122					++specdepth;
123				}
124				break;
125			}
126
127		if (ep)
128			continue;
129		if (!eflag) {
130			(void)printf("extra: %s", RP(p));
131			if (rflag) {
132				if (unlink(p->fts_accpath)) {
133					(void)printf(", not removed: %s",
134					    strerror(errno));
135				} else
136					(void)printf(", removed");
137			}
138			(void)putchar('\n');
139		}
140		(void)fts_set(t, p, FTS_SKIP);
141	}
142	(void)fts_close(t);
143	if (sflag)
144		(void)fprintf(stderr,
145		    "mtree: %s checksum: %lu\n", fullpath, crc_total);
146	return (rval);
147}
148
149static void
150miss(p, tail)
151	register NODE *p;
152	register char *tail;
153{
154	register int create;
155	register char *tp;
156
157	for (; p; p = p->next) {
158		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
159			continue;
160		(void)strcpy(tail, p->name);
161		if (!(p->flags & F_VISIT))
162			(void)printf("missing: %s", path);
163		if (p->type != F_DIR) {
164			putchar('\n');
165			continue;
166		}
167
168		create = 0;
169		if (!(p->flags & F_VISIT) && uflag)
170			if (!(p->flags & (F_UID | F_UNAME)))
171			    (void)printf(" (not created: user not specified)");
172			else if (!(p->flags & (F_GID | F_GNAME)))
173			    (void)printf(" (not created: group not specified)");
174			else if (!(p->flags & F_MODE))
175			    (void)printf(" (not created: mode not specified)");
176			else if (mkdir(path, S_IRWXU))
177				(void)printf(" (not created: %s)",
178				    strerror(errno));
179			else {
180				create = 1;
181				(void)printf(" (created)");
182			}
183
184		if (!(p->flags & F_VISIT))
185			(void)putchar('\n');
186
187		for (tp = tail; *tp; ++tp);
188		*tp = '/';
189		miss(p->child, tp + 1);
190		*tp = '\0';
191
192		if (!create)
193			continue;
194		if (chown(path, p->st_uid, p->st_gid)) {
195			(void)printf("%s: user/group/mode not modified: %s\n",
196			    path, strerror(errno));
197			continue;
198		}
199		if (chmod(path, p->st_mode))
200			(void)printf("%s: permissions not set: %s\n",
201			    path, strerror(errno));
202	}
203}
204