2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)compare.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/mtree/compare.c,v 1.15.2.4 2003/05/07 17:55:17 tobez Exp $
31 * $DragonFly: src/usr.sbin/mtree/compare.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
34 #include <sys/param.h>
45 #include "../../sbin/md5/sha1hl.h"
56 static const char *ftype(u_int
);
58 #define INDENTNAMELEN 8
61 len = printf("%s changed\n", RP(p)); \
66 compare(NODE
*s
, FTSENT
*p
)
76 if (!S_ISBLK(p
->fts_statp
->st_mode
))
80 if (!S_ISCHR(p
->fts_statp
->st_mode
))
84 if (!S_ISDIR(p
->fts_statp
->st_mode
))
88 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
92 if (!S_ISREG(p
->fts_statp
->st_mode
))
96 if (!S_ISLNK(p
->fts_statp
->st_mode
))
100 if (!S_ISSOCK(p
->fts_statp
->st_mode
)) {
102 printf("\ttype expected %s found %s\n",
103 ftype(s
->type
), inotype(p
->fts_statp
->st_mode
));
108 /* Set the uid/gid first, then set the mode. */
109 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
111 printf("%suser expected %lu found %lu",
112 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
114 if (chown(p
->fts_accpath
, s
->st_uid
, -1))
115 printf(" not modified: %s\n",
118 printf(" modified\n");
123 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
125 printf("%sgid expected %lu found %lu",
126 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
128 if (chown(p
->fts_accpath
, -1, s
->st_gid
))
129 printf(" not modified: %s\n",
132 printf(" modified\n");
137 if (s
->flags
& F_MODE
&&
138 !S_ISLNK(p
->fts_statp
->st_mode
) &&
139 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
141 printf("%spermissions expected %#o found %#o",
142 tab
, s
->st_mode
, p
->fts_statp
->st_mode
& MBITS
);
144 if (chmod(p
->fts_accpath
, s
->st_mode
))
145 printf(" not modified: %s\n",
148 printf(" modified\n");
153 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
154 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
156 printf("%slink_count expected %u found %u\n",
157 tab
, s
->st_nlink
, p
->fts_statp
->st_nlink
);
160 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
&&
161 !S_ISDIR(p
->fts_statp
->st_mode
)) {
163 printf("%ssize expected %jd found %jd\n", tab
,
164 (intmax_t)s
->st_size
, (intmax_t)p
->fts_statp
->st_size
);
169 * Catches nano-second differences, but doesn't display them.
171 if ((s
->flags
& F_TIME
) &&
172 ((s
->st_mtimespec
.tv_sec
!= p
->fts_statp
->st_mtimespec
.tv_sec
) ||
173 (s
->st_mtimespec
.tv_nsec
!= p
->fts_statp
->st_mtimespec
.tv_nsec
))) {
175 printf("%smodification time expected %.24s ",
176 tab
, ctime(&s
->st_mtimespec
.tv_sec
));
177 printf("found %.24s\n",
178 ctime(&p
->fts_statp
->st_mtimespec
.tv_sec
));
181 if (s
->flags
& F_CKSUM
) {
182 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0) {
184 printf("%scksum: %s: %s\n",
185 tab
, p
->fts_accpath
, strerror(errno
));
187 } else if (crc(fd
, &val
, &len
)) {
190 printf("%scksum: %s: %s\n",
191 tab
, p
->fts_accpath
, strerror(errno
));
195 if (s
->cksum
!= val
) {
197 printf("%scksum expected %lu found %lu\n",
205 * since chflags(2) will reset file times, the utimes() above
206 * may have been useless! oh well, we'd rather have correct
207 * flags, rather than times?
209 if ((s
->flags
& F_FLAGS
) && s
->st_flags
!= p
->fts_statp
->st_flags
) {
211 fflags
= flags_to_string(s
->st_flags
);
212 printf("%sflags expected \"%s\"", tab
, fflags
);
215 fflags
= flags_to_string(p
->fts_statp
->st_flags
);
216 printf(" found \"%s\"", fflags
);
220 if (chflags(p
->fts_accpath
, s
->st_flags
))
221 printf(" not modified: %s\n",
224 printf(" modified\n");
230 if (s
->flags
& F_MD5
) {
231 char *new_digest
, buf
[33];
233 new_digest
= MD5File(p
->fts_accpath
, buf
);
236 printf("%sMD5: %s: %s\n", tab
, p
->fts_accpath
,
239 } else if (strcmp(new_digest
, s
->md5digest
)) {
241 printf("%sMD5 expected %s found %s\n", tab
, s
->md5digest
,
248 if (s
->flags
& F_SHA1
) {
249 char *new_digest
, buf
[41];
251 new_digest
= SHA1_File(p
->fts_accpath
, buf
);
254 printf("%sSHA-1: %s: %s\n", tab
, p
->fts_accpath
,
257 } else if (strcmp(new_digest
, s
->sha1digest
)) {
259 printf("%sSHA-1 expected %s found %s\n",
260 tab
, s
->sha1digest
, new_digest
);
266 if (s
->flags
& F_RMD160
) {
267 char *new_digest
, buf
[41];
269 new_digest
= RIPEMD160_File(p
->fts_accpath
, buf
);
272 printf("%sRIPEMD160: %s: %s\n", tab
,
273 p
->fts_accpath
, strerror(errno
));
275 } else if (strcmp(new_digest
, s
->rmd160digest
)) {
277 printf("%sRIPEMD160 expected %s found %s\n",
278 tab
, s
->rmd160digest
, new_digest
);
284 if (s
->flags
& F_SLINK
&&
285 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
287 printf("%slink_ref expected %s found %s\n",
296 switch(type
& S_IFMT
) {
344 static char lbuf
[MAXPATHLEN
];
347 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
348 err(1, "line %d: %s", lineno
, name
);