compare.c revision 1.32 1 /* $NetBSD: compare.c,v 1.32 2001/10/22 07:07:46 lukem 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.32 2001/10/22 07:07:46 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fts.h>
50 #include <md5.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55
56 #include "mtree.h"
57 #include "extern.h"
58
59 #define INDENTNAMELEN 8
60 #define MARK \
61 do { \
62 len = printf("%s: ", RP(p)); \
63 if (len > INDENTNAMELEN) { \
64 tab = "\t"; \
65 (void)printf("\n"); \
66 } else { \
67 tab = ""; \
68 (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \
69 } \
70 } while (0)
71 #define LABEL if (!label++) MARK
72
73 #define CHANGEFLAGS(path, oflags) \
74 if (flags != (oflags)) { \
75 if (!label) { \
76 MARK; \
77 (void)printf("%sflags (\"%s\"", tab, \
78 flags_to_string(p->fts_statp->st_flags, "none")); \
79 } \
80 if (chflags(path, flags)) { \
81 label++; \
82 (void)printf(", not modified: %s)\n", \
83 strerror(errno)); \
84 } else \
85 (void)printf(", modified to \"%s\")\n", \
86 flags_to_string(flags, "none")); \
87 }
88
89 /* SETFLAGS:
90 * given pflags, additionally set those flags specified in sflags and
91 * selected by mask (the other flags are left unchanged). oflags is
92 * passed as reference to check if chflags is necessary.
93 */
94 #define SETFLAGS(path, sflags, pflags, oflags, mask) \
95 do { \
96 flags = ((sflags) & (mask)) | (pflags); \
97 CHANGEFLAGS(path, oflags); \
98 } while (0)
99
100 /* CLEARFLAGS:
101 * given pflags, reset the flags specified in sflags and selected by mask
102 * (the other flags are left unchanged). oflags is
103 * passed as reference to check if chflags is necessary.
104 */
105 #define CLEARFLAGS(path, sflags, pflags, oflags, mask) \
106 do { \
107 flags = (~((sflags) & (mask)) & CH_MASK) & (pflags); \
108 CHANGEFLAGS(path, oflags); \
109 } while (0)
110
111 int
112 compare(const char *name, NODE *s, FTSENT *p)
113 {
114 u_int32_t len, val, flags;
115 int fd, label;
116 const char *cp, *tab;
117 char md5buf[35];
118
119 tab = NULL;
120 label = 0;
121 switch(s->type) {
122 case F_BLOCK:
123 if (!S_ISBLK(p->fts_statp->st_mode))
124 goto typeerr;
125 break;
126 case F_CHAR:
127 if (!S_ISCHR(p->fts_statp->st_mode))
128 goto typeerr;
129 break;
130 case F_DIR:
131 if (!S_ISDIR(p->fts_statp->st_mode))
132 goto typeerr;
133 break;
134 case F_FIFO:
135 if (!S_ISFIFO(p->fts_statp->st_mode))
136 goto typeerr;
137 break;
138 case F_FILE:
139 if (!S_ISREG(p->fts_statp->st_mode))
140 goto typeerr;
141 break;
142 case F_LINK:
143 if (!S_ISLNK(p->fts_statp->st_mode))
144 goto typeerr;
145 break;
146 case F_SOCK:
147 if (!S_ISSOCK(p->fts_statp->st_mode)) {
148 typeerr: LABEL;
149 (void)printf("\ttype (%s, %s)\n",
150 nodetype(s->type), inotype(p->fts_statp->st_mode));
151 }
152 break;
153 }
154 if (iflag && !uflag) {
155 if (s->flags & F_FLAGS)
156 SETFLAGS(p->fts_accpath, s->st_flags,
157 p->fts_statp->st_flags, p->fts_statp->st_flags,
158 SP_FLGS);
159 return (label);
160 }
161 if (mflag && !uflag) {
162 if (s->flags & F_FLAGS)
163 CLEARFLAGS(p->fts_accpath, s->st_flags,
164 p->fts_statp->st_flags, p->fts_statp->st_flags,
165 SP_FLGS);
166 return (label);
167 }
168 /* Set the uid/gid first, then set the mode. */
169 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
170 LABEL;
171 (void)printf("%suser (%lu, %lu",
172 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
173 if (uflag) {
174 if (chown(p->fts_accpath, s->st_uid, -1))
175 (void)printf(", not modified: %s)\n",
176 strerror(errno));
177 else
178 (void)printf(", modified)\n");
179 } else
180 (void)printf(")\n");
181 tab = "\t";
182 }
183 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
184 LABEL;
185 (void)printf("%sgid (%lu, %lu",
186 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
187 if (uflag) {
188 if (chown(p->fts_accpath, -1, s->st_gid))
189 (void)printf(", not modified: %s)\n",
190 strerror(errno));
191 else
192 (void)printf(", modified)\n");
193 }
194 else
195 (void)printf(")\n");
196 tab = "\t";
197 }
198 if (s->flags & F_MODE &&
199 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
200 if (lflag) {
201 mode_t tmode, mode;
202
203 tmode = s->st_mode;
204 mode = p->fts_statp->st_mode & MBITS;
205 /*
206 * if none of the suid/sgid/etc bits are set,
207 * then if the mode is a subset of the target,
208 * skip.
209 */
210 if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
211 (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
212 if ((mode | tmode) == tmode)
213 goto skip;
214 }
215
216 LABEL;
217 (void)printf("%spermissions (%#lo, %#lo",
218 tab, (u_long)s->st_mode,
219 (u_long)p->fts_statp->st_mode & MBITS);
220 if (uflag) {
221 if (chmod(p->fts_accpath, s->st_mode))
222 (void)printf(", not modified: %s)\n",
223 strerror(errno));
224 else
225 (void)printf(", modified)\n");
226 }
227 else
228 (void)printf(")\n");
229 tab = "\t";
230 skip: ;
231 }
232 if (s->flags & F_DEV &&
233 (s->type == F_BLOCK || s->type == F_CHAR) &&
234 s->st_rdev != p->fts_statp->st_rdev) {
235 LABEL;
236 (void)printf("%sdevice (%#x, %#x",
237 tab, s->st_rdev, p->fts_statp->st_rdev);
238 if (uflag) {
239 /* XXXLUKEM: unlink first ? */
240 if (mknod(p->fts_accpath, s->st_mode, s->st_rdev))
241 (void)printf(", not modified: %s)\n",
242 strerror(errno));
243 else
244 (void)printf(", modified)\n");
245 } else
246 (void)printf(")\n");
247 tab = "\t";
248 }
249 if (s->flags & F_NLINK && s->type != F_DIR &&
250 s->st_nlink != p->fts_statp->st_nlink) {
251 LABEL;
252 (void)printf("%slink count (%lu, %lu)\n",
253 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
254 tab = "\t";
255 }
256 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
257 LABEL;
258 (void)printf("%ssize (%lld, %lld)\n",
259 tab, (long long)s->st_size,
260 (long long)p->fts_statp->st_size);
261 tab = "\t";
262 }
263 /*
264 * XXX
265 * Since utimes(2) only takes a timeval, there's no point in
266 * comparing the low bits of the timespec nanosecond field. This
267 * will only result in mismatches that we can never fix.
268 *
269 * Doesn't display microsecond differences.
270 */
271 if (s->flags & F_TIME) {
272 struct timeval tv[2];
273 struct stat *ps = p->fts_statp;
274 time_t smtime = s->st_mtimespec.tv_sec;
275
276 #ifdef BSD4_4
277 time_t pmtime = ps->st_mtimespec.tv_sec;
278
279 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
280 #else
281 time_t pmtime = (time_t)ps->st_mtime;
282
283 tv[1].tv_sec = ps->st_mtime;
284 tv[1].tv_usec = 0;
285 #endif
286 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
287
288 if (tv[0].tv_sec != tv[1].tv_sec ||
289 tv[0].tv_usec != tv[1].tv_usec) {
290 LABEL;
291 (void)printf("%smodification time (%.24s, ",
292 tab, ctime(&smtime));
293 (void)printf("%.24s", ctime(&pmtime));
294 if (tflag) {
295 tv[1] = tv[0];
296 if (utimes(p->fts_accpath, tv))
297 (void)printf(", not modified: %s)\n",
298 strerror(errno));
299 else
300 (void)printf(", modified)\n");
301 } else
302 (void)printf(")\n");
303 tab = "\t";
304 }
305 }
306 /*
307 * XXX
308 * since chflags(2) will reset file times, the utimes() above
309 * may have been useless! oh well, we'd rather have correct
310 * flags, rather than times?
311 */
312 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
313 || mflag || iflag)) {
314 if (s->st_flags != p->fts_statp->st_flags) {
315 LABEL;
316 (void)printf("%sflags (\"%s\" is not ", tab,
317 flags_to_string(s->st_flags, "none"));
318 (void)printf("\"%s\"",
319 flags_to_string(p->fts_statp->st_flags, "none"));
320 }
321 if (uflag) {
322 if (iflag)
323 SETFLAGS(p->fts_accpath, s->st_flags,
324 0, p->fts_statp->st_flags, CH_MASK);
325 else if (mflag)
326 CLEARFLAGS(p->fts_accpath, s->st_flags,
327 0, p->fts_statp->st_flags, SP_FLGS);
328 else
329 SETFLAGS(p->fts_accpath, s->st_flags,
330 0, p->fts_statp->st_flags,
331 (~SP_FLGS & CH_MASK));
332 } else
333 (void)printf(")\n");
334 tab = "\t";
335 }
336 if (s->flags & F_CKSUM) {
337 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
338 LABEL;
339 (void)printf("%scksum: %s: %s\n",
340 tab, p->fts_accpath, strerror(errno));
341 tab = "\t";
342 } else if (crc(fd, &val, &len)) {
343 (void)close(fd);
344 LABEL;
345 (void)printf("%scksum: %s: %s\n",
346 tab, p->fts_accpath, strerror(errno));
347 tab = "\t";
348 } else {
349 (void)close(fd);
350 if (s->cksum != val) {
351 LABEL;
352 (void)printf("%scksum (%lu, %lu)\n",
353 tab, s->cksum, (unsigned long)val);
354 }
355 tab = "\t";
356 }
357 }
358 if (s->flags & F_MD5) {
359 if (MD5File(p->fts_accpath, md5buf) == NULL) {
360 LABEL;
361 (void)printf("%smd5: %s: %s\n",
362 tab, p->fts_accpath, strerror(errno));
363 tab = "\t";
364 } else {
365 if (strcmp(s->md5sum, md5buf)) {
366 LABEL;
367 (void)printf("%smd5 (0x%s, 0x%s)\n",
368 tab, s->md5sum, md5buf);
369 }
370 tab = "\t";
371 }
372 }
373
374 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
375 LABEL;
376 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
377 }
378 return (label);
379 }
380
381 const char *
382 rlink(const char *name)
383 {
384 static char lbuf[MAXPATHLEN];
385 int len;
386
387 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
388 mtree_err("%s: %s", name, strerror(errno));
389 lbuf[len] = '\0';
390 return (lbuf);
391 }
392