1 /* $NetBSD: compare.c,v 1.55 2012/10/05 00:59:35 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.55 2012/10/05 00:59:35 christos Exp $");
45 #include <sys/param.h>
71 #define INDENTNAMELEN 8
74 len = printf("%s: ", RP(p)); \
75 if (len > INDENTNAMELEN) { \
80 printf("%*s", INDENTNAMELEN - (int)len, ""); \
83 #define LABEL if (!label++) MARK
85 #if HAVE_STRUCT_STAT_ST_FLAGS
89 if (flags != p->fts_statp->st_flags) { \
93 sf = flags_to_string(p->fts_statp->st_flags, "none"); \
94 printf("%sflags (\"%s\"", tab, sf); \
97 if (lchflags(p->fts_accpath, flags)) { \
99 printf(", not modified: %s)\n", \
102 sf = flags_to_string(flags, "none"); \
103 printf(", modified to \"%s\")\n", sf); \
109 * given pflags, additionally set those flags specified in s->st_flags and
110 * selected by mask (the other flags are left unchanged).
112 #define SETFLAGS(pflags, mask) \
114 flags = (s->st_flags & (mask)) | (pflags); \
119 * given pflags, reset the flags specified in s->st_flags and selected by mask
120 * (the other flags are left unchanged).
122 #define CLEARFLAGS(pflags, mask) \
124 flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags); \
127 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
132 lchmod(const char *path
, mode_t flags
)
138 lchown(const char *path
, uid_t owner
, gid_t group
)
145 utimes(const char *path
, const struct timeval times
[2])
152 compare(NODE
*s
, FTSENT
*p
)
155 #if HAVE_STRUCT_STAT_ST_FLAGS
159 const char *cp
, *tab
;
160 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
168 if (!S_ISBLK(p
->fts_statp
->st_mode
))
172 if (!S_ISCHR(p
->fts_statp
->st_mode
))
176 if (!S_ISDIR(p
->fts_statp
->st_mode
))
180 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
184 if (!S_ISREG(p
->fts_statp
->st_mode
))
188 if (!S_ISLNK(p
->fts_statp
->st_mode
))
193 if (!S_ISSOCK(p
->fts_statp
->st_mode
))
198 printf("\ttype (%s, %s)\n",
199 nodetype(s
->type
), inotype(p
->fts_statp
->st_mode
));
204 #if HAVE_STRUCT_STAT_ST_FLAGS
205 if (iflag
&& !uflag
) {
206 if (s
->flags
& F_FLAGS
)
207 SETFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
210 if (mflag
&& !uflag
) {
211 if (s
->flags
& F_FLAGS
)
212 CLEARFLAGS(p
->fts_statp
->st_flags
, SP_FLGS
);
216 if (s
->flags
& F_DEV
&&
217 (s
->type
== F_BLOCK
|| s
->type
== F_CHAR
) &&
218 s
->st_rdev
!= p
->fts_statp
->st_rdev
) {
220 printf("%sdevice (%#llx, %#llx",
221 tab
, (long long)s
->st_rdev
,
222 (long long)p
->fts_statp
->st_rdev
);
224 if ((unlink(p
->fts_accpath
) == -1) ||
225 (mknod(p
->fts_accpath
,
226 s
->st_mode
| nodetoino(s
->type
),
227 s
->st_rdev
) == -1) ||
228 (lchown(p
->fts_accpath
, p
->fts_statp
->st_uid
,
229 p
->fts_statp
->st_gid
) == -1) )
230 printf(", not modified: %s)\n",
233 printf(", modified)\n");
238 /* Set the uid/gid first, then set the mode. */
239 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
241 printf("%suser (%lu, %lu",
242 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
244 if (lchown(p
->fts_accpath
, s
->st_uid
, -1))
245 printf(", not modified: %s)\n",
248 printf(", modified)\n");
253 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
255 printf("%sgid (%lu, %lu",
256 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
258 if (lchown(p
->fts_accpath
, -1, s
->st_gid
))
259 printf(", not modified: %s)\n",
262 printf(", modified)\n");
268 if (s
->flags
& F_MODE
&&
269 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
274 mode
= p
->fts_statp
->st_mode
& MBITS
;
276 * if none of the suid/sgid/etc bits are set,
277 * then if the mode is a subset of the target,
280 if (!((tmode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) ||
281 (mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
))))
282 if ((mode
| tmode
) == tmode
)
287 printf("%spermissions (%#lo, %#lo",
288 tab
, (u_long
)s
->st_mode
,
289 (u_long
)p
->fts_statp
->st_mode
& MBITS
);
291 if (lchmod(p
->fts_accpath
, s
->st_mode
))
292 printf(", not modified: %s)\n",
295 printf(", modified)\n");
302 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
303 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
305 printf("%slink count (%lu, %lu)\n",
306 tab
, (u_long
)s
->st_nlink
, (u_long
)p
->fts_statp
->st_nlink
);
309 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
) {
311 printf("%ssize (%lld, %lld)\n",
312 tab
, (long long)s
->st_size
,
313 (long long)p
->fts_statp
->st_size
);
318 * Since utimes(2) only takes a timeval, there's no point in
319 * comparing the low bits of the timespec nanosecond field. This
320 * will only result in mismatches that we can never fix.
322 * Doesn't display microsecond differences.
324 if (s
->flags
& F_TIME
) {
325 struct timeval tv
[2];
326 struct stat
*ps
= p
->fts_statp
;
327 time_t smtime
= s
->st_mtimespec
.tv_sec
;
329 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
330 time_t pmtime
= ps
->st_mtimespec
.tv_sec
;
332 TIMESPEC_TO_TIMEVAL(&tv
[0], &s
->st_mtimespec
);
333 TIMESPEC_TO_TIMEVAL(&tv
[1], &ps
->st_mtimespec
);
335 time_t pmtime
= (time_t)ps
->st_mtime
;
337 tv
[0].tv_sec
= smtime
;
339 tv
[1].tv_sec
= pmtime
;
343 if (tv
[0].tv_sec
!= tv
[1].tv_sec
||
344 tv
[0].tv_usec
!= tv
[1].tv_usec
) {
346 printf("%smodification time (%.24s, ",
347 tab
, ctime(&smtime
));
348 printf("%.24s", ctime(&pmtime
));
351 if (utimes(p
->fts_accpath
, tv
))
352 printf(", not modified: %s)\n",
355 printf(", modified)\n");
361 #if HAVE_STRUCT_STAT_ST_FLAGS
364 * since lchflags(2) will reset file times, the utimes() above
365 * may have been useless! oh well, we'd rather have correct
366 * flags, rather than times?
368 if ((s
->flags
& F_FLAGS
) && ((s
->st_flags
!= p
->fts_statp
->st_flags
)
369 || mflag
|| iflag
)) {
370 if (s
->st_flags
!= p
->fts_statp
->st_flags
) {
373 f_s
= flags_to_string(s
->st_flags
, "none");
374 printf("%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
));
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("%scksum (%lu, %lu)\n",
415 tab
, s
->cksum
, (unsigned long)val
);
421 if (s
->flags
& F_MD5
) {
422 if ((digestbuf
= MD5File(p
->fts_accpath
, NULL
)) == NULL
) {
424 printf("%s%s: %s: %s\n",
425 tab
, MD5KEY
, p
->fts_accpath
, strerror(errno
));
428 if (strcmp(s
->md5digest
, digestbuf
)) {
430 printf("%s%s (0x%s, 0x%s)\n",
431 tab
, MD5KEY
, s
->md5digest
, digestbuf
);
437 #endif /* ! NO_MD5 */
439 if (s
->flags
& F_RMD160
) {
440 if ((digestbuf
= RMD160File(p
->fts_accpath
, NULL
)) == NULL
) {
442 printf("%s%s: %s: %s\n",
443 tab
, RMD160KEY
, p
->fts_accpath
, strerror(errno
));
446 if (strcmp(s
->rmd160digest
, digestbuf
)) {
448 printf("%s%s (0x%s, 0x%s)\n",
449 tab
, RMD160KEY
, s
->rmd160digest
, digestbuf
);
455 #endif /* ! NO_RMD160 */
457 if (s
->flags
& F_SHA1
) {
458 if ((digestbuf
= SHA1File(p
->fts_accpath
, NULL
)) == NULL
) {
460 printf("%s%s: %s: %s\n",
461 tab
, SHA1KEY
, p
->fts_accpath
, strerror(errno
));
464 if (strcmp(s
->sha1digest
, digestbuf
)) {
466 printf("%s%s (0x%s, 0x%s)\n",
467 tab
, SHA1KEY
, s
->sha1digest
, digestbuf
);
473 #endif /* ! NO_SHA1 */
475 if (s
->flags
& F_SHA256
) {
476 if ((digestbuf
= SHA256_File(p
->fts_accpath
, NULL
)) == NULL
) {
478 printf("%s%s: %s: %s\n",
479 tab
, SHA256KEY
, p
->fts_accpath
, strerror(errno
));
482 if (strcmp(s
->sha256digest
, digestbuf
)) {
484 printf("%s%s (0x%s, 0x%s)\n",
485 tab
, SHA256KEY
, s
->sha256digest
, digestbuf
);
491 #ifdef SHA384_BLOCK_LENGTH
492 if (s
->flags
& F_SHA384
) {
493 if ((digestbuf
= SHA384_File(p
->fts_accpath
, NULL
)) == NULL
) {
495 printf("%s%s: %s: %s\n",
496 tab
, SHA384KEY
, p
->fts_accpath
, strerror(errno
));
499 if (strcmp(s
->sha384digest
, digestbuf
)) {
501 printf("%s%s (0x%s, 0x%s)\n",
502 tab
, SHA384KEY
, s
->sha384digest
, digestbuf
);
509 if (s
->flags
& F_SHA512
) {
510 if ((digestbuf
= SHA512_File(p
->fts_accpath
, NULL
)) == NULL
) {
512 printf("%s%s: %s: %s\n",
513 tab
, SHA512KEY
, p
->fts_accpath
, strerror(errno
));
516 if (strcmp(s
->sha512digest
, digestbuf
)) {
518 printf("%s%s (0x%s, 0x%s)\n",
519 tab
, SHA512KEY
, s
->sha512digest
, digestbuf
);
525 #endif /* ! NO_SHA2 */
526 if (s
->flags
& F_SLINK
&&
527 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
529 printf("%slink ref (%s, %s", tab
, cp
, s
->slink
);
531 if ((unlink(p
->fts_accpath
) == -1) ||
532 (symlink(s
->slink
, p
->fts_accpath
) == -1) )
533 printf(", not modified: %s)\n",
536 printf(", modified)\n");
544 rlink(const char *name
)
546 static char lbuf
[MAXPATHLEN
];
549 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
550 mtree_err("%s: %s", name
, strerror(errno
));