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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)compare.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/mtree/compare.c,v 1.15.2.4 2003/05/07 17:55:17 tobez Exp $
35 * $DragonFly: src/usr.sbin/mtree/compare.c,v 1.3 2003/11/03 19:31:39 eirikn Exp $
38 #include <sys/param.h>
62 static char *ftype(u_int);
64 #define INDENTNAMELEN 8
67 len = printf("%s changed\n", RP(p)); \
86 if (!S_ISBLK(p->fts_statp->st_mode))
90 if (!S_ISCHR(p->fts_statp->st_mode))
94 if (!S_ISDIR(p->fts_statp->st_mode))
98 if (!S_ISFIFO(p->fts_statp->st_mode))
102 if (!S_ISREG(p->fts_statp->st_mode))
106 if (!S_ISLNK(p->fts_statp->st_mode))
110 if (!S_ISSOCK(p->fts_statp->st_mode)) {
112 (void)printf("\ttype expected %s found %s\n",
113 ftype(s->type), inotype(p->fts_statp->st_mode));
118 /* Set the uid/gid first, then set the mode. */
119 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
121 (void)printf("%suser expected %lu found %lu",
122 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
124 if (chown(p->fts_accpath, s->st_uid, -1))
125 (void)printf(" not modified: %s\n",
128 (void)printf(" modified\n");
133 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
135 (void)printf("%sgid expected %lu found %lu",
136 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
138 if (chown(p->fts_accpath, -1, s->st_gid))
139 (void)printf(" not modified: %s\n",
142 (void)printf(" modified\n");
147 if (s->flags & F_MODE &&
148 !S_ISLNK(p->fts_statp->st_mode) &&
149 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
151 (void)printf("%spermissions expected %#o found %#o",
152 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
154 if (chmod(p->fts_accpath, s->st_mode))
155 (void)printf(" not modified: %s\n",
158 (void)printf(" modified\n");
163 if (s->flags & F_NLINK && s->type != F_DIR &&
164 s->st_nlink != p->fts_statp->st_nlink) {
166 (void)printf("%slink_count expected %u found %u\n",
167 tab, s->st_nlink, p->fts_statp->st_nlink);
170 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
171 !S_ISDIR(p->fts_statp->st_mode)) {
173 (void)printf("%ssize expected %qd found %qd\n",
174 tab, s->st_size, p->fts_statp->st_size);
179 * Catches nano-second differences, but doesn't display them.
181 if ((s->flags & F_TIME) &&
182 ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
183 (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
185 (void)printf("%smodification time expected %.24s ",
186 tab, ctime(&s->st_mtimespec.tv_sec));
187 (void)printf("found %.24s\n",
188 ctime(&p->fts_statp->st_mtimespec.tv_sec));
191 if (s->flags & F_CKSUM) {
192 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
194 (void)printf("%scksum: %s: %s\n",
195 tab, p->fts_accpath, strerror(errno));
197 } else if (crc(fd, &val, &len)) {
200 (void)printf("%scksum: %s: %s\n",
201 tab, p->fts_accpath, strerror(errno));
205 if (s->cksum != val) {
207 (void)printf("%scksum expected %lu found %lu\n",
215 * since chflags(2) will reset file times, the utimes() above
216 * may have been useless! oh well, we'd rather have correct
217 * flags, rather than times?
219 if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
221 fflags = flags_to_string(s->st_flags);
222 (void)printf("%sflags expected \"%s\"", tab, fflags);
225 fflags = flags_to_string(p->fts_statp->st_flags);
226 (void)printf(" found \"%s\"", fflags);
230 if (chflags(p->fts_accpath, s->st_flags))
231 (void)printf(" not modified: %s\n",
234 (void)printf(" modified\n");
240 if (s->flags & F_MD5) {
241 char *new_digest, buf[33];
243 new_digest = MD5File(p->fts_accpath, buf);
246 printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
249 } else if (strcmp(new_digest, s->md5digest)) {
251 printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
258 if (s->flags & F_SHA1) {
259 char *new_digest, buf[41];
261 new_digest = SHA1_File(p->fts_accpath, buf);
264 printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
267 } else if (strcmp(new_digest, s->sha1digest)) {
269 printf("%sSHA-1 expected %s found %s\n",
270 tab, s->sha1digest, new_digest);
276 if (s->flags & F_RMD160) {
277 char *new_digest, buf[41];
279 new_digest = RIPEMD160_File(p->fts_accpath, buf);
282 printf("%sRIPEMD160: %s: %s\n", tab,
283 p->fts_accpath, strerror(errno));
285 } else if (strcmp(new_digest, s->rmd160digest)) {
287 printf("%sRIPEMD160 expected %s found %s\n",
288 tab, s->rmd160digest, new_digest);
294 if (s->flags & F_SLINK &&
295 strcmp(cp = rlink(p->fts_accpath), s->slink)) {
297 (void)printf("%slink_ref expected %s found %s\n",
307 switch(type & S_IFMT) {
357 static char lbuf[MAXPATHLEN];
360 if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
361 err(1, "line %d: %s", lineno, name);