xref: /netbsd-src/usr.sbin/mtree/compare.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: compare.c,v 1.20 1999/02/11 15:32:23 mrg 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)compare.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: compare.c,v 1.20 1999/02/11 15:32:23 mrg Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <fcntl.h>
49 #include <fts.h>
50 #include <errno.h>
51 #include <md5.h>
52 #include <stdio.h>
53 #include <time.h>
54 #include <unistd.h>
55 #include "mtree.h"
56 #include "extern.h"
57 
58 extern int iflag, mflag, tflag, uflag;
59 
60 static char *ftype __P((u_int));
61 
62 #define	INDENTNAMELEN	8
63 #define MARK                                                                  \
64 do {                                                                          \
65 	len = printf("%s: ", RP(p));                                          \
66 	if (len > INDENTNAMELEN) {                                            \
67 		tab = "\t";                                                   \
68 		(void)printf("\n");                                           \
69 	} else {                                                              \
70 		tab = "";                                                     \
71 		(void)printf("%*s", INDENTNAMELEN - (int)len, "");            \
72 	}                                                                     \
73 } while (0)
74 #define	LABEL if (!label++) MARK
75 
76 #define CHANGEFLAGS(path, oflags) \
77 	if (flags != (oflags)) {                                              \
78 		if (!label) {                                                 \
79 			MARK;                                                 \
80 			(void)printf("%sflags (\"%s\"", tab,                  \
81 			    flags_to_string(p->fts_statp->st_flags, "none")); \
82 		}                                                             \
83 		if (chflags(path, flags)) {                                   \
84 			label++;                                              \
85 			(void)printf(", not modified: %s)\n",                 \
86 			    strerror(errno));                                 \
87 		} else                                                        \
88 			(void)printf(", modified to \"%s\")\n",               \
89 			     flags_to_string(flags, "none"));                 \
90 	}
91 
92 /* SETFLAGS:
93  * given pflags, additionally set those flags specified in sflags and
94  * selected by mask (the other flags are left unchanged). oflags is
95  * passed as reference to check if chflags is necessary.
96  */
97 #define SETFLAGS(path, sflags, pflags, oflags, mask)                          \
98 do {                                                                          \
99 	flags = ((sflags) & (mask)) | (pflags);                               \
100         CHANGEFLAGS(path, oflags);                                            \
101 } while (0)
102 
103 /* CLEARFLAGS:
104  * given pflags, reset the flags specified in sflags and selected by mask
105  * (the other flags are left unchanged). oflags is
106  * passed as reference to check if chflags is necessary.
107  */
108 #define CLEARFLAGS(path, sflags, pflags, oflags, mask)                        \
109 do {                                                                          \
110 	flags = (~((sflags) & (mask)) & CH_MASK) & (pflags);                  \
111         CHANGEFLAGS(path, oflags);                                            \
112 } while (0)
113 
114 int
115 compare(name, s, p)
116 	char *name;
117 	NODE *s;
118 	FTSENT *p;
119 {
120 	u_int32_t len, val, flags;
121 	int fd, label;
122 	char *cp, *tab;
123 	char md5buf[35];
124 
125 	tab = NULL;
126 	label = 0;
127 	switch(s->type) {
128 	case F_BLOCK:
129 		if (!S_ISBLK(p->fts_statp->st_mode))
130 			goto typeerr;
131 		break;
132 	case F_CHAR:
133 		if (!S_ISCHR(p->fts_statp->st_mode))
134 			goto typeerr;
135 		break;
136 	case F_DIR:
137 		if (!S_ISDIR(p->fts_statp->st_mode))
138 			goto typeerr;
139 		break;
140 	case F_FIFO:
141 		if (!S_ISFIFO(p->fts_statp->st_mode))
142 			goto typeerr;
143 		break;
144 	case F_FILE:
145 		if (!S_ISREG(p->fts_statp->st_mode))
146 			goto typeerr;
147 		break;
148 	case F_LINK:
149 		if (!S_ISLNK(p->fts_statp->st_mode))
150 			goto typeerr;
151 		break;
152 	case F_SOCK:
153 		if (!S_ISSOCK(p->fts_statp->st_mode)) {
154 typeerr:		LABEL;
155 			(void)printf("\ttype (%s, %s)\n",
156 			    ftype(s->type), inotype(p->fts_statp->st_mode));
157 		}
158 		break;
159 	}
160 	if (iflag && !uflag) {
161 		if (s->flags & F_FLAGS)
162 		    SETFLAGS(p->fts_accpath, s->st_flags,
163 			p->fts_statp->st_flags, p->fts_statp->st_flags,
164 			SP_FLGS);
165 		return (label);
166         }
167 	if (mflag && !uflag) {
168 		if (s->flags & F_FLAGS)
169 		    CLEARFLAGS(p->fts_accpath, s->st_flags,
170 			p->fts_statp->st_flags, p->fts_statp->st_flags,
171 			SP_FLGS);
172 		return (label);
173         }
174 	/* Set the uid/gid first, then set the mode. */
175 	if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
176 		LABEL;
177 		(void)printf("%suser (%lu, %lu",
178 		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
179 		if (uflag) {
180 			if (chown(p->fts_accpath, s->st_uid, -1))
181 				(void)printf(", not modified: %s)\n",
182 				    strerror(errno));
183 			else
184 				(void)printf(", modified)\n");
185 		} else
186 			(void)printf(")\n");
187 		tab = "\t";
188 	}
189 	if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
190 		LABEL;
191 		(void)printf("%sgid (%lu, %lu",
192 		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
193 		if (uflag) {
194 			if (chown(p->fts_accpath, -1, s->st_gid))
195 				(void)printf(", not modified: %s)\n",
196 				    strerror(errno));
197 			else
198 				(void)printf(", modified)\n");
199 		}
200 		else
201 			(void)printf(")\n");
202 		tab = "\t";
203 	}
204 	if (s->flags & F_MODE &&
205 	    s->st_mode != (p->fts_statp->st_mode & MBITS)) {
206 		LABEL;
207 		(void)printf("%spermissions (%#lo, %#lo",
208 		    tab, (u_long)s->st_mode,
209 		    (u_long)p->fts_statp->st_mode & MBITS);
210 		if (uflag) {
211 			if (chmod(p->fts_accpath, s->st_mode))
212 				(void)printf(", not modified: %s)\n",
213 				    strerror(errno));
214 			else
215 				(void)printf(", modified)\n");
216 		}
217 		else
218 			(void)printf(")\n");
219 		tab = "\t";
220 	}
221 	if (s->flags & F_NLINK && s->type != F_DIR &&
222 	    s->st_nlink != p->fts_statp->st_nlink) {
223 		LABEL;
224 		(void)printf("%slink count (%lu, %lu)\n",
225 		    tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
226 		tab = "\t";
227 	}
228 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
229 		LABEL;
230 		(void)printf("%ssize (%qd, %qd)\n",
231 		    tab, (long long)s->st_size,
232 		    (long long)p->fts_statp->st_size);
233 		tab = "\t";
234 	}
235 	/*
236 	 * XXX
237 	 * Since utimes(2) only takes a timeval, there's no point in
238 	 * comparing the low bits of the timespec nanosecond field.  This
239 	 * will only result in mismatches that we can never fix.
240 	 *
241 	 * Doesn't display microsecond differences.
242 	 */
243 	if (s->flags & F_TIME) {
244 		struct timeval tv[2];
245 
246 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
247 		TIMESPEC_TO_TIMEVAL(&tv[1], &p->fts_statp->st_mtimespec);
248 		if (tv[0].tv_sec != tv[1].tv_sec ||
249 		    tv[0].tv_usec != tv[1].tv_usec) {
250 			LABEL;
251 			(void)printf("%smodification time (%.24s, ",
252 			    tab, ctime(&s->st_mtimespec.tv_sec));
253 			(void)printf("%.24s",
254 			    ctime(&p->fts_statp->st_mtimespec.tv_sec));
255 			if (tflag) {
256 				tv[1] = tv[0];
257 				if (utimes(p->fts_accpath, tv))
258 					(void)printf(", not modified: %s)\n",
259 					    strerror(errno));
260 				else
261 					(void)printf(", modified)\n");
262 			} else
263 				(void)printf(")\n");
264 			tab = "\t";
265 		}
266 	}
267 	/*
268 	 * XXX
269 	 * since chflags(2) will reset file times, the utimes() above
270 	 * may have been useless!  oh well, we'd rather have correct
271 	 * flags, rather than times?
272 	 */
273         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
274 	    || mflag || iflag)) {
275 		if (s->st_flags != p->fts_statp->st_flags) {
276 			LABEL;
277 			(void)printf("%sflags (\"%s\" is not ", tab,
278 			    flags_to_string(s->st_flags, "none"));
279 			(void)printf("\"%s\"",
280 			    flags_to_string(p->fts_statp->st_flags, "none"));
281 		}
282 		if (uflag) {
283 			if (iflag)
284 				SETFLAGS(p->fts_accpath, s->st_flags,
285 				    0, p->fts_statp->st_flags, CH_MASK);
286 			else if (mflag)
287 				CLEARFLAGS(p->fts_accpath, s->st_flags,
288 				    0, p->fts_statp->st_flags, SP_FLGS);
289 			else
290 				SETFLAGS(p->fts_accpath, s->st_flags,
291 			     	    0, p->fts_statp->st_flags,
292 				    (~SP_FLGS & CH_MASK));
293 		} else
294 			(void)printf(")\n");
295 		tab = "\t";
296 	}
297 	if (s->flags & F_CKSUM) {
298 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
299 			LABEL;
300 			(void)printf("%scksum: %s: %s\n",
301 			    tab, p->fts_accpath, strerror(errno));
302 			tab = "\t";
303 		} else if (crc(fd, &val, &len)) {
304 			(void)close(fd);
305 			LABEL;
306 			(void)printf("%scksum: %s: %s\n",
307 			    tab, p->fts_accpath, strerror(errno));
308 			tab = "\t";
309 		} else {
310 			(void)close(fd);
311 			if (s->cksum != val) {
312 				LABEL;
313 				(void)printf("%scksum (%lu, %lu)\n",
314 				    tab, s->cksum, (unsigned long)val);
315 			}
316 			tab = "\t";
317 		}
318 	}
319 	if (s->flags & F_MD5) {
320 		if (MD5File(p->fts_accpath, md5buf) == NULL) {
321 			LABEL;
322 			(void)printf("%smd5: %s: %s\n",
323 			    tab, p->fts_accpath, strerror(errno));
324 			tab = "\t";
325 		} else {
326 			if (strcmp(s->md5sum, md5buf)) {
327 				LABEL;
328 				(void)printf("%smd5 (0x%s, 0x%s)\n",
329 				    tab, s->md5sum, md5buf);
330 			}
331 			tab = "\t";
332 		}
333 	}
334 
335 	if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
336 		LABEL;
337 		(void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
338 	}
339 	return (label);
340 }
341 
342 char *
343 inotype(type)
344 	u_int type;
345 {
346 	switch(type & S_IFMT) {
347 	case S_IFBLK:
348 		return ("block");
349 	case S_IFCHR:
350 		return ("char");
351 	case S_IFDIR:
352 		return ("dir");
353 	case S_IFIFO:
354 		return ("fifo");
355 	case S_IFREG:
356 		return ("file");
357 	case S_IFLNK:
358 		return ("link");
359 	case S_IFSOCK:
360 		return ("socket");
361 	default:
362 		return ("unknown");
363 	}
364 	/* NOTREACHED */
365 }
366 
367 static char *
368 ftype(type)
369 	u_int type;
370 {
371 	switch(type) {
372 	case F_BLOCK:
373 		return ("block");
374 	case F_CHAR:
375 		return ("char");
376 	case F_DIR:
377 		return ("dir");
378 	case F_FIFO:
379 		return ("fifo");
380 	case F_FILE:
381 		return ("file");
382 	case F_LINK:
383 		return ("link");
384 	case F_SOCK:
385 		return ("socket");
386 	default:
387 		return ("unknown");
388 	}
389 	/* NOTREACHED */
390 }
391 
392 char *
393 rlink(name)
394 	char *name;
395 {
396 	static char lbuf[MAXPATHLEN];
397 	int len;
398 
399 	if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
400 		mtree_err("%s: %s", name, strerror(errno));
401 	lbuf[len] = '\0';
402 	return (lbuf);
403 }
404