compare.c revision 1.25 1 /* $NetBSD: compare.c,v 1.25 2001/07/18 04:51:54 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.25 2001/07/18 04:51:54 lukem 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 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 LABEL;
207 (void)printf("%spermissions (%#lo, %#lo",
208 tab, (u_long)s->st_mode,
209 (u_long)p->fts_statp->st_mode & MBITS);
210 if (uflag) {
211 if (chmod(p->fts_accpath, s->st_mode))
212 (void)printf(", not modified: %s)\n",
213 strerror(errno));
214 else
215 (void)printf(", modified)\n");
216 }
217 else
218 (void)printf(")\n");
219 tab = "\t";
220 }
221 if (s->flags & F_NLINK && s->type != F_DIR &&
222 s->st_nlink != p->fts_statp->st_nlink) {
223 LABEL;
224 (void)printf("%slink count (%lu, %lu)\n",
225 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
226 tab = "\t";
227 }
228 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
229 LABEL;
230 (void)printf("%ssize (%lld, %lld)\n",
231 tab, (long long)s->st_size,
232 (long long)p->fts_statp->st_size);
233 tab = "\t";
234 }
235 /*
236 * XXX
237 * Since utimes(2) only takes a timeval, there's no point in
238 * comparing the low bits of the timespec nanosecond field. This
239 * will only result in mismatches that we can never fix.
240 *
241 * Doesn't display microsecond differences.
242 */
243 if (s->flags & F_TIME) {
244 struct timeval tv[2];
245 struct stat *ps = p->fts_statp;
246 time_t smtime = s->st_mtimespec.tv_sec;
247
248 #ifdef BSD4_4
249 time_t pmtime = ps->st_mtimespec.tv_sec;
250
251 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
252 #else
253 time_t pmtime = (time_t)ps->st_mtime;
254
255 tv[1].tv_sec = ps->st_mtime;
256 tv[1].tv_usec = 0;
257 #endif
258 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
259
260 if (tv[0].tv_sec != tv[1].tv_sec ||
261 tv[0].tv_usec != tv[1].tv_usec) {
262 LABEL;
263 (void)printf("%smodification time (%.24s, ",
264 tab, ctime(&smtime));
265 (void)printf("%.24s", ctime(&pmtime));
266 if (tflag) {
267 tv[1] = tv[0];
268 if (utimes(p->fts_accpath, tv))
269 (void)printf(", not modified: %s)\n",
270 strerror(errno));
271 else
272 (void)printf(", modified)\n");
273 } else
274 (void)printf(")\n");
275 tab = "\t";
276 }
277 }
278 /*
279 * XXX
280 * since chflags(2) will reset file times, the utimes() above
281 * may have been useless! oh well, we'd rather have correct
282 * flags, rather than times?
283 */
284 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
285 || mflag || iflag)) {
286 if (s->st_flags != p->fts_statp->st_flags) {
287 LABEL;
288 (void)printf("%sflags (\"%s\" is not ", tab,
289 flags_to_string(s->st_flags, "none"));
290 (void)printf("\"%s\"",
291 flags_to_string(p->fts_statp->st_flags, "none"));
292 }
293 if (uflag) {
294 if (iflag)
295 SETFLAGS(p->fts_accpath, s->st_flags,
296 0, p->fts_statp->st_flags, CH_MASK);
297 else if (mflag)
298 CLEARFLAGS(p->fts_accpath, s->st_flags,
299 0, p->fts_statp->st_flags, SP_FLGS);
300 else
301 SETFLAGS(p->fts_accpath, s->st_flags,
302 0, p->fts_statp->st_flags,
303 (~SP_FLGS & CH_MASK));
304 } else
305 (void)printf(")\n");
306 tab = "\t";
307 }
308 if (s->flags & F_CKSUM) {
309 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
310 LABEL;
311 (void)printf("%scksum: %s: %s\n",
312 tab, p->fts_accpath, strerror(errno));
313 tab = "\t";
314 } else if (crc(fd, &val, &len)) {
315 (void)close(fd);
316 LABEL;
317 (void)printf("%scksum: %s: %s\n",
318 tab, p->fts_accpath, strerror(errno));
319 tab = "\t";
320 } else {
321 (void)close(fd);
322 if (s->cksum != val) {
323 LABEL;
324 (void)printf("%scksum (%lu, %lu)\n",
325 tab, s->cksum, (unsigned long)val);
326 }
327 tab = "\t";
328 }
329 }
330 if (s->flags & F_MD5) {
331 if (MD5File(p->fts_accpath, md5buf) == NULL) {
332 LABEL;
333 (void)printf("%smd5: %s: %s\n",
334 tab, p->fts_accpath, strerror(errno));
335 tab = "\t";
336 } else {
337 if (strcmp(s->md5sum, md5buf)) {
338 LABEL;
339 (void)printf("%smd5 (0x%s, 0x%s)\n",
340 tab, s->md5sum, md5buf);
341 }
342 tab = "\t";
343 }
344 }
345
346 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
347 LABEL;
348 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
349 }
350 return (label);
351 }
352
353 const char *
354 inotype(u_int type)
355 {
356
357 return (ftype(type & S_IFMT));
358 }
359
360 static const char *
361 ftype(u_int type)
362 {
363
364 switch(type) {
365 case F_BLOCK:
366 case S_IFBLK:
367 return ("block");
368 case F_CHAR:
369 case S_IFCHR:
370 return ("char");
371 case F_DIR:
372 case S_IFDIR:
373 return ("dir");
374 case F_FIFO:
375 case S_IFIFO:
376 return ("fifo");
377 case F_FILE:
378 case S_IFREG:
379 return ("file");
380 case F_LINK:
381 case S_IFLNK:
382 return ("link");
383 case F_SOCK:
384 case S_IFSOCK:
385 return ("socket");
386 default:
387 return ("unknown");
388 }
389 /* NOTREACHED */
390 }
391
392 const char *
393 rlink(const char *name)
394 {
395 static char lbuf[MAXPATHLEN];
396 int len;
397
398 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
399 mtree_err("%s: %s", name, strerror(errno));
400 lbuf[len] = '\0';
401 return (lbuf);
402 }
403