compare.c revision 1.26 1 /* $NetBSD: compare.c,v 1.26 2001/09/22 03:56:29 perry 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.26 2001/09/22 03:56:29 perry Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fts.h>
52 #include <md5.h>
53 #include <stdio.h>
54 #include <time.h>
55 #include <unistd.h>
56
57 #include "mtree.h"
58 #include "extern.h"
59
60 extern int iflag, lflag, mflag, tflag, uflag;
61
62 static const char *ftype(u_int);
63
64 #define INDENTNAMELEN 8
65 #define MARK \
66 do { \
67 len = printf("%s: ", RP(p)); \
68 if (len > INDENTNAMELEN) { \
69 tab = "\t"; \
70 (void)printf("\n"); \
71 } else { \
72 tab = ""; \
73 (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \
74 } \
75 } while (0)
76 #define LABEL if (!label++) MARK
77
78 #define CHANGEFLAGS(path, oflags) \
79 if (flags != (oflags)) { \
80 if (!label) { \
81 MARK; \
82 (void)printf("%sflags (\"%s\"", tab, \
83 flags_to_string(p->fts_statp->st_flags, "none")); \
84 } \
85 if (chflags(path, flags)) { \
86 label++; \
87 (void)printf(", not modified: %s)\n", \
88 strerror(errno)); \
89 } else \
90 (void)printf(", modified to \"%s\")\n", \
91 flags_to_string(flags, "none")); \
92 }
93
94 /* SETFLAGS:
95 * given pflags, additionally set those flags specified in sflags and
96 * selected by mask (the other flags are left unchanged). oflags is
97 * passed as reference to check if chflags is necessary.
98 */
99 #define SETFLAGS(path, sflags, pflags, oflags, mask) \
100 do { \
101 flags = ((sflags) & (mask)) | (pflags); \
102 CHANGEFLAGS(path, oflags); \
103 } while (0)
104
105 /* CLEARFLAGS:
106 * given pflags, reset the flags specified in sflags and selected by mask
107 * (the other flags are left unchanged). oflags is
108 * passed as reference to check if chflags is necessary.
109 */
110 #define CLEARFLAGS(path, sflags, pflags, oflags, mask) \
111 do { \
112 flags = (~((sflags) & (mask)) & CH_MASK) & (pflags); \
113 CHANGEFLAGS(path, oflags); \
114 } while (0)
115
116 int
117 compare(const char *name, NODE *s, FTSENT *p)
118 {
119 u_int32_t len, val, flags;
120 int fd, label;
121 const char *cp;
122 char *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 if (lflag) {
207 mode_t tmode, mode;
208
209 tmode = s->st_mode;
210 mode = p->fts_statp->st_mode & MBITS;
211 /*
212 * if none of the suid/sgid/etc bits are set,
213 * then if the mode is a subset of the target,
214 * skip.
215 */
216 if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
217 (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
218 if ((mode | tmode) == tmode)
219 goto skip;
220 }
221
222 LABEL;
223 (void)printf("%spermissions (%#lo, %#lo",
224 tab, (u_long)s->st_mode,
225 (u_long)p->fts_statp->st_mode & MBITS);
226 if (uflag) {
227 if (chmod(p->fts_accpath, s->st_mode))
228 (void)printf(", not modified: %s)\n",
229 strerror(errno));
230 else
231 (void)printf(", modified)\n");
232 }
233 else
234 (void)printf(")\n");
235 tab = "\t";
236 skip:
237 }
238 if (s->flags & F_NLINK && s->type != F_DIR &&
239 s->st_nlink != p->fts_statp->st_nlink) {
240 LABEL;
241 (void)printf("%slink count (%lu, %lu)\n",
242 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
243 tab = "\t";
244 }
245 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
246 LABEL;
247 (void)printf("%ssize (%lld, %lld)\n",
248 tab, (long long)s->st_size,
249 (long long)p->fts_statp->st_size);
250 tab = "\t";
251 }
252 /*
253 * XXX
254 * Since utimes(2) only takes a timeval, there's no point in
255 * comparing the low bits of the timespec nanosecond field. This
256 * will only result in mismatches that we can never fix.
257 *
258 * Doesn't display microsecond differences.
259 */
260 if (s->flags & F_TIME) {
261 struct timeval tv[2];
262 struct stat *ps = p->fts_statp;
263 time_t smtime = s->st_mtimespec.tv_sec;
264
265 #ifdef BSD4_4
266 time_t pmtime = ps->st_mtimespec.tv_sec;
267
268 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
269 #else
270 time_t pmtime = (time_t)ps->st_mtime;
271
272 tv[1].tv_sec = ps->st_mtime;
273 tv[1].tv_usec = 0;
274 #endif
275 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
276
277 if (tv[0].tv_sec != tv[1].tv_sec ||
278 tv[0].tv_usec != tv[1].tv_usec) {
279 LABEL;
280 (void)printf("%smodification time (%.24s, ",
281 tab, ctime(&smtime));
282 (void)printf("%.24s", ctime(&pmtime));
283 if (tflag) {
284 tv[1] = tv[0];
285 if (utimes(p->fts_accpath, tv))
286 (void)printf(", not modified: %s)\n",
287 strerror(errno));
288 else
289 (void)printf(", modified)\n");
290 } else
291 (void)printf(")\n");
292 tab = "\t";
293 }
294 }
295 /*
296 * XXX
297 * since chflags(2) will reset file times, the utimes() above
298 * may have been useless! oh well, we'd rather have correct
299 * flags, rather than times?
300 */
301 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
302 || mflag || iflag)) {
303 if (s->st_flags != p->fts_statp->st_flags) {
304 LABEL;
305 (void)printf("%sflags (\"%s\" is not ", tab,
306 flags_to_string(s->st_flags, "none"));
307 (void)printf("\"%s\"",
308 flags_to_string(p->fts_statp->st_flags, "none"));
309 }
310 if (uflag) {
311 if (iflag)
312 SETFLAGS(p->fts_accpath, s->st_flags,
313 0, p->fts_statp->st_flags, CH_MASK);
314 else if (mflag)
315 CLEARFLAGS(p->fts_accpath, s->st_flags,
316 0, p->fts_statp->st_flags, SP_FLGS);
317 else
318 SETFLAGS(p->fts_accpath, s->st_flags,
319 0, p->fts_statp->st_flags,
320 (~SP_FLGS & CH_MASK));
321 } else
322 (void)printf(")\n");
323 tab = "\t";
324 }
325 if (s->flags & F_CKSUM) {
326 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
327 LABEL;
328 (void)printf("%scksum: %s: %s\n",
329 tab, p->fts_accpath, strerror(errno));
330 tab = "\t";
331 } else if (crc(fd, &val, &len)) {
332 (void)close(fd);
333 LABEL;
334 (void)printf("%scksum: %s: %s\n",
335 tab, p->fts_accpath, strerror(errno));
336 tab = "\t";
337 } else {
338 (void)close(fd);
339 if (s->cksum != val) {
340 LABEL;
341 (void)printf("%scksum (%lu, %lu)\n",
342 tab, s->cksum, (unsigned long)val);
343 }
344 tab = "\t";
345 }
346 }
347 if (s->flags & F_MD5) {
348 if (MD5File(p->fts_accpath, md5buf) == NULL) {
349 LABEL;
350 (void)printf("%smd5: %s: %s\n",
351 tab, p->fts_accpath, strerror(errno));
352 tab = "\t";
353 } else {
354 if (strcmp(s->md5sum, md5buf)) {
355 LABEL;
356 (void)printf("%smd5 (0x%s, 0x%s)\n",
357 tab, s->md5sum, md5buf);
358 }
359 tab = "\t";
360 }
361 }
362
363 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
364 LABEL;
365 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
366 }
367 return (label);
368 }
369
370 const char *
371 inotype(u_int type)
372 {
373
374 return (ftype(type & S_IFMT));
375 }
376
377 static const char *
378 ftype(u_int type)
379 {
380
381 switch(type) {
382 case F_BLOCK:
383 case S_IFBLK:
384 return ("block");
385 case F_CHAR:
386 case S_IFCHR:
387 return ("char");
388 case F_DIR:
389 case S_IFDIR:
390 return ("dir");
391 case F_FIFO:
392 case S_IFIFO:
393 return ("fifo");
394 case F_FILE:
395 case S_IFREG:
396 return ("file");
397 case F_LINK:
398 case S_IFLNK:
399 return ("link");
400 case F_SOCK:
401 case S_IFSOCK:
402 return ("socket");
403 default:
404 return ("unknown");
405 }
406 /* NOTREACHED */
407 }
408
409 const char *
410 rlink(const char *name)
411 {
412 static char lbuf[MAXPATHLEN];
413 int len;
414
415 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
416 mtree_err("%s: %s", name, strerror(errno));
417 lbuf[len] = '\0';
418 return (lbuf);
419 }
420