From 198f09c6490f91ae9c9c8efea0c89ab39cf74eae Mon Sep 17 00:00:00 2001 From: wollman Date: Fri, 12 May 2000 03:03:00 +0000 Subject: Give mtree the ability to exclude files and directories from its traversal. PR: 16944 --- usr.sbin/mtree/create.c | 4 ++ usr.sbin/mtree/excludes.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ usr.sbin/mtree/extern.h | 4 ++ usr.sbin/mtree/mtree.8 | 14 ++++++ usr.sbin/mtree/mtree.c | 10 ++++- usr.sbin/mtree/verify.c | 4 ++ 6 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 usr.sbin/mtree/excludes.c diff --git a/usr.sbin/mtree/create.c b/usr.sbin/mtree/create.c index 0146bd2..bc1ae0d 100644 --- a/usr.sbin/mtree/create.c +++ b/usr.sbin/mtree/create.c @@ -107,6 +107,10 @@ cwalk() while ((p = fts_read(t))) { if (iflag) indent = p->fts_level * 4; + if (check_excludes(p->fts_name, p->fts_path)) { + fts_set(t, p, FTS_SKIP); + continue; + } switch(p->fts_info) { case FTS_D: if (!dflag) diff --git a/usr.sbin/mtree/excludes.c b/usr.sbin/mtree/excludes.c new file mode 100644 index 0000000..86c7cdf --- /dev/null +++ b/usr.sbin/mtree/excludes.c @@ -0,0 +1,107 @@ +/* + * Copyright 2000 Massachusetts Institute of Technology + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that both the above copyright notice and this + * permission notice appear in all copies, that both the above + * copyright notice and this permission notice appear in all + * supporting documentation, and that the name of M.I.T. not be used + * in advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. M.I.T. makes + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied + * warranty. + * + * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS + * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT + * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +static const char rcsid[] = + "$FreeBSD$"; + +#include +#include + +#include +#include +#include +#include +#include + +/* + * We're assuming that there won't be a whole lot of excludes, + * so it's OK to use a stupid algorithm. + */ +struct exclude { + LIST_ENTRY(exclude) link; + const char *glob; + int pathname; +}; +static LIST_HEAD(, exclude) excludes; + +void +init_excludes(void) +{ + LIST_INIT(&excludes); +} + +void +read_excludes_file(const char *name) +{ + FILE *fp; + char *line, *str; + struct exclude *e; + size_t len; + + fp = fopen(name, "r"); + if (fp == 0) + err(1, "%s", name); + + while ((line = fgetln(fp, &len)) != 0) { + if (line[len - 1] == '\n') + len--; + if (len == 0) + continue; + + str = malloc(len + 1); + e = malloc(sizeof *e); + if (str == 0 || e == 0) + errx(1, "memory allocation error"); + e->glob = str; + memcpy(str, line, len); + str[len] = '\0'; + if (strchr(str, '/')) + e->pathname = 1; + else + e->pathname = 0; + LIST_INSERT_HEAD(&excludes, e, link); + } + fclose(fp); +} + +int +check_excludes(const char *fname, const char *path) +{ + struct exclude *e; + + /* fnmatch(3) has a funny return value convention... */ +#define MATCH(g, n) (fnmatch((g), (n), FNM_PATHNAME) == 0) + + for (e = excludes.lh_first; e != 0; e = e->link.le_next) { + if (e->pathname && MATCH(e->glob, path) + || MATCH(e->glob, fname)) + return 1; + } + return 0; +} diff --git a/usr.sbin/mtree/extern.h b/usr.sbin/mtree/extern.h index c9a00b2..c57a338 100644 --- a/usr.sbin/mtree/extern.h +++ b/usr.sbin/mtree/extern.h @@ -45,3 +45,7 @@ u_int parsekey __P((char *, int *)); char *rlink __P((char *)); NODE *spec __P((void)); int verify __P((void)); + +int check_excludes __P((const char *, const char *)); +void init_excludes __P((void)); +void read_excludes_file __P((const char *)); diff --git a/usr.sbin/mtree/mtree.8 b/usr.sbin/mtree/mtree.8 index d01438f..1e2ecb5 100644 --- a/usr.sbin/mtree/mtree.8 +++ b/usr.sbin/mtree/mtree.8 @@ -46,6 +46,7 @@ .Op Fl k Ar keywords .Op Fl p Ar path .Op Fl s Ar seed +.Op Fl X Ar exclude-list .Sh DESCRIPTION The utility .Nm mtree @@ -117,6 +118,19 @@ except a status of 2 is returned if the file hierarchy did not match the specification. .It Fl x Don't descend below mount points in the file hierarchy. +.It Fl X Ar exclude-list +The specified file contains +.Xr fnmatch 3 +patterns matching files to be excluded from +the specification, one to a line. +If the pattern contains a +.Ql \&/ +character, it will be matched against entire pathnames (relative to +the starting directory); otherwise, +it will be matched against basenames only. No comments are allowed in +the +.Ar exclude-list +file. .El .Pp Specifications are mostly composed of ``keywords'', i.e. strings that diff --git a/usr.sbin/mtree/mtree.c b/usr.sbin/mtree/mtree.c index c8ac609..ee4eb50 100644 --- a/usr.sbin/mtree/mtree.c +++ b/usr.sbin/mtree/mtree.c @@ -75,7 +75,9 @@ main(argc, argv) dir = NULL; keys = KEYDEFAULT; - while ((ch = getopt(argc, argv, "cdef:iK:k:np:rs:Uux")) != -1) + init_excludes(); + + while ((ch = getopt(argc, argv, "cdef:iK:k:np:rs:UuxX:")) != -1) switch((char)ch) { case 'c': cflag = 1; @@ -128,6 +130,9 @@ main(argc, argv) case 'x': ftsoptions |= FTS_XDEV; break; + case 'X': + read_excludes_file(optarg); + break; case '?': default: usage(); @@ -158,6 +163,7 @@ static void usage() { (void)fprintf(stderr, -"usage: mtree [-cdeinrUux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"); +"usage: mtree [-cdeinrUux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n" +"\t[-X excludes]\n"); exit(1); } diff --git a/usr.sbin/mtree/verify.c b/usr.sbin/mtree/verify.c index 9422e5e..f182996 100644 --- a/usr.sbin/mtree/verify.c +++ b/usr.sbin/mtree/verify.c @@ -90,6 +90,10 @@ vwalk() level = root; specdepth = rval = 0; while ((p = fts_read(t))) { + if (check_excludes(p->fts_name, p->fts_path)) { + fts_set(t, p, FTS_SKIP); + continue; + } switch(p->fts_info) { case FTS_D: break; -- cgit v1.1