Import mdocml-1.9.14:
[netbsd-mini2440.git] / usr.sbin / mtree / compare.c
blob61c77f9ca9489b01738922a0ca964d7e34c5b5ea
1 /* $NetBSD: compare.c,v 1.51 2007/02/04 08:03:18 elad Exp $ */
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(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.51 2007/02/04 08:03:18 elad Exp $");
42 #endif
43 #endif /* not lint */
45 #include <sys/param.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
55 #ifndef NO_MD5
56 #include <md5.h>
57 #endif
58 #ifndef NO_RMD160
59 #include <rmd160.h>
60 #endif
61 #ifndef NO_SHA1
62 #include <sha1.h>
63 #endif
64 #ifndef NO_SHA2
65 #include <sha2.h>
66 #endif
68 #include "extern.h"
70 #define INDENTNAMELEN 8
71 #define MARK \
72 do { \
73 len = printf("%s: ", RP(p)); \
74 if (len > INDENTNAMELEN) { \
75 tab = "\t"; \
76 printf("\n"); \
77 } else { \
78 tab = ""; \
79 printf("%*s", INDENTNAMELEN - (int)len, ""); \
80 } \
81 } while (0)
82 #define LABEL if (!label++) MARK
84 #if HAVE_STRUCT_STAT_ST_FLAGS
87 #define CHANGEFLAGS \
88 if (flags != p->fts_statp->st_flags) { \
89 char *sf; \
90 if (!label) { \
91 MARK; \
92 sf = flags_to_string(p->fts_statp->st_flags, "none"); \
93 printf("%sflags (\"%s\"", tab, sf); \
94 free(sf); \
95 } \
96 if (lchflags(p->fts_accpath, flags)) { \
97 label++; \
98 printf(", not modified: %s)\n", \
99 strerror(errno)); \
100 } else { \
101 sf = flags_to_string(flags, "none"); \
102 printf(", modified to \"%s\")\n", sf); \
103 free(sf); \
107 /* SETFLAGS:
108 * given pflags, additionally set those flags specified in s->st_flags and
109 * selected by mask (the other flags are left unchanged).
111 #define SETFLAGS(pflags, mask) \
112 do { \
113 flags = (s->st_flags & (mask)) | (pflags); \
114 CHANGEFLAGS; \
115 } while (0)
117 /* CLEARFLAGS:
118 * given pflags, reset the flags specified in s->st_flags and selected by mask
119 * (the other flags are left unchanged).
121 #define CLEARFLAGS(pflags, mask) \
122 do { \
123 flags = (~(s->st_flags & (mask)) & CH_MASK) & (pflags); \
124 CHANGEFLAGS; \
125 } while (0)
126 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
129 compare(NODE *s, FTSENT *p)
131 u_int32_t len, val, flags;
132 int fd, label;
133 const char *cp, *tab;
134 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
135 char *digestbuf;
136 #endif
138 tab = NULL;
139 label = 0;
140 switch(s->type) {
141 case F_BLOCK:
142 if (!S_ISBLK(p->fts_statp->st_mode))
143 goto typeerr;
144 break;
145 case F_CHAR:
146 if (!S_ISCHR(p->fts_statp->st_mode))
147 goto typeerr;
148 break;
149 case F_DIR:
150 if (!S_ISDIR(p->fts_statp->st_mode))
151 goto typeerr;
152 break;
153 case F_FIFO:
154 if (!S_ISFIFO(p->fts_statp->st_mode))
155 goto typeerr;
156 break;
157 case F_FILE:
158 if (!S_ISREG(p->fts_statp->st_mode))
159 goto typeerr;
160 break;
161 case F_LINK:
162 if (!S_ISLNK(p->fts_statp->st_mode))
163 goto typeerr;
164 break;
165 #ifdef S_ISSOCK
166 case F_SOCK:
167 if (!S_ISSOCK(p->fts_statp->st_mode))
168 goto typeerr;
169 break;
170 #endif
171 typeerr: LABEL;
172 printf("\ttype (%s, %s)\n",
173 nodetype(s->type), inotype(p->fts_statp->st_mode));
174 return (label);
176 if (mtree_Wflag)
177 goto afterpermwhack;
178 #if HAVE_STRUCT_STAT_ST_FLAGS
179 if (iflag && !uflag) {
180 if (s->flags & F_FLAGS)
181 SETFLAGS(p->fts_statp->st_flags, SP_FLGS);
182 return (label);
184 if (mflag && !uflag) {
185 if (s->flags & F_FLAGS)
186 CLEARFLAGS(p->fts_statp->st_flags, SP_FLGS);
187 return (label);
189 #endif
190 if (s->flags & F_DEV &&
191 (s->type == F_BLOCK || s->type == F_CHAR) &&
192 s->st_rdev != p->fts_statp->st_rdev) {
193 LABEL;
194 printf("%sdevice (%#llx, %#llx",
195 tab, (long long)s->st_rdev,
196 (long long)p->fts_statp->st_rdev);
197 if (uflag) {
198 if ((unlink(p->fts_accpath) == -1) ||
199 (mknod(p->fts_accpath,
200 s->st_mode | nodetoino(s->type),
201 s->st_rdev) == -1) ||
202 (lchown(p->fts_accpath, p->fts_statp->st_uid,
203 p->fts_statp->st_gid) == -1) )
204 printf(", not modified: %s)\n",
205 strerror(errno));
206 else
207 printf(", modified)\n");
208 } else
209 printf(")\n");
210 tab = "\t";
212 /* Set the uid/gid first, then set the mode. */
213 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
214 LABEL;
215 printf("%suser (%lu, %lu",
216 tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
217 if (uflag) {
218 if (lchown(p->fts_accpath, s->st_uid, -1))
219 printf(", not modified: %s)\n",
220 strerror(errno));
221 else
222 printf(", modified)\n");
223 } else
224 printf(")\n");
225 tab = "\t";
227 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
228 LABEL;
229 printf("%sgid (%lu, %lu",
230 tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
231 if (uflag) {
232 if (lchown(p->fts_accpath, -1, s->st_gid))
233 printf(", not modified: %s)\n",
234 strerror(errno));
235 else
236 printf(", modified)\n");
238 else
239 printf(")\n");
240 tab = "\t";
242 if (s->flags & F_MODE &&
243 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
244 if (lflag) {
245 mode_t tmode, mode;
247 tmode = s->st_mode;
248 mode = p->fts_statp->st_mode & MBITS;
250 * if none of the suid/sgid/etc bits are set,
251 * then if the mode is a subset of the target,
252 * skip.
254 if (!((tmode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) ||
255 (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO))))
256 if ((mode | tmode) == tmode)
257 goto skip;
260 LABEL;
261 printf("%spermissions (%#lo, %#lo",
262 tab, (u_long)s->st_mode,
263 (u_long)p->fts_statp->st_mode & MBITS);
264 if (uflag) {
265 if (lchmod(p->fts_accpath, s->st_mode))
266 printf(", not modified: %s)\n",
267 strerror(errno));
268 else
269 printf(", modified)\n");
271 else
272 printf(")\n");
273 tab = "\t";
274 skip: ;
276 if (s->flags & F_NLINK && s->type != F_DIR &&
277 s->st_nlink != p->fts_statp->st_nlink) {
278 LABEL;
279 printf("%slink count (%lu, %lu)\n",
280 tab, (u_long)s->st_nlink, (u_long)p->fts_statp->st_nlink);
281 tab = "\t";
283 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
284 LABEL;
285 printf("%ssize (%lld, %lld)\n",
286 tab, (long long)s->st_size,
287 (long long)p->fts_statp->st_size);
288 tab = "\t";
291 * XXX
292 * Since utimes(2) only takes a timeval, there's no point in
293 * comparing the low bits of the timespec nanosecond field. This
294 * will only result in mismatches that we can never fix.
296 * Doesn't display microsecond differences.
298 if (s->flags & F_TIME) {
299 struct timeval tv[2];
300 struct stat *ps = p->fts_statp;
301 time_t smtime = s->st_mtimespec.tv_sec;
303 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
304 time_t pmtime = ps->st_mtimespec.tv_sec;
306 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
307 TIMESPEC_TO_TIMEVAL(&tv[1], &ps->st_mtimespec);
308 #else
309 time_t pmtime = (time_t)ps->st_mtime;
311 tv[0].tv_sec = smtime;
312 tv[0].tv_usec = 0;
313 tv[1].tv_sec = pmtime;
314 tv[1].tv_usec = 0;
315 #endif
317 if (tv[0].tv_sec != tv[1].tv_sec ||
318 tv[0].tv_usec != tv[1].tv_usec) {
319 LABEL;
320 printf("%smodification time (%.24s, ",
321 tab, ctime(&smtime));
322 printf("%.24s", ctime(&pmtime));
323 if (tflag) {
324 tv[1] = tv[0];
325 if (utimes(p->fts_accpath, tv))
326 printf(", not modified: %s)\n",
327 strerror(errno));
328 else
329 printf(", modified)\n");
330 } else
331 printf(")\n");
332 tab = "\t";
335 #if HAVE_STRUCT_STAT_ST_FLAGS
337 * XXX
338 * since lchflags(2) will reset file times, the utimes() above
339 * may have been useless! oh well, we'd rather have correct
340 * flags, rather than times?
342 if ((s->flags & F_FLAGS) && ((s->st_flags != p->fts_statp->st_flags)
343 || mflag || iflag)) {
344 if (s->st_flags != p->fts_statp->st_flags) {
345 char *f_s;
346 LABEL;
347 f_s = flags_to_string(s->st_flags, "none");
348 printf("%sflags (\"%s\" is not ", tab, f_s);
349 free(f_s);
350 f_s = flags_to_string(p->fts_statp->st_flags, "none");
351 printf("\"%s\"", f_s);
352 free(f_s);
354 if (uflag) {
355 if (iflag)
356 SETFLAGS(0, CH_MASK);
357 else if (mflag)
358 CLEARFLAGS(0, SP_FLGS);
359 else
360 SETFLAGS(0, (~SP_FLGS & CH_MASK));
361 } else
362 printf(")\n");
363 tab = "\t";
365 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
368 * from this point, no more permission checking or whacking
369 * occurs, only checking of stuff like checksums and symlinks.
371 afterpermwhack:
372 if (s->flags & F_CKSUM) {
373 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
374 LABEL;
375 printf("%scksum: %s: %s\n",
376 tab, p->fts_accpath, strerror(errno));
377 tab = "\t";
378 } else if (crc(fd, &val, &len)) {
379 close(fd);
380 LABEL;
381 printf("%scksum: %s: %s\n",
382 tab, p->fts_accpath, strerror(errno));
383 tab = "\t";
384 } else {
385 close(fd);
386 if (s->cksum != val) {
387 LABEL;
388 printf("%scksum (%lu, %lu)\n",
389 tab, s->cksum, (unsigned long)val);
391 tab = "\t";
394 #ifndef NO_MD5
395 if (s->flags & F_MD5) {
396 if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL) {
397 LABEL;
398 printf("%smd5: %s: %s\n",
399 tab, p->fts_accpath, strerror(errno));
400 tab = "\t";
401 } else {
402 if (strcmp(s->md5digest, digestbuf)) {
403 LABEL;
404 printf("%smd5 (0x%s, 0x%s)\n",
405 tab, s->md5digest, digestbuf);
407 tab = "\t";
408 free(digestbuf);
411 #endif /* ! NO_MD5 */
412 #ifndef NO_RMD160
413 if (s->flags & F_RMD160) {
414 if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL) {
415 LABEL;
416 printf("%srmd160: %s: %s\n",
417 tab, p->fts_accpath, strerror(errno));
418 tab = "\t";
419 } else {
420 if (strcmp(s->rmd160digest, digestbuf)) {
421 LABEL;
422 printf("%srmd160 (0x%s, 0x%s)\n",
423 tab, s->rmd160digest, digestbuf);
425 tab = "\t";
426 free(digestbuf);
429 #endif /* ! NO_RMD160 */
430 #ifndef NO_SHA1
431 if (s->flags & F_SHA1) {
432 if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL) {
433 LABEL;
434 printf("%ssha1: %s: %s\n",
435 tab, p->fts_accpath, strerror(errno));
436 tab = "\t";
437 } else {
438 if (strcmp(s->sha1digest, digestbuf)) {
439 LABEL;
440 printf("%ssha1 (0x%s, 0x%s)\n",
441 tab, s->sha1digest, digestbuf);
443 tab = "\t";
444 free(digestbuf);
447 #endif /* ! NO_SHA1 */
448 #ifndef NO_SHA2
449 if (s->flags & F_SHA256) {
450 if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL) {
451 LABEL;
452 printf("%ssha256: %s: %s\n",
453 tab, p->fts_accpath, strerror(errno));
454 tab = "\t";
455 } else {
456 if (strcmp(s->sha256digest, digestbuf)) {
457 LABEL;
458 printf("%ssha256 (0x%s, 0x%s)\n",
459 tab, s->sha256digest, digestbuf);
461 tab = "\t";
462 free(digestbuf);
465 if (s->flags & F_SHA384) {
466 if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL) {
467 LABEL;
468 printf("%ssha384: %s: %s\n",
469 tab, p->fts_accpath, strerror(errno));
470 tab = "\t";
471 } else {
472 if (strcmp(s->sha384digest, digestbuf)) {
473 LABEL;
474 printf("%ssha384 (0x%s, 0x%s)\n",
475 tab, s->sha384digest, digestbuf);
477 tab = "\t";
478 free(digestbuf);
481 if (s->flags & F_SHA512) {
482 if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL) {
483 LABEL;
484 printf("%ssha512: %s: %s\n",
485 tab, p->fts_accpath, strerror(errno));
486 tab = "\t";
487 } else {
488 if (strcmp(s->sha512digest, digestbuf)) {
489 LABEL;
490 printf("%ssha512 (0x%s, 0x%s)\n",
491 tab, s->sha512digest, digestbuf);
493 tab = "\t";
494 free(digestbuf);
497 #endif /* ! NO_SHA2 */
498 if (s->flags & F_SLINK &&
499 strcmp(cp = rlink(p->fts_accpath), s->slink)) {
500 LABEL;
501 printf("%slink ref (%s, %s", tab, cp, s->slink);
502 if (uflag) {
503 if ((unlink(p->fts_accpath) == -1) ||
504 (symlink(s->slink, p->fts_accpath) == -1) )
505 printf(", not modified: %s)\n",
506 strerror(errno));
507 else
508 printf(", modified)\n");
509 } else
510 printf(")\n");
512 return (label);
515 const char *
516 rlink(const char *name)
518 static char lbuf[MAXPATHLEN];
519 int len;
521 if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
522 mtree_err("%s: %s", name, strerror(errno));
523 lbuf[len] = '\0';
524 return (lbuf);