compare.c revision 30027
1/*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$Id$"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/stat.h> 44#include <err.h> 45#include <errno.h> 46#include <fcntl.h> 47#include <fts.h> 48#include <md5.h> 49#include <stdio.h> 50#include <time.h> 51#include <unistd.h> 52#include "mtree.h" 53#include "extern.h" 54 55extern int uflag; 56extern int lineno; 57 58static char *ftype __P((u_int)); 59 60#define INDENTNAMELEN 8 61#define LABEL \ 62 if (!label++) { \ 63 len = printf("%s: ", RP(p)); \ 64 if (len > INDENTNAMELEN) { \ 65 tab = "\t"; \ 66 (void)printf("\n"); \ 67 } else { \ 68 tab = ""; \ 69 (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \ 70 } \ 71 } 72 73int 74compare(name, s, p) 75 char *name; 76 register NODE *s; 77 register FTSENT *p; 78{ 79 extern int uflag; 80 u_long len, val; 81 int fd, label; 82 char *cp, *tab = ""; 83 84 label = 0; 85 switch(s->type) { 86 case F_BLOCK: 87 if (!S_ISBLK(p->fts_statp->st_mode)) 88 goto typeerr; 89 break; 90 case F_CHAR: 91 if (!S_ISCHR(p->fts_statp->st_mode)) 92 goto typeerr; 93 break; 94 case F_DIR: 95 if (!S_ISDIR(p->fts_statp->st_mode)) 96 goto typeerr; 97 break; 98 case F_FIFO: 99 if (!S_ISFIFO(p->fts_statp->st_mode)) 100 goto typeerr; 101 break; 102 case F_FILE: 103 if (!S_ISREG(p->fts_statp->st_mode)) 104 goto typeerr; 105 break; 106 case F_LINK: 107 if (!S_ISLNK(p->fts_statp->st_mode)) 108 goto typeerr; 109 break; 110 case F_SOCK: 111 if (!S_ISSOCK(p->fts_statp->st_mode)) { 112typeerr: LABEL; 113 (void)printf("\ttype (%s, %s)\n", 114 ftype(s->type), inotype(p->fts_statp->st_mode)); 115 } 116 break; 117 } 118 /* Set the uid/gid first, then set the mode. */ 119 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) { 120 LABEL; 121 (void)printf("%suser (%lu, %lu", 122 tab, s->st_uid, p->fts_statp->st_uid); 123 if (uflag) 124 if (chown(p->fts_accpath, s->st_uid, -1)) 125 (void)printf(", not modified: %s)\n", 126 strerror(errno)); 127 else 128 (void)printf(", modified)\n"); 129 else 130 (void)printf(")\n"); 131 tab = "\t"; 132 } 133 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) { 134 LABEL; 135 (void)printf("%sgid (%lu, %lu", 136 tab, s->st_gid, p->fts_statp->st_gid); 137 if (uflag) 138 if (chown(p->fts_accpath, -1, s->st_gid)) 139 (void)printf(", not modified: %s)\n", 140 strerror(errno)); 141 else 142 (void)printf(", modified)\n"); 143 else 144 (void)printf(")\n"); 145 tab = "\t"; 146 } 147 if (s->flags & F_MODE && 148 s->st_mode != (p->fts_statp->st_mode & MBITS)) { 149 LABEL; 150 (void)printf("%spermissions (%#o, %#o", 151 tab, s->st_mode, p->fts_statp->st_mode & MBITS); 152 if (uflag) 153 if (chmod(p->fts_accpath, s->st_mode)) 154 (void)printf(", not modified: %s)\n", 155 strerror(errno)); 156 else 157 (void)printf(", modified)\n"); 158 else 159 (void)printf(")\n"); 160 tab = "\t"; 161 } 162 if (s->flags & F_NLINK && s->type != F_DIR && 163 s->st_nlink != p->fts_statp->st_nlink) { 164 LABEL; 165 (void)printf("%slink count (%u, %u)\n", 166 tab, s->st_nlink, p->fts_statp->st_nlink); 167 tab = "\t"; 168 } 169 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) { 170 LABEL; 171 (void)printf("%ssize (%qd, %qd)\n", 172 tab, s->st_size, p->fts_statp->st_size); 173 tab = "\t"; 174 } 175 /* 176 * XXX 177 * Catches nano-second differences, but doesn't display them. 178 */ 179 if ((s->flags & F_TIME) && 180 ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) || 181 (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) { 182 LABEL; 183 (void)printf("%smodification time (%.24s, ", 184 tab, ctime(&s->st_mtimespec.tv_sec)); 185 (void)printf("%.24s)\n", 186 ctime(&p->fts_statp->st_mtimespec.tv_sec)); 187 tab = "\t"; 188 } 189 if (s->flags & F_CKSUM) 190 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 191 LABEL; 192 (void)printf("%scksum: %s: %s\n", 193 tab, p->fts_accpath, strerror(errno)); 194 tab = "\t"; 195 } else if (crc(fd, &val, &len)) { 196 (void)close(fd); 197 LABEL; 198 (void)printf("%scksum: %s: %s\n", 199 tab, p->fts_accpath, strerror(errno)); 200 tab = "\t"; 201 } else { 202 (void)close(fd); 203 if (s->cksum != val) { 204 LABEL; 205 (void)printf("%scksum (%lu, %lu)\n", 206 tab, s->cksum, val); 207 } 208 tab = "\t"; 209 } 210 if (s->flags & F_MD5) { 211 char *new_digest, buf[33]; 212 213 new_digest = MD5File(p->fts_accpath,buf); 214 if (!new_digest) { 215 LABEL; 216 printf("%sMD5File: %s: %s\n", tab, p->fts_accpath, 217 strerror(errno)); 218 tab = "\t"; 219 } else if (strcmp(new_digest, s->md5digest)) { 220 LABEL; 221 printf("%sMD5 (%s, %s)\n", tab, s->md5digest, 222 new_digest); 223 tab = "\t"; 224 } 225 } 226 227 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 228 LABEL; 229 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 230 } 231 return (label); 232} 233 234char * 235inotype(type) 236 u_int type; 237{ 238 switch(type & S_IFMT) { 239 case S_IFBLK: 240 return ("block"); 241 case S_IFCHR: 242 return ("char"); 243 case S_IFDIR: 244 return ("dir"); 245 case S_IFIFO: 246 return ("fifo"); 247 case S_IFREG: 248 return ("file"); 249 case S_IFLNK: 250 return ("link"); 251 case S_IFSOCK: 252 return ("socket"); 253 default: 254 return ("unknown"); 255 } 256 /* NOTREACHED */ 257} 258 259static char * 260ftype(type) 261 u_int type; 262{ 263 switch(type) { 264 case F_BLOCK: 265 return ("block"); 266 case F_CHAR: 267 return ("char"); 268 case F_DIR: 269 return ("dir"); 270 case F_FIFO: 271 return ("fifo"); 272 case F_FILE: 273 return ("file"); 274 case F_LINK: 275 return ("link"); 276 case F_SOCK: 277 return ("socket"); 278 default: 279 return ("unknown"); 280 } 281 /* NOTREACHED */ 282} 283 284char * 285rlink(name) 286 char *name; 287{ 288 static char lbuf[MAXPATHLEN]; 289 register int len; 290 291 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 292 err(1, "line %d: %s", lineno, name); 293 lbuf[len] = '\0'; 294 return (lbuf); 295} 296