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
)
139 #if HAVE_STRUCT_STAT_ST_FLAGS
143 const char *cp
, *tab
;
144 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
152 if (!S_ISBLK(p
->fts_statp
->st_mode
))
156 if (!S_ISCHR(p
->fts_statp
->st_mode
))
160 if (!S_ISDIR(p
->fts_statp
->st_mode
))
164 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
168 if (!S_ISREG(p
->fts_statp
->st_mode
))
172 if (!S_ISLNK(p
->fts_statp
->st_mode
))
177 if (!S_ISSOCK(p
->fts_statp
->st_mode
))
182 printf(flavor
== F_FREEBSD9
?
183 "\ttype expected %s found %s\n" : "\ttype (%s, %s)\n",
184 nodetype(s
->type
), inotype(p
->fts_statp
->st_mode
));
189 #if HAVE_STRUCT_STAT_ST_FLAGS
190 if (iflag
&& !uflag
) {
191 if (s
->flags
& F_FLAGS
)
192 SETFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
195 if (mflag
&& !uflag
) {
196 if (s
->flags
& F_FLAGS
)
197 CLEARFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
201 if (s
->flags
& F_DEV
&&
202 (s
->type
== F_BLOCK
|| s
->type
== F_CHAR
) &&
203 s
->st_rdev
!= p
->fts_statp
->st_rdev
) {
205 printf(flavor
== F_FREEBSD9
?
206 "%sdevice expected %#jx found %#jx" :
207 "%sdevice (%#jx, %#jx",
208 tab
, (uintmax_t)s
->st_rdev
,
209 (uintmax_t)p
->fts_statp
->st_rdev
);
211 if ((unlink(p
->fts_accpath
) == -1) ||
212 (mknod(p
->fts_accpath
,
213 s
->st_mode
| nodetoino(s
->type
),
214 s
->st_rdev
) == -1) ||
215 (lchown(p
->fts_accpath
, p
->fts_statp
->st_uid
,
216 p
->fts_statp
->st_gid
) == -1) )
217 printf(", not modified: %s%s\n",
219 flavor
== F_FREEBSD9
? "" : ")");
221 printf(", modified%s\n",
222 flavor
== F_FREEBSD9
? "" : ")");
227 /* Set the uid/gid first, then set the mode. */
228 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
230 printf(flavor
== F_FREEBSD9
?
231 "%suser expected %lu found %lu" : "%suser (%lu, %lu",
232 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
234 if (lchown(p
->fts_accpath
, s
->st_uid
, -1))
235 printf(", not modified: %s%s\n",
237 flavor
== F_FREEBSD9
? "" : ")");
239 printf(", modified%s\n",
240 flavor
== F_FREEBSD9
? "" : ")");
245 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
247 printf(flavor
== F_FREEBSD9
?
248 "%sgid expected %lu found %lu" : "%sgid (%lu, %lu",
249 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
251 if (lchown(p
->fts_accpath
, -1, s
->st_gid
))
252 printf(", not modified: %s%s\n",
254 flavor
== F_FREEBSD9
? "" : ")");
256 printf(", modified%s\n",
257 flavor
== F_FREEBSD9
? "" : ")");
263 if (s
->flags
& F_MODE
&&
264 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
269 mode
= p
->fts_statp
->st_mode
& MBITS
;
271 * if none of the suid/sgid/etc bits are set,
272 * then if the mode is a subset of the target,
275 if (!((tmode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) ||
276 (mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
))))
277 if ((mode
| tmode
) == tmode
)
282 printf(flavor
== F_FREEBSD9
?
283 "%spermissions expcted %#lo found %#lo" :
284 "%spermissions (%#lo, %#lo",
285 tab
, (u_long
)s
->st_mode
,
286 (u_long
)p
->fts_statp
->st_mode
& MBITS
);
288 if (lchmod(p
->fts_accpath
, s
->st_mode
))
289 printf(", not modified: %s%s\n",
291 flavor
== F_FREEBSD9
? "" : ")");
293 printf(", modified%s\n",
294 flavor
== F_FREEBSD9
? "" : ")");
301 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
302 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
304 printf(flavor
== F_FREEBSD9
?
305 "%slink count expected %lu found %lu\n" :
306 "%slink count (%lu, %lu)\n",
307 tab
, (u_long
)s
->st_nlink
, (u_long
)p
->fts_statp
->st_nlink
);
310 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
) {
312 printf(flavor
== F_FREEBSD9
?
313 "%ssize expected %ju found %ju\n" : "%ssize (%ju, %ju)\n",
314 tab
, (uintmax_t)s
->st_size
,
315 (uintmax_t)p
->fts_statp
->st_size
);
320 * Since utimes(2) only takes a timeval, there's no point in
321 * comparing the low bits of the timespec nanosecond field. This
322 * will only result in mismatches that we can never fix.
324 * Doesn't display microsecond differences.
326 if (s
->flags
& F_TIME
) {
327 struct timeval tv
[2];
328 struct stat
*ps
= p
->fts_statp
;
329 time_t smtime
= s
->st_mtimespec
.tv_sec
;
331 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
332 time_t pmtime
= ps
->st_mtimespec
.tv_sec
;
334 TIMESPEC_TO_TIMEVAL(&tv
[0], &s
->st_mtimespec
);
335 TIMESPEC_TO_TIMEVAL(&tv
[1], &ps
->st_mtimespec
);
337 time_t pmtime
= (time_t)ps
->st_mtime
;
339 tv
[0].tv_sec
= smtime
;
341 tv
[1].tv_sec
= pmtime
;
345 if (tv
[0].tv_sec
!= tv
[1].tv_sec
||
346 tv
[0].tv_usec
!= tv
[1].tv_usec
) {
348 printf(flavor
== F_FREEBSD9
?
349 "%smodification time expected %.24s found " :
350 "%smodification time (%.24s, ",
351 tab
, ctime(&smtime
));
352 printf("%.24s", ctime(&pmtime
));
355 if (utimes(p
->fts_accpath
, tv
))
356 printf(", not modified: %s%s\n",
358 flavor
== F_FREEBSD9
? "" : ")");
360 printf(", modified%s\n",
361 flavor
== F_FREEBSD9
? "" : ")");
363 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
367 #if HAVE_STRUCT_STAT_ST_FLAGS
370 * since lchflags(2) will reset file times, the utimes() above
371 * may have been useless! oh well, we'd rather have correct
372 * flags, rather than times?
374 if ((s
->flags
& F_FLAGS
) && ((s
->st_flags
!= p
->fts_statp
->st_flags
)
375 || mflag
|| iflag
)) {
376 if (s
->st_flags
!= p
->fts_statp
->st_flags
) {
379 f_s
= flags_to_string(s
->st_flags
, "none");
380 printf(flavor
== F_FREEBSD9
?
381 "%sflags expected \"%s\" found " :
382 "%sflags (\"%s\" is not ", tab
, f_s
);
384 f_s
= flags_to_string(p
->fts_statp
->st_flags
, "none");
385 printf("\"%s\"", f_s
);
390 SETFLAGS(0, CH_MASK
);
392 CLEARFLAGS(0, SP_FLGS
);
394 SETFLAGS(0, (~SP_FLGS
& CH_MASK
));
396 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
399 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
402 * from this point, no more permission checking or whacking
403 * occurs, only checking of stuff like checksums and symlinks.
406 if (s
->flags
& F_CKSUM
) {
407 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0) {
409 printf("%scksum: %s: %s\n",
410 tab
, p
->fts_accpath
, strerror(errno
));
412 } else if (crc(fd
, &val
, &len
)) {
415 printf("%scksum: %s: %s\n",
416 tab
, p
->fts_accpath
, strerror(errno
));
420 if (s
->cksum
!= val
) {
422 printf(flavor
== F_FREEBSD9
?
423 "%scksum expected %lu found %lu\n" :
424 "%scksum (%lu, %lu)\n",
425 tab
, s
->cksum
, (unsigned long)val
);
431 if (s
->flags
& F_MD5
) {
432 if ((digestbuf
= MD5File(p
->fts_accpath
, NULL
)) == NULL
) {
434 printf("%s%s: %s: %s\n",
435 tab
, MD5KEY
, p
->fts_accpath
, strerror(errno
));
438 if (strcmp(s
->md5digest
, digestbuf
)) {
440 printf(flavor
== F_FREEBSD9
?
441 "%s%s expected %s found %s\n" :
442 "%s%s (0x%s, 0x%s)\n",
443 tab
, MD5KEY
, s
->md5digest
, digestbuf
);
449 #endif /* ! NO_MD5 */
451 if (s
->flags
& F_RMD160
) {
452 if ((digestbuf
= RMD160File(p
->fts_accpath
, NULL
)) == NULL
) {
454 printf("%s%s: %s: %s\n",
455 tab
, RMD160KEY
, p
->fts_accpath
, strerror(errno
));
458 if (strcmp(s
->rmd160digest
, digestbuf
)) {
460 printf(flavor
== F_FREEBSD9
?
461 "%s%s expected %s found %s\n" :
462 "%s%s (0x%s, 0x%s)\n",
463 tab
, RMD160KEY
, s
->rmd160digest
, digestbuf
);
469 #endif /* ! NO_RMD160 */
471 if (s
->flags
& F_SHA1
) {
472 if ((digestbuf
= SHA1File(p
->fts_accpath
, NULL
)) == NULL
) {
474 printf("%s%s: %s: %s\n",
475 tab
, SHA1KEY
, p
->fts_accpath
, strerror(errno
));
478 if (strcmp(s
->sha1digest
, digestbuf
)) {
480 printf(flavor
== F_FREEBSD9
?
481 "%s%s expected %s found %s\n" :
482 "%s%s (0x%s, 0x%s)\n",
483 tab
, SHA1KEY
, s
->sha1digest
, digestbuf
);
489 #endif /* ! NO_SHA1 */
491 if (s
->flags
& F_SHA256
) {
492 if ((digestbuf
= SHA256_File(p
->fts_accpath
, NULL
)) == NULL
) {
494 printf("%s%s: %s: %s\n",
495 tab
, SHA256KEY
, p
->fts_accpath
, strerror(errno
));
498 if (strcmp(s
->sha256digest
, digestbuf
)) {
500 printf(flavor
== F_FREEBSD9
?
501 "%s%s expected %s found %s\n" :
502 "%s%s (0x%s, 0x%s)\n",
503 tab
, SHA256KEY
, s
->sha256digest
, digestbuf
);
509 #ifdef SHA384_BLOCK_LENGTH
510 if (s
->flags
& F_SHA384
) {
511 if ((digestbuf
= SHA384_File(p
->fts_accpath
, NULL
)) == NULL
) {
513 printf("%s%s: %s: %s\n",
514 tab
, SHA384KEY
, p
->fts_accpath
, strerror(errno
));
517 if (strcmp(s
->sha384digest
, digestbuf
)) {
519 printf(flavor
== F_FREEBSD9
?
520 "%s%s expected %s found %s\n" :
521 "%s%s (0x%s, 0x%s)\n",
522 tab
, SHA384KEY
, s
->sha384digest
, digestbuf
);
529 if (s
->flags
& F_SHA512
) {
530 if ((digestbuf
= SHA512_File(p
->fts_accpath
, NULL
)) == NULL
) {
532 printf("%s%s: %s: %s\n",
533 tab
, SHA512KEY
, p
->fts_accpath
, strerror(errno
));
536 if (strcmp(s
->sha512digest
, digestbuf
)) {
538 printf(flavor
== F_FREEBSD9
?
539 "%s%s expected %s found %s\n" :
540 "%s%s (0x%s, 0x%s)\n",
541 tab
, SHA512KEY
, s
->sha512digest
, digestbuf
);
547 #endif /* ! NO_SHA2 */
548 if (s
->flags
& F_SLINK
&&
549 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
551 printf(flavor
== F_FREEBSD9
?
552 "%slink ref expected %s found %s" :
553 "%slink ref (%s, %s", tab
, cp
, s
->slink
);
555 if ((unlink(p
->fts_accpath
) == -1) ||
556 (symlink(s
->slink
, p
->fts_accpath
) == -1) )
557 printf(", not modified: %s%s\n",
559 flavor
== F_FREEBSD9
? "" : ")");
561 printf(", modified%s\n",
562 flavor
== F_FREEBSD9
? "" : ")");
564 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
570 rlink(const char *name
)
572 static char lbuf
[MAXPATHLEN
];
575 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
576 mtree_err("%s: %s", name
, strerror(errno
));