verify.c revision 30027
1/*- 2 * Copyright (c) 1990, 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[] = "@(#)verify.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 <dirent.h> 45#include <err.h> 46#include <errno.h> 47#include <fts.h> 48#include <fnmatch.h> 49#include <stdio.h> 50#include <unistd.h> 51#include "mtree.h" 52#include "extern.h" 53 54extern long int crc_total; 55extern int ftsoptions; 56extern int dflag, eflag, rflag, sflag, uflag; 57extern char fullpath[MAXPATHLEN]; 58extern int lineno; 59 60static NODE *root; 61static char path[MAXPATHLEN]; 62 63static void miss __P((NODE *, char *)); 64static int vwalk __P((void)); 65 66int 67verify() 68{ 69 int rval; 70 71 root = spec(); 72 rval = vwalk(); 73 miss(root, path); 74 return (rval); 75} 76 77static int 78vwalk() 79{ 80 register FTS *t; 81 register FTSENT *p; 82 register NODE *ep, *level; 83 int specdepth, rval; 84 char *argv[2]; 85 86 argv[0] = "."; 87 argv[1] = NULL; 88 if ((t = fts_open(argv, ftsoptions, NULL)) == NULL) 89 err(1, "line %d: fts_open", lineno); 90 level = root; 91 specdepth = rval = 0; 92 while ((p = fts_read(t))) { 93 switch(p->fts_info) { 94 case FTS_D: 95 break; 96 case FTS_DP: 97 if (specdepth > p->fts_level) { 98 for (level = level->parent; level->prev; 99 level = level->prev); 100 --specdepth; 101 } 102 continue; 103 case FTS_DNR: 104 case FTS_ERR: 105 case FTS_NS: 106 warnx("%s: %s", RP(p), strerror(p->fts_errno)); 107 continue; 108 default: 109 if (dflag) 110 continue; 111 } 112 113 if (specdepth != p->fts_level) 114 goto extra; 115 for (ep = level; ep; ep = ep->next) 116 if ((ep->flags & F_MAGIC && 117 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || 118 !strcmp(ep->name, p->fts_name)) { 119 ep->flags |= F_VISIT; 120 if (compare(ep->name, ep, p)) 121 rval = MISMATCHEXIT; 122 if (ep->flags & F_IGN) 123 (void)fts_set(t, p, FTS_SKIP); 124 else if (ep->child && ep->type == F_DIR && 125 p->fts_info == FTS_D) { 126 level = ep->child; 127 ++specdepth; 128 } 129 break; 130 } 131 132 if (ep) 133 continue; 134extra: 135 if (!eflag) { 136 (void)printf("extra: %s", RP(p)); 137 if (rflag) { 138 if ((S_ISDIR(p->fts_statp->st_mode) 139 ? rmdir : unlink)(p->fts_accpath)) { 140 (void)printf(", not removed: %s", 141 strerror(errno)); 142 } else 143 (void)printf(", removed"); 144 } 145 (void)putchar('\n'); 146 } 147 (void)fts_set(t, p, FTS_SKIP); 148 } 149 (void)fts_close(t); 150 if (sflag) 151 warnx("%s checksum: %lu", fullpath, crc_total); 152 return (rval); 153} 154 155static void 156miss(p, tail) 157 register NODE *p; 158 register char *tail; 159{ 160 register int create; 161 register char *tp; 162 163 for (; p; p = p->next) { 164 if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) 165 continue; 166 (void)strcpy(tail, p->name); 167 if (!(p->flags & F_VISIT)) 168 (void)printf("missing: %s", path); 169 if (p->type != F_DIR) { 170 putchar('\n'); 171 continue; 172 } 173 174 create = 0; 175 if (!(p->flags & F_VISIT) && uflag) 176 if (!(p->flags & (F_UID | F_UNAME))) 177 (void)printf(" (not created: user not specified)"); 178 else if (!(p->flags & (F_GID | F_GNAME))) 179 (void)printf(" (not created: group not specified)"); 180 else if (!(p->flags & F_MODE)) 181 (void)printf(" (not created: mode not specified)"); 182 else if (mkdir(path, S_IRWXU)) 183 (void)printf(" (not created: %s)", 184 strerror(errno)); 185 else { 186 create = 1; 187 (void)printf(" (created)"); 188 } 189 190 if (!(p->flags & F_VISIT)) 191 (void)putchar('\n'); 192 193 for (tp = tail; *tp; ++tp); 194 *tp = '/'; 195 miss(p->child, tp + 1); 196 *tp = '\0'; 197 198 if (!create) 199 continue; 200 if (chown(path, p->st_uid, p->st_gid)) { 201 (void)printf("%s: user/group/mode not modified: %s\n", 202 path, strerror(errno)); 203 continue; 204 } 205 if (chmod(path, p->st_mode)) 206 (void)printf("%s: permissions not set: %s\n", 207 path, strerror(errno)); 208 } 209} 210