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