compare.c revision 9490
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 351553Srgrimesstatic char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93"; 361553Srgrimes#endif /* not lint */ 371553Srgrimes 381553Srgrimes#include <sys/param.h> 391553Srgrimes#include <sys/stat.h> 401553Srgrimes#include <fcntl.h> 411553Srgrimes#include <fts.h> 421553Srgrimes#include <errno.h> 431553Srgrimes#include <stdio.h> 441553Srgrimes#include <time.h> 451553Srgrimes#include <unistd.h> 466286Swollman#include <md5.h> 471553Srgrimes#include "mtree.h" 481553Srgrimes#include "extern.h" 491553Srgrimes 501553Srgrimesextern int uflag; 511553Srgrimes 521553Srgrimesstatic char *ftype __P((u_int)); 531553Srgrimes 541553Srgrimes#define INDENTNAMELEN 8 551553Srgrimes#define LABEL \ 561553Srgrimes if (!label++) { \ 571553Srgrimes len = printf("%s: ", RP(p)); \ 581553Srgrimes if (len > INDENTNAMELEN) { \ 591553Srgrimes tab = "\t"; \ 601553Srgrimes (void)printf("\n"); \ 611553Srgrimes } else { \ 621553Srgrimes tab = ""; \ 632860Srgrimes (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \ 641553Srgrimes } \ 651553Srgrimes } 661553Srgrimes 671553Srgrimesint 681553Srgrimescompare(name, s, p) 691553Srgrimes char *name; 701553Srgrimes register NODE *s; 711553Srgrimes register FTSENT *p; 721553Srgrimes{ 731553Srgrimes extern int uflag; 741553Srgrimes u_long len, val; 751553Srgrimes int fd, label; 762860Srgrimes char *cp, *tab = ""; 771553Srgrimes 781553Srgrimes label = 0; 791553Srgrimes switch(s->type) { 801553Srgrimes case F_BLOCK: 811553Srgrimes if (!S_ISBLK(p->fts_statp->st_mode)) 821553Srgrimes goto typeerr; 831553Srgrimes break; 841553Srgrimes case F_CHAR: 851553Srgrimes if (!S_ISCHR(p->fts_statp->st_mode)) 861553Srgrimes goto typeerr; 871553Srgrimes break; 881553Srgrimes case F_DIR: 891553Srgrimes if (!S_ISDIR(p->fts_statp->st_mode)) 901553Srgrimes goto typeerr; 911553Srgrimes break; 921553Srgrimes case F_FIFO: 931553Srgrimes if (!S_ISFIFO(p->fts_statp->st_mode)) 941553Srgrimes goto typeerr; 951553Srgrimes break; 961553Srgrimes case F_FILE: 971553Srgrimes if (!S_ISREG(p->fts_statp->st_mode)) 981553Srgrimes goto typeerr; 991553Srgrimes break; 1001553Srgrimes case F_LINK: 1011553Srgrimes if (!S_ISLNK(p->fts_statp->st_mode)) 1021553Srgrimes goto typeerr; 1031553Srgrimes break; 1041553Srgrimes case F_SOCK: 1051553Srgrimes if (!S_ISSOCK(p->fts_statp->st_mode)) { 1061553Srgrimestypeerr: LABEL; 1071553Srgrimes (void)printf("\ttype (%s, %s)\n", 1081553Srgrimes ftype(s->type), inotype(p->fts_statp->st_mode)); 1091553Srgrimes } 1101553Srgrimes break; 1111553Srgrimes } 1121553Srgrimes /* Set the uid/gid first, then set the mode. */ 1131553Srgrimes if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) { 1141553Srgrimes LABEL; 1152860Srgrimes (void)printf("%suser (%lu, %lu", 1161553Srgrimes tab, s->st_uid, p->fts_statp->st_uid); 1171553Srgrimes if (uflag) 1181553Srgrimes if (chown(p->fts_accpath, s->st_uid, -1)) 1191553Srgrimes (void)printf(", not modified: %s)\n", 1201553Srgrimes strerror(errno)); 1211553Srgrimes else 1221553Srgrimes (void)printf(", modified)\n"); 1231553Srgrimes else 1241553Srgrimes (void)printf(")\n"); 1251553Srgrimes tab = "\t"; 1261553Srgrimes } 1271553Srgrimes if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) { 1281553Srgrimes LABEL; 1292860Srgrimes (void)printf("%sgid (%lu, %lu", 1301553Srgrimes tab, s->st_gid, p->fts_statp->st_gid); 1311553Srgrimes if (uflag) 1321553Srgrimes if (chown(p->fts_accpath, -1, s->st_gid)) 1331553Srgrimes (void)printf(", not modified: %s)\n", 1341553Srgrimes strerror(errno)); 1351553Srgrimes else 1361553Srgrimes (void)printf(", modified)\n"); 1371553Srgrimes else 1381553Srgrimes (void)printf(")\n"); 1391553Srgrimes tab = "\t"; 1401553Srgrimes } 1411553Srgrimes if (s->flags & F_MODE && 1421553Srgrimes s->st_mode != (p->fts_statp->st_mode & MBITS)) { 1431553Srgrimes LABEL; 1441553Srgrimes (void)printf("%spermissions (%#o, %#o", 1451553Srgrimes tab, s->st_mode, p->fts_statp->st_mode & MBITS); 1461553Srgrimes if (uflag) 1471553Srgrimes if (chmod(p->fts_accpath, s->st_mode)) 1481553Srgrimes (void)printf(", not modified: %s)\n", 1491553Srgrimes strerror(errno)); 1501553Srgrimes else 1511553Srgrimes (void)printf(", modified)\n"); 1521553Srgrimes else 1531553Srgrimes (void)printf(")\n"); 1541553Srgrimes tab = "\t"; 1551553Srgrimes } 1561553Srgrimes if (s->flags & F_NLINK && s->type != F_DIR && 1571553Srgrimes s->st_nlink != p->fts_statp->st_nlink) { 1581553Srgrimes LABEL; 1591553Srgrimes (void)printf("%slink count (%u, %u)\n", 1601553Srgrimes tab, s->st_nlink, p->fts_statp->st_nlink); 1611553Srgrimes tab = "\t"; 1621553Srgrimes } 1631553Srgrimes if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) { 1641553Srgrimes LABEL; 1651553Srgrimes (void)printf("%ssize (%qd, %qd)\n", 1661553Srgrimes tab, s->st_size, p->fts_statp->st_size); 1671553Srgrimes tab = "\t"; 1681553Srgrimes } 1691553Srgrimes /* 1701553Srgrimes * XXX 1711553Srgrimes * Catches nano-second differences, but doesn't display them. 1721553Srgrimes */ 1733052Sdg if ((s->flags & F_TIME) && 1743052Sdg ((s->st_mtimespec.ts_sec != p->fts_statp->st_mtimespec.ts_sec) || 1753052Sdg (s->st_mtimespec.ts_nsec != p->fts_statp->st_mtimespec.ts_nsec))) { 1761553Srgrimes LABEL; 1771553Srgrimes (void)printf("%smodification time (%.24s, ", 1781553Srgrimes tab, ctime(&s->st_mtimespec.ts_sec)); 1791553Srgrimes (void)printf("%.24s)\n", 1801553Srgrimes ctime(&p->fts_statp->st_mtimespec.ts_sec)); 1811553Srgrimes tab = "\t"; 1821553Srgrimes } 1831553Srgrimes if (s->flags & F_CKSUM) 1841553Srgrimes if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 1851553Srgrimes LABEL; 1861553Srgrimes (void)printf("%scksum: %s: %s\n", 1871553Srgrimes tab, p->fts_accpath, strerror(errno)); 1881553Srgrimes tab = "\t"; 1891553Srgrimes } else if (crc(fd, &val, &len)) { 1901553Srgrimes (void)close(fd); 1911553Srgrimes LABEL; 1921553Srgrimes (void)printf("%scksum: %s: %s\n", 1931553Srgrimes tab, p->fts_accpath, strerror(errno)); 1941553Srgrimes tab = "\t"; 1951553Srgrimes } else { 1961553Srgrimes (void)close(fd); 1971553Srgrimes if (s->cksum != val) { 1981553Srgrimes LABEL; 1998857Srgrimes (void)printf("%scksum (%lu, %lu)\n", 2001553Srgrimes tab, s->cksum, val); 2011553Srgrimes } 2021553Srgrimes tab = "\t"; 2031553Srgrimes } 2046286Swollman if (s->flags & F_MD5) { 2059490Sphk char *new_digest, buf[33]; 2068857Srgrimes 2079490Sphk new_digest = MD5File(p->fts_accpath,buf); 2086286Swollman if (!new_digest) { 2096286Swollman LABEL; 2106286Swollman printf("%sMD5File: %s: %s\n", tab, p->fts_accpath, 2116286Swollman strerror(errno)); 2126286Swollman tab = "\t"; 2136286Swollman } else if (strcmp(new_digest, s->md5digest)) { 2146286Swollman LABEL; 2158857Srgrimes printf("%sMD5 (%s, %s)\n", tab, s->md5digest, 2166286Swollman new_digest); 2176286Swollman tab = "\t"; 2186286Swollman } 2196286Swollman } 2206286Swollman 2211553Srgrimes if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 2221553Srgrimes LABEL; 2231553Srgrimes (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 2241553Srgrimes } 2251553Srgrimes return (label); 2261553Srgrimes} 2271553Srgrimes 2281553Srgrimeschar * 2291553Srgrimesinotype(type) 2301553Srgrimes u_int type; 2311553Srgrimes{ 2321553Srgrimes switch(type & S_IFMT) { 2331553Srgrimes case S_IFBLK: 2341553Srgrimes return ("block"); 2351553Srgrimes case S_IFCHR: 2361553Srgrimes return ("char"); 2371553Srgrimes case S_IFDIR: 2381553Srgrimes return ("dir"); 2391553Srgrimes case S_IFIFO: 2401553Srgrimes return ("fifo"); 2411553Srgrimes case S_IFREG: 2421553Srgrimes return ("file"); 2431553Srgrimes case S_IFLNK: 2441553Srgrimes return ("link"); 2451553Srgrimes case S_IFSOCK: 2461553Srgrimes return ("socket"); 2471553Srgrimes default: 2481553Srgrimes return ("unknown"); 2491553Srgrimes } 2501553Srgrimes /* NOTREACHED */ 2511553Srgrimes} 2521553Srgrimes 2531553Srgrimesstatic char * 2541553Srgrimesftype(type) 2551553Srgrimes u_int type; 2561553Srgrimes{ 2571553Srgrimes switch(type) { 2581553Srgrimes case F_BLOCK: 2591553Srgrimes return ("block"); 2601553Srgrimes case F_CHAR: 2611553Srgrimes return ("char"); 2621553Srgrimes case F_DIR: 2631553Srgrimes return ("dir"); 2641553Srgrimes case F_FIFO: 2651553Srgrimes return ("fifo"); 2661553Srgrimes case F_FILE: 2671553Srgrimes return ("file"); 2681553Srgrimes case F_LINK: 2691553Srgrimes return ("link"); 2701553Srgrimes case F_SOCK: 2711553Srgrimes return ("socket"); 2721553Srgrimes default: 2731553Srgrimes return ("unknown"); 2741553Srgrimes } 2751553Srgrimes /* NOTREACHED */ 2761553Srgrimes} 2771553Srgrimes 2781553Srgrimeschar * 2791553Srgrimesrlink(name) 2801553Srgrimes char *name; 2811553Srgrimes{ 2821553Srgrimes static char lbuf[MAXPATHLEN]; 2831553Srgrimes register int len; 2841553Srgrimes 2851553Srgrimes if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 2861553Srgrimes err("%s: %s", name, strerror(errno)); 2871553Srgrimes lbuf[len] = '\0'; 2881553Srgrimes return (lbuf); 2891553Srgrimes} 290