compare.c revision 1.6 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 /* from: static char sccsid[] = "@(#)compare.c 5.10 (Berkeley) 3/31/92"; */
36 static char *rcsid = "$Id: compare.c,v 1.6 1994/03/27 09:09:42 cgd Exp $";
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <fts.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include "mtree.h"
48 #include "extern.h"
49
50 extern int uflag;
51
52 static char *ftype __P((u_int));
53
54 #define INDENTNAMELEN 8
55 #define LABEL \
56 if (!label++) { \
57 len = printf("%s: ", RP(p)); \
58 if (len > INDENTNAMELEN) { \
59 tab = "\t"; \
60 (void)printf("\n"); \
61 } else { \
62 tab = ""; \
63 (void)printf("%*s", INDENTNAMELEN - len, ""); \
64 } \
65 }
66
67 int
68 compare(name, s, p)
69 char *name;
70 register NODE *s;
71 register FTSENT *p;
72 {
73 extern int uflag;
74 u_long len, val;
75 int fd, label;
76 char *cp, *tab;
77
78 label = 0;
79 switch(s->type) {
80 case F_BLOCK:
81 if (!S_ISBLK(p->fts_statp->st_mode))
82 goto typeerr;
83 break;
84 case F_CHAR:
85 if (!S_ISCHR(p->fts_statp->st_mode))
86 goto typeerr;
87 break;
88 case F_DIR:
89 if (!S_ISDIR(p->fts_statp->st_mode))
90 goto typeerr;
91 break;
92 case F_FIFO:
93 if (!S_ISFIFO(p->fts_statp->st_mode))
94 goto typeerr;
95 break;
96 case F_FILE:
97 if (!S_ISREG(p->fts_statp->st_mode))
98 goto typeerr;
99 break;
100 case F_LINK:
101 if (!S_ISLNK(p->fts_statp->st_mode))
102 goto typeerr;
103 break;
104 case F_SOCK:
105 if (!S_ISSOCK(p->fts_statp->st_mode)) {
106 typeerr: LABEL;
107 (void)printf("\ttype (%s, %s)\n",
108 ftype(s->type), inotype(p->fts_statp->st_mode));
109 }
110 break;
111 }
112 /* Set the uid/gid first, then set the mode. */
113 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
114 LABEL;
115 (void)printf("%suser (%u, %u",
116 tab, s->st_uid, p->fts_statp->st_uid);
117 if (uflag)
118 if (chown(p->fts_accpath, s->st_uid, -1))
119 (void)printf(", not modified: %s)\n",
120 strerror(errno));
121 else
122 (void)printf(", modified)\n");
123 else
124 (void)printf(")\n");
125 tab = "\t";
126 }
127 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
128 LABEL;
129 (void)printf("%sgid (%u, %u",
130 tab, s->st_gid, p->fts_statp->st_gid);
131 if (uflag)
132 if (chown(p->fts_accpath, -1, s->st_gid))
133 (void)printf(", not modified: %s)\n",
134 strerror(errno));
135 else
136 (void)printf(", modified)\n");
137 else
138 (void)printf(")\n");
139 tab = "\t";
140 }
141 if (s->flags & F_MODE &&
142 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
143 LABEL;
144 (void)printf("%spermissions (%#o, %#o",
145 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
146 if (uflag)
147 if (chmod(p->fts_accpath, s->st_mode))
148 (void)printf(", not modified: %s)\n",
149 strerror(errno));
150 else
151 (void)printf(", modified)\n");
152 else
153 (void)printf(")\n");
154 tab = "\t";
155 }
156 if (s->flags & F_NLINK && s->type != F_DIR &&
157 s->st_nlink != p->fts_statp->st_nlink) {
158 LABEL;
159 (void)printf("%slink count (%u, %u)\n",
160 tab, s->st_nlink, p->fts_statp->st_nlink);
161 tab = "\t";
162 }
163 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
164 LABEL;
165 (void)printf("%ssize (%qd, %qd)\n",
166 tab, s->st_size, p->fts_statp->st_size);
167 tab = "\t";
168 }
169 if (s->flags & F_TIME && s->st_mtime != p->fts_statp->st_mtime) {
170 LABEL;
171 (void)printf("%smodification time (%.24s, ",
172 tab, ctime(&s->st_mtime));
173 (void)printf("%.24s)\n", ctime(&p->fts_statp->st_mtime));
174 tab = "\t";
175 }
176 if (s->flags & F_CKSUM)
177 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
178 LABEL;
179 (void)printf("%scksum: %s: %s\n",
180 tab, p->fts_accpath, strerror(errno));
181 tab = "\t";
182 } else if (crc(fd, &val, &len)) {
183 (void)close(fd);
184 LABEL;
185 (void)printf("%scksum: %s: %s\n",
186 tab, p->fts_accpath, strerror(errno));
187 tab = "\t";
188 } else {
189 (void)close(fd);
190 if (s->cksum != val) {
191 LABEL;
192 (void)printf("%scksum (%lu, %lu)\n",
193 tab, s->cksum, val);
194 }
195 tab = "\t";
196 }
197 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
198 LABEL;
199 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
200 }
201 return (label);
202 }
203
204 char *
205 inotype(type)
206 u_int type;
207 {
208 switch(type & S_IFMT) {
209 case S_IFBLK:
210 return ("block");
211 case S_IFCHR:
212 return ("char");
213 case S_IFDIR:
214 return ("dir");
215 case S_IFIFO:
216 return ("fifo");
217 case S_IFREG:
218 return ("file");
219 case S_IFLNK:
220 return ("link");
221 case S_IFSOCK:
222 return ("socket");
223 default:
224 return ("unknown");
225 }
226 /* NOTREACHED */
227 }
228
229 static char *
230 ftype(type)
231 u_int type;
232 {
233 switch(type) {
234 case F_BLOCK:
235 return ("block");
236 case F_CHAR:
237 return ("char");
238 case F_DIR:
239 return ("dir");
240 case F_FIFO:
241 return ("fifo");
242 case F_FILE:
243 return ("file");
244 case F_LINK:
245 return ("link");
246 case F_SOCK:
247 return ("socket");
248 default:
249 return ("unknown");
250 }
251 /* NOTREACHED */
252 }
253
254 char *
255 rlink(name)
256 char *name;
257 {
258 static char lbuf[MAXPATHLEN];
259 register int len;
260
261 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
262 err("%s: %s", name, strerror(errno));
263 lbuf[len] = '\0';
264 return (lbuf);
265 }
266