1 /* $NetBSD: compare.c,v 1.13 1997/10/17 11:46:30 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef 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.13 1997/10/17 11:46:30 lukem Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 #include <fcntl.h> 48 #include <fts.h> 49 #include <errno.h> 50 #include <stdio.h> 51 #include <time.h> 52 #include <unistd.h> 53 #include "mtree.h" 54 #include "extern.h" 55 56 extern int tflag, uflag; 57 58 static 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 73 int 74 compare(name, s, p) 75 char *name; 76 NODE *s; 77 FTSENT *p; 78 { 79 u_long len, val; 80 int fd, label; 81 char *cp, *tab; 82 83 tab = NULL; 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)) { 112 typeerr: 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 (%u, %u", 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 (%u, %u", 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 * Since utimes(2) only takes a timeval, there's no point in 178 * comparing the low bits of the timespec nanosecond field. This 179 * will only result in mismatches that we can never fix. 180 * 181 * Doesn't display microsecond differences. 182 */ 183 if (s->flags & F_TIME) { 184 struct timeval tv[2]; 185 186 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec); 187 TIMESPEC_TO_TIMEVAL(&tv[1], &p->fts_statp->st_mtimespec); 188 if (tv[0].tv_sec != tv[1].tv_sec || 189 tv[0].tv_usec != tv[1].tv_usec) { 190 LABEL; 191 (void)printf("%smodification time (%.24s, ", 192 tab, ctime(&s->st_mtimespec.tv_sec)); 193 (void)printf("%.24s", 194 ctime(&p->fts_statp->st_mtimespec.tv_sec)); 195 if (tflag) { 196 tv[1] = tv[0]; 197 if (utimes(p->fts_accpath, tv)) 198 (void)printf(", not modified: %s)\n", 199 strerror(errno)); 200 else 201 (void)printf(", modified)\n"); 202 } else 203 (void)printf(")\n"); 204 tab = "\t"; 205 } 206 } 207 if (s->flags & F_CKSUM) 208 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 209 LABEL; 210 (void)printf("%scksum: %s: %s\n", 211 tab, p->fts_accpath, strerror(errno)); 212 tab = "\t"; 213 } else if (crc(fd, &val, &len)) { 214 (void)close(fd); 215 LABEL; 216 (void)printf("%scksum: %s: %s\n", 217 tab, p->fts_accpath, strerror(errno)); 218 tab = "\t"; 219 } else { 220 (void)close(fd); 221 if (s->cksum != val) { 222 LABEL; 223 (void)printf("%scksum (%lu, %lu)\n", 224 tab, s->cksum, val); 225 } 226 tab = "\t"; 227 } 228 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 229 LABEL; 230 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 231 } 232 return (label); 233 } 234 235 char * 236 inotype(type) 237 u_int type; 238 { 239 switch(type & S_IFMT) { 240 case S_IFBLK: 241 return ("block"); 242 case S_IFCHR: 243 return ("char"); 244 case S_IFDIR: 245 return ("dir"); 246 case S_IFIFO: 247 return ("fifo"); 248 case S_IFREG: 249 return ("file"); 250 case S_IFLNK: 251 return ("link"); 252 case S_IFSOCK: 253 return ("socket"); 254 default: 255 return ("unknown"); 256 } 257 /* NOTREACHED */ 258 } 259 260 static char * 261 ftype(type) 262 u_int type; 263 { 264 switch(type) { 265 case F_BLOCK: 266 return ("block"); 267 case F_CHAR: 268 return ("char"); 269 case F_DIR: 270 return ("dir"); 271 case F_FIFO: 272 return ("fifo"); 273 case F_FILE: 274 return ("file"); 275 case F_LINK: 276 return ("link"); 277 case F_SOCK: 278 return ("socket"); 279 default: 280 return ("unknown"); 281 } 282 /* NOTREACHED */ 283 } 284 285 char * 286 rlink(name) 287 char *name; 288 { 289 static char lbuf[MAXPATHLEN]; 290 int len; 291 292 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 293 err("%s: %s", name, strerror(errno)); 294 lbuf[len] = '\0'; 295 return (lbuf); 296 } 297