compare.c revision 1.24 1 /* $NetBSD: compare.c,v 1.24 2001/03/09 03:09:45 simonb 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.24 2001/03/09 03:09:45 simonb 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, mflag, tflag, uflag;
61
62 static 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(char *name, NODE *s, FTSENT *p)
118 {
119 u_int32_t len, val, flags;
120 int fd, label;
121 char *cp, *tab;
122 char md5buf[35];
123
124 tab = NULL;
125 label = 0;
126 switch(s->type) {
127 case F_BLOCK:
128 if (!S_ISBLK(p->fts_statp->st_mode))
129 goto typeerr;
130 break;
131 case F_CHAR:
132 if (!S_ISCHR(p->fts_statp->st_mode))
133 goto typeerr;
134 break;
135 case F_DIR:
136 if (!S_ISDIR(p->fts_statp->st_mode))
137 goto typeerr;
138 break;
139 case F_FIFO:
140 if (!S_ISFIFO(p->fts_statp->st_mode))
141 goto typeerr;
142 break;
143 case F_FILE:
144 if (!S_ISREG(p->fts_statp->st_mode))
145 goto typeerr;
146 break;
147 case F_LINK:
148 if (!S_ISLNK(p->fts_statp->st_mode))
149 goto typeerr;
150 break;
151 case F_SOCK:
152 if (!S_ISSOCK(p->fts_statp->st_mode)) {
153 typeerr: LABEL;
154 (void)printf("\ttype (%s, %s)\n",
155 ftype(s->type), inotype(p->fts_statp->st_mode));
156 }
157 break;
158 }
159 if (iflag && !uflag) {
160 if (s->flags & F_FLAGS)
161 SETFLAGS(p->fts_accpath, s->st_flags,
162 p->fts_statp->st_flags, p->fts_statp->st_flags,
163 SP_FLGS);
164 return (label);
165 }
166 if (mflag && !uflag) {
167 if (s->flags & F_FLAGS)
168 CLEARFLAGS(p->fts_accpath, s->st_flags,
169 p->fts_statp->st_flags, p->fts_statp->st_flags,
170 SP_FLGS);
171 return (label);
172 }
173 /* Set the uid/gid first, then set the mode. */
174 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
175 LABEL;
176 (void)printf("%suser (%lu, %lu",
177 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
178 if (uflag) {
179 if (chown(p->fts_accpath, s->st_uid, -1))
180 (void)printf(", not modified: %s)\n",
181 strerror(errno));
182 else
183 (void)printf(", modified)\n");
184 } else
185 (void)printf(")\n");
186 tab = "\t";
187 }
188 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
189 LABEL;
190 (void)printf("%sgid (%lu, %lu",
191 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
192 if (uflag) {
193 if (chown(p->fts_accpath, -1, s->st_gid))
194 (void)printf(", not modified: %s)\n",
195 strerror(errno));
196 else
197 (void)printf(", modified)\n");
198 }
199 else
200 (void)printf(")\n");
201 tab = "\t";
202 }
203 if (s->flags & F_MODE &&
204 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
205 LABEL;
206 (void)printf("%spermissions (%#lo, %#lo",
207 tab, (u_long)s->st_mode,
208 (u_long)p->fts_statp->st_mode & MBITS);
209 if (uflag) {
210 if (chmod(p->fts_accpath, s->st_mode))
211 (void)printf(", not modified: %s)\n",
212 strerror(errno));
213 else
214 (void)printf(", modified)\n");
215 }
216 else
217 (void)printf(")\n");
218 tab = "\t";
219 }
220 if (s->flags & F_NLINK && s->type != F_DIR &&
221 s->st_nlink != p->fts_statp->st_nlink) {
222 LABEL;
223 (void)printf("%slink count (%lu, %lu)\n",
224 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
225 tab = "\t";
226 }
227 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
228 LABEL;
229 (void)printf("%ssize (%lld, %lld)\n",
230 tab, (long long)s->st_size,
231 (long long)p->fts_statp->st_size);
232 tab = "\t";
233 }
234 /*
235 * XXX
236 * Since utimes(2) only takes a timeval, there's no point in
237 * comparing the low bits of the timespec nanosecond field. This
238 * will only result in mismatches that we can never fix.
239 *
240 * Doesn't display microsecond differences.
241 */
242 if (s->flags & F_TIME) {
243 struct timeval tv[2];
244 struct stat *ps = p->fts_statp;
245 time_t smtime = s->st_mtimespec.tv_sec;
246
247 #ifdef BSD4_4
248 time_t pmtime = ps->st_mtimespec.tv_sec;
249
250 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
251 #else
252 time_t pmtime = (time_t)ps->st_mtime;
253
254 tv[1].tv_sec = ps->st_mtime;
255 tv[1].tv_usec = 0;
256 #endif
257 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
258
259 if (tv[0].tv_sec != tv[1].tv_sec ||
260 tv[0].tv_usec != tv[1].tv_usec) {
261 LABEL;
262 (void)printf("%smodification time (%.24s, ",
263 tab, ctime(&smtime));
264 (void)printf("%.24s", ctime(&pmtime));
265 if (tflag) {
266 tv[1] = tv[0];
267 if (utimes(p->fts_accpath, tv))
268 (void)printf(", not modified: %s)\n",
269 strerror(errno));
270 else
271 (void)printf(", modified)\n");
272 } else
273 (void)printf(")\n");
274 tab = "\t";
275 }
276 }
277 /*
278 * XXX
279 * since chflags(2) will reset file times, the utimes() above
280 * may have been useless! oh well, we'd rather have correct
281 * flags, rather than times?
282 */
283 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
284 || mflag || iflag)) {
285 if (s->st_flags != p->fts_statp->st_flags) {
286 LABEL;
287 (void)printf("%sflags (\"%s\" is not ", tab,
288 flags_to_string(s->st_flags, "none"));
289 (void)printf("\"%s\"",
290 flags_to_string(p->fts_statp->st_flags, "none"));
291 }
292 if (uflag) {
293 if (iflag)
294 SETFLAGS(p->fts_accpath, s->st_flags,
295 0, p->fts_statp->st_flags, CH_MASK);
296 else if (mflag)
297 CLEARFLAGS(p->fts_accpath, s->st_flags,
298 0, p->fts_statp->st_flags, SP_FLGS);
299 else
300 SETFLAGS(p->fts_accpath, s->st_flags,
301 0, p->fts_statp->st_flags,
302 (~SP_FLGS & CH_MASK));
303 } else
304 (void)printf(")\n");
305 tab = "\t";
306 }
307 if (s->flags & F_CKSUM) {
308 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
309 LABEL;
310 (void)printf("%scksum: %s: %s\n",
311 tab, p->fts_accpath, strerror(errno));
312 tab = "\t";
313 } else if (crc(fd, &val, &len)) {
314 (void)close(fd);
315 LABEL;
316 (void)printf("%scksum: %s: %s\n",
317 tab, p->fts_accpath, strerror(errno));
318 tab = "\t";
319 } else {
320 (void)close(fd);
321 if (s->cksum != val) {
322 LABEL;
323 (void)printf("%scksum (%lu, %lu)\n",
324 tab, s->cksum, (unsigned long)val);
325 }
326 tab = "\t";
327 }
328 }
329 if (s->flags & F_MD5) {
330 if (MD5File(p->fts_accpath, md5buf) == NULL) {
331 LABEL;
332 (void)printf("%smd5: %s: %s\n",
333 tab, p->fts_accpath, strerror(errno));
334 tab = "\t";
335 } else {
336 if (strcmp(s->md5sum, md5buf)) {
337 LABEL;
338 (void)printf("%smd5 (0x%s, 0x%s)\n",
339 tab, s->md5sum, md5buf);
340 }
341 tab = "\t";
342 }
343 }
344
345 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
346 LABEL;
347 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
348 }
349 return (label);
350 }
351
352 char *
353 inotype(u_int type)
354 {
355
356 switch(type & S_IFMT) {
357 case S_IFBLK:
358 return ("block");
359 case S_IFCHR:
360 return ("char");
361 case S_IFDIR:
362 return ("dir");
363 case S_IFIFO:
364 return ("fifo");
365 case S_IFREG:
366 return ("file");
367 case S_IFLNK:
368 return ("link");
369 case S_IFSOCK:
370 return ("socket");
371 default:
372 return ("unknown");
373 }
374 /* NOTREACHED */
375 }
376
377 static char *
378 ftype(u_int type)
379 {
380
381 switch(type) {
382 case F_BLOCK:
383 return ("block");
384 case F_CHAR:
385 return ("char");
386 case F_DIR:
387 return ("dir");
388 case F_FIFO:
389 return ("fifo");
390 case F_FILE:
391 return ("file");
392 case F_LINK:
393 return ("link");
394 case F_SOCK:
395 return ("socket");
396 default:
397 return ("unknown");
398 }
399 /* NOTREACHED */
400 }
401
402 char *
403 rlink(char *name)
404 {
405 static char lbuf[MAXPATHLEN];
406 int len;
407
408 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
409 mtree_err("%s: %s", name, strerror(errno));
410 lbuf[len] = '\0';
411 return (lbuf);
412 }
413