Home | History | Annotate | Line # | Download | only in mtree
      1 /*	$NetBSD: verify.c,v 1.50 2024/12/11 14:52:26 christos 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 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     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.50 2024/12/11 14:52:26 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 
     48 #if ! HAVE_NBTOOL_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(FILE *fi)
     68 {
     69 	int rval;
     70 
     71 	root = spec(fi);
     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, dcmp)) == 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 		if (!find_only(p->fts_path)) {
     99 			fts_set(t, p, FTS_SKIP);
    100 			continue;
    101 		}
    102 		switch(p->fts_info) {
    103 		case FTS_D:
    104 		case FTS_SL:
    105 			break;
    106 		case FTS_DP:
    107 			if (specdepth > p->fts_level) {
    108 				for (level = level->parent; level->prev;
    109 				    level = level->prev)
    110 					continue;
    111 				--specdepth;
    112 			}
    113 			continue;
    114 		case FTS_DNR:
    115 		case FTS_ERR:
    116 		case FTS_NS:
    117 			warnx("%s: %s", RP(p), strerror(p->fts_errno));
    118 			continue;
    119 		default:
    120 			if (dflag)
    121 				continue;
    122 		}
    123 
    124 		if (specdepth != p->fts_level)
    125 			goto extra;
    126 		for (ep = level; ep; ep = ep->next)
    127 			if ((ep->flags & F_MAGIC &&
    128 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
    129 			    !strcmp(ep->name, p->fts_name)) {
    130 				ep->flags |= F_VISIT;
    131 				if ((ep->flags & F_NOCHANGE) == 0 &&
    132 				    compare(ep, p))
    133 					rval = MISMATCHEXIT;
    134 				if (!(ep->flags & F_IGN) &&
    135 				    ep->type == F_DIR &&
    136 				    p->fts_info == FTS_D) {
    137 					if (ep->child) {
    138 						level = ep->child;
    139 						++specdepth;
    140 					}
    141 				} else
    142 					fts_set(t, p, FTS_SKIP);
    143 				break;
    144 			}
    145 
    146 		if (ep)
    147 			continue;
    148  extra:
    149 		if (!eflag && !(dflag && p->fts_info == FTS_SL)) {
    150 			printf(flavor == F_FREEBSD9 ? "%s extra" : "extra: %s",
    151 			    RP(p));
    152 			if (rflag) {
    153 #if HAVE_STRUCT_STAT_ST_FLAGS
    154 				if (rflag > 1 &&
    155 				    lchflags(p->fts_accpath, 0) == -1)
    156 					printf(" (chflags %s)",
    157 					    strerror(errno));
    158 #endif
    159 				if ((S_ISDIR(p->fts_statp->st_mode)
    160 				    ? rmdir : unlink)(p->fts_accpath)) {
    161 					printf(", not removed: %s",
    162 					    strerror(errno));
    163 				} else
    164 					printf(", removed");
    165 			}
    166 			putchar('\n');
    167 		}
    168 		fts_set(t, p, FTS_SKIP);
    169 	}
    170 	if (errno != 0)
    171 		mtree_err("fts_read: %s", strerror(errno));
    172 	fts_close(t);
    173 	if (sflag)
    174 		warnx("%s checksum: %u", fullpath, crc_total);
    175 	return (rval);
    176 }
    177 
    178 static void
    179 miss(NODE *p, char *tail)
    180 {
    181 	int create;
    182 	char *tp;
    183 	const char *type;
    184 	u_long flags;
    185 
    186 	for (; p; p = p->next) {
    187 		if (p->flags & F_OPT && !(p->flags & F_VISIT))
    188 			continue;
    189 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
    190 			continue;
    191 		strcpy(tail, p->name);
    192 		if (!(p->flags & F_VISIT)) {
    193 			/* Don't print missing message if file exists as a
    194 			   symbolic link and the -q flag is set. */
    195 			struct stat statbuf;
    196 
    197 			if (qflag && stat(path, &statbuf) == 0 &&
    198 			    S_ISDIR(statbuf.st_mode))
    199 				p->flags |= F_VISIT;
    200 			else
    201 				(void)printf("%s missing", path);
    202 		}
    203 		switch (p->type) {
    204 		case F_BLOCK:
    205 		case F_CHAR:
    206 			type = "device";
    207 			break;
    208 		case F_DIR:
    209 			type = "directory";
    210 			break;
    211 		case F_LINK:
    212 			type = "symlink";
    213 			break;
    214 		default:
    215 			putchar('\n');
    216 			continue;
    217 		}
    218 
    219 		create = 0;
    220 		if (!(p->flags & F_VISIT) && uflag) {
    221 			if (mtree_Wflag || p->type == F_LINK)
    222 				goto createit;
    223 			if (!(p->flags & (F_UID | F_UNAME)))
    224 			    printf(
    225 				" (%s not created: user not specified)", type);
    226 			else if (!(p->flags & (F_GID | F_GNAME)))
    227 			    printf(
    228 				" (%s not created: group not specified)", type);
    229 			else if (!(p->flags & F_MODE))
    230 			    printf(
    231 				" (%s not created: mode not specified)", type);
    232 			else
    233  createit:
    234 			switch (p->type) {
    235 			case F_BLOCK:
    236 			case F_CHAR:
    237 				if (mtree_Wflag)
    238 					continue;
    239 				if (!(p->flags & F_DEV))
    240 					printf(
    241 				    " (%s not created: device not specified)",
    242 					    type);
    243 				else if (mknod(path,
    244 				    p->st_mode | nodetoino(p->type),
    245 				    p->st_rdev) == -1)
    246 					printf(" (%s not created: %s)\n",
    247 					    type, strerror(errno));
    248 				else
    249 					create = 1;
    250 				break;
    251 			case F_LINK:
    252 				if (!(p->flags & F_SLINK))
    253 					printf(
    254 				    " (%s not created: link not specified)\n",
    255 					    type);
    256 				else if (symlink(p->slink, path))
    257 					printf(
    258 					    " (%s not created: %s)\n",
    259 					    type, strerror(errno));
    260 				else
    261 					create = 1;
    262 				break;
    263 			case F_DIR:
    264 				if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO))
    265 					printf(" (not created: %s)",
    266 					    strerror(errno));
    267 				else
    268 					create = 1;
    269 				break;
    270 			default:
    271 				mtree_err("can't create create %s",
    272 				    nodetype(p->type));
    273 			}
    274 		}
    275 		if (create)
    276 			printf(" (created)");
    277 		if (p->type == F_DIR) {
    278 			if (!(p->flags & F_VISIT))
    279 				putchar('\n');
    280 			for (tp = tail; *tp; ++tp)
    281 				continue;
    282 			*tp = '/';
    283 			miss(p->child, tp + 1);
    284 			*tp = '\0';
    285 		} else
    286 			putchar('\n');
    287 
    288 		if (!create || mtree_Wflag)
    289 			continue;
    290 		if ((p->flags & (F_UID | F_UNAME)) &&
    291 		    (p->flags & (F_GID | F_GNAME)) &&
    292 		    (lchown(path, p->st_uid, p->st_gid))) {
    293 			printf("%s: user/group/mode not modified: %s\n",
    294 			    path, strerror(errno));
    295 			printf("%s: warning: file mode %snot set\n", path,
    296 			    (p->flags & F_FLAGS) ? "and file flags " : "");
    297 			continue;
    298 		}
    299 		if (p->flags & F_MODE) {
    300 			if (lchmod(path, p->st_mode))
    301 				printf("%s: permissions not set: %s\n",
    302 				    path, strerror(errno));
    303 		}
    304 #if HAVE_STRUCT_STAT_ST_FLAGS
    305 		if ((p->flags & F_FLAGS) && p->st_flags) {
    306 			if (iflag)
    307 				flags = p->st_flags;
    308 			else
    309 				flags = p->st_flags & ~SP_FLGS;
    310 			if (lchflags(path, flags))
    311 				printf("%s: file flags not set: %s\n",
    312 				    path, strerror(errno));
    313 		}
    314 #endif	/* HAVE_STRUCT_STAT_ST_FLAGS */
    315 	}
    316 }
    317