1 /*- 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)compare.c 5.13 (Berkeley) 09/01/92"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #include <fts.h> 16 #include <errno.h> 17 #include <stdio.h> 18 #include <time.h> 19 #include <unistd.h> 20 #include "mtree.h" 21 #include "extern.h" 22 23 extern int uflag; 24 25 static char *ftype __P((u_int)); 26 27 #define INDENTNAMELEN 8 28 #define LABEL \ 29 if (!label++) { \ 30 len = printf("%s: ", RP(p)); \ 31 if (len > INDENTNAMELEN) { \ 32 tab = "\t"; \ 33 (void)printf("\n"); \ 34 } else { \ 35 tab = ""; \ 36 (void)printf("%*s", INDENTNAMELEN - len, ""); \ 37 } \ 38 } 39 40 int 41 compare(name, s, p) 42 char *name; 43 register NODE *s; 44 register FTSENT *p; 45 { 46 extern int uflag; 47 u_long len, val; 48 int fd, label; 49 char *cp, *tab; 50 51 label = 0; 52 switch(s->type) { 53 case F_BLOCK: 54 if (!S_ISBLK(p->fts_statp->st_mode)) 55 goto typeerr; 56 break; 57 case F_CHAR: 58 if (!S_ISCHR(p->fts_statp->st_mode)) 59 goto typeerr; 60 break; 61 case F_DIR: 62 if (!S_ISDIR(p->fts_statp->st_mode)) 63 goto typeerr; 64 break; 65 case F_FIFO: 66 if (!S_ISFIFO(p->fts_statp->st_mode)) 67 goto typeerr; 68 break; 69 case F_FILE: 70 if (!S_ISREG(p->fts_statp->st_mode)) 71 goto typeerr; 72 break; 73 case F_LINK: 74 if (!S_ISLNK(p->fts_statp->st_mode)) 75 goto typeerr; 76 break; 77 case F_SOCK: 78 if (!S_ISSOCK(p->fts_statp->st_mode)) { 79 typeerr: LABEL; 80 (void)printf("\ttype (%s, %s)\n", 81 ftype(s->type), inotype(p->fts_statp->st_mode)); 82 } 83 break; 84 } 85 /* Set the uid/gid first, then set the mode. */ 86 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) { 87 LABEL; 88 (void)printf("%suser (%u, %u", 89 tab, s->st_uid, p->fts_statp->st_uid); 90 if (uflag) 91 if (chown(p->fts_accpath, s->st_uid, -1)) 92 (void)printf(", not modified: %s)\n", 93 strerror(errno)); 94 else 95 (void)printf(", modified)\n"); 96 else 97 (void)printf(")\n"); 98 tab = "\t"; 99 } 100 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) { 101 LABEL; 102 (void)printf("%sgid (%u, %u", 103 tab, s->st_gid, p->fts_statp->st_gid); 104 if (uflag) 105 if (chown(p->fts_accpath, -1, s->st_gid)) 106 (void)printf(", not modified: %s)\n", 107 strerror(errno)); 108 else 109 (void)printf(", modified)\n"); 110 else 111 (void)printf(")\n"); 112 tab = "\t"; 113 } 114 if (s->flags & F_MODE && 115 s->st_mode != (p->fts_statp->st_mode & MBITS)) { 116 LABEL; 117 (void)printf("%spermissions (%#o, %#o", 118 tab, s->st_mode, p->fts_statp->st_mode & MBITS); 119 if (uflag) 120 if (chmod(p->fts_accpath, s->st_mode)) 121 (void)printf(", not modified: %s)\n", 122 strerror(errno)); 123 else 124 (void)printf(", modified)\n"); 125 else 126 (void)printf(")\n"); 127 tab = "\t"; 128 } 129 if (s->flags & F_NLINK && s->type != F_DIR && 130 s->st_nlink != p->fts_statp->st_nlink) { 131 LABEL; 132 (void)printf("%slink count (%u, %u)\n", 133 tab, s->st_nlink, p->fts_statp->st_nlink); 134 tab = "\t"; 135 } 136 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) { 137 LABEL; 138 (void)printf("%ssize (%qd, %qd)\n", 139 tab, s->st_size, p->fts_statp->st_size); 140 tab = "\t"; 141 } 142 /* 143 * XXX 144 * Catches nano-second differences, but doesn't display them. 145 */ 146 if (s->flags & F_TIME && 147 s->st_mtimespec.ts_sec != p->fts_statp->st_mtimespec.ts_sec || 148 s->st_mtimespec.ts_nsec != p->fts_statp->st_mtimespec.ts_nsec) { 149 LABEL; 150 (void)printf("%smodification time (%.24s, ", 151 tab, ctime(&s->st_mtimespec.ts_sec)); 152 (void)printf("%.24s)\n", 153 ctime(&p->fts_statp->st_mtimespec.ts_sec)); 154 tab = "\t"; 155 } 156 if (s->flags & F_CKSUM) 157 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) { 158 LABEL; 159 (void)printf("%scksum: %s: %s\n", 160 tab, p->fts_accpath, strerror(errno)); 161 tab = "\t"; 162 } else if (crc(fd, &val, &len)) { 163 (void)close(fd); 164 LABEL; 165 (void)printf("%scksum: %s: %s\n", 166 tab, p->fts_accpath, strerror(errno)); 167 tab = "\t"; 168 } else { 169 (void)close(fd); 170 if (s->cksum != val) { 171 LABEL; 172 (void)printf("%scksum (%lu, %lu)\n", 173 tab, s->cksum, val); 174 } 175 tab = "\t"; 176 } 177 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) { 178 LABEL; 179 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink); 180 } 181 return (label); 182 } 183 184 char * 185 inotype(type) 186 u_int type; 187 { 188 switch(type & S_IFMT) { 189 case S_IFBLK: 190 return ("block"); 191 case S_IFCHR: 192 return ("char"); 193 case S_IFDIR: 194 return ("dir"); 195 case S_IFIFO: 196 return ("fifo"); 197 case S_IFREG: 198 return ("file"); 199 case S_IFLNK: 200 return ("link"); 201 case S_IFSOCK: 202 return ("socket"); 203 default: 204 return ("unknown"); 205 } 206 /* NOTREACHED */ 207 } 208 209 static char * 210 ftype(type) 211 u_int type; 212 { 213 switch(type) { 214 case F_BLOCK: 215 return ("block"); 216 case F_CHAR: 217 return ("char"); 218 case F_DIR: 219 return ("dir"); 220 case F_FIFO: 221 return ("fifo"); 222 case F_FILE: 223 return ("file"); 224 case F_LINK: 225 return ("link"); 226 case F_SOCK: 227 return ("socket"); 228 default: 229 return ("unknown"); 230 } 231 /* NOTREACHED */ 232 } 233 234 char * 235 rlink(name) 236 char *name; 237 { 238 static char lbuf[MAXPATHLEN]; 239 register int len; 240 241 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1) 242 err("%s: %s", name, strerror(errno)); 243 lbuf[len] = '\0'; 244 return (lbuf); 245 } 246