HAMMER 63/Many: IO Error handling features
[dragonfly.git] / usr.sbin / mtree / compare.c
blob5fddafd0e0e12d6f0ae06feaf7055cbdf280790e
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
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.5 2004/03/15 16:24:22 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <fts.h>
44 #ifdef MD5
45 #include <md5.h>
46 #endif
47 #ifdef SHA1
48 #include <sha.h>
49 #endif
50 #ifdef RMD160
51 #include <ripemd.h>
52 #endif
53 #include <stdio.h>
54 #include <time.h>
55 #include <unistd.h>
56 #include "mtree.h"
57 #include "extern.h"
59 extern int uflag;
60 extern int lineno;
62 static char *ftype(u_int);
64 #define INDENTNAMELEN 8
65 #define LABEL \
66 if (!label++) { \
67 len = printf("%s changed\n", RP(p)); \
68 tab = "\t"; \
71 int
72 compare(char *name, NODE *s, FTSENT *p)
74 extern int uflag;
75 u_long len, val;
76 int fd, label;
77 char *cp, *tab = "";
78 char *fflags;
80 label = 0;
81 switch(s->type) {
82 case F_BLOCK:
83 if (!S_ISBLK(p->fts_statp->st_mode))
84 goto typeerr;
85 break;
86 case F_CHAR:
87 if (!S_ISCHR(p->fts_statp->st_mode))
88 goto typeerr;
89 break;
90 case F_DIR:
91 if (!S_ISDIR(p->fts_statp->st_mode))
92 goto typeerr;
93 break;
94 case F_FIFO:
95 if (!S_ISFIFO(p->fts_statp->st_mode))
96 goto typeerr;
97 break;
98 case F_FILE:
99 if (!S_ISREG(p->fts_statp->st_mode))
100 goto typeerr;
101 break;
102 case F_LINK:
103 if (!S_ISLNK(p->fts_statp->st_mode))
104 goto typeerr;
105 break;
106 case F_SOCK:
107 if (!S_ISSOCK(p->fts_statp->st_mode)) {
108 typeerr: LABEL;
109 printf("\ttype expected %s found %s\n",
110 ftype(s->type), inotype(p->fts_statp->st_mode));
111 return (label);
113 break;
115 /* Set the uid/gid first, then set the mode. */
116 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
117 LABEL;
118 printf("%suser expected %lu found %lu",
119 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
120 if (uflag)
121 if (chown(p->fts_accpath, s->st_uid, -1))
122 printf(" not modified: %s\n",
123 strerror(errno));
124 else
125 printf(" modified\n");
126 else
127 printf("\n");
128 tab = "\t";
130 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
131 LABEL;
132 printf("%sgid expected %lu found %lu",
133 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
134 if (uflag)
135 if (chown(p->fts_accpath, -1, s->st_gid))
136 printf(" not modified: %s\n",
137 strerror(errno));
138 else
139 printf(" modified\n");
140 else
141 printf("\n");
142 tab = "\t";
144 if (s->flags & F_MODE &&
145 !S_ISLNK(p->fts_statp->st_mode) &&
146 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
147 LABEL;
148 printf("%spermissions expected %#o found %#o",
149 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
150 if (uflag)
151 if (chmod(p->fts_accpath, s->st_mode))
152 printf(" not modified: %s\n",
153 strerror(errno));
154 else
155 printf(" modified\n");
156 else
157 printf("\n");
158 tab = "\t";
160 if (s->flags & F_NLINK && s->type != F_DIR &&
161 s->st_nlink != p->fts_statp->st_nlink) {
162 LABEL;
163 printf("%slink_count expected %u found %u\n",
164 tab, s->st_nlink, p->fts_statp->st_nlink);
165 tab = "\t";
167 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
168 !S_ISDIR(p->fts_statp->st_mode)) {
169 LABEL;
170 printf("%ssize expected %qd found %qd\n",
171 tab, s->st_size, p->fts_statp->st_size);
172 tab = "\t";
175 * XXX
176 * Catches nano-second differences, but doesn't display them.
178 if ((s->flags & F_TIME) &&
179 ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
180 (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
181 LABEL;
182 printf("%smodification time expected %.24s ",
183 tab, ctime(&s->st_mtimespec.tv_sec));
184 printf("found %.24s\n",
185 ctime(&p->fts_statp->st_mtimespec.tv_sec));
186 tab = "\t";
188 if (s->flags & F_CKSUM) {
189 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
190 LABEL;
191 printf("%scksum: %s: %s\n",
192 tab, p->fts_accpath, strerror(errno));
193 tab = "\t";
194 } else if (crc(fd, &val, &len)) {
195 close(fd);
196 LABEL;
197 printf("%scksum: %s: %s\n",
198 tab, p->fts_accpath, strerror(errno));
199 tab = "\t";
200 } else {
201 close(fd);
202 if (s->cksum != val) {
203 LABEL;
204 printf("%scksum expected %lu found %lu\n",
205 tab, s->cksum, val);
206 tab = "\t";
211 * XXX
212 * since chflags(2) will reset file times, the utimes() above
213 * may have been useless! oh well, we'd rather have correct
214 * flags, rather than times?
216 if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
217 LABEL;
218 fflags = flags_to_string(s->st_flags);
219 printf("%sflags expected \"%s\"", tab, fflags);
220 free(fflags);
222 fflags = flags_to_string(p->fts_statp->st_flags);
223 printf(" found \"%s\"", fflags);
224 free(fflags);
226 if (uflag)
227 if (chflags(p->fts_accpath, s->st_flags))
228 printf(" not modified: %s\n",
229 strerror(errno));
230 else
231 printf(" modified\n");
232 else
233 printf("\n");
234 tab = "\t";
236 #ifdef MD5
237 if (s->flags & F_MD5) {
238 char *new_digest, buf[33];
240 new_digest = MD5File(p->fts_accpath, buf);
241 if (!new_digest) {
242 LABEL;
243 printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
244 strerror(errno));
245 tab = "\t";
246 } else if (strcmp(new_digest, s->md5digest)) {
247 LABEL;
248 printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
249 new_digest);
250 tab = "\t";
253 #endif /* MD5 */
254 #ifdef SHA1
255 if (s->flags & F_SHA1) {
256 char *new_digest, buf[41];
258 new_digest = SHA1_File(p->fts_accpath, buf);
259 if (!new_digest) {
260 LABEL;
261 printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
262 strerror(errno));
263 tab = "\t";
264 } else if (strcmp(new_digest, s->sha1digest)) {
265 LABEL;
266 printf("%sSHA-1 expected %s found %s\n",
267 tab, s->sha1digest, new_digest);
268 tab = "\t";
271 #endif /* SHA1 */
272 #ifdef RMD160
273 if (s->flags & F_RMD160) {
274 char *new_digest, buf[41];
276 new_digest = RIPEMD160_File(p->fts_accpath, buf);
277 if (!new_digest) {
278 LABEL;
279 printf("%sRIPEMD160: %s: %s\n", tab,
280 p->fts_accpath, strerror(errno));
281 tab = "\t";
282 } else if (strcmp(new_digest, s->rmd160digest)) {
283 LABEL;
284 printf("%sRIPEMD160 expected %s found %s\n",
285 tab, s->rmd160digest, new_digest);
286 tab = "\t";
289 #endif /* RMD160 */
291 if (s->flags & F_SLINK &&
292 strcmp(cp = rlink(p->fts_accpath), s->slink)) {
293 LABEL;
294 printf("%slink_ref expected %s found %s\n",
295 tab, s->slink, cp);
297 return (label);
300 char *
301 inotype(u_int type)
303 switch(type & S_IFMT) {
304 case S_IFBLK:
305 return ("block");
306 case S_IFCHR:
307 return ("char");
308 case S_IFDIR:
309 return ("dir");
310 case S_IFIFO:
311 return ("fifo");
312 case S_IFREG:
313 return ("file");
314 case S_IFLNK:
315 return ("link");
316 case S_IFSOCK:
317 return ("socket");
318 default:
319 return ("unknown");
321 /* NOTREACHED */
324 static char *
325 ftype(u_int type)
327 switch(type) {
328 case F_BLOCK:
329 return ("block");
330 case F_CHAR:
331 return ("char");
332 case F_DIR:
333 return ("dir");
334 case F_FIFO:
335 return ("fifo");
336 case F_FILE:
337 return ("file");
338 case F_LINK:
339 return ("link");
340 case F_SOCK:
341 return ("socket");
342 default:
343 return ("unknown");
345 /* NOTREACHED */
348 char *
349 rlink(char *name)
351 static char lbuf[MAXPATHLEN];
352 int len;
354 if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
355 err(1, "line %d: %s", lineno, name);
356 lbuf[len] = '\0';
357 return (lbuf);