file_cmds-321.40.3.tar.gz
[apple/file_cmds.git] / mtree / specspec.c
1 /*-
2 * Copyright (c) 2003 Poul-Henning Kamp
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/usr.sbin/mtree/specspec.c,v 1.6 2005/03/29 11:44:17 tobez Exp $");
29
30 #include <sys/param.h>
31 #include <err.h>
32 #include <grp.h>
33 #include <pwd.h>
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 #include "metrics.h"
38 #include "mtree.h"
39 #include "extern.h"
40
41 #define FF(a, b, c, d) \
42 (((a)->flags & (c)) && ((b)->flags & (c)) && ((a)->d) != ((b)->d))
43 #define FS(a, b, c, d) \
44 (((a)->flags & (c)) && ((b)->flags & (c)) && strcmp((a)->d,(b)->d))
45 #define FM(a, b, c, d) \
46 (((a)->flags & (c)) && ((b)->flags & (c)) && memcmp(&(a)->d,&(b)->d, sizeof (a)->d))
47
48 static void
49 shownode(NODE *n, int f, char const *path)
50 {
51 struct group *gr;
52 struct passwd *pw;
53
54 printf("%s%s %s", path, n->name, ftype(n->type));
55 if (f & F_CKSUM)
56 printf(" cksum=%lu", n->cksum);
57 if (f & F_GID)
58 printf(" gid=%d", n->st_gid);
59 if (f & F_GNAME) {
60 gr = getgrgid(n->st_gid);
61 if (gr == NULL)
62 printf(" gid=%d", n->st_gid);
63 else
64 printf(" gname=%s", gr->gr_name);
65 }
66 if (f & F_MODE)
67 printf(" mode=%o", n->st_mode);
68 if (f & F_NLINK)
69 printf(" nlink=%d", n->st_nlink);
70 if (f & F_SIZE)
71 printf(" size=%jd", (intmax_t)n->st_size);
72 if (f & F_TIME)
73 printf(" time=%ld.%09ld", n->st_mtimespec.tv_sec, n->st_mtimespec.tv_nsec);
74 if (f & F_UID)
75 printf(" uid=%d", n->st_uid);
76 if (f & F_UNAME) {
77 pw = getpwuid(n->st_uid);
78 if (pw == NULL)
79 printf(" uid=%d", n->st_uid);
80 else
81 printf(" uname=%s", pw->pw_name);
82 }
83 if (f & F_MD5)
84 printf(" md5digest=%s", n->md5digest);
85 if (f & F_SHA1)
86 printf(" sha1digest=%s", n->sha1digest);
87 if (f & F_RMD160)
88 printf(" rmd160digest=%s", n->rmd160digest);
89 if (f & F_SHA256)
90 printf(" sha256digest=%s", n->sha256digest);
91 if (f & F_FLAGS)
92 printf(" flags=%s", flags_to_string(n->st_flags));
93 if (f & F_BTIME)
94 printf(" btime=%ld.%09ld", n->st_birthtimespec.tv_sec, n->st_birthtimespec.tv_nsec);
95 if (f & F_ATIME)
96 printf(" atime=%ld.%09ld", n->st_atimespec.tv_sec, n->st_atimespec.tv_nsec);
97 if (f & F_CTIME)
98 printf(" ctime=%ld.%09ld", n->st_ctimespec.tv_sec, n->st_ctimespec.tv_nsec);
99 if (f & F_PTIME)
100 printf(" ptime=%ld.%09ld", n->st_ptimespec.tv_sec, n->st_ptimespec.tv_nsec);
101 if (f & F_XATTRS)
102 printf(" xattrsdigest=%s.%llu", n->xattrsdigest, n->xdstream_priv_id);
103 if (f & F_INODE)
104 printf(" inode=%llu", n->st_ino);
105 if (f & F_ACL)
106 printf(" acldigest=%s", n->acldigest);
107 if (f & F_SIBLINGID)
108 printf(" siblingid=%llu", n->sibling_id);
109
110 printf("\n");
111 }
112
113 static int
114 mismatch(NODE *n1, NODE *n2, int differ, char const *path)
115 {
116
117 if (n2 == NULL) {
118 shownode(n1, differ, path);
119 return (1);
120 }
121 if (n1 == NULL) {
122 printf("\t");
123 shownode(n2, differ, path);
124 return (1);
125 }
126 if (!(differ & keys))
127 return(0);
128 printf("\t\t");
129 shownode(n1, differ, path);
130 printf("\t\t");
131 shownode(n2, differ, path);
132 return (1);
133 }
134
135 static int
136 compare_nodes(NODE *n1, NODE *n2, char const *path)
137 {
138 int differs;
139
140 if (n1 != NULL && n1->type == F_LINK)
141 n1->flags &= ~F_MODE;
142 if (n2 != NULL && n2->type == F_LINK)
143 n2->flags &= ~F_MODE;
144 differs = 0;
145 if ((n1 == NULL) && (n2 == NULL)) {
146 return 0;
147 } else if (n1 == NULL) {
148 differs = n2->flags;
149 RECORD_FAILURE(111, WARN_MISMATCH);
150 mismatch(n1, n2, differs, path);
151 return (1);
152 } else if (n2 == NULL) {
153 differs = n1->flags;
154 RECORD_FAILURE(112, WARN_MISMATCH);
155 mismatch(n1, n2, differs, path);
156 return (1);
157 } else if (n1->type != n2->type) {
158 differs = 0;
159 RECORD_FAILURE(113, WARN_MISMATCH);
160 mismatch(n1, n2, differs, path);
161 return (1);
162 }
163 if (FF(n1, n2, F_CKSUM, cksum))
164 differs |= F_CKSUM;
165 if (FF(n1, n2, F_GID, st_gid))
166 differs |= F_GID;
167 if (FF(n1, n2, F_GNAME, st_gid))
168 differs |= F_GNAME;
169 if (FF(n1, n2, F_MODE, st_mode))
170 differs |= F_MODE;
171 if (FF(n1, n2, F_NLINK, st_nlink))
172 differs |= F_NLINK;
173 if (FF(n1, n2, F_SIZE, st_size))
174 differs |= F_SIZE;
175 if (FS(n1, n2, F_SLINK, slink))
176 differs |= F_SLINK;
177 if (FM(n1, n2, F_TIME, st_mtimespec))
178 differs |= F_TIME;
179 if (FF(n1, n2, F_UID, st_uid))
180 differs |= F_UID;
181 if (FF(n1, n2, F_UNAME, st_uid))
182 differs |= F_UNAME;
183 if (FS(n1, n2, F_MD5, md5digest))
184 differs |= F_MD5;
185 if (FS(n1, n2, F_SHA1, sha1digest))
186 differs |= F_SHA1;
187 if (FS(n1, n2, F_RMD160, rmd160digest))
188 differs |= F_RMD160;
189 if (FS(n1, n2, F_SHA256, sha256digest))
190 differs |= F_SHA256;
191 if (FF(n1, n2, F_FLAGS, st_flags))
192 differs |= F_FLAGS;
193 if (FM(n1, n2, F_BTIME, st_birthtimespec))
194 differs |= F_BTIME;
195 if (FM(n1, n2, F_ATIME, st_atimespec))
196 differs |= F_ATIME;
197 if (FM(n1, n2, F_CTIME, st_ctimespec))
198 differs |= F_CTIME;
199 if (FM(n1, n2, F_PTIME, st_ptimespec))
200 differs |= F_PTIME;
201 if (FS(n1, n2, F_XATTRS, xattrsdigest))
202 differs |= F_XATTRS;
203 if (FF(n1, n2, F_INODE, st_ino))
204 differs |= F_INODE;
205 if (FS(n1, n2, F_ACL, acldigest))
206 differs |= F_ACL;
207 if (FF(n1, n2, F_SIBLINGID, sibling_id))
208 differs |= F_SIBLINGID;
209
210 if (differs) {
211 RECORD_FAILURE(114, WARN_MISMATCH);
212 mismatch(n1, n2, differs, path);
213 return (1);
214 }
215 return (0);
216 }
217 static int
218 walk_in_the_forest(NODE *t1, NODE *t2, char const *path)
219 {
220 int r, i, c;
221 NODE *c1, *c2, *n1, *n2;
222 char *np;
223
224 r = 0;
225
226 if (t1 != NULL)
227 c1 = t1->child;
228 else
229 c1 = NULL;
230 if (t2 != NULL)
231 c2 = t2->child;
232 else
233 c2 = NULL;
234 while (c1 != NULL || c2 != NULL) {
235 n1 = n2 = NULL;
236 if (c1 != NULL)
237 n1 = c1->next;
238 if (c2 != NULL)
239 n2 = c2->next;
240 if (c1 != NULL && c2 != NULL) {
241 if (c1->type != F_DIR && c2->type == F_DIR) {
242 n2 = c2;
243 c2 = NULL;
244 } else if (c1->type == F_DIR && c2->type != F_DIR) {
245 n1 = c1;
246 c1 = NULL;
247 } else {
248 i = strcmp(c1->name, c2->name);
249 if (i > 0) {
250 n1 = c1;
251 c1 = NULL;
252 } else if (i < 0) {
253 n2 = c2;
254 c2 = NULL;
255 }
256 }
257 }
258 if (c1 == NULL && c2->type == F_DIR) {
259 asprintf(&np, "%s%s/", path, c2->name);
260 i = walk_in_the_forest(c1, c2, np);
261 if (i) {
262 RECORD_FAILURE(115, WARN_MISMATCH);
263 }
264 free(np);
265 c = compare_nodes(c1, c2, path);
266 if (c) {
267 RECORD_FAILURE(116, WARN_MISMATCH);
268 }
269 i += c;
270 } else if (c2 == NULL && c1->type == F_DIR) {
271 asprintf(&np, "%s%s/", path, c1->name);
272 i = walk_in_the_forest(c1, c2, np);
273 if (i) {
274 RECORD_FAILURE(117, WARN_MISMATCH);
275 }
276 free(np);
277 c = compare_nodes(c1, c2, path);
278 if (c) {
279 RECORD_FAILURE(118, WARN_MISMATCH);
280 }
281 i += c;
282 } else if (c1 == NULL || c2 == NULL) {
283 i = compare_nodes(c1, c2, path);
284 if (i) {
285 RECORD_FAILURE(119, WARN_MISMATCH);
286 }
287 } else if (c1->type == F_DIR && c2->type == F_DIR) {
288 asprintf(&np, "%s%s/", path, c1->name);
289 i = walk_in_the_forest(c1, c2, np);
290 if (i) {
291 RECORD_FAILURE(120, WARN_MISMATCH);
292 }
293 free(np);
294 c = compare_nodes(c1, c2, path);
295 if (c) {
296 RECORD_FAILURE(121, WARN_MISMATCH);
297 }
298 i += c;
299 } else {
300 i = compare_nodes(c1, c2, path);
301 if (i) {
302 RECORD_FAILURE(122, WARN_MISMATCH);
303 }
304 }
305 r += i;
306 c1 = n1;
307 c2 = n2;
308 }
309 return (r);
310 }
311
312 int
313 mtree_specspec(FILE *fi, FILE *fj)
314 {
315 int rval;
316 NODE *root1, *root2;
317
318 root1 = mtree_readspec(fi);
319 root2 = mtree_readspec(fj);
320 rval = walk_in_the_forest(root1, root2, "");
321 rval += compare_nodes(root1, root2, "");
322 if (rval > 0) {
323 RECORD_FAILURE(123, WARN_MISMATCH);
324 return (MISMATCHEXIT);
325 }
326 return (0);
327 }