file_cmds-264.1.1.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 "mtree.h"
38 #include "extern.h"
39
40 #define FF(a, b, c, d) \
41 (((a)->flags & (c)) && ((b)->flags & (c)) && ((a)->d) != ((b)->d))
42 #define FS(a, b, c, d) \
43 (((a)->flags & (c)) && ((b)->flags & (c)) && strcmp((a)->d,(b)->d))
44 #define FM(a, b, c, d) \
45 (((a)->flags & (c)) && ((b)->flags & (c)) && memcmp(&(a)->d,&(b)->d, sizeof (a)->d))
46
47 static void
48 shownode(NODE *n, int f, char const *path)
49 {
50 struct group *gr;
51 struct passwd *pw;
52
53 printf("%s%s %s", path, n->name, ftype(n->type));
54 if (f & F_CKSUM)
55 printf(" cksum=%lu", n->cksum);
56 if (f & F_GID)
57 printf(" gid=%d", n->st_gid);
58 if (f & F_GNAME) {
59 gr = getgrgid(n->st_gid);
60 if (gr == NULL)
61 printf(" gid=%d", n->st_gid);
62 else
63 printf(" gname=%s", gr->gr_name);
64 }
65 if (f & F_MODE)
66 printf(" mode=%o", n->st_mode);
67 if (f & F_NLINK)
68 printf(" nlink=%d", n->st_nlink);
69 if (f & F_SIZE)
70 printf(" size=%jd", (intmax_t)n->st_size);
71 if (f & F_TIME)
72 printf(" time=%ld.%09ld", n->st_mtimespec.tv_sec, n->st_mtimespec.tv_nsec);
73 if (f & F_UID)
74 printf(" uid=%d", n->st_uid);
75 if (f & F_UNAME) {
76 pw = getpwuid(n->st_uid);
77 if (pw == NULL)
78 printf(" uid=%d", n->st_uid);
79 else
80 printf(" uname=%s", pw->pw_name);
81 }
82 if (f & F_MD5)
83 printf(" md5digest=%s", n->md5digest);
84 if (f & F_SHA1)
85 printf(" sha1digest=%s", n->sha1digest);
86 if (f & F_RMD160)
87 printf(" rmd160digest=%s", n->rmd160digest);
88 if (f & F_SHA256)
89 printf(" sha256digest=%s", n->sha256digest);
90 if (f & F_FLAGS)
91 printf(" flags=%s", flags_to_string(n->st_flags));
92 if (f & F_BTIME)
93 printf(" btime=%ld.%09ld", n->st_birthtimespec.tv_sec, n->st_birthtimespec.tv_nsec);
94 if (f & F_ATIME)
95 printf(" atime=%ld.%09ld", n->st_atimespec.tv_sec, n->st_atimespec.tv_nsec);
96 if (f & F_CTIME)
97 printf(" ctime=%ld.%09ld", n->st_ctimespec.tv_sec, n->st_ctimespec.tv_nsec);
98 if (f & F_PTIME)
99 printf(" ptime=%ld.%09ld", n->st_ptimespec.tv_sec, n->st_ptimespec.tv_nsec);
100 if (f & F_XATTRS)
101 printf(" xattrsdigest=%s", n->xattrsdigest);
102 if (f & F_INODE)
103 printf(" inode=%llu", n->st_ino);
104 if (f & F_ACL)
105 printf(" acldigest=%s", n->acldigest);
106
107 printf("\n");
108 }
109
110 static int
111 mismatch(NODE *n1, NODE *n2, int differ, char const *path)
112 {
113
114 if (n2 == NULL) {
115 shownode(n1, differ, path);
116 return (1);
117 }
118 if (n1 == NULL) {
119 printf("\t");
120 shownode(n2, differ, path);
121 return (1);
122 }
123 if (!(differ & keys))
124 return(0);
125 printf("\t\t");
126 shownode(n1, differ, path);
127 printf("\t\t");
128 shownode(n2, differ, path);
129 return (1);
130 }
131
132 static int
133 compare_nodes(NODE *n1, NODE *n2, char const *path)
134 {
135 int differs;
136
137 if (n1 != NULL && n1->type == F_LINK)
138 n1->flags &= ~F_MODE;
139 if (n2 != NULL && n2->type == F_LINK)
140 n2->flags &= ~F_MODE;
141 differs = 0;
142 if ((n1 == NULL) && (n2 == NULL)) {
143 return 0;
144 } else if (n1 == NULL) {
145 differs = n2->flags;
146 mismatch(n1, n2, differs, path);
147 return (1);
148 } else if (n2 == NULL) {
149 differs = n1->flags;
150 mismatch(n1, n2, differs, path);
151 return (1);
152 } else if (n1->type != n2->type) {
153 differs = 0;
154 mismatch(n1, n2, differs, path);
155 return (1);
156 }
157 if (FF(n1, n2, F_CKSUM, cksum))
158 differs |= F_CKSUM;
159 if (FF(n1, n2, F_GID, st_gid))
160 differs |= F_GID;
161 if (FF(n1, n2, F_GNAME, st_gid))
162 differs |= F_GNAME;
163 if (FF(n1, n2, F_MODE, st_mode))
164 differs |= F_MODE;
165 if (FF(n1, n2, F_NLINK, st_nlink))
166 differs |= F_NLINK;
167 if (FF(n1, n2, F_SIZE, st_size))
168 differs |= F_SIZE;
169 if (FS(n1, n2, F_SLINK, slink))
170 differs |= F_SLINK;
171 if (FM(n1, n2, F_TIME, st_mtimespec))
172 differs |= F_TIME;
173 if (FF(n1, n2, F_UID, st_uid))
174 differs |= F_UID;
175 if (FF(n1, n2, F_UNAME, st_uid))
176 differs |= F_UNAME;
177 if (FS(n1, n2, F_MD5, md5digest))
178 differs |= F_MD5;
179 if (FS(n1, n2, F_SHA1, sha1digest))
180 differs |= F_SHA1;
181 if (FS(n1, n2, F_RMD160, rmd160digest))
182 differs |= F_RMD160;
183 if (FS(n1, n2, F_SHA256, sha256digest))
184 differs |= F_SHA256;
185 if (FF(n1, n2, F_FLAGS, st_flags))
186 differs |= F_FLAGS;
187 if (FM(n1, n2, F_BTIME, st_birthtimespec))
188 differs |= F_BTIME;
189 if (FM(n1, n2, F_ATIME, st_atimespec))
190 differs |= F_ATIME;
191 if (FM(n1, n2, F_CTIME, st_ctimespec))
192 differs |= F_CTIME;
193 if (FM(n1, n2, F_PTIME, st_ptimespec))
194 differs |= F_PTIME;
195 if (FS(n1, n2, F_XATTRS, xattrsdigest))
196 differs |= F_XATTRS;
197 if (FF(n1, n2, F_INODE, st_ino))
198 differs |= F_INODE;
199 if (FS(n1, n2, F_ACL, acldigest))
200 differs |= F_ACL;
201
202 if (differs) {
203 mismatch(n1, n2, differs, path);
204 return (1);
205 }
206 return (0);
207 }
208 static int
209 walk_in_the_forest(NODE *t1, NODE *t2, char const *path)
210 {
211 int r, i;
212 NODE *c1, *c2, *n1, *n2;
213 char *np;
214
215 r = 0;
216
217 if (t1 != NULL)
218 c1 = t1->child;
219 else
220 c1 = NULL;
221 if (t2 != NULL)
222 c2 = t2->child;
223 else
224 c2 = NULL;
225 while (c1 != NULL || c2 != NULL) {
226 n1 = n2 = NULL;
227 if (c1 != NULL)
228 n1 = c1->next;
229 if (c2 != NULL)
230 n2 = c2->next;
231 if (c1 != NULL && c2 != NULL) {
232 if (c1->type != F_DIR && c2->type == F_DIR) {
233 n2 = c2;
234 c2 = NULL;
235 } else if (c1->type == F_DIR && c2->type != F_DIR) {
236 n1 = c1;
237 c1 = NULL;
238 } else {
239 i = strcmp(c1->name, c2->name);
240 if (i > 0) {
241 n1 = c1;
242 c1 = NULL;
243 } else if (i < 0) {
244 n2 = c2;
245 c2 = NULL;
246 }
247 }
248 }
249 if (c1 == NULL && c2->type == F_DIR) {
250 asprintf(&np, "%s%s/", path, c2->name);
251 i = walk_in_the_forest(c1, c2, np);
252 free(np);
253 i += compare_nodes(c1, c2, path);
254 } else if (c2 == NULL && c1->type == F_DIR) {
255 asprintf(&np, "%s%s/", path, c1->name);
256 i = walk_in_the_forest(c1, c2, np);
257 free(np);
258 i += compare_nodes(c1, c2, path);
259 } else if (c1 == NULL || c2 == NULL) {
260 i = compare_nodes(c1, c2, path);
261 } else if (c1->type == F_DIR && c2->type == F_DIR) {
262 asprintf(&np, "%s%s/", path, c1->name);
263 i = walk_in_the_forest(c1, c2, np);
264 free(np);
265 i += compare_nodes(c1, c2, path);
266 } else {
267 i = compare_nodes(c1, c2, path);
268 }
269 r += i;
270 c1 = n1;
271 c2 = n2;
272 }
273 return (r);
274 }
275
276 int
277 mtree_specspec(FILE *fi, FILE *fj)
278 {
279 int rval;
280 NODE *root1, *root2;
281
282 root1 = mtree_readspec(fi);
283 root2 = mtree_readspec(fj);
284 rval = walk_in_the_forest(root1, root2, "");
285 rval += compare_nodes(root1, root2, "");
286 if (rval > 0)
287 return (MISMATCHEXIT);
288 return (0);
289 }