Home | History | Annotate | Line # | Download | only in mtree
verify.c revision 1.21
      1 /*	$NetBSD: verify.c,v 1.21 2001/10/18 05:06:02 lukem 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 #ifndef 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.21 2001/10/18 05:06:02 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 
     48 #include <dirent.h>
     49 #include <errno.h>
     50 #include <fnmatch.h>
     51 #include <fts.h>
     52 #include <stdio.h>
     53 #include <unistd.h>
     54 
     55 #include "mtree.h"
     56 #include "extern.h"
     57 
     58 static NODE *root;
     59 static char path[MAXPATHLEN];
     60 
     61 static void	miss(NODE *, char *);
     62 static int	vwalk(void);
     63 
     64 int
     65 verify(void)
     66 {
     67 	int rval;
     68 
     69 	root = spec();
     70 	rval = vwalk();
     71 	miss(root, path);
     72 	return (rval);
     73 }
     74 
     75 static int
     76 vwalk(void)
     77 {
     78 	FTS *t;
     79 	FTSENT *p;
     80 	NODE *ep, *level;
     81 	int ftsdepth, specdepth, rval;
     82 	char  dot[] = ".";		/* XXX: work around gcc warning */
     83 	char *argv[] = { dot, NULL };
     84 
     85 	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
     86 		mtree_err("fts_open: %s", strerror(errno));
     87 	level = root;
     88 	ftsdepth = specdepth = rval = 0;
     89 	while ((p = fts_read(t)) != NULL) {
     90 		switch(p->fts_info) {
     91 		case FTS_D:
     92 			++ftsdepth;
     93 			break;
     94 		case FTS_DP:
     95 			--ftsdepth;
     96 			if (specdepth > ftsdepth) {
     97 				for (level = level->parent; level->prev;
     98 				      level = level->prev);
     99 				--specdepth;
    100 			}
    101 			continue;
    102 		case FTS_DNR:
    103 		case FTS_ERR:
    104 		case FTS_NS:
    105 			(void)fprintf(stderr, "mtree: %s: %s\n",
    106 			    RP(p), strerror(errno));
    107 			continue;
    108 		default:
    109 			if (dflag)
    110 				continue;
    111 		}
    112 
    113 		for (ep = level; ep; ep = ep->next)
    114 			if ((ep->flags & F_MAGIC &&
    115 			    !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
    116 			    !strcmp(ep->name, p->fts_name)) {
    117 				ep->flags |= F_VISIT;
    118 				if (compare(ep->name, ep, p))
    119 					rval = MISMATCHEXIT;
    120 				if (!(ep->flags & F_IGN) &&
    121 				    ep->child && ep->type == F_DIR &&
    122 				    p->fts_info == FTS_D) {
    123 					level = ep->child;
    124 					++specdepth;
    125 				} else
    126 					(void)fts_set(t, p, FTS_SKIP);
    127 				break;
    128 			}
    129 
    130 		if (ep)
    131 			continue;
    132 		if (!eflag) {
    133 			(void)printf("extra: %s", RP(p));
    134 			if (rflag) {
    135 				if (unlink(p->fts_accpath)) {
    136 					(void)printf(", not removed: %s",
    137 					    strerror(errno));
    138 				} else
    139 					(void)printf(", removed");
    140 			}
    141 			(void)putchar('\n');
    142 		}
    143 		(void)fts_set(t, p, FTS_SKIP);
    144 	}
    145 	(void)fts_close(t);
    146 	if (sflag)
    147 		(void)fprintf(stderr,
    148 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
    149 	return (rval);
    150 }
    151 
    152 static void
    153 miss(NODE *p, char *tail)
    154 {
    155 	int create;
    156 	char *tp;
    157 	u_int32_t flags;
    158 
    159 	for (; p; p = p->next) {
    160 		if (p->flags & F_OPT && !(p->flags & F_VISIT))
    161 			continue;
    162 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
    163 			continue;
    164 		(void)strcpy(tail, p->name);
    165 		if (!(p->flags & F_VISIT))
    166 			(void)printf("missing: %s", path);
    167 		if (p->type != F_DIR) {
    168 			putchar('\n');
    169 			continue;
    170 		}
    171 
    172 		create = 0;
    173 		if (!(p->flags & F_VISIT) && uflag) {
    174 			if (!(p->flags & (F_UID | F_UNAME)))
    175 			    (void)printf(" (not created: user not specified)");
    176 			else if (!(p->flags & (F_GID | F_GNAME)))
    177 			    (void)printf(" (not created: group not specified)");
    178 			else if (!(p->flags & F_MODE))
    179 			    (void)printf(" (not created: mode not specified)");
    180 			else if (mkdir(path, S_IRWXU))
    181 				(void)printf(" (not created: %s)",
    182 				    strerror(errno));
    183 			else {
    184 				create = 1;
    185 				(void)printf(" (created)");
    186 			}
    187 		}
    188 		if (!(p->flags & F_VISIT))
    189 			(void)putchar('\n');
    190 
    191 		for (tp = tail; *tp; ++tp)
    192 			;
    193 		*tp = '/';
    194 		miss(p->child, tp + 1);
    195 		*tp = '\0';
    196 
    197 		if (!create)
    198 			continue;
    199 	/* XXXLUKEM: what about devices? */
    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 			(void)printf("%s: warning: file mode %snot set\n", path,
    204 			    (p->flags & F_FLAGS) ? "and file flags " : "");
    205 			continue;
    206 		}
    207 		if (chmod(path, p->st_mode))
    208 			(void)printf("%s: permissions not set: %s\n",
    209 			    path, strerror(errno));
    210 		if ((p->flags & F_FLAGS) && p->st_flags) {
    211 			if (iflag)
    212 				flags = p->st_flags;
    213 			else
    214 				flags = p->st_flags & ~SP_FLGS;
    215 			if (chflags(path, flags))
    216 				(void)printf("%s: file flags not set: %s\n",
    217 				    path, strerror(errno));
    218 		}
    219 	}
    220 }
    221