compare.c revision 9490
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 35static char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93"; 36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <fcntl.h> 41#include <fts.h> 42#include <errno.h> 43#include <stdio.h> 44#include <time.h> 45#include <unistd.h> 46#include <md5.h> 47#include "mtree.h" 48#include "extern.h" 49 50extern int uflag; 51 52static char *ftype __P((u_int)); 53 54#define INDENTNAMELEN 8 55#define LABEL \ 56 if (!label++) { \ 57 len = printf("%s: ", RP(p)); \ 58 if (len > INDENTNAMELEN) { \ 59 tab = "\t"; \ 60 (void)printf("\n"); \ 61 } else { \ 62 tab = ""; \ 63 (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \ 64 } \ 65 } 66 67int 68compare(name, s, p) 69 char *name; 70 register NODE *s; 71 register FTSENT *p; 72{ 73 extern int uflag; 74 u_long len, val; 75 int fd, label; 76 char *cp, *tab = ""; 77 78 label = 0; 79 switch(s->type) { 80 case F_BLOCK: 81 if (!S_ISBLK(p->fts_statp->st_mode)) 82 goto typeerr; 83 break; 84 case F_CHAR: 85 if (!S_ISCHR(p->fts_statp->st_mode)) 86 goto typeerr; 87 break; 88 case F_DIR: 89 if (!S_ISDIR(p->fts_statp->st_mode)) 90 goto typeerr; 91 break; 92 case F_FIFO: 93 if (!S_ISFIFO(p->fts_statp->st_mode)) 94 goto typeerr; 95 break; 96 case F_FILE: 97 if (!S_ISREG(p->fts_statp->st_mode)) 98 goto typeerr; 99 break; 100 case F_LINK: 101 if (!S_ISLNK(p->fts_statp->st_mode)) 102 goto typeerr; 103 break; 104 case F_SOCK: 105 if (!S_ISSOCK(p->fts_statp->st_mode)) { 106typeerr: LABEL; 107 (void)printf("\ttype (%s, %s)\n", 108 ftype(s->type), inotype(p->fts_statp->st_mode)); 109 } 110 break; 111 } 112 /* Set the uid/gid first, then set the mode. */ 113 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) { 114 LABEL; 115 (void)printf("%suser (%lu, %lu", 116 tab, s->st_uid, p->fts_statp->st_uid); 117 if (uflag) 118 if (chown(p->fts_accpath, s->st_uid, -1)) 119 (void)printf(", not modified: %s)\n", 120 strerror(errno)); 121 else 122 (void)printf(", modified)\n"); 123 else 124 (void)printf(")\n"); 125 tab = "\t"; 126 } 127 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) { 128 LABEL; 129 (void)printf("%sgid (%lu, %lu", 130 tab, s->st_gid, p->fts_statp->st_gid); 131 if (uflag) 132 if (chown(p->fts_accpath, -1, s->st_gid)) 133 (void)printf(", not modified: %s)\n", 134 strerror(errno)); 135 else 136 (void)printf(", modified)\n"); 137 else 138 (void)printf(")\n"); 139 tab = "\t"; 140 } 141 if (s->flags & F_MODE && 142 s->st_mode != (p->fts_statp->st_mode & MBITS)) { 143 LABEL; 144 (void)printf("%spermissions (%#o, %#o", 145 tab, s->st_mode, p->fts_statp->st_mode & MBITS); 146 if (uflag) 147 if (chmod(p->fts_accpath, s->st_mode)) 148 (void)printf(", not modified: %s)\n", 149 strerror(errno)); 150 else 151 (void)printf(", modified)\n"); 152 else 153 (void)printf(")\n"); 154 tab = "\t"; 155 } 156 if (s->flags & F_NLINK && s->type != F_DIR && 157 s->st_nlink != p->fts_statp->st_nlink) { 158 LABEL; 159 (void)printf("%slink count (%u, %u)\n", 160 tab, s->st_nlink, p->fts_statp->st_nlink); 161 tab = "\t"; 162 } 163 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) { 164 LABEL; 165 (void)printf("%ssize (%qd, %qd)\n", 166 tab, s->st_size, p->fts_statp->st_size); 167 tab = "\t"; 168 } 169 /* 170 * XXX 171 * Catches nano-second differences, but doesn't display them. 172 */ 173 if ((s->flags & F_TIME) && 174 ((s->st_mtimespec.ts_sec != p->fts_statp->st_mtimespec.ts_sec) || 175 (s->st_mtimespec.ts_nsec != p->fts_statp->st_mtimespec.ts_nsec))) { 176 LABEL; 177 (void)printf("%smodification time (%.24s, ", 178 tab, ctime(&s->st_mtimespec.ts_sec)); 179 (void)printf("%.24s)\n", 180 ctime(&p->fts_statp->st_mtimespec.ts_sec)); 181 tab = "\t"; 182 } 183 if (s->flags & F_CKSUM) 184 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 185 LABEL; 186 (void)printf("%scksum: %s: %s\n", 187 tab, p->fts_accpath, strerror(errno)); 188 tab = "\t"; 189 } else if (crc(fd, &val, &len)) { 190 (void)close(fd); 191 LABEL; 192 (void)printf("%scksum: %s: %s\n", 193 tab, p->fts_accpath, strerror(errno)); 194 tab = "\t"; 195 } else { 196 (void)close(fd); 197 if (s->cksum != val) { 198 LABEL; 199 (void)printf("%scksum (%lu, %lu)\n", 200 tab, s->cksum, val); 201 } 202 tab = "\t"; 203 } 204 if (s->flags & F_MD5) { 205 char *new_digest, buf[33]; 206 207 new_digest = MD5File(p->fts_accpath,buf); 208 if (!new_digest) { 209 LABEL; 210 printf("%sMD5File: %s: %s\n", tab, p->fts_accpath, 211 strerror(errno)); 212 tab = "\t"; 213 } else if (strcmp(new_digest, s->md5digest)) { 214 LABEL; 215 printf("%sMD5 (%s, %s)\n", tab, s->md5digest, 216 new_digest); 217 tab = "\t"; 218 } 219 } 220 221 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 222 LABEL; 223 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 224 } 225 return (label); 226} 227 228char * 229inotype(type) 230 u_int type; 231{ 232 switch(type & S_IFMT) { 233 case S_IFBLK: 234 return ("block"); 235 case S_IFCHR: 236 return ("char"); 237 case S_IFDIR: 238 return ("dir"); 239 case S_IFIFO: 240 return ("fifo"); 241 case S_IFREG: 242 return ("file"); 243 case S_IFLNK: 244 return ("link"); 245 case S_IFSOCK: 246 return ("socket"); 247 default: 248 return ("unknown"); 249 } 250 /* NOTREACHED */ 251} 252 253static char * 254ftype(type) 255 u_int type; 256{ 257 switch(type) { 258 case F_BLOCK: 259 return ("block"); 260 case F_CHAR: 261 return ("char"); 262 case F_DIR: 263 return ("dir"); 264 case F_FIFO: 265 return ("fifo"); 266 case F_FILE: 267 return ("file"); 268 case F_LINK: 269 return ("link"); 270 case F_SOCK: 271 return ("socket"); 272 default: 273 return ("unknown"); 274 } 275 /* NOTREACHED */ 276} 277 278char * 279rlink(name) 280 char *name; 281{ 282 static char lbuf[MAXPATHLEN]; 283 register int len; 284 285 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 286 err("%s: %s", name, strerror(errno)); 287 lbuf[len] = '\0'; 288 return (lbuf); 289} 290