Home | History | Annotate | Line # | Download | only in mtree
misc.c revision 1.25.2.2
      1  1.25.2.2       snj /*	$NetBSD: misc.c,v 1.25.2.2 2005/10/01 05:56:40 snj Exp $	*/
      2       1.3       cgd 
      3       1.1       cgd /*-
      4       1.2       cgd  * Copyright (c) 1991, 1993
      5       1.2       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.24       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.2       cgd  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
     32       1.1       cgd  */
     33       1.1       cgd 
     34      1.25       jmc #if HAVE_NBTOOL_CONFIG_H
     35      1.25       jmc #include "nbtool_config.h"
     36      1.25       jmc #endif
     37      1.25       jmc 
     38       1.5     lukem #include <sys/cdefs.h>
     39      1.22        tv #if defined(__RCSID) && !defined(lint)
     40  1.25.2.2       snj __RCSID("$NetBSD: misc.c,v 1.25.2.2 2005/10/01 05:56:40 snj Exp $");
     41       1.5     lukem #endif /* not lint */
     42       1.5     lukem 
     43       1.1       cgd #include <sys/types.h>
     44       1.1       cgd #include <sys/stat.h>
     45       1.9    simonb 
     46       1.9    simonb #include <stdarg.h>
     47       1.1       cgd #include <stdio.h>
     48      1.18     lukem #include <stdlib.h>
     49      1.18     lukem #include <string.h>
     50       1.9    simonb 
     51       1.1       cgd #include "extern.h"
     52       1.1       cgd 
     53       1.1       cgd typedef struct _key {
     54      1.10     lukem 	const char	*name;		/* key name */
     55      1.10     lukem 	u_int		val;		/* value */
     56       1.1       cgd 
     57       1.1       cgd #define	NEEDVALUE	0x01
     58      1.10     lukem 	u_int		flags;
     59       1.1       cgd } KEY;
     60       1.1       cgd 
     61      1.10     lukem /* NB: the following tables must be sorted lexically. */
     62       1.1       cgd static KEY keylist[] = {
     63       1.5     lukem 	{"cksum",	F_CKSUM,	NEEDVALUE},
     64      1.16     lukem 	{"device",	F_DEV,		NEEDVALUE},
     65       1.7       mrg 	{"flags",	F_FLAGS,	NEEDVALUE},
     66       1.5     lukem 	{"gid",		F_GID,		NEEDVALUE},
     67       1.5     lukem 	{"gname",	F_GNAME,	NEEDVALUE},
     68       1.5     lukem 	{"ignore",	F_IGN,		0},
     69       1.5     lukem 	{"link",	F_SLINK,	NEEDVALUE},
     70       1.8     jwise 	{"md5",		F_MD5,		NEEDVALUE},
     71      1.20     lukem 	{"md5digest",	F_MD5,		NEEDVALUE},
     72       1.5     lukem 	{"mode",	F_MODE,		NEEDVALUE},
     73       1.5     lukem 	{"nlink",	F_NLINK,	NEEDVALUE},
     74       1.5     lukem 	{"optional",	F_OPT,		0},
     75      1.20     lukem 	{"rmd160",	F_RMD160,	NEEDVALUE},
     76      1.20     lukem 	{"rmd160digest",F_RMD160,	NEEDVALUE},
     77      1.20     lukem 	{"sha1",	F_SHA1,		NEEDVALUE},
     78      1.20     lukem 	{"sha1digest",	F_SHA1,		NEEDVALUE},
     79  1.25.2.1      tron 	{"sha256",	F_SHA256,	NEEDVALUE},
     80  1.25.2.1      tron 	{"sha256digest",F_SHA256,	NEEDVALUE},
     81  1.25.2.1      tron 	{"sha384",	F_SHA384,	NEEDVALUE},
     82  1.25.2.1      tron 	{"sha384digest",F_SHA384,	NEEDVALUE},
     83  1.25.2.1      tron 	{"sha512",	F_SHA512,	NEEDVALUE},
     84  1.25.2.1      tron 	{"sha512digest",F_SHA512,	NEEDVALUE},
     85  1.25.2.2       snj 	{"size",	F_SIZE,		NEEDVALUE},
     86  1.25.2.2       snj 	{"tags",	F_TAGS,		NEEDVALUE},
     87  1.25.2.2       snj 	{"time",	F_TIME,		NEEDVALUE},
     88  1.25.2.2       snj 	{"type",	F_TYPE,		NEEDVALUE},
     89  1.25.2.2       snj 	{"uid",		F_UID,		NEEDVALUE},
     90  1.25.2.2       snj 	{"uname",	F_UNAME,	NEEDVALUE}
     91       1.1       cgd };
     92       1.1       cgd 
     93      1.10     lukem static KEY typelist[] = {
     94      1.10     lukem 	{"block",	F_BLOCK,	},
     95      1.10     lukem 	{"char",	F_CHAR,		},
     96      1.10     lukem 	{"dir",		F_DIR,		},
     97      1.10     lukem 	{"fifo",	F_FIFO,		},
     98      1.10     lukem 	{"file",	F_FILE,		},
     99      1.10     lukem 	{"link",	F_LINK,		},
    100      1.10     lukem 	{"socket",	F_SOCK,		},
    101      1.10     lukem };
    102      1.10     lukem 
    103      1.18     lukem slist_t	excludetags, includetags;
    104      1.18     lukem int	keys = KEYDEFAULT;
    105      1.18     lukem 
    106      1.18     lukem 
    107       1.9    simonb int keycompare(const void *, const void *);
    108       1.5     lukem 
    109       1.1       cgd u_int
    110      1.10     lukem parsekey(const char *name, int *needvaluep)
    111       1.1       cgd {
    112      1.15     lukem 	static int allbits;
    113       1.1       cgd 	KEY *k, tmp;
    114       1.1       cgd 
    115      1.15     lukem 	if (allbits == 0) {
    116      1.15     lukem 		int i;
    117      1.15     lukem 
    118      1.15     lukem 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
    119      1.15     lukem 			allbits |= keylist[i].val;
    120      1.15     lukem 	}
    121       1.1       cgd 	tmp.name = name;
    122      1.15     lukem 	if (strcmp(name, "all") == 0)
    123      1.15     lukem 		return (allbits);
    124       1.1       cgd 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
    125       1.1       cgd 	    sizeof(KEY), keycompare);
    126       1.1       cgd 	if (k == NULL)
    127      1.13     lukem 		mtree_err("unknown keyword `%s'", name);
    128       1.1       cgd 
    129       1.1       cgd 	if (needvaluep)
    130       1.1       cgd 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
    131      1.10     lukem 
    132      1.10     lukem 	return (k->val);
    133      1.10     lukem }
    134      1.10     lukem 
    135      1.10     lukem u_int
    136      1.10     lukem parsetype(const char *name)
    137      1.10     lukem {
    138      1.10     lukem 	KEY *k, tmp;
    139      1.10     lukem 
    140      1.10     lukem 	tmp.name = name;
    141      1.10     lukem 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
    142      1.10     lukem 	    sizeof(KEY), keycompare);
    143      1.10     lukem 	if (k == NULL)
    144      1.13     lukem 		mtree_err("unknown file type `%s'", name);
    145       1.8     jwise 
    146       1.1       cgd 	return (k->val);
    147       1.1       cgd }
    148       1.1       cgd 
    149       1.1       cgd int
    150       1.9    simonb keycompare(const void *a, const void *b)
    151       1.1       cgd {
    152       1.9    simonb 
    153      1.17     lukem 	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
    154       1.1       cgd }
    155       1.1       cgd 
    156       1.1       cgd void
    157       1.6  wsanchez mtree_err(const char *fmt, ...)
    158       1.1       cgd {
    159       1.1       cgd 	va_list ap;
    160       1.9    simonb 
    161       1.1       cgd 	va_start(ap, fmt);
    162      1.18     lukem 	vwarnx(fmt, ap);
    163       1.1       cgd 	va_end(ap);
    164      1.19     lukem 	if (mtree_lineno)
    165      1.18     lukem 		warnx("failed at line %lu of the specification",
    166      1.19     lukem 		    (u_long) mtree_lineno);
    167       1.1       cgd 	exit(1);
    168       1.1       cgd 	/* NOTREACHED */
    169      1.14     lukem }
    170      1.14     lukem 
    171      1.14     lukem void
    172      1.14     lukem addtag(slist_t *list, char *elem)
    173      1.14     lukem {
    174      1.14     lukem 
    175      1.14     lukem #define	TAG_CHUNK 20
    176      1.14     lukem 
    177      1.14     lukem 	if ((list->count % TAG_CHUNK) == 0) {
    178      1.14     lukem 		char **new;
    179      1.14     lukem 
    180      1.14     lukem 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
    181      1.14     lukem 		    * sizeof(char *));
    182      1.14     lukem 		if (new == NULL)
    183      1.14     lukem 			mtree_err("memory allocation error");
    184      1.14     lukem 		list->list = new;
    185      1.14     lukem 	}
    186      1.14     lukem 	list->list[list->count] = elem;
    187      1.14     lukem 	list->count++;
    188      1.14     lukem }
    189      1.14     lukem 
    190      1.14     lukem void
    191      1.14     lukem parsetags(slist_t *list, char *args)
    192      1.14     lukem {
    193      1.14     lukem 	char	*p, *e;
    194      1.14     lukem 	int	len;
    195      1.14     lukem 
    196      1.14     lukem 	if (args == NULL) {
    197      1.14     lukem 		addtag(list, NULL);
    198      1.14     lukem 		return;
    199      1.14     lukem 	}
    200      1.14     lukem 	while ((p = strsep(&args, ",")) != NULL) {
    201      1.14     lukem 		if (*p == '\0')
    202      1.14     lukem 			continue;
    203      1.14     lukem 		len = strlen(p) + 3;	/* "," + p + ",\0" */
    204      1.14     lukem 		if ((e = malloc(len)) == NULL)
    205      1.14     lukem 			mtree_err("memory allocation error");
    206      1.14     lukem 		snprintf(e, len, ",%s,", p);
    207      1.14     lukem 		addtag(list, e);
    208      1.14     lukem 	}
    209      1.14     lukem }
    210      1.14     lukem 
    211      1.14     lukem /*
    212      1.14     lukem  * matchtags
    213      1.14     lukem  *	returns 0 if there's a match from the exclude list in the node's tags,
    214      1.14     lukem  *	or there's an include list and no match.
    215      1.14     lukem  *	return 1 otherwise.
    216      1.14     lukem  */
    217      1.14     lukem int
    218      1.14     lukem matchtags(NODE *node)
    219      1.14     lukem {
    220      1.14     lukem 	int	i;
    221      1.14     lukem 
    222      1.14     lukem 	if (node->tags) {
    223      1.14     lukem 		for (i = 0; i < excludetags.count; i++)
    224      1.14     lukem 			if (strstr(node->tags, excludetags.list[i]))
    225      1.14     lukem 				break;
    226      1.20     lukem 		if (i < excludetags.count)
    227      1.14     lukem 			return (0);
    228      1.14     lukem 
    229      1.14     lukem 		for (i = 0; i < includetags.count; i++)
    230      1.14     lukem 			if (strstr(node->tags, includetags.list[i]))
    231      1.14     lukem 				break;
    232      1.14     lukem 		if (i > 0 && i == includetags.count)
    233      1.14     lukem 			return (0);
    234      1.14     lukem 	} else if (includetags.count > 0) {
    235      1.14     lukem 		return (0);
    236      1.14     lukem 	}
    237      1.14     lukem 	return (1);
    238      1.18     lukem }
    239      1.18     lukem 
    240      1.18     lukem u_int
    241      1.18     lukem nodetoino(u_int type)
    242      1.18     lukem {
    243      1.18     lukem 
    244      1.18     lukem 	switch (type) {
    245      1.18     lukem 	case F_BLOCK:
    246      1.18     lukem 		return S_IFBLK;
    247      1.18     lukem 	case F_CHAR:
    248      1.18     lukem 		return S_IFCHR;
    249      1.18     lukem 	case F_DIR:
    250      1.18     lukem 		return S_IFDIR;
    251      1.18     lukem 	case F_FIFO:
    252      1.18     lukem 		return S_IFIFO;
    253      1.18     lukem 	case F_FILE:
    254      1.18     lukem 		return S_IFREG;
    255      1.18     lukem 	case F_LINK:
    256      1.18     lukem 		return S_IFLNK;
    257      1.25       jmc #ifdef S_IFSOCK
    258      1.18     lukem 	case F_SOCK:
    259      1.18     lukem 		return S_IFSOCK;
    260      1.25       jmc #endif
    261      1.18     lukem 	default:
    262      1.18     lukem 		printf("unknown type %d", type);
    263      1.18     lukem 		abort();
    264      1.18     lukem 	}
    265      1.18     lukem 	/* NOTREACHED */
    266      1.18     lukem }
    267      1.18     lukem 
    268      1.18     lukem const char *
    269      1.18     lukem nodetype(u_int type)
    270      1.18     lukem {
    271      1.18     lukem 
    272      1.18     lukem 	return (inotype(nodetoino(type)));
    273      1.18     lukem }
    274      1.18     lukem 
    275      1.18     lukem 
    276      1.18     lukem const char *
    277      1.18     lukem inotype(u_int type)
    278      1.18     lukem {
    279      1.18     lukem 
    280      1.18     lukem 	switch (type & S_IFMT) {
    281      1.18     lukem 	case S_IFBLK:
    282      1.18     lukem 		return ("block");
    283      1.18     lukem 	case S_IFCHR:
    284      1.18     lukem 		return ("char");
    285      1.18     lukem 	case S_IFDIR:
    286      1.18     lukem 		return ("dir");
    287      1.18     lukem 	case S_IFIFO:
    288      1.18     lukem 		return ("fifo");
    289      1.18     lukem 	case S_IFREG:
    290      1.18     lukem 		return ("file");
    291      1.18     lukem 	case S_IFLNK:
    292      1.18     lukem 		return ("link");
    293      1.25       jmc #ifdef S_IFSOCK
    294      1.18     lukem 	case S_IFSOCK:
    295      1.18     lukem 		return ("socket");
    296      1.25       jmc #endif
    297      1.18     lukem 	default:
    298      1.18     lukem 		return ("unknown");
    299      1.18     lukem 	}
    300      1.18     lukem 	/* NOTREACHED */
    301       1.1       cgd }
    302