Home | History | Annotate | Line # | Download | only in mtree
excludes.c revision 1.1
      1  1.1  lukem /*
      2  1.1  lukem  * Copyright 2000 Massachusetts Institute of Technology
      3  1.1  lukem  *
      4  1.1  lukem  * Permission to use, copy, modify, and distribute this software and
      5  1.1  lukem  * its documentation for any purpose and without fee is hereby
      6  1.1  lukem  * granted, provided that both the above copyright notice and this
      7  1.1  lukem  * permission notice appear in all copies, that both the above
      8  1.1  lukem  * copyright notice and this permission notice appear in all
      9  1.1  lukem  * supporting documentation, and that the name of M.I.T. not be used
     10  1.1  lukem  * in advertising or publicity pertaining to distribution of the
     11  1.1  lukem  * software without specific, written prior permission.  M.I.T. makes
     12  1.1  lukem  * no representations about the suitability of this software for any
     13  1.1  lukem  * purpose.  It is provided "as is" without express or implied
     14  1.1  lukem  * warranty.
     15  1.1  lukem  *
     16  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     17  1.1  lukem  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     18  1.1  lukem  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  lukem  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     20  1.1  lukem  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21  1.1  lukem  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22  1.1  lukem  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     23  1.1  lukem  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  1.1  lukem  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  1.1  lukem  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     26  1.1  lukem  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  lukem  * SUCH DAMAGE.
     28  1.1  lukem  */
     29  1.1  lukem 
     30  1.1  lukem static const char rcsid[] =
     31  1.1  lukem   "$FreeBSD: src/usr.sbin/mtree/excludes.c,v 1.5 2000/12/29 18:04:54 ben Exp $";
     32  1.1  lukem 
     33  1.1  lukem #include <sys/types.h>
     34  1.1  lukem #include <sys/time.h>		/* XXX for mtree.h */
     35  1.1  lukem #include <sys/queue.h>
     36  1.1  lukem 
     37  1.1  lukem #include <err.h>
     38  1.1  lukem #include <fnmatch.h>
     39  1.1  lukem #include <fts.h>
     40  1.1  lukem #include <stdio.h>
     41  1.1  lukem #include <stdlib.h>
     42  1.1  lukem 
     43  1.1  lukem #include "mtree.h"		/* XXX for extern.h */
     44  1.1  lukem #include "extern.h"
     45  1.1  lukem 
     46  1.1  lukem /*
     47  1.1  lukem  * We're assuming that there won't be a whole lot of excludes,
     48  1.1  lukem  * so it's OK to use a stupid algorithm.
     49  1.1  lukem  */
     50  1.1  lukem struct exclude {
     51  1.1  lukem 	LIST_ENTRY(exclude) link;
     52  1.1  lukem 	const char *glob;
     53  1.1  lukem 	int pathname;
     54  1.1  lukem };
     55  1.1  lukem static LIST_HEAD(, exclude) excludes;
     56  1.1  lukem 
     57  1.1  lukem void
     58  1.1  lukem init_excludes(void)
     59  1.1  lukem {
     60  1.1  lukem 	LIST_INIT(&excludes);
     61  1.1  lukem }
     62  1.1  lukem 
     63  1.1  lukem void
     64  1.1  lukem read_excludes_file(const char *name)
     65  1.1  lukem {
     66  1.1  lukem 	FILE *fp;
     67  1.1  lukem 	char *line, *str;
     68  1.1  lukem 	struct exclude *e;
     69  1.1  lukem 	size_t len;
     70  1.1  lukem 
     71  1.1  lukem 	fp = fopen(name, "r");
     72  1.1  lukem 	if (fp == 0)
     73  1.1  lukem 		err(1, "%s", name);
     74  1.1  lukem 
     75  1.1  lukem 	while ((line = fgetln(fp, &len)) != 0) {
     76  1.1  lukem 		if (line[len - 1] == '\n')
     77  1.1  lukem 			len--;
     78  1.1  lukem 		if (len == 0)
     79  1.1  lukem 			continue;
     80  1.1  lukem 
     81  1.1  lukem 		str = malloc(len + 1);
     82  1.1  lukem 		e = malloc(sizeof *e);
     83  1.1  lukem 		if (str == 0 || e == 0)
     84  1.1  lukem 			errx(1, "memory allocation error");
     85  1.1  lukem 		e->glob = str;
     86  1.1  lukem 		memcpy(str, line, len);
     87  1.1  lukem 		str[len] = '\0';
     88  1.1  lukem 		if (strchr(str, '/'))
     89  1.1  lukem 			e->pathname = 1;
     90  1.1  lukem 		else
     91  1.1  lukem 			e->pathname = 0;
     92  1.1  lukem 		LIST_INSERT_HEAD(&excludes, e, link);
     93  1.1  lukem 	}
     94  1.1  lukem 	fclose(fp);
     95  1.1  lukem }
     96  1.1  lukem 
     97  1.1  lukem int
     98  1.1  lukem check_excludes(const char *fname, const char *path)
     99  1.1  lukem {
    100  1.1  lukem 	struct exclude *e;
    101  1.1  lukem 
    102  1.1  lukem 	/* fnmatch(3) has a funny return value convention... */
    103  1.1  lukem #define MATCH(g, n) (fnmatch((g), (n), FNM_PATHNAME) == 0)
    104  1.1  lukem 
    105  1.1  lukem 	LIST_FOREACH(e, &excludes, link) {
    106  1.1  lukem 		if (e->pathname && MATCH(e->glob, path)
    107  1.1  lukem 		    || MATCH(e->glob, fname))
    108  1.1  lukem 			return 1;
    109  1.1  lukem 	}
    110  1.1  lukem 	return 0;
    111  1.1  lukem }
    112