1 /* @(#)verify.c 8.1 (Berkeley) 6/6/93 */ 2 /* $NetBSD: verify.c,v 1.46 2015/01/23 20:28:24 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/stat.h> 35 36 #include <dirent.h> 37 #include <errno.h> 38 #include <fnmatch.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <unistd.h> 42 43 #include "extern.h" 44 45 static NODE *root; 46 static char path[MAXPATHLEN]; 47 48 static void miss(NODE *, char *); 49 static int vwalk(void); 50 51 int 52 verify(FILE *fi) 53 { 54 int rval; 55 56 root = spec(fi); 57 rval = vwalk(); 58 miss(root, path); 59 return (rval); 60 } 61 62 static int 63 vwalk(void) 64 { 65 FTS *t; 66 FTSENT *p; 67 NODE *ep, *level; 68 int specdepth, rval; 69 char *argv[2]; 70 char dot[] = "."; 71 argv[0] = dot; 72 argv[1] = NULL; 73 74 if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) 75 mtree_err("fts_open: %s", strerror(errno)); 76 level = root; 77 specdepth = rval = 0; 78 while ((p = fts_read(t)) != NULL) { 79 if (check_excludes(p->fts_name, p->fts_path)) { 80 fts_set(t, p, FTS_SKIP); 81 continue; 82 } 83 if (!find_only(p->fts_path)) { 84 fts_set(t, p, FTS_SKIP); 85 continue; 86 } 87 switch(p->fts_info) { 88 case FTS_D: 89 case FTS_SL: 90 break; 91 case FTS_DP: 92 if (specdepth > p->fts_level) { 93 for (level = level->parent; level->prev; 94 level = level->prev) 95 continue; 96 --specdepth; 97 } 98 continue; 99 case FTS_DNR: 100 case FTS_ERR: 101 case FTS_NS: 102 warnx("%s: %s", RP(p), strerror(p->fts_errno)); 103 continue; 104 default: 105 if (dflag) 106 continue; 107 } 108 109 if (specdepth != p->fts_level) 110 goto extra; 111 for (ep = level; ep; ep = ep->next) 112 if ((ep->flags & F_MAGIC && 113 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || 114 !strcmp(ep->name, p->fts_name)) { 115 ep->flags |= F_VISIT; 116 if ((ep->flags & F_NOCHANGE) == 0 && 117 compare(ep, p)) 118 rval = MISMATCHEXIT; 119 if (!(ep->flags & F_IGN) && 120 ep->type == F_DIR && 121 p->fts_info == FTS_D) { 122 if (ep->child) { 123 level = ep->child; 124 ++specdepth; 125 } 126 } else 127 fts_set(t, p, FTS_SKIP); 128 break; 129 } 130 131 if (ep) 132 continue; 133 extra: 134 if (!eflag && !(dflag && p->fts_info == FTS_SL)) { 135 printf("extra: %s", RP(p)); 136 if (rflag) { 137 #if HAVE_STRUCT_STAT_ST_FLAGS 138 if (rflag > 1 && 139 lchflags(p->fts_accpath, 0) == -1) 140 printf(" (chflags %s)", 141 strerror(errno)); 142 #endif 143 if ((S_ISDIR(p->fts_statp->st_mode) 144 ? rmdir : unlink)(p->fts_accpath)) { 145 printf(", not removed: %s", 146 strerror(errno)); 147 } else 148 printf(", removed"); 149 } 150 putchar('\n'); 151 } 152 fts_set(t, p, FTS_SKIP); 153 } 154 fts_close(t); 155 if (sflag) 156 warnx("%s checksum: %u", fullpath, crc_total); 157 return (rval); 158 } 159 160 static void 161 miss(NODE *p, char *tail) 162 { 163 int create; 164 char *tp; 165 const char *type; 166 #if HAVE_STRUCT_STAT_ST_FLAGS 167 u_int32_t flags; 168 #endif 169 170 for (; p; p = p->next) { 171 if (p->flags & F_OPT && !(p->flags & F_VISIT)) 172 continue; 173 if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) 174 continue; 175 strcpy(tail, p->name); 176 if (!(p->flags & F_VISIT)) { 177 /* Don't print missing message if file exists as a 178 symbolic link and the -q flag is set. */ 179 struct stat statbuf; 180 181 if (qflag && stat(path, &statbuf) == 0 && 182 S_ISDIR(statbuf.st_mode)) 183 p->flags |= F_VISIT; 184 else 185 (void)printf("%s missing", path); 186 } 187 switch (p->type) { 188 case F_BLOCK: 189 case F_CHAR: 190 type = "device"; 191 break; 192 case F_DIR: 193 type = "directory"; 194 break; 195 case F_LINK: 196 type = "symlink"; 197 break; 198 default: 199 putchar('\n'); 200 continue; 201 } 202 203 create = 0; 204 if (!(p->flags & F_VISIT) && uflag) { 205 if (mtree_Wflag || p->type == F_LINK) 206 goto createit; 207 if (!(p->flags & (F_UID | F_UNAME))) 208 printf( 209 " (%s not created: user not specified)", type); 210 else if (!(p->flags & (F_GID | F_GNAME))) 211 printf( 212 " (%s not created: group not specified)", type); 213 else if (!(p->flags & F_MODE)) 214 printf( 215 " (%s not created: mode not specified)", type); 216 else 217 createit: 218 switch (p->type) { 219 case F_BLOCK: 220 case F_CHAR: 221 if (mtree_Wflag) 222 continue; 223 if (!(p->flags & F_DEV)) 224 printf( 225 " (%s not created: device not specified)", 226 type); 227 else if (mknod(path, 228 p->st_mode | nodetoino(p->type), 229 p->st_rdev) == -1) 230 printf(" (%s not created: %s)\n", 231 type, strerror(errno)); 232 else 233 create = 1; 234 break; 235 case F_LINK: 236 if (!(p->flags & F_SLINK)) 237 printf( 238 " (%s not created: link not specified)\n", 239 type); 240 else if (symlink(p->slink, path)) 241 printf( 242 " (%s not created: %s)\n", 243 type, strerror(errno)); 244 else 245 create = 1; 246 break; 247 case F_DIR: 248 if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO)) 249 printf(" (not created: %s)", 250 strerror(errno)); 251 else 252 create = 1; 253 break; 254 default: 255 mtree_err("can't create create %s", 256 nodetype(p->type)); 257 } 258 } 259 if (create) 260 printf(" (created)"); 261 if (p->type == F_DIR) { 262 if (!(p->flags & F_VISIT)) 263 putchar('\n'); 264 for (tp = tail; *tp; ++tp) 265 continue; 266 *tp = '/'; 267 miss(p->child, tp + 1); 268 *tp = '\0'; 269 } else 270 putchar('\n'); 271 272 if (!create || mtree_Wflag) 273 continue; 274 if ((p->flags & (F_UID | F_UNAME)) && 275 (p->flags & (F_GID | F_GNAME)) && 276 (lchown(path, p->st_uid, p->st_gid))) { 277 printf("%s: user/group/mode not modified: %s\n", 278 path, strerror(errno)); 279 printf("%s: warning: file mode %snot set\n", path, 280 (p->flags & F_FLAGS) ? "and file flags " : ""); 281 continue; 282 } 283 if (p->flags & F_MODE) { 284 if (lchmod(path, p->st_mode)) 285 printf("%s: permissions not set: %s\n", 286 path, strerror(errno)); 287 } 288 #if HAVE_STRUCT_STAT_ST_FLAGS 289 if ((p->flags & F_FLAGS) && p->st_flags) { 290 if (iflag) 291 flags = p->st_flags; 292 else 293 flags = p->st_flags & ~SP_FLGS; 294 if (lchflags(path, flags)) 295 printf("%s: file flags not set: %s\n", 296 path, strerror(errno)); 297 } 298 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 299 } 300 } 301