1 /* $NetBSD: compare.c,v 1.58 2013/11/21 18:39:50 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: compare.c,v 1.58 2013/11/21 18:39:50 christos Exp $");
45 #include <sys/param.h>
72 #define INDENTNAMELEN 8
75 if (flavor == F_FREEBSD9) { \
76 len = printf("%s changed\n", RP(p)); \
79 len = printf("%s: ", RP(p)); \
80 if (len > INDENTNAMELEN) { \
85 printf("%*s", INDENTNAMELEN - (int)len, ""); \
89 #define LABEL if (!label++) MARK
91 #if HAVE_STRUCT_STAT_ST_FLAGS
95 if (flags != p->fts_statp->st_flags) { \
99 sf = flags_to_string(p->fts_statp->st_flags, "none"); \
100 printf("%sflags (\"%s\"", tab, sf); \
103 if (lchflags(p->fts_accpath, flags)) { \
105 printf(", not modified: %s)\n", \
108 sf = flags_to_string(flags, "none"); \
109 printf(", modified to \"%s\")\n", sf); \
115 * given pflags, additionally set those flags specified in s->st_flags and
116 * selected by mask (the other flags are left unchanged).
118 #define SETFLAGS(pflags, mask) \
120 flags = (s->st_flags & (mask)) | (pflags); \
125 * given pflags, reset the flags specified in s->st_flags and selected by mask
126 * (the other flags are left unchanged).
128 #define CLEARFLAGS(pflags, mask) \
130 flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags); \
133 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
136 compare(NODE
*s
, FTSENT
*p
)
138 u_int32_t len
, val
, flags
;
140 const char *cp
, *tab
;
141 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
149 if (!S_ISBLK(p
->fts_statp
->st_mode
))
153 if (!S_ISCHR(p
->fts_statp
->st_mode
))
157 if (!S_ISDIR(p
->fts_statp
->st_mode
))
161 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
165 if (!S_ISREG(p
->fts_statp
->st_mode
))
169 if (!S_ISLNK(p
->fts_statp
->st_mode
))
174 if (!S_ISSOCK(p
->fts_statp
->st_mode
))
179 printf(flavor
== F_FREEBSD9
?
180 "\ttype expected %s found %s\n" : "\ttype (%s, %s)\n",
181 nodetype(s
->type
), inotype(p
->fts_statp
->st_mode
));
186 #if HAVE_STRUCT_STAT_ST_FLAGS
187 if (iflag
&& !uflag
) {
188 if (s
->flags
& F_FLAGS
)
189 SETFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
192 if (mflag
&& !uflag
) {
193 if (s
->flags
& F_FLAGS
)
194 CLEARFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
198 if (s
->flags
& F_DEV
&&
199 (s
->type
== F_BLOCK
|| s
->type
== F_CHAR
) &&
200 s
->st_rdev
!= p
->fts_statp
->st_rdev
) {
202 printf(flavor
== F_FREEBSD9
?
203 "%sdevice expected %#jx found %#jx" :
204 "%sdevice (%#jx, %#jx",
205 tab
, (uintmax_t)s
->st_rdev
,
206 (uintmax_t)p
->fts_statp
->st_rdev
);
208 if ((unlink(p
->fts_accpath
) == -1) ||
209 (mknod(p
->fts_accpath
,
210 s
->st_mode
| nodetoino(s
->type
),
211 s
->st_rdev
) == -1) ||
212 (lchown(p
->fts_accpath
, p
->fts_statp
->st_uid
,
213 p
->fts_statp
->st_gid
) == -1) )
214 printf(", not modified: %s%s\n",
216 flavor
== F_FREEBSD9
? "" : ")");
218 printf(", modified%s\n",
219 flavor
== F_FREEBSD9
? "" : ")");
224 /* Set the uid/gid first, then set the mode. */
225 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
227 printf(flavor
== F_FREEBSD9
?
228 "%suser expected %lu found %lu" : "%suser (%lu, %lu",
229 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
231 if (lchown(p
->fts_accpath
, s
->st_uid
, -1))
232 printf(", not modified: %s%s\n",
234 flavor
== F_FREEBSD9
? "" : ")");
236 printf(", modified%s\n",
237 flavor
== F_FREEBSD9
? "" : ")");
242 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
244 printf(flavor
== F_FREEBSD9
?
245 "%sgid expected %lu found %lu" : "%sgid (%lu, %lu",
246 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
248 if (lchown(p
->fts_accpath
, -1, s
->st_gid
))
249 printf(", not modified: %s%s\n",
251 flavor
== F_FREEBSD9
? "" : ")");
253 printf(", modified%s\n",
254 flavor
== F_FREEBSD9
? "" : ")");
260 if (s
->flags
& F_MODE
&&
261 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
266 mode
= p
->fts_statp
->st_mode
& MBITS
;
268 * if none of the suid/sgid/etc bits are set,
269 * then if the mode is a subset of the target,
272 if (!((tmode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) ||
273 (mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
))))
274 if ((mode
| tmode
) == tmode
)
279 printf(flavor
== F_FREEBSD9
?
280 "%spermissions expcted %#lo found %#lo" :
281 "%spermissions (%#lo, %#lo",
282 tab
, (u_long
)s
->st_mode
,
283 (u_long
)p
->fts_statp
->st_mode
& MBITS
);
285 if (lchmod(p
->fts_accpath
, s
->st_mode
))
286 printf(", not modified: %s%s\n",
288 flavor
== F_FREEBSD9
? "" : ")");
290 printf(", modified%s\n",
291 flavor
== F_FREEBSD9
? "" : ")");
298 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
299 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
301 printf(flavor
== F_FREEBSD9
?
302 "%slink count expected %lu found %lu\n" :
303 "%slink count (%lu, %lu)\n",
304 tab
, (u_long
)s
->st_nlink
, (u_long
)p
->fts_statp
->st_nlink
);
307 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
) {
309 printf(flavor
== F_FREEBSD9
?
310 "%ssize expected %ju found %ju\n" : "%ssize (%ju, %ju)\n",
311 tab
, (uintmax_t)s
->st_size
,
312 (uintmax_t)p
->fts_statp
->st_size
);
317 * Since utimes(2) only takes a timeval, there's no point in
318 * comparing the low bits of the timespec nanosecond field. This
319 * will only result in mismatches that we can never fix.
321 * Doesn't display microsecond differences.
323 if (s
->flags
& F_TIME
) {
324 struct timeval tv
[2];
325 struct stat
*ps
= p
->fts_statp
;
326 time_t smtime
= s
->st_mtimespec
.tv_sec
;
328 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
329 time_t pmtime
= ps
->st_mtimespec
.tv_sec
;
331 TIMESPEC_TO_TIMEVAL(&tv
[0], &s
->st_mtimespec
);
332 TIMESPEC_TO_TIMEVAL(&tv
[1], &ps
->st_mtimespec
);
334 time_t pmtime
= (time_t)ps
->st_mtime
;
336 tv
[0].tv_sec
= smtime
;
338 tv
[1].tv_sec
= pmtime
;
342 if (tv
[0].tv_sec
!= tv
[1].tv_sec
||
343 tv
[0].tv_usec
!= tv
[1].tv_usec
) {
345 printf(flavor
== F_FREEBSD9
?
346 "%smodification time expected %.24s found " :
347 "%smodification time (%.24s, ",
348 tab
, ctime(&smtime
));
349 printf("%.24s", ctime(&pmtime
));
352 if (utimes(p
->fts_accpath
, tv
))
353 printf(", not modified: %s%s\n",
355 flavor
== F_FREEBSD9
? "" : ")");
357 printf(", modified%s\n",
358 flavor
== F_FREEBSD9
? "" : ")");
360 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
364 #if HAVE_STRUCT_STAT_ST_FLAGS
367 * since lchflags(2) will reset file times, the utimes() above
368 * may have been useless! oh well, we'd rather have correct
369 * flags, rather than times?
371 if ((s
->flags
& F_FLAGS
) && ((s
->st_flags
!= p
->fts_statp
->st_flags
)
372 || mflag
|| iflag
)) {
373 if (s
->st_flags
!= p
->fts_statp
->st_flags
) {
376 f_s
= flags_to_string(s
->st_flags
, "none");
377 printf(flavor
== F_FREEBSD9
?
378 "%sflags expected \"%s\" found " :
379 "%sflags (\"%s\" is not ", tab
, f_s
);
381 f_s
= flags_to_string(p
->fts_statp
->st_flags
, "none");
382 printf("\"%s\"", f_s
);
387 SETFLAGS(0, CH_MASK
);
389 CLEARFLAGS(0, SP_FLGS
);
391 SETFLAGS(0, (~SP_FLGS
& CH_MASK
));
393 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
396 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
399 * from this point, no more permission checking or whacking
400 * occurs, only checking of stuff like checksums and symlinks.
403 if (s
->flags
& F_CKSUM
) {
404 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0) {
406 printf("%scksum: %s: %s\n",
407 tab
, p
->fts_accpath
, strerror(errno
));
409 } else if (crc(fd
, &val
, &len
)) {
412 printf("%scksum: %s: %s\n",
413 tab
, p
->fts_accpath
, strerror(errno
));
417 if (s
->cksum
!= val
) {
419 printf(flavor
== F_FREEBSD9
?
420 "%scksum expected %lu found %lu\n" :
421 "%scksum (%lu, %lu)\n",
422 tab
, s
->cksum
, (unsigned long)val
);
428 if (s
->flags
& F_MD5
) {
429 if ((digestbuf
= MD5File(p
->fts_accpath
, NULL
)) == NULL
) {
431 printf("%s%s: %s: %s\n",
432 tab
, MD5KEY
, p
->fts_accpath
, strerror(errno
));
435 if (strcmp(s
->md5digest
, digestbuf
)) {
437 printf(flavor
== F_FREEBSD9
?
438 "%s%s expected %s found %s\n" :
439 "%s%s (0x%s, 0x%s)\n",
440 tab
, MD5KEY
, s
->md5digest
, digestbuf
);
446 #endif /* ! NO_MD5 */
448 if (s
->flags
& F_RMD160
) {
449 if ((digestbuf
= RMD160File(p
->fts_accpath
, NULL
)) == NULL
) {
451 printf("%s%s: %s: %s\n",
452 tab
, RMD160KEY
, p
->fts_accpath
, strerror(errno
));
455 if (strcmp(s
->rmd160digest
, digestbuf
)) {
457 printf(flavor
== F_FREEBSD9
?
458 "%s%s expected %s found %s\n" :
459 "%s%s (0x%s, 0x%s)\n",
460 tab
, RMD160KEY
, s
->rmd160digest
, digestbuf
);
466 #endif /* ! NO_RMD160 */
468 if (s
->flags
& F_SHA1
) {
469 if ((digestbuf
= SHA1File(p
->fts_accpath
, NULL
)) == NULL
) {
471 printf("%s%s: %s: %s\n",
472 tab
, SHA1KEY
, p
->fts_accpath
, strerror(errno
));
475 if (strcmp(s
->sha1digest
, digestbuf
)) {
477 printf(flavor
== F_FREEBSD9
?
478 "%s%s expected %s found %s\n" :
479 "%s%s (0x%s, 0x%s)\n",
480 tab
, SHA1KEY
, s
->sha1digest
, digestbuf
);
486 #endif /* ! NO_SHA1 */
488 if (s
->flags
& F_SHA256
) {
489 if ((digestbuf
= SHA256_File(p
->fts_accpath
, NULL
)) == NULL
) {
491 printf("%s%s: %s: %s\n",
492 tab
, SHA256KEY
, p
->fts_accpath
, strerror(errno
));
495 if (strcmp(s
->sha256digest
, digestbuf
)) {
497 printf(flavor
== F_FREEBSD9
?
498 "%s%s expected %s found %s\n" :
499 "%s%s (0x%s, 0x%s)\n",
500 tab
, SHA256KEY
, s
->sha256digest
, digestbuf
);
506 #ifdef SHA384_BLOCK_LENGTH
507 if (s
->flags
& F_SHA384
) {
508 if ((digestbuf
= SHA384_File(p
->fts_accpath
, NULL
)) == NULL
) {
510 printf("%s%s: %s: %s\n",
511 tab
, SHA384KEY
, p
->fts_accpath
, strerror(errno
));
514 if (strcmp(s
->sha384digest
, digestbuf
)) {
516 printf(flavor
== F_FREEBSD9
?
517 "%s%s expected %s found %s\n" :
518 "%s%s (0x%s, 0x%s)\n",
519 tab
, SHA384KEY
, s
->sha384digest
, digestbuf
);
526 if (s
->flags
& F_SHA512
) {
527 if ((digestbuf
= SHA512_File(p
->fts_accpath
, NULL
)) == NULL
) {
529 printf("%s%s: %s: %s\n",
530 tab
, SHA512KEY
, p
->fts_accpath
, strerror(errno
));
533 if (strcmp(s
->sha512digest
, digestbuf
)) {
535 printf(flavor
== F_FREEBSD9
?
536 "%s%s expected %s found %s\n" :
537 "%s%s (0x%s, 0x%s)\n",
538 tab
, SHA512KEY
, s
->sha512digest
, digestbuf
);
544 #endif /* ! NO_SHA2 */
545 if (s
->flags
& F_SLINK
&&
546 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
548 printf(flavor
== F_FREEBSD9
?
549 "%slink ref expected %s found %s" :
550 "%slink ref (%s, %s", tab
, cp
, s
->slink
);
552 if ((unlink(p
->fts_accpath
) == -1) ||
553 (symlink(s
->slink
, p
->fts_accpath
) == -1) )
554 printf(", not modified: %s%s\n",
556 flavor
== F_FREEBSD9
? "" : ")");
558 printf(", modified%s\n",
559 flavor
== F_FREEBSD9
? "" : ")");
561 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
567 rlink(const char *name
)
569 static char lbuf
[MAXPATHLEN
];
572 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
573 mtree_err("%s: %s", name
, strerror(errno
));