1 /* @(#)compare.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: compare.c,v 1.58 2013/11/21 18:39:50 christos Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
37 #include <sys/param.h>
67 #define INDENTNAMELEN 8
70 if (flavor == F_FREEBSD9) { \
71 len = printf("%s changed\n", RP(p)); \
74 len = printf("%s: ", RP(p)); \
75 if (len > INDENTNAMELEN) { \
80 printf("%*s", INDENTNAMELEN - (int)len, ""); \
84 #define LABEL if (!label++) MARK
86 #if HAVE_STRUCT_STAT_ST_FLAGS
90 if (flags != p->fts_statp->st_flags) { \
94 sf = flags_to_string(p->fts_statp->st_flags, "none"); \
95 printf("%sflags (\"%s\"", tab, sf); \
98 if (lchflags(p->fts_accpath, flags)) { \
100 printf(", not modified: %s)\n", \
103 sf = flags_to_string(flags, "none"); \
104 printf(", modified to \"%s\")\n", sf); \
110 * given pflags, additionally set those flags specified in s->st_flags and
111 * selected by mask (the other flags are left unchanged).
113 #define SETFLAGS(pflags, mask) \
115 flags = (s->st_flags & (mask)) | (pflags); \
120 * given pflags, reset the flags specified in s->st_flags and selected by mask
121 * (the other flags are left unchanged).
123 #define CLEARFLAGS(pflags, mask) \
125 flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags); \
128 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
131 compare(NODE
*s
, FTSENT
*p
)
133 u_int32_t len
, val
, flags
;
135 const char *cp
, *tab
;
136 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
144 if (!S_ISBLK(p
->fts_statp
->st_mode
))
148 if (!S_ISCHR(p
->fts_statp
->st_mode
))
152 if (!S_ISDIR(p
->fts_statp
->st_mode
))
156 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
160 if (!S_ISREG(p
->fts_statp
->st_mode
))
164 if (!S_ISLNK(p
->fts_statp
->st_mode
))
169 if (!S_ISSOCK(p
->fts_statp
->st_mode
))
174 printf(flavor
== F_FREEBSD9
?
175 "\ttype expected %s found %s\n" : "\ttype (%s, %s)\n",
176 nodetype(s
->type
), inotype(p
->fts_statp
->st_mode
));
181 #if HAVE_STRUCT_STAT_ST_FLAGS
182 if (iflag
&& !uflag
) {
183 if (s
->flags
& F_FLAGS
)
184 SETFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
187 if (mflag
&& !uflag
) {
188 if (s
->flags
& F_FLAGS
)
189 CLEARFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
193 if (s
->flags
& F_DEV
&&
194 (s
->type
== F_BLOCK
|| s
->type
== F_CHAR
) &&
195 s
->st_rdev
!= p
->fts_statp
->st_rdev
) {
197 printf(flavor
== F_FREEBSD9
?
198 "%sdevice expected %#jx found %#jx" :
199 "%sdevice (%#jx, %#jx",
200 tab
, (uintmax_t)s
->st_rdev
,
201 (uintmax_t)p
->fts_statp
->st_rdev
);
203 if ((unlink(p
->fts_accpath
) == -1) ||
204 (mknod(p
->fts_accpath
,
205 s
->st_mode
| nodetoino(s
->type
),
206 s
->st_rdev
) == -1) ||
207 (lchown(p
->fts_accpath
, p
->fts_statp
->st_uid
,
208 p
->fts_statp
->st_gid
) == -1) )
209 printf(", not modified: %s%s\n",
211 flavor
== F_FREEBSD9
? "" : ")");
213 printf(", modified%s\n",
214 flavor
== F_FREEBSD9
? "" : ")");
219 /* Set the uid/gid first, then set the mode. */
220 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
222 printf(flavor
== F_FREEBSD9
?
223 "%suser expected %lu found %lu" : "%suser (%lu, %lu",
224 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
226 if (lchown(p
->fts_accpath
, s
->st_uid
, -1))
227 printf(", not modified: %s%s\n",
229 flavor
== F_FREEBSD9
? "" : ")");
231 printf(", modified%s\n",
232 flavor
== F_FREEBSD9
? "" : ")");
237 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
239 printf(flavor
== F_FREEBSD9
?
240 "%sgid expected %lu found %lu" : "%sgid (%lu, %lu",
241 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
243 if (lchown(p
->fts_accpath
, -1, s
->st_gid
))
244 printf(", not modified: %s%s\n",
246 flavor
== F_FREEBSD9
? "" : ")");
248 printf(", modified%s\n",
249 flavor
== F_FREEBSD9
? "" : ")");
255 if (s
->flags
& F_MODE
&&
256 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
261 mode
= p
->fts_statp
->st_mode
& MBITS
;
263 * if none of the suid/sgid/etc bits are set,
264 * then if the mode is a subset of the target,
267 if (!((tmode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) ||
268 (mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
))))
269 if ((mode
| tmode
) == tmode
)
274 printf(flavor
== F_FREEBSD9
?
275 "%spermissions expcted %#lo found %#lo" :
276 "%spermissions (%#lo, %#lo",
277 tab
, (u_long
)s
->st_mode
,
278 (u_long
)p
->fts_statp
->st_mode
& MBITS
);
280 if (lchmod(p
->fts_accpath
, s
->st_mode
))
281 printf(", not modified: %s%s\n",
283 flavor
== F_FREEBSD9
? "" : ")");
285 printf(", modified%s\n",
286 flavor
== F_FREEBSD9
? "" : ")");
293 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
294 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
296 printf(flavor
== F_FREEBSD9
?
297 "%slink count expected %lu found %lu\n" :
298 "%slink count (%lu, %lu)\n",
299 tab
, (u_long
)s
->st_nlink
, (u_long
)p
->fts_statp
->st_nlink
);
302 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
) {
304 printf(flavor
== F_FREEBSD9
?
305 "%ssize expected %ju found %ju\n" : "%ssize (%ju, %ju)\n",
306 tab
, (uintmax_t)s
->st_size
,
307 (uintmax_t)p
->fts_statp
->st_size
);
312 * Since utimes(2) only takes a timeval, there's no point in
313 * comparing the low bits of the timespec nanosecond field. This
314 * will only result in mismatches that we can never fix.
316 * Doesn't display microsecond differences.
318 if (s
->flags
& F_TIME
) {
319 struct timeval tv
[2];
320 struct stat
*ps
= p
->fts_statp
;
321 time_t smtime
= s
->st_mtimespec
.tv_sec
;
323 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
324 time_t pmtime
= ps
->st_mtimespec
.tv_sec
;
326 TIMESPEC_TO_TIMEVAL(&tv
[0], &s
->st_mtimespec
);
327 TIMESPEC_TO_TIMEVAL(&tv
[1], &ps
->st_mtimespec
);
329 time_t pmtime
= (time_t)ps
->st_mtime
;
331 tv
[0].tv_sec
= smtime
;
333 tv
[1].tv_sec
= pmtime
;
337 if (tv
[0].tv_sec
!= tv
[1].tv_sec
||
338 tv
[0].tv_usec
!= tv
[1].tv_usec
) {
340 printf(flavor
== F_FREEBSD9
?
341 "%smodification time expected %.24s found " :
342 "%smodification time (%.24s, ",
343 tab
, ctime(&smtime
));
344 printf("%.24s", ctime(&pmtime
));
347 if (utimes(p
->fts_accpath
, tv
))
348 printf(", not modified: %s%s\n",
350 flavor
== F_FREEBSD9
? "" : ")");
352 printf(", modified%s\n",
353 flavor
== F_FREEBSD9
? "" : ")");
355 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
359 #if HAVE_STRUCT_STAT_ST_FLAGS
362 * since lchflags(2) will reset file times, the utimes() above
363 * may have been useless! oh well, we'd rather have correct
364 * flags, rather than times?
366 if ((s
->flags
& F_FLAGS
) && ((s
->st_flags
!= p
->fts_statp
->st_flags
)
367 || mflag
|| iflag
)) {
368 if (s
->st_flags
!= p
->fts_statp
->st_flags
) {
371 f_s
= flags_to_string(s
->st_flags
, "none");
372 printf(flavor
== F_FREEBSD9
?
373 "%sflags expected \"%s\" found " :
374 "%sflags (\"%s\" is not ", tab
, f_s
);
376 f_s
= flags_to_string(p
->fts_statp
->st_flags
, "none");
377 printf("\"%s\"", f_s
);
382 SETFLAGS(0, CH_MASK
);
384 CLEARFLAGS(0, SP_FLGS
);
386 SETFLAGS(0, (~SP_FLGS
& CH_MASK
));
388 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
391 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
394 * from this point, no more permission checking or whacking
395 * occurs, only checking of stuff like checksums and symlinks.
398 if (s
->flags
& F_CKSUM
) {
399 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0) {
401 printf("%scksum: %s: %s\n",
402 tab
, p
->fts_accpath
, strerror(errno
));
404 } else if (crc(fd
, &val
, &len
)) {
407 printf("%scksum: %s: %s\n",
408 tab
, p
->fts_accpath
, strerror(errno
));
412 if (s
->cksum
!= val
) {
414 printf(flavor
== F_FREEBSD9
?
415 "%scksum expected %lu found %lu\n" :
416 "%scksum (%lu, %lu)\n",
417 tab
, s
->cksum
, (unsigned long)val
);
423 if (s
->flags
& F_MD5
) {
424 if ((digestbuf
= MD5File(p
->fts_accpath
, NULL
)) == NULL
) {
426 printf("%s%s: %s: %s\n",
427 tab
, MD5KEY
, p
->fts_accpath
, strerror(errno
));
430 if (strcmp(s
->md5digest
, digestbuf
)) {
432 printf(flavor
== F_FREEBSD9
?
433 "%s%s expected %s found %s\n" :
434 "%s%s (0x%s, 0x%s)\n",
435 tab
, MD5KEY
, s
->md5digest
, digestbuf
);
441 #endif /* ! NO_MD5 */
443 if (s
->flags
& F_RMD160
) {
444 if ((digestbuf
= RIPEMD160_File(p
->fts_accpath
, NULL
)) == NULL
) {
446 printf("%s%s: %s: %s\n",
447 tab
, RMD160KEY
, p
->fts_accpath
, strerror(errno
));
450 if (strcmp(s
->rmd160digest
, digestbuf
)) {
452 printf(flavor
== F_FREEBSD9
?
453 "%s%s expected %s found %s\n" :
454 "%s%s (0x%s, 0x%s)\n",
455 tab
, RMD160KEY
, s
->rmd160digest
, digestbuf
);
461 #endif /* ! NO_RMD160 */
463 if (s
->flags
& F_SHA1
) {
464 if ((digestbuf
= SHA1_File(p
->fts_accpath
, NULL
)) == NULL
) {
466 printf("%s%s: %s: %s\n",
467 tab
, SHA1KEY
, p
->fts_accpath
, strerror(errno
));
470 if (strcmp(s
->sha1digest
, digestbuf
)) {
472 printf(flavor
== F_FREEBSD9
?
473 "%s%s expected %s found %s\n" :
474 "%s%s (0x%s, 0x%s)\n",
475 tab
, SHA1KEY
, s
->sha1digest
, digestbuf
);
481 #endif /* ! NO_SHA1 */
483 if (s
->flags
& F_SHA256
) {
484 if ((digestbuf
= SHA256_File(p
->fts_accpath
, NULL
)) == NULL
) {
486 printf("%s%s: %s: %s\n",
487 tab
, SHA256KEY
, p
->fts_accpath
, strerror(errno
));
490 if (strcmp(s
->sha256digest
, digestbuf
)) {
492 printf(flavor
== F_FREEBSD9
?
493 "%s%s expected %s found %s\n" :
494 "%s%s (0x%s, 0x%s)\n",
495 tab
, SHA256KEY
, s
->sha256digest
, digestbuf
);
501 #ifdef SHA384_BLOCK_LENGTH
502 #if !defined(__DragonFly__)
503 if (s
->flags
& F_SHA384
) {
504 if ((digestbuf
= SHA384_File(p
->fts_accpath
, NULL
)) == NULL
) {
506 printf("%s%s: %s: %s\n",
507 tab
, SHA384KEY
, p
->fts_accpath
, strerror(errno
));
510 if (strcmp(s
->sha384digest
, digestbuf
)) {
512 printf(flavor
== F_FREEBSD9
?
513 "%s%s expected %s found %s\n" :
514 "%s%s (0x%s, 0x%s)\n",
515 tab
, SHA384KEY
, s
->sha384digest
, digestbuf
);
523 if (s
->flags
& F_SHA512
) {
524 if ((digestbuf
= SHA512_File(p
->fts_accpath
, NULL
)) == NULL
) {
526 printf("%s%s: %s: %s\n",
527 tab
, SHA512KEY
, p
->fts_accpath
, strerror(errno
));
530 if (strcmp(s
->sha512digest
, digestbuf
)) {
532 printf(flavor
== F_FREEBSD9
?
533 "%s%s expected %s found %s\n" :
534 "%s%s (0x%s, 0x%s)\n",
535 tab
, SHA512KEY
, s
->sha512digest
, digestbuf
);
541 #endif /* ! NO_SHA2 */
542 if (s
->flags
& F_SLINK
&&
543 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
545 printf(flavor
== F_FREEBSD9
?
546 "%slink ref expected %s found %s" :
547 "%slink ref (%s, %s", tab
, cp
, s
->slink
);
549 if ((unlink(p
->fts_accpath
) == -1) ||
550 (symlink(s
->slink
, p
->fts_accpath
) == -1) )
551 printf(", not modified: %s%s\n",
553 flavor
== F_FREEBSD9
? "" : ")");
555 printf(", modified%s\n",
556 flavor
== F_FREEBSD9
? "" : ")");
558 printf("%s\n", flavor
== F_FREEBSD9
? "" : ")");
564 rlink(const char *name
)
566 static char lbuf
[MAXPATHLEN
];
569 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
570 mtree_err("%s: %s", name
, strerror(errno
));