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