verify.c revision 121299
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#if 0 35#ifndef lint 36static char sccsid[] = "@(#)verify.c 8.1 (Berkeley) 6/6/93"; 37#endif /* not lint */ 38#endif 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/usr.sbin/mtree/verify.c 121299 2003-10-21 07:58:52Z phk $"); 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 int ftsoptions; 55extern int dflag, eflag, qflag, rflag, sflag, uflag; 56extern char fullpath[MAXPATHLEN]; 57extern int lineno; 58 59static NODE *root; 60static char path[MAXPATHLEN]; 61 62static void miss(NODE *, char *); 63static int vwalk(void); 64 65int 66verify(void) 67{ 68 int rval; 69 70 root = spec(); 71 rval = vwalk(); 72 miss(root, path); 73 return (rval); 74} 75 76static int 77vwalk(void) 78{ 79 FTS *t; 80 FTSENT *p; 81 NODE *ep, *level; 82 int specdepth, rval; 83 char *argv[2]; 84 char dot[] = "."; 85 86 argv[0] = dot; 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 if (check_excludes(p->fts_name, p->fts_path)) { 94 fts_set(t, p, FTS_SKIP); 95 continue; 96 } 97 switch(p->fts_info) { 98 case FTS_D: 99 case FTS_SL: 100 break; 101 case FTS_DP: 102 if (specdepth > p->fts_level) { 103 for (level = level->parent; level->prev; 104 level = level->prev); 105 --specdepth; 106 } 107 continue; 108 case FTS_DNR: 109 case FTS_ERR: 110 case FTS_NS: 111 warnx("%s: %s", RP(p), strerror(p->fts_errno)); 112 continue; 113 default: 114 if (dflag) 115 continue; 116 } 117 118 if (specdepth != p->fts_level) 119 goto extra; 120 for (ep = level; ep; ep = ep->next) 121 if ((ep->flags & F_MAGIC && 122 !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) || 123 !strcmp(ep->name, p->fts_name)) { 124 ep->flags |= F_VISIT; 125 if ((ep->flags & F_NOCHANGE) == 0 && 126 compare(ep->name, ep, p)) 127 rval = MISMATCHEXIT; 128 if (ep->flags & F_IGN) 129 (void)fts_set(t, p, FTS_SKIP); 130 else if (ep->child && ep->type == F_DIR && 131 p->fts_info == FTS_D) { 132 level = ep->child; 133 ++specdepth; 134 } 135 break; 136 } 137 138 if (ep) 139 continue; 140extra: 141 if (!eflag) { 142 (void)printf("%s extra", RP(p)); 143 if (rflag) { 144 if ((S_ISDIR(p->fts_statp->st_mode) 145 ? rmdir : unlink)(p->fts_accpath)) { 146 (void)printf(", not removed: %s", 147 strerror(errno)); 148 } else 149 (void)printf(", removed"); 150 } 151 (void)putchar('\n'); 152 } 153 (void)fts_set(t, p, FTS_SKIP); 154 } 155 (void)fts_close(t); 156 if (sflag) 157 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); 158 return (rval); 159} 160 161static void 162miss(NODE *p, char *tail) 163{ 164 int create; 165 char *tp; 166 const char *type; 167 168 for (; p; p = p->next) { 169 if (p->type != F_DIR && (dflag || p->flags & F_VISIT)) 170 continue; 171 (void)strcpy(tail, p->name); 172 if (!(p->flags & F_VISIT)) { 173 /* Don't print missing message if file exists as a 174 symbolic link and the -q flag is set. */ 175 struct stat statbuf; 176 177 if (qflag && stat(path, &statbuf) == 0) 178 p->flags |= F_VISIT; 179 else 180 (void)printf("%s missing", path); 181 } 182 if (p->type != F_DIR && p->type != F_LINK) { 183 putchar('\n'); 184 continue; 185 } 186 187 create = 0; 188 if (p->type == F_LINK) 189 type = "symlink"; 190 else 191 type = "directory"; 192 if (!(p->flags & F_VISIT) && uflag) { 193 if (!(p->flags & (F_UID | F_UNAME))) 194 (void)printf(" (%s not created: user not specified)", type); 195 else if (!(p->flags & (F_GID | F_GNAME))) 196 (void)printf(" (%s not created: group not specified)", type); 197 else if (p->type == F_LINK) { 198 if (symlink(p->slink, path)) 199 (void)printf(" (symlink not created: %s)\n", 200 strerror(errno)); 201 else 202 (void)printf(" (created)\n"); 203 if (lchown(path, p->st_uid, p->st_gid)) 204 (void)printf("%s: user/group not modified: %s\n", 205 path, strerror(errno)); 206 continue; 207 } else if (!(p->flags & F_MODE)) 208 (void)printf(" (directory not created: mode not specified)"); 209 else if (mkdir(path, S_IRWXU)) 210 (void)printf(" (directory not created: %s)", 211 strerror(errno)); 212 else { 213 create = 1; 214 (void)printf(" (created)"); 215 } 216 } 217 if (!(p->flags & F_VISIT)) 218 (void)putchar('\n'); 219 220 for (tp = tail; *tp; ++tp); 221 *tp = '/'; 222 miss(p->child, tp + 1); 223 *tp = '\0'; 224 225 if (!create) 226 continue; 227 if (chown(path, p->st_uid, p->st_gid)) { 228 (void)printf("%s: user/group/mode not modified: %s\n", 229 path, strerror(errno)); 230 (void)printf("%s: warning: file mode %snot set\n", path, 231 (p->flags & F_FLAGS) ? "and file flags " : ""); 232 continue; 233 } 234 if (chmod(path, p->st_mode)) 235 (void)printf("%s: permissions not set: %s\n", 236 path, strerror(errno)); 237 if ((p->flags & F_FLAGS) && p->st_flags && 238 chflags(path, p->st_flags)) 239 (void)printf("%s: file flags not set: %s\n", 240 path, strerror(errno)); 241 } 242} 243