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