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
32 static char sccsid
[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/mtree/compare.c,v 1.34 2005/03/29 11:44:17 tobez Exp $");
38 #include <sys/param.h>
59 #endif /* !__APPLE__ */
70 #include "commoncrypto.h"
71 #endif /* __APPLE__ */
73 #define INDENTNAMELEN 8
76 len = printf("%s changed\n", RP(p)); \
81 compare(char *name __unused
, NODE
*s
, FTSENT
*p
)
89 char *fflags
, *badflags
;
95 if (!S_ISBLK(p
->fts_statp
->st_mode
))
99 if (!S_ISCHR(p
->fts_statp
->st_mode
))
103 if (!S_ISDIR(p
->fts_statp
->st_mode
))
107 if (!S_ISFIFO(p
->fts_statp
->st_mode
))
111 if (!S_ISREG(p
->fts_statp
->st_mode
))
115 if (!S_ISLNK(p
->fts_statp
->st_mode
))
119 if (!S_ISSOCK(p
->fts_statp
->st_mode
)) {
121 (void)printf("\ttype expected %s found %s\n",
122 ftype(s
->type
), inotype(p
->fts_statp
->st_mode
));
127 /* Set the uid/gid first, then set the mode. */
128 if (s
->flags
& (F_UID
| F_UNAME
) && s
->st_uid
!= p
->fts_statp
->st_uid
) {
130 (void)printf("%suser expected %lu found %lu",
131 tab
, (u_long
)s
->st_uid
, (u_long
)p
->fts_statp
->st_uid
);
133 if (chown(p
->fts_accpath
, s
->st_uid
, -1))
134 (void)printf(" not modified: %s\n",
137 (void)printf(" modified\n");
142 if (s
->flags
& (F_GID
| F_GNAME
) && s
->st_gid
!= p
->fts_statp
->st_gid
) {
144 (void)printf("%sgid expected %lu found %lu",
145 tab
, (u_long
)s
->st_gid
, (u_long
)p
->fts_statp
->st_gid
);
147 if (chown(p
->fts_accpath
, -1, s
->st_gid
))
148 (void)printf(" not modified: %s\n",
151 (void)printf(" modified\n");
156 if (s
->flags
& F_MODE
&&
157 !S_ISLNK(p
->fts_statp
->st_mode
) &&
158 s
->st_mode
!= (p
->fts_statp
->st_mode
& MBITS
)) {
160 (void)printf("%spermissions expected %#o found %#o",
161 tab
, s
->st_mode
, p
->fts_statp
->st_mode
& MBITS
);
163 if (chmod(p
->fts_accpath
, s
->st_mode
))
164 (void)printf(" not modified: %s\n",
167 (void)printf(" modified\n");
172 if (s
->flags
& F_NLINK
&& s
->type
!= F_DIR
&&
173 s
->st_nlink
!= p
->fts_statp
->st_nlink
) {
175 (void)printf("%slink_count expected %u found %u\n",
176 tab
, s
->st_nlink
, p
->fts_statp
->st_nlink
);
179 if (s
->flags
& F_SIZE
&& s
->st_size
!= p
->fts_statp
->st_size
&&
180 !S_ISDIR(p
->fts_statp
->st_mode
)) {
182 (void)printf("%ssize expected %jd found %jd\n", tab
,
183 (intmax_t)s
->st_size
, (intmax_t)p
->fts_statp
->st_size
);
186 if ((s
->flags
& F_TIME
) &&
187 ((s
->st_mtimespec
.tv_sec
!= p
->fts_statp
->st_mtimespec
.tv_sec
) ||
188 (s
->st_mtimespec
.tv_nsec
!= p
->fts_statp
->st_mtimespec
.tv_nsec
))) {
190 (void)printf("%smodification time expected %.24s.%09ld ",
191 tab
, ctime(&s
->st_mtimespec
.tv_sec
), s
->st_mtimespec
.tv_nsec
);
192 (void)printf("found %.24s.%09ld",
193 ctime(&p
->fts_statp
->st_mtimespec
.tv_sec
), p
->fts_statp
->st_mtimespec
.tv_nsec
);
195 tv
[0].tv_sec
= s
->st_mtimespec
.tv_sec
;
196 tv
[0].tv_usec
= s
->st_mtimespec
.tv_nsec
/ 1000;
198 if (utimes(p
->fts_accpath
, tv
))
199 (void)printf(" not modified: %s\n",
202 (void)printf(" modified\n");
207 if (s
->flags
& F_CKSUM
) {
208 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0) {
210 (void)printf("%scksum: %s: %s\n",
211 tab
, p
->fts_accpath
, strerror(errno
));
213 } else if (crc(fd
, &val
, &len
)) {
216 (void)printf("%scksum: %s: %s\n",
217 tab
, p
->fts_accpath
, strerror(errno
));
221 if (s
->cksum
!= val
) {
223 (void)printf("%scksum expected %lu found %lu\n",
224 tab
, s
->cksum
, (unsigned long)val
);
229 if (s
->flags
& F_FLAGS
) {
230 // There are unpublished flags that should not fail comparison
231 // we convert to string and back to filter them out
232 fflags
= badflags
= flags_to_string(p
->fts_statp
->st_flags
);
233 if (strcmp("none", fflags
) == 0) {
235 } else if (strtofflags(&badflags
, &flags
, NULL
) != 0)
236 errx(1, "invalid flag %s", badflags
);
238 if (s
->st_flags
!= flags
) {
240 fflags
= flags_to_string(s
->st_flags
);
241 (void)printf("%sflags expected \"%s\"", tab
, fflags
);
244 fflags
= flags_to_string(flags
);
245 (void)printf(" found \"%s\"", fflags
);
249 if (chflags(p
->fts_accpath
, (u_int
)s
->st_flags
))
250 (void)printf(" not modified: %s\n",
253 (void)printf(" modified\n");
260 if (s
->flags
& F_MD5
) {
261 char *new_digest
, buf
[33];
263 new_digest
= MD5File(p
->fts_accpath
, buf
);
266 printf("%sMD5: %s: %s\n", tab
, p
->fts_accpath
,
269 } else if (strcmp(new_digest
, s
->md5digest
)) {
271 printf("%sMD5 expected %s found %s\n", tab
, s
->md5digest
,
276 #endif /* ENABLE_MD5 */
278 if (s
->flags
& F_SHA1
) {
279 char *new_digest
, buf
[41];
281 new_digest
= SHA1_File(p
->fts_accpath
, buf
);
284 printf("%sSHA-1: %s: %s\n", tab
, p
->fts_accpath
,
287 } else if (strcmp(new_digest
, s
->sha1digest
)) {
289 printf("%sSHA-1 expected %s found %s\n",
290 tab
, s
->sha1digest
, new_digest
);
294 #endif /* ENABLE_SHA1 */
296 if (s
->flags
& F_RMD160
) {
297 char *new_digest
, buf
[41];
299 new_digest
= RIPEMD160_File(p
->fts_accpath
, buf
);
302 printf("%sRIPEMD160: %s: %s\n", tab
,
303 p
->fts_accpath
, strerror(errno
));
305 } else if (strcmp(new_digest
, s
->rmd160digest
)) {
307 printf("%sRIPEMD160 expected %s found %s\n",
308 tab
, s
->rmd160digest
, new_digest
);
312 #endif /* ENABLE_RMD160 */
314 if (s
->flags
& F_SHA256
) {
315 char *new_digest
, buf
[kSHA256NullTerminatedBuffLen
];
317 new_digest
= SHA256_File(p
->fts_accpath
, buf
);
320 printf("%sSHA-256: %s: %s\n", tab
, p
->fts_accpath
,
323 } else if (strcmp(new_digest
, s
->sha256digest
)) {
325 printf("%sSHA-256 expected %s found %s\n",
326 tab
, s
->sha256digest
, new_digest
);
330 #endif /* ENABLE_SHA256 */
332 if (s
->flags
& F_SLINK
&&
333 strcmp(cp
= rlink(p
->fts_accpath
), s
->slink
)) {
335 (void)printf("%slink_ref expected %s found %s\n",
338 if ((s
->flags
& F_BTIME
) &&
339 ((s
->st_birthtimespec
.tv_sec
!= p
->fts_statp
->st_birthtimespec
.tv_sec
) ||
340 (s
->st_birthtimespec
.tv_nsec
!= p
->fts_statp
->st_birthtimespec
.tv_nsec
))) {
342 (void)printf("%sbirth time expected %.24s.%09ld ",
343 tab
, ctime(&s
->st_birthtimespec
.tv_sec
), s
->st_birthtimespec
.tv_nsec
);
344 (void)printf("found %.24s.%09ld\n",
345 ctime(&p
->fts_statp
->st_birthtimespec
.tv_sec
), p
->fts_statp
->st_birthtimespec
.tv_nsec
);
348 if ((s
->flags
& F_ATIME
) &&
349 ((s
->st_atimespec
.tv_sec
!= p
->fts_statp
->st_atimespec
.tv_sec
) ||
350 (s
->st_atimespec
.tv_nsec
!= p
->fts_statp
->st_atimespec
.tv_nsec
))) {
352 (void)printf("%saccess time expected %.24s.%09ld ",
353 tab
, ctime(&s
->st_atimespec
.tv_sec
), s
->st_atimespec
.tv_nsec
);
354 (void)printf("found %.24s.%09ld\n",
355 ctime(&p
->fts_statp
->st_atimespec
.tv_sec
), p
->fts_statp
->st_atimespec
.tv_nsec
);
358 if ((s
->flags
& F_CTIME
) &&
359 ((s
->st_ctimespec
.tv_sec
!= p
->fts_statp
->st_ctimespec
.tv_sec
) ||
360 (s
->st_ctimespec
.tv_nsec
!= p
->fts_statp
->st_ctimespec
.tv_nsec
))) {
362 (void)printf("%smetadata modification time expected %.24s.%09ld ",
363 tab
, ctime(&s
->st_ctimespec
.tv_sec
), s
->st_ctimespec
.tv_nsec
);
364 (void)printf("found %.24s.%09ld\n",
365 ctime(&p
->fts_statp
->st_ctimespec
.tv_sec
), p
->fts_statp
->st_ctimespec
.tv_nsec
);
368 if (s
->flags
& F_PTIME
) {
370 struct timespec ptimespec
= ptime(p
->fts_accpath
, &supported
);
373 (void)printf("%stime added to parent folder expected %.24s.%09ld found that it is not supported\n",
374 tab
, ctime(&s
->st_ptimespec
.tv_sec
), s
->st_ptimespec
.tv_nsec
);
376 } else if ((s
->st_ptimespec
.tv_sec
!= ptimespec
.tv_sec
) ||
377 (s
->st_ptimespec
.tv_nsec
!= ptimespec
.tv_nsec
)) {
379 (void)printf("%stime added to parent folder expected %.24s.%09ld ",
380 tab
, ctime(&s
->st_ptimespec
.tv_sec
), s
->st_ptimespec
.tv_nsec
);
381 (void)printf("found %.24s.%09ld\n",
382 ctime(&ptimespec
.tv_sec
), ptimespec
.tv_nsec
);
386 if (s
->flags
& F_XATTRS
) {
387 char *new_digest
, buf
[kSHA256NullTerminatedBuffLen
];
388 new_digest
= SHA256_Path_XATTRs(p
->fts_accpath
, buf
);
391 printf("%sxattrsdigest missing, expected: %s\n", tab
, s
->xattrsdigest
);
393 } else if (strcmp(new_digest
, s
->xattrsdigest
)) {
395 printf("%sxattrsdigest expected %s found %s\n",
396 tab
, s
->xattrsdigest
, new_digest
);
400 if ((s
->flags
& F_INODE
) &&
401 (p
->fts_statp
->st_ino
!= s
->st_ino
)) {
403 (void)printf("%sinode expected %llu found %llu\n",
404 tab
, s
->st_ino
, p
->fts_ino
);
407 if (s
->flags
& F_ACL
) {
408 char *new_digest
, buf
[kSHA256NullTerminatedBuffLen
];
409 new_digest
= SHA256_Path_ACL(p
->fts_accpath
, buf
);
412 printf("%sacldigest missing, expected: %s\n", tab
, s
->acldigest
);
414 } else if (strcmp(new_digest
, s
->acldigest
)) {
416 printf("%sacldigest expected %s found %s\n",
417 tab
, s
->acldigest
, new_digest
);
428 switch(type
& S_IFMT
) {
476 static char lbuf
[MAXPATHLEN
];
479 if ((len
= readlink(name
, lbuf
, sizeof(lbuf
) - 1)) == -1)
480 err(1, "line %d: %s", lineno
, name
);