xref: /netbsd-src/usr.sbin/mtree/compare.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
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.
20  *
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.
32  */
33 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)compare.c	5.7 (Berkeley) 5/25/90";*/
36 static char rcsid[] = "$Id: compare.c,v 1.3 1993/08/06 03:48:27 deraadt Exp $";
37 #endif /* not lint */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <fts.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <time.h>
45 #include "mtree.h"
46 
47 #define	LABEL \
48 	if (!label++) \
49 		(void)printf("%s: ", RP(p)); \
50 
51 compare(name, s, p)
52 	char *name;
53 	register NODE *s;
54 	register FTSENT *p;
55 {
56 	extern int exitval, uflag;
57 	int label;
58 	char *ftype(), *inotype(), *rlink();
59 
60 	label = 0;
61 	switch(s->type) {
62 	case F_BLOCK:
63 		if (!S_ISBLK(p->fts_statp->st_mode))
64 			goto typeerr;
65 		break;
66 	case F_CHAR:
67 		if (!S_ISCHR(p->fts_statp->st_mode))
68 			goto typeerr;
69 		break;
70 	case F_DIR:
71 		if (!S_ISDIR(p->fts_statp->st_mode))
72 			goto typeerr;
73 		break;
74 	case F_FIFO:
75 		if (!S_ISFIFO(p->fts_statp->st_mode))
76 			goto typeerr;
77 		break;
78 	case F_FILE:
79 		if (!S_ISREG(p->fts_statp->st_mode))
80 			goto typeerr;
81 		break;
82 	case F_LINK:
83 		if (!S_ISLNK(p->fts_statp->st_mode))
84 			goto typeerr;
85 		break;
86 	case F_SOCK:
87 		if (!S_ISFIFO(p->fts_statp->st_mode)) {
88 typeerr:		LABEL;
89 			(void)printf("\n\ttype (%s, %s)",
90 			    ftype(s->type), inotype(p->fts_statp->st_mode));
91 		}
92 		break;
93 	}
94 	if (s->flags & F_MODE && s->st_mode != (p->fts_statp->st_mode & MBITS)) {
95 		LABEL;
96 		(void)printf("\n\tpermissions (%#o, %#o%s",
97 		    s->st_mode, p->fts_statp->st_mode & MBITS, uflag ? "" : ")");
98 		if (uflag)
99 			if (chmod(p->fts_accpath, s->st_mode))
100 				(void)printf(", not modified: %s)",
101 				    strerror(errno));
102 			else
103 				(void)printf(", modified)");
104 	}
105 	if (s->flags & F_OWNER && s->st_uid != p->fts_statp->st_uid) {
106 		LABEL;
107 		(void)printf("\n\towner (%u, %u%s",
108 		    s->st_uid, p->fts_statp->st_uid, uflag ? "" : ")");
109 		if (uflag)
110 			if (chown(p->fts_accpath, s->st_uid, -1))
111 				(void)printf(", not modified: %s)",
112 				    strerror(errno));
113 			else
114 				(void)printf(", modified)");
115 	}
116 	if (s->flags & F_GROUP && s->st_gid != p->fts_statp->st_gid) {
117 		LABEL;
118 		(void)printf("\n\tgroup (%u, %u%s",
119 		    s->st_gid, p->fts_statp->st_gid, uflag ? "" : ")");
120 		if (uflag)
121 			if (chown(p->fts_accpath, -1, s->st_gid))
122 				(void)printf(", not modified: %s)",
123 				    strerror(errno));
124 			else
125 				(void)printf(", modified)");
126 	}
127 	if (s->flags & F_NLINK && s->type != F_DIR &&
128 	    s->st_nlink != p->fts_statp->st_nlink) {
129 		LABEL;
130 		(void)printf("\n\tlink count (%u, %u)",
131 		    s->st_nlink, p->fts_statp->st_nlink);
132 	}
133 	if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
134 		LABEL;
135 		(void)printf("\n\tsize (%ld, %ld)",
136 		    s->st_size, p->fts_statp->st_size);
137 	}
138 	if (s->flags & F_SLINK) {
139 		char *cp;
140 
141 		if (strcmp(cp = rlink(name), s->slink)) {
142 			LABEL;
143 			(void)printf("\n\tlink ref (%s, %s)", cp, s->slink);
144 		}
145 	}
146 	if (s->flags & F_TIME && s->st_mtime != p->fts_statp->st_mtime) {
147 		LABEL;
148 		(void)printf("\n\tmodification time (%.24s, ",
149 		    ctime(&s->st_mtime));
150 		(void)printf("%.24s)", ctime(&p->fts_statp->st_mtime));
151 	}
152 	if (label) {
153 		exitval = 2;
154 		putchar('\n');
155 	}
156 }
157 
158 char *
159 inotype(type)
160 	mode_t type;
161 {
162 	switch(type & S_IFMT) {
163 	case S_IFBLK:
164 		return("block");
165 	case S_IFCHR:
166 		return("char");
167 	case S_IFDIR:
168 		return("dir");
169 	case S_IFREG:
170 		return("file");
171 	case S_IFLNK:
172 		return("link");
173 	case S_IFSOCK:
174 		return("socket");
175 	default:
176 		return("unknown");
177 	}
178 	/* NOTREACHED */
179 }
180 
181 char *
182 ftype(type)
183 	u_int type;
184 {
185 	switch(type) {
186 	case F_BLOCK:
187 		return("block");
188 	case F_CHAR:
189 		return("char");
190 	case F_DIR:
191 		return("dir");
192 	case F_FIFO:
193 		return("fifo");
194 	case F_FILE:
195 		return("file");
196 	case F_LINK:
197 		return("link");
198 	case F_SOCK:
199 		return("socket");
200 	default:
201 		return("unknown");
202 	}
203 	/* NOTREACHED */
204 }
205 
206 char *
207 rlink(name)
208 	char *name;
209 {
210 	register int len;
211 	static char lbuf[MAXPATHLEN];
212 
213 	len = readlink(name, lbuf, sizeof(lbuf));
214 	if (len == -1) {
215 		(void)fprintf(stderr, "mtree: %s: %s.\n",
216 		    name, strerror(errno));
217 		exit(1);
218 	}
219 	lbuf[len] = '\0';
220 	return(lbuf);
221 }
222