compare.c revision 30027
11553Srgrimes/*- 21553Srgrimes * Copyright (c) 1989, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 3. All advertising materials mentioning features or use of this software 141553Srgrimes * must display the following acknowledgement: 151553Srgrimes * This product includes software developed by the University of 161553Srgrimes * California, Berkeley and its contributors. 171553Srgrimes * 4. Neither the name of the University nor the names of its contributors 181553Srgrimes * may be used to endorse or promote products derived from this software 191553Srgrimes * without specific prior written permission. 201553Srgrimes * 211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311553Srgrimes * SUCH DAMAGE. 321553Srgrimes */ 331553Srgrimes 341553Srgrimes#ifndef lint 3530027Scharnier#if 0 361553Srgrimesstatic char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93"; 3730027Scharnier#endif 3830027Scharnierstatic const char rcsid[] = 3930027Scharnier "$Id$"; 401553Srgrimes#endif /* not lint */ 411553Srgrimes 421553Srgrimes#include <sys/param.h> 431553Srgrimes#include <sys/stat.h> 4430027Scharnier#include <err.h> 4530027Scharnier#include <errno.h> 461553Srgrimes#include <fcntl.h> 471553Srgrimes#include <fts.h> 4830027Scharnier#include <md5.h> 491553Srgrimes#include <stdio.h> 501553Srgrimes#include <time.h> 511553Srgrimes#include <unistd.h> 521553Srgrimes#include "mtree.h" 531553Srgrimes#include "extern.h" 541553Srgrimes 551553Srgrimesextern int uflag; 5630027Scharnierextern int lineno; 571553Srgrimes 581553Srgrimesstatic char *ftype __P((u_int)); 591553Srgrimes 601553Srgrimes#define INDENTNAMELEN 8 611553Srgrimes#define LABEL \ 621553Srgrimes if (!label++) { \ 631553Srgrimes len = printf("%s: ", RP(p)); \ 641553Srgrimes if (len > INDENTNAMELEN) { \ 651553Srgrimes tab = "\t"; \ 661553Srgrimes (void)printf("\n"); \ 671553Srgrimes } else { \ 681553Srgrimes tab = ""; \ 692860Srgrimes (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \ 701553Srgrimes } \ 711553Srgrimes } 721553Srgrimes 731553Srgrimesint 741553Srgrimescompare(name, s, p) 751553Srgrimes char *name; 761553Srgrimes register NODE *s; 771553Srgrimes register FTSENT *p; 781553Srgrimes{ 791553Srgrimes extern int uflag; 801553Srgrimes u_long len, val; 811553Srgrimes int fd, label; 822860Srgrimes char *cp, *tab = ""; 831553Srgrimes 841553Srgrimes label = 0; 851553Srgrimes switch(s->type) { 861553Srgrimes case F_BLOCK: 871553Srgrimes if (!S_ISBLK(p->fts_statp->st_mode)) 881553Srgrimes goto typeerr; 891553Srgrimes break; 901553Srgrimes case F_CHAR: 911553Srgrimes if (!S_ISCHR(p->fts_statp->st_mode)) 921553Srgrimes goto typeerr; 931553Srgrimes break; 941553Srgrimes case F_DIR: 951553Srgrimes if (!S_ISDIR(p->fts_statp->st_mode)) 961553Srgrimes goto typeerr; 971553Srgrimes break; 981553Srgrimes case F_FIFO: 991553Srgrimes if (!S_ISFIFO(p->fts_statp->st_mode)) 1001553Srgrimes goto typeerr; 1011553Srgrimes break; 1021553Srgrimes case F_FILE: 1031553Srgrimes if (!S_ISREG(p->fts_statp->st_mode)) 1041553Srgrimes goto typeerr; 1051553Srgrimes break; 1061553Srgrimes case F_LINK: 1071553Srgrimes if (!S_ISLNK(p->fts_statp->st_mode)) 1081553Srgrimes goto typeerr; 1091553Srgrimes break; 1101553Srgrimes case F_SOCK: 1111553Srgrimes if (!S_ISSOCK(p->fts_statp->st_mode)) { 1121553Srgrimestypeerr: LABEL; 1131553Srgrimes (void)printf("\ttype (%s, %s)\n", 1141553Srgrimes ftype(s->type), inotype(p->fts_statp->st_mode)); 1151553Srgrimes } 1161553Srgrimes break; 1171553Srgrimes } 1181553Srgrimes /* Set the uid/gid first, then set the mode. */ 1191553Srgrimes if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) { 1201553Srgrimes LABEL; 1212860Srgrimes (void)printf("%suser (%lu, %lu", 1221553Srgrimes tab, s->st_uid, p->fts_statp->st_uid); 1231553Srgrimes if (uflag) 1241553Srgrimes if (chown(p->fts_accpath, s->st_uid, -1)) 1251553Srgrimes (void)printf(", not modified: %s)\n", 1261553Srgrimes strerror(errno)); 1271553Srgrimes else 1281553Srgrimes (void)printf(", modified)\n"); 1291553Srgrimes else 1301553Srgrimes (void)printf(")\n"); 1311553Srgrimes tab = "\t"; 1321553Srgrimes } 1331553Srgrimes if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) { 1341553Srgrimes LABEL; 1352860Srgrimes (void)printf("%sgid (%lu, %lu", 1361553Srgrimes tab, s->st_gid, p->fts_statp->st_gid); 1371553Srgrimes if (uflag) 1381553Srgrimes if (chown(p->fts_accpath, -1, s->st_gid)) 1391553Srgrimes (void)printf(", not modified: %s)\n", 1401553Srgrimes strerror(errno)); 1411553Srgrimes else 1421553Srgrimes (void)printf(", modified)\n"); 1431553Srgrimes else 1441553Srgrimes (void)printf(")\n"); 1451553Srgrimes tab = "\t"; 1461553Srgrimes } 1471553Srgrimes if (s->flags & F_MODE && 1481553Srgrimes s->st_mode != (p->fts_statp->st_mode & MBITS)) { 1491553Srgrimes LABEL; 1501553Srgrimes (void)printf("%spermissions (%#o, %#o", 1511553Srgrimes tab, s->st_mode, p->fts_statp->st_mode & MBITS); 1521553Srgrimes if (uflag) 1531553Srgrimes if (chmod(p->fts_accpath, s->st_mode)) 1541553Srgrimes (void)printf(", not modified: %s)\n", 1551553Srgrimes strerror(errno)); 1561553Srgrimes else 1571553Srgrimes (void)printf(", modified)\n"); 1581553Srgrimes else 1591553Srgrimes (void)printf(")\n"); 1601553Srgrimes tab = "\t"; 1611553Srgrimes } 1621553Srgrimes if (s->flags & F_NLINK && s->type != F_DIR && 1631553Srgrimes s->st_nlink != p->fts_statp->st_nlink) { 1641553Srgrimes LABEL; 1651553Srgrimes (void)printf("%slink count (%u, %u)\n", 1661553Srgrimes tab, s->st_nlink, p->fts_statp->st_nlink); 1671553Srgrimes tab = "\t"; 1681553Srgrimes } 1691553Srgrimes if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) { 1701553Srgrimes LABEL; 1711553Srgrimes (void)printf("%ssize (%qd, %qd)\n", 1721553Srgrimes tab, s->st_size, p->fts_statp->st_size); 1731553Srgrimes tab = "\t"; 1741553Srgrimes } 1751553Srgrimes /* 1761553Srgrimes * XXX 1771553Srgrimes * Catches nano-second differences, but doesn't display them. 1781553Srgrimes */ 1793052Sdg if ((s->flags & F_TIME) && 18018404Snate ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) || 18118404Snate (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) { 1821553Srgrimes LABEL; 1831553Srgrimes (void)printf("%smodification time (%.24s, ", 18418404Snate tab, ctime(&s->st_mtimespec.tv_sec)); 1851553Srgrimes (void)printf("%.24s)\n", 18618404Snate ctime(&p->fts_statp->st_mtimespec.tv_sec)); 1871553Srgrimes tab = "\t"; 1881553Srgrimes } 1891553Srgrimes if (s->flags & F_CKSUM) 1901553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 1911553Srgrimes LABEL; 1921553Srgrimes (void)printf("%scksum: %s: %s\n", 1931553Srgrimes tab, p->fts_accpath, strerror(errno)); 1941553Srgrimes tab = "\t"; 1951553Srgrimes } else if (crc(fd, &val, &len)) { 1961553Srgrimes (void)close(fd); 1971553Srgrimes LABEL; 1981553Srgrimes (void)printf("%scksum: %s: %s\n", 1991553Srgrimes tab, p->fts_accpath, strerror(errno)); 2001553Srgrimes tab = "\t"; 2011553Srgrimes } else { 2021553Srgrimes (void)close(fd); 2031553Srgrimes if (s->cksum != val) { 2041553Srgrimes LABEL; 2058857Srgrimes (void)printf("%scksum (%lu, %lu)\n", 2061553Srgrimes tab, s->cksum, val); 2071553Srgrimes } 2081553Srgrimes tab = "\t"; 2091553Srgrimes } 2106286Swollman if (s->flags & F_MD5) { 2119490Sphk char *new_digest, buf[33]; 2128857Srgrimes 2139490Sphk new_digest = MD5File(p->fts_accpath,buf); 2146286Swollman if (!new_digest) { 2156286Swollman LABEL; 2166286Swollman printf("%sMD5File: %s: %s\n", tab, p->fts_accpath, 2176286Swollman strerror(errno)); 2186286Swollman tab = "\t"; 2196286Swollman } else if (strcmp(new_digest, s->md5digest)) { 2206286Swollman LABEL; 2218857Srgrimes printf("%sMD5 (%s, %s)\n", tab, s->md5digest, 2226286Swollman new_digest); 2236286Swollman tab = "\t"; 2246286Swollman } 2256286Swollman } 2266286Swollman 2271553Srgrimes if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 2281553Srgrimes LABEL; 2291553Srgrimes (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 2301553Srgrimes } 2311553Srgrimes return (label); 2321553Srgrimes} 2331553Srgrimes 2341553Srgrimeschar * 2351553Srgrimesinotype(type) 2361553Srgrimes u_int type; 2371553Srgrimes{ 2381553Srgrimes switch(type & S_IFMT) { 2391553Srgrimes case S_IFBLK: 2401553Srgrimes return ("block"); 2411553Srgrimes case S_IFCHR: 2421553Srgrimes return ("char"); 2431553Srgrimes case S_IFDIR: 2441553Srgrimes return ("dir"); 2451553Srgrimes case S_IFIFO: 2461553Srgrimes return ("fifo"); 2471553Srgrimes case S_IFREG: 2481553Srgrimes return ("file"); 2491553Srgrimes case S_IFLNK: 2501553Srgrimes return ("link"); 2511553Srgrimes case S_IFSOCK: 2521553Srgrimes return ("socket"); 2531553Srgrimes default: 2541553Srgrimes return ("unknown"); 2551553Srgrimes } 2561553Srgrimes /* NOTREACHED */ 2571553Srgrimes} 2581553Srgrimes 2591553Srgrimesstatic char * 2601553Srgrimesftype(type) 2611553Srgrimes u_int type; 2621553Srgrimes{ 2631553Srgrimes switch(type) { 2641553Srgrimes case F_BLOCK: 2651553Srgrimes return ("block"); 2661553Srgrimes case F_CHAR: 2671553Srgrimes return ("char"); 2681553Srgrimes case F_DIR: 2691553Srgrimes return ("dir"); 2701553Srgrimes case F_FIFO: 2711553Srgrimes return ("fifo"); 2721553Srgrimes case F_FILE: 2731553Srgrimes return ("file"); 2741553Srgrimes case F_LINK: 2751553Srgrimes return ("link"); 2761553Srgrimes case F_SOCK: 2771553Srgrimes return ("socket"); 2781553Srgrimes default: 2791553Srgrimes return ("unknown"); 2801553Srgrimes } 2811553Srgrimes /* NOTREACHED */ 2821553Srgrimes} 2831553Srgrimes 2841553Srgrimeschar * 2851553Srgrimesrlink(name) 2861553Srgrimes char *name; 2871553Srgrimes{ 2881553Srgrimes static char lbuf[MAXPATHLEN]; 2891553Srgrimes register int len; 2901553Srgrimes 2911553Srgrimes if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 29230027Scharnier err(1, "line %d: %s", lineno, name); 2931553Srgrimes lbuf[len] = '\0'; 2941553Srgrimes return (lbuf); 2951553Srgrimes} 296