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