specspec.c revision 1.2.4.2 1 1.2.4.2 tls /* $NetBSD: specspec.c,v 1.2.4.2 2012/11/20 03:03:02 tls Exp $ */
2 1.2.4.2 tls
3 1.2.4.2 tls /*-
4 1.2.4.2 tls * Copyright (c) 2003 Poul-Henning Kamp
5 1.2.4.2 tls * All rights reserved.
6 1.2.4.2 tls *
7 1.2.4.2 tls * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 tls * modification, are permitted provided that the following conditions
9 1.2.4.2 tls * are met:
10 1.2.4.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 tls * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 tls * documentation and/or other materials provided with the distribution.
15 1.2.4.2 tls *
16 1.2.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.4.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.4.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.4.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.4.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.4.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.4.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.4.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.4.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.4.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.4.2 tls * SUCH DAMAGE.
27 1.2.4.2 tls */
28 1.2.4.2 tls
29 1.2.4.2 tls #if HAVE_NBTOOL_CONFIG_H
30 1.2.4.2 tls #include "nbtool_config.h"
31 1.2.4.2 tls #endif
32 1.2.4.2 tls
33 1.2.4.2 tls #include <sys/cdefs.h>
34 1.2.4.2 tls __RCSID("$NetBSD: specspec.c,v 1.2.4.2 2012/11/20 03:03:02 tls Exp $");
35 1.2.4.2 tls
36 1.2.4.2 tls #include <err.h>
37 1.2.4.2 tls #include <grp.h>
38 1.2.4.2 tls #include <pwd.h>
39 1.2.4.2 tls #include <time.h>
40 1.2.4.2 tls #include <stdio.h>
41 1.2.4.2 tls #include <stdint.h>
42 1.2.4.2 tls #include <stdlib.h>
43 1.2.4.2 tls #include <string.h>
44 1.2.4.2 tls #include <unistd.h>
45 1.2.4.2 tls #include "mtree.h"
46 1.2.4.2 tls #include "extern.h"
47 1.2.4.2 tls
48 1.2.4.2 tls #define FF(a, b, c, d) \
49 1.2.4.2 tls (((a)->flags & (c)) && ((b)->flags & (c)) && ((a)->d) != ((b)->d))
50 1.2.4.2 tls #define FS(a, b, c, d) \
51 1.2.4.2 tls (((a)->flags & (c)) && ((b)->flags & (c)) && strcmp((a)->d,(b)->d))
52 1.2.4.2 tls #define FM(a, b, c, d) \
53 1.2.4.2 tls (((a)->flags & (c)) && ((b)->flags & (c)) && memcmp(&(a)->d,&(b)->d, sizeof (a)->d))
54 1.2.4.2 tls
55 1.2.4.2 tls static void
56 1.2.4.2 tls shownode(NODE *n, int f, char const *path)
57 1.2.4.2 tls {
58 1.2.4.2 tls struct group *gr;
59 1.2.4.2 tls struct passwd *pw;
60 1.2.4.2 tls
61 1.2.4.2 tls printf("%s%s %s", path, n->name, inotype(nodetoino(n->type)));
62 1.2.4.2 tls if (f & F_CKSUM)
63 1.2.4.2 tls printf(" cksum=%lu", n->cksum);
64 1.2.4.2 tls if (f & F_GID)
65 1.2.4.2 tls printf(" gid=%d", n->st_gid);
66 1.2.4.2 tls if (f & F_GNAME) {
67 1.2.4.2 tls gr = getgrgid(n->st_gid);
68 1.2.4.2 tls if (gr == NULL)
69 1.2.4.2 tls printf(" gid=%d", n->st_gid);
70 1.2.4.2 tls else
71 1.2.4.2 tls printf(" gname=%s", gr->gr_name);
72 1.2.4.2 tls }
73 1.2.4.2 tls if (f & F_MODE)
74 1.2.4.2 tls printf(" mode=%o", n->st_mode);
75 1.2.4.2 tls if (f & F_NLINK)
76 1.2.4.2 tls printf(" nlink=%d", n->st_nlink);
77 1.2.4.2 tls if (f & F_SIZE)
78 1.2.4.2 tls printf(" size=%jd", (intmax_t)n->st_size);
79 1.2.4.2 tls if (f & F_UID)
80 1.2.4.2 tls printf(" uid=%d", n->st_uid);
81 1.2.4.2 tls if (f & F_UNAME) {
82 1.2.4.2 tls pw = getpwuid(n->st_uid);
83 1.2.4.2 tls if (pw == NULL)
84 1.2.4.2 tls printf(" uid=%d", n->st_uid);
85 1.2.4.2 tls else
86 1.2.4.2 tls printf(" uname=%s", pw->pw_name);
87 1.2.4.2 tls }
88 1.2.4.2 tls if (f & F_MD5)
89 1.2.4.2 tls printf(" %s=%s", MD5KEY, n->md5digest);
90 1.2.4.2 tls if (f & F_SHA1)
91 1.2.4.2 tls printf(" %s=%s", SHA1KEY, n->sha1digest);
92 1.2.4.2 tls if (f & F_RMD160)
93 1.2.4.2 tls printf(" %s=%s", RMD160KEY, n->rmd160digest);
94 1.2.4.2 tls if (f & F_SHA256)
95 1.2.4.2 tls printf(" %s=%s", SHA256KEY, n->sha256digest);
96 1.2.4.2 tls if (f & F_SHA384)
97 1.2.4.2 tls printf(" %s=%s", SHA384KEY, n->sha384digest);
98 1.2.4.2 tls if (f & F_SHA512)
99 1.2.4.2 tls printf(" %s=%s", SHA512KEY, n->sha512digest);
100 1.2.4.2 tls if (f & F_FLAGS)
101 1.2.4.2 tls printf(" flags=%s", flags_to_string(n->st_flags, "none"));
102 1.2.4.2 tls printf("\n");
103 1.2.4.2 tls }
104 1.2.4.2 tls
105 1.2.4.2 tls static int
106 1.2.4.2 tls mismatch(NODE *n1, NODE *n2, int differ, char const *path)
107 1.2.4.2 tls {
108 1.2.4.2 tls
109 1.2.4.2 tls if (n2 == NULL) {
110 1.2.4.2 tls shownode(n1, differ, path);
111 1.2.4.2 tls return (1);
112 1.2.4.2 tls }
113 1.2.4.2 tls if (n1 == NULL) {
114 1.2.4.2 tls printf("\t");
115 1.2.4.2 tls shownode(n2, differ, path);
116 1.2.4.2 tls return (1);
117 1.2.4.2 tls }
118 1.2.4.2 tls if (!(differ & keys))
119 1.2.4.2 tls return(0);
120 1.2.4.2 tls printf("\t\t");
121 1.2.4.2 tls shownode(n1, differ, path);
122 1.2.4.2 tls printf("\t\t");
123 1.2.4.2 tls shownode(n2, differ, path);
124 1.2.4.2 tls return (1);
125 1.2.4.2 tls }
126 1.2.4.2 tls
127 1.2.4.2 tls static int
128 1.2.4.2 tls compare_nodes(NODE *n1, NODE *n2, char const *path)
129 1.2.4.2 tls {
130 1.2.4.2 tls int differs;
131 1.2.4.2 tls
132 1.2.4.2 tls if (n1 != NULL && n1->type == F_LINK)
133 1.2.4.2 tls n1->flags &= ~F_MODE;
134 1.2.4.2 tls if (n2 != NULL && n2->type == F_LINK)
135 1.2.4.2 tls n2->flags &= ~F_MODE;
136 1.2.4.2 tls differs = 0;
137 1.2.4.2 tls if (n1 == NULL && n2 != NULL) {
138 1.2.4.2 tls differs = n2->flags;
139 1.2.4.2 tls mismatch(n1, n2, differs, path);
140 1.2.4.2 tls return (1);
141 1.2.4.2 tls }
142 1.2.4.2 tls if (n1 != NULL && n2 == NULL) {
143 1.2.4.2 tls differs = n1->flags;
144 1.2.4.2 tls mismatch(n1, n2, differs, path);
145 1.2.4.2 tls return (1);
146 1.2.4.2 tls }
147 1.2.4.2 tls if (n1->type != n2->type) {
148 1.2.4.2 tls differs = 0;
149 1.2.4.2 tls mismatch(n1, n2, differs, path);
150 1.2.4.2 tls return (1);
151 1.2.4.2 tls }
152 1.2.4.2 tls if (FF(n1, n2, F_CKSUM, cksum))
153 1.2.4.2 tls differs |= F_CKSUM;
154 1.2.4.2 tls if (FF(n1, n2, F_GID, st_gid))
155 1.2.4.2 tls differs |= F_GID;
156 1.2.4.2 tls if (FF(n1, n2, F_GNAME, st_gid))
157 1.2.4.2 tls differs |= F_GNAME;
158 1.2.4.2 tls if (FF(n1, n2, F_MODE, st_mode))
159 1.2.4.2 tls differs |= F_MODE;
160 1.2.4.2 tls if (FF(n1, n2, F_NLINK, st_nlink))
161 1.2.4.2 tls differs |= F_NLINK;
162 1.2.4.2 tls if (FF(n1, n2, F_SIZE, st_size))
163 1.2.4.2 tls differs |= F_SIZE;
164 1.2.4.2 tls if (FS(n1, n2, F_SLINK, slink))
165 1.2.4.2 tls differs |= F_SLINK;
166 1.2.4.2 tls if (FM(n1, n2, F_TIME, st_mtimespec))
167 1.2.4.2 tls differs |= F_TIME;
168 1.2.4.2 tls if (FF(n1, n2, F_UID, st_uid))
169 1.2.4.2 tls differs |= F_UID;
170 1.2.4.2 tls if (FF(n1, n2, F_UNAME, st_uid))
171 1.2.4.2 tls differs |= F_UNAME;
172 1.2.4.2 tls if (FS(n1, n2, F_MD5, md5digest))
173 1.2.4.2 tls differs |= F_MD5;
174 1.2.4.2 tls if (FS(n1, n2, F_SHA1, sha1digest))
175 1.2.4.2 tls differs |= F_SHA1;
176 1.2.4.2 tls if (FS(n1, n2, F_RMD160, rmd160digest))
177 1.2.4.2 tls differs |= F_RMD160;
178 1.2.4.2 tls if (FS(n1, n2, F_SHA256, sha256digest))
179 1.2.4.2 tls differs |= F_SHA256;
180 1.2.4.2 tls if (FS(n1, n2, F_SHA384, sha384digest))
181 1.2.4.2 tls differs |= F_SHA384;
182 1.2.4.2 tls if (FS(n1, n2, F_SHA512, sha512digest))
183 1.2.4.2 tls differs |= F_SHA512;
184 1.2.4.2 tls if (FF(n1, n2, F_FLAGS, st_flags))
185 1.2.4.2 tls differs |= F_FLAGS;
186 1.2.4.2 tls if (differs) {
187 1.2.4.2 tls mismatch(n1, n2, differs, path);
188 1.2.4.2 tls return (1);
189 1.2.4.2 tls }
190 1.2.4.2 tls return (0);
191 1.2.4.2 tls }
192 1.2.4.2 tls static int
193 1.2.4.2 tls walk_in_the_forest(NODE *t1, NODE *t2, char const *path)
194 1.2.4.2 tls {
195 1.2.4.2 tls int r, i;
196 1.2.4.2 tls NODE *c1, *c2, *n1, *n2;
197 1.2.4.2 tls char *np;
198 1.2.4.2 tls
199 1.2.4.2 tls r = 0;
200 1.2.4.2 tls
201 1.2.4.2 tls if (t1 != NULL)
202 1.2.4.2 tls c1 = t1->child;
203 1.2.4.2 tls else
204 1.2.4.2 tls c1 = NULL;
205 1.2.4.2 tls if (t2 != NULL)
206 1.2.4.2 tls c2 = t2->child;
207 1.2.4.2 tls else
208 1.2.4.2 tls c2 = NULL;
209 1.2.4.2 tls while (c1 != NULL || c2 != NULL) {
210 1.2.4.2 tls n1 = n2 = NULL;
211 1.2.4.2 tls if (c1 != NULL)
212 1.2.4.2 tls n1 = c1->next;
213 1.2.4.2 tls if (c2 != NULL)
214 1.2.4.2 tls n2 = c2->next;
215 1.2.4.2 tls if (c1 != NULL && c2 != NULL) {
216 1.2.4.2 tls if (c1->type != F_DIR && c2->type == F_DIR) {
217 1.2.4.2 tls n2 = c2;
218 1.2.4.2 tls c2 = NULL;
219 1.2.4.2 tls } else if (c1->type == F_DIR && c2->type != F_DIR) {
220 1.2.4.2 tls n1 = c1;
221 1.2.4.2 tls c1 = NULL;
222 1.2.4.2 tls } else {
223 1.2.4.2 tls i = strcmp(c1->name, c2->name);
224 1.2.4.2 tls if (i > 0) {
225 1.2.4.2 tls n1 = c1;
226 1.2.4.2 tls c1 = NULL;
227 1.2.4.2 tls } else if (i < 0) {
228 1.2.4.2 tls n2 = c2;
229 1.2.4.2 tls c2 = NULL;
230 1.2.4.2 tls }
231 1.2.4.2 tls }
232 1.2.4.2 tls }
233 1.2.4.2 tls if (c1 == NULL && c2->type == F_DIR) {
234 1.2.4.2 tls asprintf(&np, "%s%s/", path, c2->name);
235 1.2.4.2 tls i = walk_in_the_forest(c1, c2, np);
236 1.2.4.2 tls free(np);
237 1.2.4.2 tls i += compare_nodes(c1, c2, path);
238 1.2.4.2 tls } else if (c2 == NULL && c1->type == F_DIR) {
239 1.2.4.2 tls asprintf(&np, "%s%s/", path, c1->name);
240 1.2.4.2 tls i = walk_in_the_forest(c1, c2, np);
241 1.2.4.2 tls free(np);
242 1.2.4.2 tls i += compare_nodes(c1, c2, path);
243 1.2.4.2 tls } else if (c1 == NULL || c2 == NULL) {
244 1.2.4.2 tls i = compare_nodes(c1, c2, path);
245 1.2.4.2 tls } else if (c1->type == F_DIR && c2->type == F_DIR) {
246 1.2.4.2 tls asprintf(&np, "%s%s/", path, c1->name);
247 1.2.4.2 tls i = walk_in_the_forest(c1, c2, np);
248 1.2.4.2 tls free(np);
249 1.2.4.2 tls i += compare_nodes(c1, c2, path);
250 1.2.4.2 tls } else {
251 1.2.4.2 tls i = compare_nodes(c1, c2, path);
252 1.2.4.2 tls }
253 1.2.4.2 tls r += i;
254 1.2.4.2 tls c1 = n1;
255 1.2.4.2 tls c2 = n2;
256 1.2.4.2 tls }
257 1.2.4.2 tls return (r);
258 1.2.4.2 tls }
259 1.2.4.2 tls
260 1.2.4.2 tls int
261 1.2.4.2 tls mtree_specspec(FILE *fi, FILE *fj)
262 1.2.4.2 tls {
263 1.2.4.2 tls int rval;
264 1.2.4.2 tls NODE *root1, *root2;
265 1.2.4.2 tls
266 1.2.4.2 tls root1 = spec(fi);
267 1.2.4.2 tls root2 = spec(fj);
268 1.2.4.2 tls rval = walk_in_the_forest(root1, root2, "");
269 1.2.4.2 tls rval += compare_nodes(root1, root2, "");
270 1.2.4.2 tls if (rval > 0)
271 1.2.4.2 tls return (MISMATCHEXIT);
272 1.2.4.2 tls return (0);
273 1.2.4.2 tls }
274