xref: /netbsd-src/usr.sbin/mtree/compare.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: compare.c,v 1.22 1999/07/10 19:59:28 christos 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.22 1999/07/10 19:59:28 christos 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 		struct stat *ps = p->fts_statp;
246 		time_t smtime = s->st_mtimespec.tv_sec;
247 
248 #ifdef BSD4_4
249 		time_t pmtime = ps->st_mtimespec.tv_sec;
250 
251 		TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
252 #else
253 		time_t pmtime = (time_t)ps->st_mtime;
254 
255 		tv[1].tv_sec = ps->st_mtime;
256 		tv[1].tv_usec = 0;
257 #endif
258 		TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
259 
260 		if (tv[0].tv_sec != tv[1].tv_sec ||
261 		    tv[0].tv_usec != tv[1].tv_usec) {
262 			LABEL;
263 			(void)printf("%smodification time (%.24s, ",
264 			    tab, ctime(&smtime));
265 			(void)printf("%.24s", ctime(&pmtime));
266 			if (tflag) {
267 				tv[1] = tv[0];
268 				if (utimes(p->fts_accpath, tv))
269 					(void)printf(", not modified: %s)\n",
270 					    strerror(errno));
271 				else
272 					(void)printf(", modified)\n");
273 			} else
274 				(void)printf(")\n");
275 			tab = "\t";
276 		}
277 	}
278 	/*
279 	 * XXX
280 	 * since chflags(2) will reset file times, the utimes() above
281 	 * may have been useless!  oh well, we'd rather have correct
282 	 * flags, rather than times?
283 	 */
284         if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
285 	    || mflag || iflag)) {
286 		if (s->st_flags != p->fts_statp->st_flags) {
287 			LABEL;
288 			(void)printf("%sflags (\"%s\" is not ", tab,
289 			    flags_to_string(s->st_flags, "none"));
290 			(void)printf("\"%s\"",
291 			    flags_to_string(p->fts_statp->st_flags, "none"));
292 		}
293 		if (uflag) {
294 			if (iflag)
295 				SETFLAGS(p->fts_accpath, s->st_flags,
296 				    0, p->fts_statp->st_flags, CH_MASK);
297 			else if (mflag)
298 				CLEARFLAGS(p->fts_accpath, s->st_flags,
299 				    0, p->fts_statp->st_flags, SP_FLGS);
300 			else
301 				SETFLAGS(p->fts_accpath, s->st_flags,
302 			     	    0, p->fts_statp->st_flags,
303 				    (~SP_FLGS & CH_MASK));
304 		} else
305 			(void)printf(")\n");
306 		tab = "\t";
307 	}
308 	if (s->flags & F_CKSUM) {
309 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
310 			LABEL;
311 			(void)printf("%scksum: %s: %s\n",
312 			    tab, p->fts_accpath, strerror(errno));
313 			tab = "\t";
314 		} else if (crc(fd, &val, &len)) {
315 			(void)close(fd);
316 			LABEL;
317 			(void)printf("%scksum: %s: %s\n",
318 			    tab, p->fts_accpath, strerror(errno));
319 			tab = "\t";
320 		} else {
321 			(void)close(fd);
322 			if (s->cksum != val) {
323 				LABEL;
324 				(void)printf("%scksum (%lu, %lu)\n",
325 				    tab, s->cksum, (unsigned long)val);
326 			}
327 			tab = "\t";
328 		}
329 	}
330 	if (s->flags & F_MD5) {
331 		if (MD5File(p->fts_accpath, md5buf) == NULL) {
332 			LABEL;
333 			(void)printf("%smd5: %s: %s\n",
334 			    tab, p->fts_accpath, strerror(errno));
335 			tab = "\t";
336 		} else {
337 			if (strcmp(s->md5sum, md5buf)) {
338 				LABEL;
339 				(void)printf("%smd5 (0x%s, 0x%s)\n",
340 				    tab, s->md5sum, md5buf);
341 			}
342 			tab = "\t";
343 		}
344 	}
345 
346 	if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
347 		LABEL;
348 		(void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
349 	}
350 	return (label);
351 }
352 
353 char *
354 inotype(type)
355 	u_int type;
356 {
357 	switch(type & S_IFMT) {
358 	case S_IFBLK:
359 		return ("block");
360 	case S_IFCHR:
361 		return ("char");
362 	case S_IFDIR:
363 		return ("dir");
364 	case S_IFIFO:
365 		return ("fifo");
366 	case S_IFREG:
367 		return ("file");
368 	case S_IFLNK:
369 		return ("link");
370 	case S_IFSOCK:
371 		return ("socket");
372 	default:
373 		return ("unknown");
374 	}
375 	/* NOTREACHED */
376 }
377 
378 static char *
379 ftype(type)
380 	u_int type;
381 {
382 	switch(type) {
383 	case F_BLOCK:
384 		return ("block");
385 	case F_CHAR:
386 		return ("char");
387 	case F_DIR:
388 		return ("dir");
389 	case F_FIFO:
390 		return ("fifo");
391 	case F_FILE:
392 		return ("file");
393 	case F_LINK:
394 		return ("link");
395 	case F_SOCK:
396 		return ("socket");
397 	default:
398 		return ("unknown");
399 	}
400 	/* NOTREACHED */
401 }
402 
403 char *
404 rlink(name)
405 	char *name;
406 {
407 	static char lbuf[MAXPATHLEN];
408 	int len;
409 
410 	if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
411 		mtree_err("%s: %s", name, strerror(errno));
412 	lbuf[len] = '\0';
413 	return (lbuf);
414 }
415