Give mtree the ability to exclude files and directories from its traversal.
PR: 16944
This commit is contained in:
parent
9f8b8491c9
commit
dc785636aa
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=60418
@ -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)
|
||||
|
107
usr.sbin/mtree/excludes.c
Normal file
107
usr.sbin/mtree/excludes.c
Normal file
@ -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 <sys/types.h>
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <fnmatch.h>
|
||||
#include <fts.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
@ -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 *));
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user