compare.c revision 1.18 1 1.18 jwise /* $NetBSD: compare.c,v 1.18 1998/12/06 19:07:53 jwise Exp $ */
2 1.8 cgd
3 1.1 cgd /*-
4 1.7 cgd * Copyright (c) 1989, 1993
5 1.7 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.13 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 cgd #if 0
39 1.7 cgd static char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
40 1.8 cgd #else
41 1.18 jwise __RCSID("$NetBSD: compare.c,v 1.18 1998/12/06 19:07:53 jwise Exp $");
42 1.8 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/stat.h>
47 1.18 jwise #include <sys/types.h>
48 1.4 cgd #include <fcntl.h>
49 1.1 cgd #include <fts.h>
50 1.1 cgd #include <errno.h>
51 1.18 jwise #include <md5.h>
52 1.1 cgd #include <stdio.h>
53 1.1 cgd #include <time.h>
54 1.4 cgd #include <unistd.h>
55 1.1 cgd #include "mtree.h"
56 1.4 cgd #include "extern.h"
57 1.1 cgd
58 1.11 mycroft extern int tflag, uflag;
59 1.4 cgd
60 1.4 cgd static char *ftype __P((u_int));
61 1.4 cgd
62 1.4 cgd #define INDENTNAMELEN 8
63 1.1 cgd #define LABEL \
64 1.4 cgd if (!label++) { \
65 1.4 cgd len = printf("%s: ", RP(p)); \
66 1.4 cgd if (len > INDENTNAMELEN) { \
67 1.4 cgd tab = "\t"; \
68 1.4 cgd (void)printf("\n"); \
69 1.4 cgd } else { \
70 1.4 cgd tab = ""; \
71 1.12 mikel (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \
72 1.4 cgd } \
73 1.4 cgd }
74 1.1 cgd
75 1.4 cgd int
76 1.1 cgd compare(name, s, p)
77 1.1 cgd char *name;
78 1.13 lukem NODE *s;
79 1.13 lukem FTSENT *p;
80 1.1 cgd {
81 1.16 wsanchez u_int32_t len, val;
82 1.4 cgd int fd, label;
83 1.4 cgd char *cp, *tab;
84 1.18 jwise char md5buf[35];
85 1.1 cgd
86 1.13 lukem tab = NULL;
87 1.1 cgd label = 0;
88 1.1 cgd switch(s->type) {
89 1.1 cgd case F_BLOCK:
90 1.3 deraadt if (!S_ISBLK(p->fts_statp->st_mode))
91 1.1 cgd goto typeerr;
92 1.1 cgd break;
93 1.1 cgd case F_CHAR:
94 1.3 deraadt if (!S_ISCHR(p->fts_statp->st_mode))
95 1.1 cgd goto typeerr;
96 1.1 cgd break;
97 1.1 cgd case F_DIR:
98 1.3 deraadt if (!S_ISDIR(p->fts_statp->st_mode))
99 1.1 cgd goto typeerr;
100 1.1 cgd break;
101 1.1 cgd case F_FIFO:
102 1.3 deraadt if (!S_ISFIFO(p->fts_statp->st_mode))
103 1.1 cgd goto typeerr;
104 1.1 cgd break;
105 1.1 cgd case F_FILE:
106 1.3 deraadt if (!S_ISREG(p->fts_statp->st_mode))
107 1.1 cgd goto typeerr;
108 1.1 cgd break;
109 1.1 cgd case F_LINK:
110 1.3 deraadt if (!S_ISLNK(p->fts_statp->st_mode))
111 1.1 cgd goto typeerr;
112 1.1 cgd break;
113 1.1 cgd case F_SOCK:
114 1.4 cgd if (!S_ISSOCK(p->fts_statp->st_mode)) {
115 1.1 cgd typeerr: LABEL;
116 1.4 cgd (void)printf("\ttype (%s, %s)\n",
117 1.3 deraadt ftype(s->type), inotype(p->fts_statp->st_mode));
118 1.1 cgd }
119 1.1 cgd break;
120 1.1 cgd }
121 1.4 cgd /* Set the uid/gid first, then set the mode. */
122 1.4 cgd if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
123 1.1 cgd LABEL;
124 1.4 cgd (void)printf("%suser (%u, %u",
125 1.4 cgd tab, s->st_uid, p->fts_statp->st_uid);
126 1.1 cgd if (uflag)
127 1.4 cgd if (chown(p->fts_accpath, s->st_uid, -1))
128 1.4 cgd (void)printf(", not modified: %s)\n",
129 1.1 cgd strerror(errno));
130 1.1 cgd else
131 1.4 cgd (void)printf(", modified)\n");
132 1.4 cgd else
133 1.4 cgd (void)printf(")\n");
134 1.4 cgd tab = "\t";
135 1.1 cgd }
136 1.4 cgd if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
137 1.1 cgd LABEL;
138 1.4 cgd (void)printf("%sgid (%u, %u",
139 1.4 cgd tab, s->st_gid, p->fts_statp->st_gid);
140 1.1 cgd if (uflag)
141 1.4 cgd if (chown(p->fts_accpath, -1, s->st_gid))
142 1.5 cgd (void)printf(", not modified: %s)\n",
143 1.1 cgd strerror(errno));
144 1.1 cgd else
145 1.5 cgd (void)printf(", modified)\n");
146 1.4 cgd else
147 1.4 cgd (void)printf(")\n");
148 1.4 cgd tab = "\t";
149 1.1 cgd }
150 1.4 cgd if (s->flags & F_MODE &&
151 1.4 cgd s->st_mode != (p->fts_statp->st_mode & MBITS)) {
152 1.1 cgd LABEL;
153 1.4 cgd (void)printf("%spermissions (%#o, %#o",
154 1.4 cgd tab, s->st_mode, p->fts_statp->st_mode & MBITS);
155 1.1 cgd if (uflag)
156 1.4 cgd if (chmod(p->fts_accpath, s->st_mode))
157 1.5 cgd (void)printf(", not modified: %s)\n",
158 1.1 cgd strerror(errno));
159 1.1 cgd else
160 1.5 cgd (void)printf(", modified)\n");
161 1.4 cgd else
162 1.4 cgd (void)printf(")\n");
163 1.4 cgd tab = "\t";
164 1.1 cgd }
165 1.1 cgd if (s->flags & F_NLINK && s->type != F_DIR &&
166 1.3 deraadt s->st_nlink != p->fts_statp->st_nlink) {
167 1.1 cgd LABEL;
168 1.4 cgd (void)printf("%slink count (%u, %u)\n",
169 1.4 cgd tab, s->st_nlink, p->fts_statp->st_nlink);
170 1.4 cgd tab = "\t";
171 1.1 cgd }
172 1.3 deraadt if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
173 1.1 cgd LABEL;
174 1.6 cgd (void)printf("%ssize (%qd, %qd)\n",
175 1.14 enami tab, (long long)s->st_size,
176 1.14 enami (long long)p->fts_statp->st_size);
177 1.4 cgd tab = "\t";
178 1.4 cgd }
179 1.7 cgd /*
180 1.7 cgd * XXX
181 1.11 mycroft * Since utimes(2) only takes a timeval, there's no point in
182 1.11 mycroft * comparing the low bits of the timespec nanosecond field. This
183 1.11 mycroft * will only result in mismatches that we can never fix.
184 1.11 mycroft *
185 1.11 mycroft * Doesn't display microsecond differences.
186 1.7 cgd */
187 1.11 mycroft if (s->flags & F_TIME) {
188 1.11 mycroft struct timeval tv[2];
189 1.11 mycroft
190 1.11 mycroft TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
191 1.11 mycroft TIMESPEC_TO_TIMEVAL(&tv[1], &p->fts_statp->st_mtimespec);
192 1.11 mycroft if (tv[0].tv_sec != tv[1].tv_sec ||
193 1.11 mycroft tv[0].tv_usec != tv[1].tv_usec) {
194 1.11 mycroft LABEL;
195 1.11 mycroft (void)printf("%smodification time (%.24s, ",
196 1.11 mycroft tab, ctime(&s->st_mtimespec.tv_sec));
197 1.11 mycroft (void)printf("%.24s",
198 1.11 mycroft ctime(&p->fts_statp->st_mtimespec.tv_sec));
199 1.11 mycroft if (tflag) {
200 1.11 mycroft tv[1] = tv[0];
201 1.11 mycroft if (utimes(p->fts_accpath, tv))
202 1.11 mycroft (void)printf(", not modified: %s)\n",
203 1.11 mycroft strerror(errno));
204 1.11 mycroft else
205 1.11 mycroft (void)printf(", modified)\n");
206 1.11 mycroft } else
207 1.11 mycroft (void)printf(")\n");
208 1.11 mycroft tab = "\t";
209 1.11 mycroft }
210 1.17 mrg }
211 1.17 mrg /*
212 1.17 mrg * XXX
213 1.17 mrg * since chflags(2) will reset file times, the utimes() above
214 1.17 mrg * may have been useless! oh well, we'd rather have correct
215 1.17 mrg * flags, rather than times?
216 1.17 mrg */
217 1.17 mrg if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
218 1.17 mrg LABEL;
219 1.17 mrg (void)printf("%sflags (\"%s\" is not ", tab,
220 1.17 mrg flags_to_string(s->st_flags, "none"));
221 1.17 mrg (void)printf("\"%s\"",
222 1.17 mrg flags_to_string(p->fts_statp->st_flags, "none"));
223 1.17 mrg if (uflag)
224 1.17 mrg if (chflags(p->fts_accpath, s->st_flags))
225 1.17 mrg (void)printf(", not modified: %s)\n",
226 1.17 mrg strerror(errno));
227 1.17 mrg else
228 1.17 mrg (void)printf(", modified)\n");
229 1.17 mrg else
230 1.17 mrg (void)printf(")\n");
231 1.17 mrg tab = "\t";
232 1.1 cgd }
233 1.15 ross if (s->flags & F_CKSUM) {
234 1.4 cgd if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
235 1.4 cgd LABEL;
236 1.4 cgd (void)printf("%scksum: %s: %s\n",
237 1.4 cgd tab, p->fts_accpath, strerror(errno));
238 1.4 cgd tab = "\t";
239 1.4 cgd } else if (crc(fd, &val, &len)) {
240 1.4 cgd (void)close(fd);
241 1.1 cgd LABEL;
242 1.4 cgd (void)printf("%scksum: %s: %s\n",
243 1.4 cgd tab, p->fts_accpath, strerror(errno));
244 1.4 cgd tab = "\t";
245 1.4 cgd } else {
246 1.4 cgd (void)close(fd);
247 1.4 cgd if (s->cksum != val) {
248 1.4 cgd LABEL;
249 1.4 cgd (void)printf("%scksum (%lu, %lu)\n",
250 1.16 wsanchez tab, s->cksum, (unsigned long)val);
251 1.4 cgd }
252 1.4 cgd tab = "\t";
253 1.1 cgd }
254 1.15 ross }
255 1.18 jwise if (s->flags & F_MD5) {
256 1.18 jwise if (MD5File(p->fts_accpath, md5buf) == NULL) {
257 1.18 jwise LABEL;
258 1.18 jwise (void)printf("%smd5: %s: %s\n",
259 1.18 jwise tab, p->fts_accpath, strerror(errno));
260 1.18 jwise tab = "\t";
261 1.18 jwise } else {
262 1.18 jwise if (strcmp(s->md5sum, md5buf)) {
263 1.18 jwise LABEL;
264 1.18 jwise (void)printf("%smd5 (0x%s, 0x%s)\n",
265 1.18 jwise tab, s->md5sum, md5buf);
266 1.18 jwise }
267 1.18 jwise tab = "\t";
268 1.18 jwise }
269 1.18 jwise }
270 1.18 jwise
271 1.4 cgd if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
272 1.1 cgd LABEL;
273 1.4 cgd (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
274 1.1 cgd }
275 1.4 cgd return (label);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd char *
279 1.1 cgd inotype(type)
280 1.4 cgd u_int type;
281 1.1 cgd {
282 1.1 cgd switch(type & S_IFMT) {
283 1.1 cgd case S_IFBLK:
284 1.4 cgd return ("block");
285 1.1 cgd case S_IFCHR:
286 1.4 cgd return ("char");
287 1.1 cgd case S_IFDIR:
288 1.4 cgd return ("dir");
289 1.4 cgd case S_IFIFO:
290 1.4 cgd return ("fifo");
291 1.1 cgd case S_IFREG:
292 1.4 cgd return ("file");
293 1.1 cgd case S_IFLNK:
294 1.4 cgd return ("link");
295 1.1 cgd case S_IFSOCK:
296 1.4 cgd return ("socket");
297 1.1 cgd default:
298 1.4 cgd return ("unknown");
299 1.1 cgd }
300 1.1 cgd /* NOTREACHED */
301 1.1 cgd }
302 1.1 cgd
303 1.4 cgd static char *
304 1.1 cgd ftype(type)
305 1.1 cgd u_int type;
306 1.1 cgd {
307 1.1 cgd switch(type) {
308 1.1 cgd case F_BLOCK:
309 1.4 cgd return ("block");
310 1.1 cgd case F_CHAR:
311 1.4 cgd return ("char");
312 1.1 cgd case F_DIR:
313 1.4 cgd return ("dir");
314 1.1 cgd case F_FIFO:
315 1.4 cgd return ("fifo");
316 1.1 cgd case F_FILE:
317 1.4 cgd return ("file");
318 1.1 cgd case F_LINK:
319 1.4 cgd return ("link");
320 1.1 cgd case F_SOCK:
321 1.4 cgd return ("socket");
322 1.1 cgd default:
323 1.4 cgd return ("unknown");
324 1.1 cgd }
325 1.1 cgd /* NOTREACHED */
326 1.1 cgd }
327 1.1 cgd
328 1.1 cgd char *
329 1.1 cgd rlink(name)
330 1.1 cgd char *name;
331 1.1 cgd {
332 1.4 cgd static char lbuf[MAXPATHLEN];
333 1.13 lukem int len;
334 1.1 cgd
335 1.4 cgd if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
336 1.16 wsanchez mtree_err("%s: %s", name, strerror(errno));
337 1.1 cgd lbuf[len] = '\0';
338 1.4 cgd return (lbuf);
339 1.1 cgd }
340