xref: /netbsd-src/usr.sbin/mtree/compare.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: compare.c,v 1.8 1995/03/07 21:12:05 cgd Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: compare.c,v 1.8 1995/03/07 21:12:05 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <fts.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <time.h>
51 #include <unistd.h>
52 #include "mtree.h"
53 #include "extern.h"
54 
55 extern int uflag;
56 
57 static char *ftype __P((u_int));
58 
59 #define	INDENTNAMELEN	8
60 #define	LABEL \
61 	if (!label++) { \
62 		len = printf("%s: ", RP(p)); \
63 		if (len > INDENTNAMELEN) { \
64 			tab = "\t"; \
65 			(void)printf("\n"); \
66 		} else { \
67 			tab = ""; \
68 			(void)printf("%*s", INDENTNAMELEN - len, ""); \
69 		} \
70 	}
71 
72 int
73 compare(name, s, p)
74 	char *name;
75 	register NODE *s;
76 	register FTSENT *p;
77 {
78 	extern int uflag;
79 	u_long len, val;
80 	int fd, label;
81 	char *cp, *tab;
82 
83 	label = 0;
84 	switch(s->type) {
85 	case F_BLOCK:
86 		if (!S_ISBLK(p->fts_statp->st_mode))
87 			goto typeerr;
88 		break;
89 	case F_CHAR:
90 		if (!S_ISCHR(p->fts_statp->st_mode))
91 			goto typeerr;
92 		break;
93 	case F_DIR:
94 		if (!S_ISDIR(p->fts_statp->st_mode))
95 			goto typeerr;
96 		break;
97 	case F_FIFO:
98 		if (!S_ISFIFO(p->fts_statp->st_mode))
99 			goto typeerr;
100 		break;
101 	case F_FILE:
102 		if (!S_ISREG(p->fts_statp->st_mode))
103 			goto typeerr;
104 		break;
105 	case F_LINK:
106 		if (!S_ISLNK(p->fts_statp->st_mode))
107 			goto typeerr;
108 		break;
109 	case F_SOCK:
110 		if (!S_ISSOCK(p->fts_statp->st_mode)) {
111 typeerr:		LABEL;
112 			(void)printf("\ttype (%s, %s)\n",
113 			    ftype(s->type), inotype(p->fts_statp->st_mode));
114 		}
115 		break;
116 	}
117 	/* Set the uid/gid first, then set the mode. */
118 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
119 		LABEL;
120 		(void)printf("%suser (%u, %u",
121 		    tab, s->st_uid, p->fts_statp->st_uid);
122 		if (uflag)
123 			if (chown(p->fts_accpath, s->st_uid, -1))
124 				(void)printf(", not modified: %s)\n",
125 				    strerror(errno));
126 			else
127 				(void)printf(", modified)\n");
128 		else
129 			(void)printf(")\n");
130 		tab = "\t";
131 	}
132 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
133 		LABEL;
134 		(void)printf("%sgid (%u, %u",
135 		    tab, s->st_gid, p->fts_statp->st_gid);
136 		if (uflag)
137 			if (chown(p->fts_accpath, -1, s->st_gid))
138 				(void)printf(", not modified: %s)\n",
139 				    strerror(errno));
140 			else
141 				(void)printf(", modified)\n");
142 		else
143 			(void)printf(")\n");
144 		tab = "\t";
145 	}
146 	if (s->flags & F_MODE &&
147 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
148 		LABEL;
149 		(void)printf("%spermissions (%#o, %#o",
150 		    tab, s->st_mode, p->fts_statp->st_mode & MBITS);
151 		if (uflag)
152 			if (chmod(p->fts_accpath, s->st_mode))
153 				(void)printf(", not modified: %s)\n",
154 				    strerror(errno));
155 			else
156 				(void)printf(", modified)\n");
157 		else
158 			(void)printf(")\n");
159 		tab = "\t";
160 	}
161 	if (s->flags & F_NLINK && s->type != F_DIR &&
162 	    s->st_nlink != p->fts_statp->st_nlink) {
163 		LABEL;
164 		(void)printf("%slink count (%u, %u)\n",
165 		    tab, s->st_nlink, p->fts_statp->st_nlink);
166 		tab = "\t";
167 	}
168 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
169 		LABEL;
170 		(void)printf("%ssize (%qd, %qd)\n",
171 		    tab, s->st_size, p->fts_statp->st_size);
172 		tab = "\t";
173 	}
174 	/*
175 	 * XXX
176 	 * Catches nano-second differences, but doesn't display them.
177 	 */
178 	if (s->flags & F_TIME &&
179 	    s->st_mtimespec.ts_sec != p->fts_statp->st_mtimespec.ts_sec ||
180 	    s->st_mtimespec.ts_nsec != p->fts_statp->st_mtimespec.ts_nsec) {
181 		LABEL;
182 		(void)printf("%smodification time (%.24s, ",
183 		    tab, ctime(&s->st_mtimespec.ts_sec));
184 		(void)printf("%.24s)\n",
185 		    ctime(&p->fts_statp->st_mtimespec.ts_sec));
186 		tab = "\t";
187 	}
188 	if (s->flags & F_CKSUM)
189 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
190 			LABEL;
191 			(void)printf("%scksum: %s: %s\n",
192 			    tab, p->fts_accpath, strerror(errno));
193 			tab = "\t";
194 		} else if (crc(fd, &val, &len)) {
195 			(void)close(fd);
196 			LABEL;
197 			(void)printf("%scksum: %s: %s\n",
198 			    tab, p->fts_accpath, strerror(errno));
199 			tab = "\t";
200 		} else {
201 			(void)close(fd);
202 			if (s->cksum != val) {
203 				LABEL;
204 				(void)printf("%scksum (%lu, %lu)\n",
205 				    tab, s->cksum, val);
206 			}
207 			tab = "\t";
208 		}
209 	if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
210 		LABEL;
211 		(void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
212 	}
213 	return (label);
214 }
215 
216 char *
217 inotype(type)
218 	u_int type;
219 {
220 	switch(type & S_IFMT) {
221 	case S_IFBLK:
222 		return ("block");
223 	case S_IFCHR:
224 		return ("char");
225 	case S_IFDIR:
226 		return ("dir");
227 	case S_IFIFO:
228 		return ("fifo");
229 	case S_IFREG:
230 		return ("file");
231 	case S_IFLNK:
232 		return ("link");
233 	case S_IFSOCK:
234 		return ("socket");
235 	default:
236 		return ("unknown");
237 	}
238 	/* NOTREACHED */
239 }
240 
241 static char *
242 ftype(type)
243 	u_int type;
244 {
245 	switch(type) {
246 	case F_BLOCK:
247 		return ("block");
248 	case F_CHAR:
249 		return ("char");
250 	case F_DIR:
251 		return ("dir");
252 	case F_FIFO:
253 		return ("fifo");
254 	case F_FILE:
255 		return ("file");
256 	case F_LINK:
257 		return ("link");
258 	case F_SOCK:
259 		return ("socket");
260 	default:
261 		return ("unknown");
262 	}
263 	/* NOTREACHED */
264 }
265 
266 char *
267 rlink(name)
268 	char *name;
269 {
270 	static char lbuf[MAXPATHLEN];
271 	register int len;
272 
273 	if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
274 		err("%s: %s", name, strerror(errno));
275 	lbuf[len] = '\0';
276 	return (lbuf);
277 }
278