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