exclude-list in mtree (2)

2014-06-20 Thread Manuel Giraud
Hi,

In the previous patch, I forgot to check for excluded files at
verification time (duh). I also add the name of the excluded files list
into the mtree output so it can be retrieved later (e.g. in
/usr/libexec/security).

Index: Makefile
===
RCS file: /cvs/src/usr.sbin/mtree/Makefile,v
retrieving revision 1.9
diff -u -p -r1.9 Makefile
--- Makefile	15 Apr 2013 06:25:18 -	1.9
+++ Makefile	20 Jun 2014 12:45:35 -
@@ -3,6 +3,6 @@
 PROG=	mtree
 #CFLAGS+=-DDEBUG
 MAN=	mtree.8
-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
 
 .include 
Index: create.c
===
RCS file: /cvs/src/usr.sbin/mtree/create.c,v
retrieving revision 1.29
diff -u -p -r1.29 create.c
--- create.c	22 Aug 2013 04:43:41 -	1.29
+++ create.c	20 Jun 2014 12:45:35 -
@@ -58,6 +58,7 @@ extern int ftsoptions;
 extern int dflag, iflag, nflag, sflag;
 extern u_int keys;
 extern char fullpath[MAXPATHLEN];
+extern char *excludefile;
 
 static gid_t gid;
 static uid_t uid;
@@ -82,6 +83,8 @@ cwalk(void)
 	(void)printf(
 	"#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
 	getlogin(), host, fullpath, ctime(&clock));
+	if (excludefile)
+		printf("#\texclude: %s\n", excludefile);
 
 	argv[0] = ".";
 	argv[1] = NULL;
@@ -90,6 +93,10 @@ cwalk(void)
 	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	1 Jan 1970 00:00:00 -
+++ excludes.c	20 Jun 2014 12:45:35 -
@@ -0,0 +1,118 @@
+/*
+ * 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 
+#include "mtree.h"
+#include "extern.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);
+}
+
+int
+read_excludes_file(const char *name)
+{
+	FILE *fp;
+	char *buf, *lbuf;
+	struct exclude *e;
+	size_t len;
+
+
+	fp = fopen(name, "r");
+	if (fp == NULL) {
+		return 1;
+	}
+
+	lbuf = NULL;
+	while ((buf = fgetln(fp, &len))) {
+		if (buf[len - 1] == '\n') {
+			if (len == 1) 
+continue;
+			buf[len - 1] = '\0';
+		} else {
+			len++;
+			if ((lbuf = malloc(len)) == NULL)
+err(1, NULL);
+			memcpy(lbuf, buf, len - 1);
+			lbuf[len - 1] = '\0';
+			buf = lbuf;
+		}
+
+		if ((e = malloc(sizeof *e)) == NULL)
+			err(1, NULL);
+		if ((e->glob = malloc(len)) == NULL)
+			err(1, NULL);
+		memcpy((char *) e->glob, buf, len);
+		if (strchr(e->glob, '/'))
+			e->pathname = 1;
+		else
+			e->pathname = 0;
+		LIST_INSERT_HEAD(&excludes, e, link);
+	}
+	free(lbuf);
+	fclose(fp);
+
+	return 0;
+}
+
+int
+check_excludes(const char *fname, const char *path)
+{
+	struct exclude *e;
+
+	/* Remove leading dot-slash before path match */
+	if ((path[0] == '.') && (path[1] == '/'))
+		path += 2;
+
+	LIST_FOREACH(e, &excludes, link) {
+		if ((e->pathname && !fnmatch(e->glob, path, FNM_PATHNAME))
+		|| !fnmatch(

Re: exclude-list in mtree (2)

2014-06-20 Thread Manuel Giraud
Same with patch inline:
Index: Makefile
===
RCS file: /cvs/src/usr.sbin/mtree/Makefile,v
retrieving revision 1.9
diff -u -p -r1.9 Makefile
--- Makefile15 Apr 2013 06:25:18 -  1.9
+++ Makefile20 Jun 2014 12:45:35 -
@@ -3,6 +3,6 @@
 PROG=  mtree
 #CFLAGS+=-DDEBUG
 MAN=   mtree.8
-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
 
 .include 
Index: create.c
===
RCS file: /cvs/src/usr.sbin/mtree/create.c,v
retrieving revision 1.29
diff -u -p -r1.29 create.c
--- create.c22 Aug 2013 04:43:41 -  1.29
+++ create.c20 Jun 2014 12:45:35 -
@@ -58,6 +58,7 @@ extern int ftsoptions;
 extern int dflag, iflag, nflag, sflag;
 extern u_int keys;
 extern char fullpath[MAXPATHLEN];
+extern char *excludefile;
 
 static gid_t gid;
 static uid_t uid;
@@ -82,6 +83,8 @@ cwalk(void)
(void)printf(
"#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
getlogin(), host, fullpath, ctime(&clock));
+   if (excludefile)
+   printf("#\texclude: %s\n", excludefile);
 
argv[0] = ".";
argv[1] = NULL;
@@ -90,6 +93,10 @@ cwalk(void)
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   1 Jan 1970 00:00:00 -
+++ excludes.c  20 Jun 2014 12:45:35 -
@@ -0,0 +1,118 @@
+/*
+ * 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 
+#include "mtree.h"
+#include "extern.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);
+}
+
+int
+read_excludes_file(const char *name)
+{
+   FILE *fp;
+   char *buf, *lbuf;
+   struct exclude *e;
+   size_t len;
+
+
+   fp = fopen(name, "r");
+   if (fp == NULL) {
+   return 1;
+   }
+
+   lbuf = NULL;
+   while ((buf = fgetln(fp, &len))) {
+   if (buf[len - 1] == '\n') {
+   if (len == 1) 
+   continue;
+   buf[len - 1] = '\0';
+   } else {
+   len++;
+   if ((lbuf = malloc(len)) == NULL)
+   err(1, NULL);
+   memcpy(lbuf, buf, len - 1);
+   lbuf[len - 1] = '\0';
+   buf = lbuf;
+   }
+
+   if ((e = malloc(sizeof *e)) == NULL)
+   err(1, NULL);
+   if ((e->glob = malloc(len)) == NULL)
+   err(1, NULL);
+   memcpy((char *) e->glob, buf, len);
+   if (strchr(e->glob, '/'))
+ 

Re: exclude-list in mtree (2)

2014-06-20 Thread Tobias Stoeckmann
On Fri, Jun 20, 2014 at 04:34:19PM +0200, Manuel Giraud wrote:
> + lbuf = NULL;
> + while ((buf = fgetln(fp, &len))) {
> + if (buf[len - 1] == '\n') {
> + if (len == 1) 
> + continue;
> + buf[len - 1] = '\0';
> + } else {
> + len++;
> + if ((lbuf = malloc(len)) == NULL)
> + err(1, NULL);
> + memcpy(lbuf, buf, len - 1);
> + lbuf[len - 1] = '\0';
> + buf = lbuf;
> + }

What is the rational behind checking for len == 1 in '\n'
case, but not for "last line without new line"?

If you want to skip empty lines, you should check after
the if/else-block for buf[0] == '\0', imho.


Tobias



Re: exclude-list in mtree (2)

2014-06-20 Thread Manuel Giraud
Tobias Stoeckmann  writes:

> On Fri, Jun 20, 2014 at 04:34:19PM +0200, Manuel Giraud wrote:
>> +lbuf = NULL;
>> +while ((buf = fgetln(fp, &len))) {
>> +if (buf[len - 1] == '\n') {
>> +if (len == 1) 
>> +continue;
>> +buf[len - 1] = '\0';
>> +} else {
>> +len++;
>> +if ((lbuf = malloc(len)) == NULL)
>> +err(1, NULL);
>> +memcpy(lbuf, buf, len - 1);
>> +lbuf[len - 1] = '\0';
>> +buf = lbuf;
>> +}
>
> What is the rational behind checking for len == 1 in '\n'
> case, but not for "last line without new line"?
>
> If you want to skip empty lines, you should check after
> the if/else-block for buf[0] == '\0', imho.

Ok:
Index: Makefile
===
RCS file: /cvs/src/usr.sbin/mtree/Makefile,v
retrieving revision 1.9
diff -u -p -r1.9 Makefile
--- Makefile15 Apr 2013 06:25:18 -  1.9
+++ Makefile20 Jun 2014 15:33:51 -
@@ -3,6 +3,6 @@
 PROG=  mtree
 #CFLAGS+=-DDEBUG
 MAN=   mtree.8
-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
 
 .include 
Index: create.c
===
RCS file: /cvs/src/usr.sbin/mtree/create.c,v
retrieving revision 1.29
diff -u -p -r1.29 create.c
--- create.c22 Aug 2013 04:43:41 -  1.29
+++ create.c20 Jun 2014 15:33:51 -
@@ -58,6 +58,7 @@ extern int ftsoptions;
 extern int dflag, iflag, nflag, sflag;
 extern u_int keys;
 extern char fullpath[MAXPATHLEN];
+extern char *excludefile;
 
 static gid_t gid;
 static uid_t uid;
@@ -82,6 +83,8 @@ cwalk(void)
(void)printf(
"#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
getlogin(), host, fullpath, ctime(&clock));
+   if (excludefile)
+   printf("#\texclude: %s\n", excludefile);
 
argv[0] = ".";
argv[1] = NULL;
@@ -90,6 +93,10 @@ cwalk(void)
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   1 Jan 1970 00:00:00 -
+++ excludes.c  20 Jun 2014 15:33:51 -
@@ -0,0 +1,118 @@
+/*
+ * 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 
+#include "mtree.h"
+#include "extern.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);
+}
+
+int
+read_excludes_file(const char *name)
+{
+   FILE *fp;
+   char *buf, *lbuf;
+   struct exclude *e;
+   size_t len;
+
+
+   fp = fopen(name, "r");
+   if (fp == NULL) {
+   return 1;
+