Home | History | Annotate | Line # | Download | only in mtree
verify.c revision 1.6
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * 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 #ifndef lint
     35 /* from: static char sccsid[] = "@(#)verify.c	5.11 (Berkeley) 4/17/92"; */
     36 static char *rcsid = "$Id: verify.c,v 1.6 1993/11/02 07:51:14 cgd Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/stat.h>
     41 #include <dirent.h>
     42 #include <fts.h>
     43 #include <unistd.h>
     44 #include <errno.h>
     45 #include <stdio.h>
     46 #include "mtree.h"
     47 #include "extern.h"
     48 
     49 extern int crc_total, ftsoptions;
     50 extern int dflag, eflag, rflag, sflag, uflag;
     51 extern char fullpath[MAXPATHLEN];
     52 
     53 static NODE *root;
     54 static char path[MAXPATHLEN];
     55 
     56 static void	miss __P((NODE *, char *));
     57 static int	vwalk __P((void));
     58 
     59 int
     60 verify()
     61 {
     62 	int rval;
     63 
     64 	root = spec();
     65 	rval = vwalk();
     66 	miss(root, path);
     67 	return (rval);
     68 }
     69 
     70 static int
     71 vwalk()
     72 {
     73 	register FTS *t;
     74 	register FTSENT *p;
     75 	register NODE *ep, *level;
     76 	int ftsdepth, specdepth, rval;
     77 	char *argv[2];
     78 
     79 	argv[0] = ".";
     80 	argv[1] = NULL;
     81 	if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
     82 		err("fts_open: %s", strerror(errno));
     83 	level = root;
     84 	ftsdepth = specdepth = rval = 0;
     85 	while (p = fts_read(t)) {
     86 		switch(p->fts_info) {
     87 		case FTS_D:
     88 			++ftsdepth;
     89 			break;
     90 		case FTS_DP:
     91 			--ftsdepth;
     92 			if (specdepth > ftsdepth) {
     93 				for (level = level->parent; level->prev;
     94 				      level = level->prev);
     95 				--specdepth;
     96 			}
     97 			continue;
     98 		case FTS_DNR:
     99 		case FTS_ERR:
    100 		case FTS_NS:
    101 			(void)fprintf(stderr, "mtree: %s: %s\n",
    102 			    RP(p), strerror(errno));
    103 			continue;
    104 		default:
    105 			if (dflag)
    106 				continue;
    107 		}
    108 
    109 		for (ep = level; ep; ep = ep->next)
    110 			if (ep->flags & F_MAGIC && fnmatch(ep->name,
    111 			    p->fts_name, FNM_PATHNAME|FNM_QUOTE) ||
    112 			    !strcmp(ep->name, p->fts_name)) {
    113 				ep->flags |= F_VISIT;
    114 				if (compare(ep->name, ep, p))
    115 					rval = MISMATCHEXIT;
    116 				if (ep->flags & F_IGN)
    117 					(void)fts_set(t, p, FTS_SKIP);
    118 				else if (ep->child && ep->type == F_DIR &&
    119 				    p->fts_info == FTS_D) {
    120 					level = ep->child;
    121 					++specdepth;
    122 				}
    123 				break;
    124 			}
    125 
    126 		if (ep)
    127 			continue;
    128 		if (!eflag) {
    129 			(void)printf("extra: %s", RP(p));
    130 			if (rflag) {
    131 				if (unlink(p->fts_accpath)) {
    132 					(void)printf(", not removed: %s",
    133 					    strerror(errno));
    134 				} else
    135 					(void)printf(", removed");
    136 			}
    137 			(void)putchar('\n');
    138 		}
    139 		(void)fts_set(t, p, FTS_SKIP);
    140 	}
    141 	(void)fts_close(t);
    142 	if (sflag)
    143 		(void)fprintf(stderr,
    144 		    "mtree: %s checksum: %lu\n", fullpath, crc_total);
    145 	return (rval);
    146 }
    147 
    148 static void
    149 miss(p, tail)
    150 	register NODE *p;
    151 	register char *tail;
    152 {
    153 	register int create;
    154 	register char *tp;
    155 
    156 	for (; p; p = p->next) {
    157 		if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
    158 			continue;
    159 		(void)strcpy(tail, p->name);
    160 		if (!(p->flags & F_VISIT))
    161 			(void)printf("missing: %s", path);
    162 		if (p->type != F_DIR) {
    163 			putchar('\n');
    164 			continue;
    165 		}
    166 
    167 		create = 0;
    168 		if (!(p->flags & F_VISIT) && uflag)
    169 			if (!(p->flags & (F_UID | F_UNAME)))
    170 			    (void)printf(" (not created: user not specified)");
    171 			else if (!(p->flags & (F_GID | F_GNAME)))
    172 			    (void)printf(" (not created: group not specified)");
    173 			else if (!(p->flags & F_MODE))
    174 			    (void)printf(" (not created: mode not specified)");
    175 			else if (mkdir(path, S_IRWXU))
    176 				(void)printf(" (not created: %s)",
    177 				    strerror(errno));
    178 			else {
    179 				create = 1;
    180 				(void)printf(" (created)");
    181 			}
    182 
    183 		if (!(p->flags & F_VISIT))
    184 			(void)putchar('\n');
    185 
    186 		for (tp = tail; *tp; ++tp);
    187 		*tp = '/';
    188 		miss(p->child, tp + 1);
    189 		*tp = '\0';
    190 
    191 		if (!create)
    192 			continue;
    193 		if (chown(path, p->st_uid, p->st_gid)) {
    194 			(void)printf("%s: user/group/mode not modified: %s\n",
    195 			    path, strerror(errno));
    196 			continue;
    197 		}
    198 		if (chmod(path, p->st_mode))
    199 			(void)printf("%s: permissions not set: %s\n",
    200 			    path, strerror(errno));
    201 	}
    202 }
    203