compare.c revision 1.33 1 1.33 lukem /* $NetBSD: compare.c,v 1.33 2001/10/25 14:47:39 lukem 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.33 lukem __RCSID("$NetBSD: compare.c,v 1.33 2001/10/25 14:47:39 lukem 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.24 simonb
47 1.24 simonb #include <errno.h>
48 1.4 cgd #include <fcntl.h>
49 1.1 cgd #include <fts.h>
50 1.18 jwise #include <md5.h>
51 1.1 cgd #include <stdio.h>
52 1.32 lukem #include <string.h>
53 1.1 cgd #include <time.h>
54 1.4 cgd #include <unistd.h>
55 1.24 simonb
56 1.1 cgd #include "mtree.h"
57 1.4 cgd #include "extern.h"
58 1.4 cgd
59 1.4 cgd #define INDENTNAMELEN 8
60 1.20 mrg #define MARK \
61 1.20 mrg do { \
62 1.20 mrg len = printf("%s: ", RP(p)); \
63 1.20 mrg if (len > INDENTNAMELEN) { \
64 1.20 mrg tab = "\t"; \
65 1.20 mrg (void)printf("\n"); \
66 1.20 mrg } else { \
67 1.20 mrg tab = ""; \
68 1.20 mrg (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \
69 1.20 mrg } \
70 1.20 mrg } while (0)
71 1.20 mrg #define LABEL if (!label++) MARK
72 1.20 mrg
73 1.20 mrg #define CHANGEFLAGS(path, oflags) \
74 1.20 mrg if (flags != (oflags)) { \
75 1.20 mrg if (!label) { \
76 1.20 mrg MARK; \
77 1.20 mrg (void)printf("%sflags (\"%s\"", tab, \
78 1.20 mrg flags_to_string(p->fts_statp->st_flags, "none")); \
79 1.20 mrg } \
80 1.20 mrg if (chflags(path, flags)) { \
81 1.20 mrg label++; \
82 1.20 mrg (void)printf(", not modified: %s)\n", \
83 1.20 mrg strerror(errno)); \
84 1.20 mrg } else \
85 1.20 mrg (void)printf(", modified to \"%s\")\n", \
86 1.20 mrg flags_to_string(flags, "none")); \
87 1.4 cgd }
88 1.1 cgd
89 1.20 mrg /* SETFLAGS:
90 1.20 mrg * given pflags, additionally set those flags specified in sflags and
91 1.20 mrg * selected by mask (the other flags are left unchanged). oflags is
92 1.20 mrg * passed as reference to check if chflags is necessary.
93 1.20 mrg */
94 1.20 mrg #define SETFLAGS(path, sflags, pflags, oflags, mask) \
95 1.20 mrg do { \
96 1.20 mrg flags = ((sflags) & (mask)) | (pflags); \
97 1.20 mrg CHANGEFLAGS(path, oflags); \
98 1.20 mrg } while (0)
99 1.20 mrg
100 1.20 mrg /* CLEARFLAGS:
101 1.20 mrg * given pflags, reset the flags specified in sflags and selected by mask
102 1.20 mrg * (the other flags are left unchanged). oflags is
103 1.20 mrg * passed as reference to check if chflags is necessary.
104 1.20 mrg */
105 1.20 mrg #define CLEARFLAGS(path, sflags, pflags, oflags, mask) \
106 1.20 mrg do { \
107 1.20 mrg flags = (~((sflags) & (mask)) & CH_MASK) & (pflags); \
108 1.20 mrg CHANGEFLAGS(path, oflags); \
109 1.20 mrg } while (0)
110 1.20 mrg
111 1.4 cgd int
112 1.25 lukem compare(const char *name, NODE *s, FTSENT *p)
113 1.1 cgd {
114 1.20 mrg u_int32_t len, val, flags;
115 1.4 cgd int fd, label;
116 1.30 lukem const char *cp, *tab;
117 1.18 jwise char md5buf[35];
118 1.1 cgd
119 1.13 lukem tab = NULL;
120 1.1 cgd label = 0;
121 1.1 cgd switch(s->type) {
122 1.1 cgd case F_BLOCK:
123 1.3 deraadt if (!S_ISBLK(p->fts_statp->st_mode))
124 1.1 cgd goto typeerr;
125 1.1 cgd break;
126 1.1 cgd case F_CHAR:
127 1.3 deraadt if (!S_ISCHR(p->fts_statp->st_mode))
128 1.1 cgd goto typeerr;
129 1.1 cgd break;
130 1.1 cgd case F_DIR:
131 1.3 deraadt if (!S_ISDIR(p->fts_statp->st_mode))
132 1.1 cgd goto typeerr;
133 1.1 cgd break;
134 1.1 cgd case F_FIFO:
135 1.3 deraadt if (!S_ISFIFO(p->fts_statp->st_mode))
136 1.1 cgd goto typeerr;
137 1.1 cgd break;
138 1.1 cgd case F_FILE:
139 1.3 deraadt if (!S_ISREG(p->fts_statp->st_mode))
140 1.1 cgd goto typeerr;
141 1.1 cgd break;
142 1.1 cgd case F_LINK:
143 1.3 deraadt if (!S_ISLNK(p->fts_statp->st_mode))
144 1.1 cgd goto typeerr;
145 1.1 cgd break;
146 1.1 cgd case F_SOCK:
147 1.4 cgd if (!S_ISSOCK(p->fts_statp->st_mode)) {
148 1.1 cgd typeerr: LABEL;
149 1.4 cgd (void)printf("\ttype (%s, %s)\n",
150 1.27 lukem nodetype(s->type), inotype(p->fts_statp->st_mode));
151 1.1 cgd }
152 1.1 cgd break;
153 1.1 cgd }
154 1.33 lukem if (Wflag)
155 1.33 lukem goto afterpermwhack;
156 1.20 mrg if (iflag && !uflag) {
157 1.20 mrg if (s->flags & F_FLAGS)
158 1.20 mrg SETFLAGS(p->fts_accpath, s->st_flags,
159 1.20 mrg p->fts_statp->st_flags, p->fts_statp->st_flags,
160 1.20 mrg SP_FLGS);
161 1.20 mrg return (label);
162 1.20 mrg }
163 1.20 mrg if (mflag && !uflag) {
164 1.20 mrg if (s->flags & F_FLAGS)
165 1.20 mrg CLEARFLAGS(p->fts_accpath, s->st_flags,
166 1.20 mrg p->fts_statp->st_flags, p->fts_statp->st_flags,
167 1.20 mrg SP_FLGS);
168 1.20 mrg return (label);
169 1.20 mrg }
170 1.4 cgd /* Set the uid/gid first, then set the mode. */
171 1.4 cgd if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
172 1.1 cgd LABEL;
173 1.19 christos (void)printf("%suser (%lu, %lu",
174 1.19 christos tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
175 1.19 christos if (uflag) {
176 1.4 cgd if (chown(p->fts_accpath, s->st_uid, -1))
177 1.4 cgd (void)printf(", not modified: %s)\n",
178 1.1 cgd strerror(errno));
179 1.1 cgd else
180 1.4 cgd (void)printf(", modified)\n");
181 1.19 christos } else
182 1.4 cgd (void)printf(")\n");
183 1.4 cgd tab = "\t";
184 1.1 cgd }
185 1.4 cgd if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
186 1.1 cgd LABEL;
187 1.19 christos (void)printf("%sgid (%lu, %lu",
188 1.19 christos tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
189 1.19 christos if (uflag) {
190 1.4 cgd if (chown(p->fts_accpath, -1, s->st_gid))
191 1.5 cgd (void)printf(", not modified: %s)\n",
192 1.1 cgd strerror(errno));
193 1.1 cgd else
194 1.5 cgd (void)printf(", modified)\n");
195 1.19 christos }
196 1.4 cgd else
197 1.4 cgd (void)printf(")\n");
198 1.4 cgd tab = "\t";
199 1.1 cgd }
200 1.4 cgd if (s->flags & F_MODE &&
201 1.4 cgd s->st_mode != (p->fts_statp->st_mode & MBITS)) {
202 1.26 perry if (lflag) {
203 1.26 perry mode_t tmode, mode;
204 1.26 perry
205 1.26 perry tmode = s->st_mode;
206 1.26 perry mode = p->fts_statp->st_mode & MBITS;
207 1.26 perry /*
208 1.26 perry * if none of the suid/sgid/etc bits are set,
209 1.26 perry * then if the mode is a subset of the target,
210 1.26 perry * skip.
211 1.26 perry */
212 1.26 perry if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
213 1.26 perry (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
214 1.26 perry if ((mode | tmode) == tmode)
215 1.26 perry goto skip;
216 1.26 perry }
217 1.26 perry
218 1.1 cgd LABEL;
219 1.19 christos (void)printf("%spermissions (%#lo, %#lo",
220 1.19 christos tab, (u_long)s->st_mode,
221 1.19 christos (u_long)p->fts_statp->st_mode & MBITS);
222 1.19 christos if (uflag) {
223 1.4 cgd if (chmod(p->fts_accpath, s->st_mode))
224 1.5 cgd (void)printf(", not modified: %s)\n",
225 1.1 cgd strerror(errno));
226 1.1 cgd else
227 1.5 cgd (void)printf(", modified)\n");
228 1.19 christos }
229 1.4 cgd else
230 1.4 cgd (void)printf(")\n");
231 1.4 cgd tab = "\t";
232 1.31 lukem skip: ;
233 1.29 lukem }
234 1.29 lukem if (s->flags & F_DEV &&
235 1.29 lukem (s->type == F_BLOCK || s->type == F_CHAR) &&
236 1.29 lukem s->st_rdev != p->fts_statp->st_rdev) {
237 1.29 lukem LABEL;
238 1.29 lukem (void)printf("%sdevice (%#x, %#x",
239 1.29 lukem tab, s->st_rdev, p->fts_statp->st_rdev);
240 1.29 lukem if (uflag) {
241 1.29 lukem /* XXXLUKEM: unlink first ? */
242 1.29 lukem if (mknod(p->fts_accpath, s->st_mode, s->st_rdev))
243 1.29 lukem (void)printf(", not modified: %s)\n",
244 1.29 lukem strerror(errno));
245 1.29 lukem else
246 1.29 lukem (void)printf(", modified)\n");
247 1.29 lukem } else
248 1.29 lukem (void)printf(")\n");
249 1.29 lukem tab = "\t";
250 1.1 cgd }
251 1.1 cgd if (s->flags & F_NLINK && s->type != F_DIR &&
252 1.3 deraadt s->st_nlink != p->fts_statp->st_nlink) {
253 1.1 cgd LABEL;
254 1.19 christos (void)printf("%slink count (%lu, %lu)\n",
255 1.19 christos tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
256 1.4 cgd tab = "\t";
257 1.1 cgd }
258 1.3 deraadt if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
259 1.1 cgd LABEL;
260 1.23 lukem (void)printf("%ssize (%lld, %lld)\n",
261 1.14 enami tab, (long long)s->st_size,
262 1.14 enami (long long)p->fts_statp->st_size);
263 1.4 cgd tab = "\t";
264 1.4 cgd }
265 1.7 cgd /*
266 1.7 cgd * XXX
267 1.11 mycroft * Since utimes(2) only takes a timeval, there's no point in
268 1.11 mycroft * comparing the low bits of the timespec nanosecond field. This
269 1.11 mycroft * will only result in mismatches that we can never fix.
270 1.11 mycroft *
271 1.11 mycroft * Doesn't display microsecond differences.
272 1.7 cgd */
273 1.11 mycroft if (s->flags & F_TIME) {
274 1.11 mycroft struct timeval tv[2];
275 1.21 christos struct stat *ps = p->fts_statp;
276 1.22 christos time_t smtime = s->st_mtimespec.tv_sec;
277 1.22 christos
278 1.21 christos #ifdef BSD4_4
279 1.21 christos time_t pmtime = ps->st_mtimespec.tv_sec;
280 1.11 mycroft
281 1.21 christos TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
282 1.21 christos #else
283 1.22 christos time_t pmtime = (time_t)ps->st_mtime;
284 1.21 christos
285 1.21 christos tv[1].tv_sec = ps->st_mtime;
286 1.21 christos tv[1].tv_usec = 0;
287 1.21 christos #endif
288 1.22 christos TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
289 1.21 christos
290 1.11 mycroft if (tv[0].tv_sec != tv[1].tv_sec ||
291 1.11 mycroft tv[0].tv_usec != tv[1].tv_usec) {
292 1.11 mycroft LABEL;
293 1.11 mycroft (void)printf("%smodification time (%.24s, ",
294 1.21 christos tab, ctime(&smtime));
295 1.21 christos (void)printf("%.24s", ctime(&pmtime));
296 1.11 mycroft if (tflag) {
297 1.11 mycroft tv[1] = tv[0];
298 1.11 mycroft if (utimes(p->fts_accpath, tv))
299 1.11 mycroft (void)printf(", not modified: %s)\n",
300 1.11 mycroft strerror(errno));
301 1.11 mycroft else
302 1.11 mycroft (void)printf(", modified)\n");
303 1.11 mycroft } else
304 1.11 mycroft (void)printf(")\n");
305 1.11 mycroft tab = "\t";
306 1.11 mycroft }
307 1.17 mrg }
308 1.17 mrg /*
309 1.17 mrg * XXX
310 1.17 mrg * since chflags(2) will reset file times, the utimes() above
311 1.17 mrg * may have been useless! oh well, we'd rather have correct
312 1.17 mrg * flags, rather than times?
313 1.17 mrg */
314 1.20 mrg if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
315 1.20 mrg || mflag || iflag)) {
316 1.20 mrg if (s->st_flags != p->fts_statp->st_flags) {
317 1.20 mrg LABEL;
318 1.20 mrg (void)printf("%sflags (\"%s\" is not ", tab,
319 1.20 mrg flags_to_string(s->st_flags, "none"));
320 1.20 mrg (void)printf("\"%s\"",
321 1.20 mrg flags_to_string(p->fts_statp->st_flags, "none"));
322 1.20 mrg }
323 1.19 christos if (uflag) {
324 1.20 mrg if (iflag)
325 1.20 mrg SETFLAGS(p->fts_accpath, s->st_flags,
326 1.20 mrg 0, p->fts_statp->st_flags, CH_MASK);
327 1.20 mrg else if (mflag)
328 1.20 mrg CLEARFLAGS(p->fts_accpath, s->st_flags,
329 1.20 mrg 0, p->fts_statp->st_flags, SP_FLGS);
330 1.17 mrg else
331 1.20 mrg SETFLAGS(p->fts_accpath, s->st_flags,
332 1.20 mrg 0, p->fts_statp->st_flags,
333 1.20 mrg (~SP_FLGS & CH_MASK));
334 1.19 christos } else
335 1.17 mrg (void)printf(")\n");
336 1.17 mrg tab = "\t";
337 1.1 cgd }
338 1.33 lukem
339 1.33 lukem /*
340 1.33 lukem * from this point, no more permission checking or whacking
341 1.33 lukem * occurs, only checking of stuff like checksums and symlinks.
342 1.33 lukem */
343 1.33 lukem afterpermwhack:
344 1.15 ross if (s->flags & F_CKSUM) {
345 1.4 cgd if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
346 1.4 cgd LABEL;
347 1.4 cgd (void)printf("%scksum: %s: %s\n",
348 1.4 cgd tab, p->fts_accpath, strerror(errno));
349 1.4 cgd tab = "\t";
350 1.4 cgd } else if (crc(fd, &val, &len)) {
351 1.4 cgd (void)close(fd);
352 1.1 cgd LABEL;
353 1.4 cgd (void)printf("%scksum: %s: %s\n",
354 1.4 cgd tab, p->fts_accpath, strerror(errno));
355 1.4 cgd tab = "\t";
356 1.4 cgd } else {
357 1.4 cgd (void)close(fd);
358 1.4 cgd if (s->cksum != val) {
359 1.4 cgd LABEL;
360 1.4 cgd (void)printf("%scksum (%lu, %lu)\n",
361 1.16 wsanchez tab, s->cksum, (unsigned long)val);
362 1.4 cgd }
363 1.4 cgd tab = "\t";
364 1.1 cgd }
365 1.15 ross }
366 1.18 jwise if (s->flags & F_MD5) {
367 1.18 jwise if (MD5File(p->fts_accpath, md5buf) == NULL) {
368 1.18 jwise LABEL;
369 1.18 jwise (void)printf("%smd5: %s: %s\n",
370 1.18 jwise tab, p->fts_accpath, strerror(errno));
371 1.18 jwise tab = "\t";
372 1.18 jwise } else {
373 1.18 jwise if (strcmp(s->md5sum, md5buf)) {
374 1.18 jwise LABEL;
375 1.18 jwise (void)printf("%smd5 (0x%s, 0x%s)\n",
376 1.18 jwise tab, s->md5sum, md5buf);
377 1.18 jwise }
378 1.18 jwise tab = "\t";
379 1.18 jwise }
380 1.18 jwise }
381 1.18 jwise
382 1.4 cgd if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
383 1.1 cgd LABEL;
384 1.4 cgd (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
385 1.1 cgd }
386 1.4 cgd return (label);
387 1.1 cgd }
388 1.1 cgd
389 1.25 lukem const char *
390 1.25 lukem rlink(const char *name)
391 1.1 cgd {
392 1.4 cgd static char lbuf[MAXPATHLEN];
393 1.13 lukem int len;
394 1.1 cgd
395 1.4 cgd if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
396 1.16 wsanchez mtree_err("%s: %s", name, strerror(errno));
397 1.1 cgd lbuf[len] = '\0';
398 1.4 cgd return (lbuf);
399 1.1 cgd }
400