From owner-freebsd-bugs Wed Feb 23 13:10:16 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id BF62137BD72 for ; Wed, 23 Feb 2000 13:10:00 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id NAA19177; Wed, 23 Feb 2000 13:10:00 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from mintaka.lcs.mit.edu (mintaka.lcs.mit.edu [18.26.0.36]) by hub.freebsd.org (Postfix) with ESMTP id 969E537B9B2 for ; Wed, 23 Feb 2000 13:07:48 -0800 (PST) (envelope-from wollman@mintaka.lcs.mit.edu) Received: (from wollman@localhost) by mintaka.lcs.mit.edu (8.9.3/8.9.3) id QAA09179; Wed, 23 Feb 2000 16:07:33 -0500 (EST) (envelope-from wollman) Message-Id: <200002232107.QAA09179@mintaka.lcs.mit.edu> Date: Wed, 23 Feb 2000 16:07:33 -0500 (EST) From: wollman@LCS.MIT.EDU Reply-To: wollman@LCS.MIT.EDU To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/16944: Give mtree(8) an ``exclude'' facility Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 16944 >Category: bin >Synopsis: Give mtree(8) an ``exclude'' facility >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Wed Feb 23 13:10:00 PST 2000 >Closed-Date: >Last-Modified: >Originator: Garrett A. Wollman >Release: FreeBSD 3.4-STABLE i386 >Organization: MIT Laboratory for Computer Science >Environment: >Description: In thinking about how to make mtree(8) part of an IDS, I hit upon the insurmountable problem that mtree doesn't know how to exclude files (or directories) from consideration, and thus requires enormous amounts of hand-editing work to remove ``normally changing'' files from an auto-generated specification. In most other tools which perform a similar function, there is such a mechanism. It turned out to be very simple to write. >How-To-Repeat: mtree -c -K md5digest -p / | more >Fix: Although this patch is relative to 3.4, it should apply cleanly to -current as well. Index: Makefile =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/Makefile,v retrieving revision 1.6.2.1 diff -u -r1.6.2.1 Makefile --- Makefile 1999/08/29 15:44:16 1.6.2.1 +++ Makefile 2000/02/23 20:43:16 @@ -2,7 +2,7 @@ # $FreeBSD: src/usr.sbin/mtree/Makefile,v 1.6.2.1 1999/08/29 15:44:16 peter Exp $ PROG= mtree -SRCS= compare.c crc.c create.c misc.c mtree.c spec.c verify.c +SRCS= compare.c crc.c create.c excludes.c misc.c mtree.c spec.c verify.c MAN8= mtree.8 .PATH: ${.CURDIR}/../../usr.bin/cksum Index: create.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/create.c,v retrieving revision 1.13.2.1 diff -u -r1.13.2.1 create.c --- create.c 1999/08/29 15:44:17 1.13.2.1 +++ create.c 2000/02/23 20:44:38 @@ -97,6 +97,10 @@ 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) Index: excludes.c =================================================================== RCS file: excludes.c diff -N excludes.c --- /dev/null Wed Feb 23 15:44:28 2000 +++ excludes.c Wed Feb 23 15:49:28 2000 @@ -0,0 +1,102 @@ +/* + * 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. + */ + +#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; + 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; +} Index: extern.h =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/extern.h,v retrieving revision 1.2 diff -u -r1.2 extern.h --- extern.h 1997/10/01 06:30:00 1.2 +++ extern.h 2000/02/23 20:39:04 @@ -41,3 +41,7 @@ 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 *)); Index: mtree.8 =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/mtree.8,v retrieving revision 1.13.2.1 diff -u -r1.13.2.1 mtree.8 --- mtree.8 1999/08/29 15:44:18 1.13.2.1 +++ mtree.8 2000/02/23 20:59:30 @@ -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 @@ 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 Index: mtree.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/mtree.c,v retrieving revision 1.7.2.1 diff -u -r1.7.2.1 mtree.c --- mtree.c 1999/08/29 15:44:19 1.7.2.1 +++ mtree.c 2000/02/23 20:46:43 @@ -75,7 +75,9 @@ 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 @@ case 'x': ftsoptions |= FTS_XDEV; break; + case 'X': + read_excludes_file(optarg); + break; case '?': default: usage(); @@ -158,6 +163,7 @@ 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); } Index: verify.c =================================================================== RCS file: /home/ncvs/src/usr.sbin/mtree/verify.c,v retrieving revision 1.7.2.1 diff -u -r1.7.2.1 verify.c --- verify.c 1999/08/29 15:44:19 1.7.2.1 +++ verify.c 2000/02/23 20:52:30 @@ -90,6 +90,10 @@ 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; >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message