svn commit: r368467 - in head: bin/chflags bin/chmod bin/cp bin/ls bin/rm bin/setfacl contrib/mtree usr.bin/du usr.bin/grep usr.bin/gzip usr.sbin/chown usr.sbin/ckdist usr.sbin/fmtree usr.sbin/setfmac

Bryan Drewery bdrewery at FreeBSD.org
Tue Dec 8 23:38:30 UTC 2020


Author: bdrewery
Date: Tue Dec  8 23:38:26 2020
New Revision: 368467
URL: https://svnweb.freebsd.org/changeset/base/368467

Log:
  fts_read: Handle error from a NULL return better.
  
  This is addressing cases such as fts_read(3) encountering an [EIO]
  from fchdir(2) when FTS_NOCHDIR is not set.  That would otherwise be
  seen as a successful traversal in some of these cases while silently
  discarding expected work.
  
  As noted in r264201, fts_read() does not set errno to 0 on a successful
  EOF so it needs to be set before calling it.  Otherwise we might see
  a random error from one of the iterations.
  
  gzip is ignoring most errors and could be improved separately.
  
  Reviewed by:	vangyzen
  Sponsored by:	Dell EMC
  Differential Revision:	https://reviews.freebsd.org/D27184

Modified:
  head/bin/chflags/chflags.c
  head/bin/chmod/chmod.c
  head/bin/cp/cp.c
  head/bin/ls/ls.c
  head/bin/rm/rm.c
  head/bin/setfacl/setfacl.c
  head/contrib/mtree/create.c
  head/contrib/mtree/verify.c
  head/usr.bin/du/du.c
  head/usr.bin/grep/util.c
  head/usr.bin/gzip/gzip.c
  head/usr.sbin/chown/chown.c
  head/usr.sbin/ckdist/ckdist.c
  head/usr.sbin/fmtree/create.c
  head/usr.sbin/fmtree/verify.c
  head/usr.sbin/setfmac/setfmac.c

Modified: head/bin/chflags/chflags.c
==============================================================================
--- head/bin/chflags/chflags.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/chflags/chflags.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -163,7 +163,7 @@ main(int argc, char *argv[])
 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
 		err(1, NULL);
 
-	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
+	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
 		int atflag;
 
 		if ((fts_options & FTS_LOGICAL) ||

Modified: head/bin/chmod/chmod.c
==============================================================================
--- head/bin/chmod/chmod.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/chmod/chmod.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -164,7 +164,7 @@ done:	argv += optind;
 
 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
 		err(1, "fts_open");
-	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
+	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
 		int atflag;
 
 		if ((fts_options & FTS_LOGICAL) ||

Modified: head/bin/cp/cp.c
==============================================================================
--- head/bin/cp/cp.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/cp/cp.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -282,7 +282,8 @@ copy(char *argv[], enum op type, int fts_options)
 
 	if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
 		err(1, "fts_open");
-	for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
+	for (badcp = rval = 0; errno = 0, (curr = fts_read(ftsp)) != NULL;
+            badcp = 0) {
 		switch (curr->fts_info) {
 		case FTS_NS:
 		case FTS_DNR:

Modified: head/bin/ls/ls.c
==============================================================================
--- head/bin/ls/ls.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/ls/ls.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -645,7 +645,7 @@ traverse(int argc, char *argv[], int options)
 	ch_options = !f_recursive && !f_label &&
 	    options & FTS_NOSTAT ? FTS_NAMEONLY : 0;
 
-	while ((p = fts_read(ftsp)) != NULL)
+	while (errno = 0, (p = fts_read(ftsp)) != NULL)
 		switch (p->fts_info) {
 		case FTS_DC:
 			warnx("%s: directory causes a cycle", p->fts_name);

Modified: head/bin/rm/rm.c
==============================================================================
--- head/bin/rm/rm.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/rm/rm.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -207,7 +207,7 @@ rm_tree(char **argv)
 			return;
 		err(1, "fts_open");
 	}
-	while ((p = fts_read(fts)) != NULL) {
+	while (errno = 0, (p = fts_read(fts)) != NULL) {
 		switch (p->fts_info) {
 		case FTS_DNR:
 			if (!fflag || p->fts_errno != ENOENT) {

Modified: head/bin/setfacl/setfacl.c
==============================================================================
--- head/bin/setfacl/setfacl.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/bin/setfacl/setfacl.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -498,8 +498,10 @@ main(int argc, char *argv[])
 	/* Open all files. */
 	if ((ftsp = fts_open(files_list, fts_options | FTS_NOSTAT, 0)) == NULL)
 		err(1, "fts_open");
-	while ((file = fts_read(ftsp)) != NULL)
+	while (errno = 0, (file = fts_read(ftsp)) != NULL)
 		carried_error += handle_file(ftsp, file);
+	if (errno != 0)
+		err(1, "fts_read");
 
 	return (carried_error);
 }

Modified: head/contrib/mtree/create.c
==============================================================================
--- head/contrib/mtree/create.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/contrib/mtree/create.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -129,7 +129,7 @@ cwalk(FILE *fp)
 
 	if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
 		mtree_err("fts_open: %s", strerror(errno));
-	while ((p = fts_read(t)) != NULL) {
+	while (errno = 0, (p = fts_read(t)) != NULL) {
 		if (jflag)
 			indent = p->fts_level * 4;
 		if (check_excludes(p->fts_name, p->fts_path)) {
@@ -173,6 +173,8 @@ cwalk(FILE *fp)
 
 		}
 	}
+	if (errno != 0)
+		mtree_err("fts_read: %s", strerror(errno));
 	fts_close(t);
 	if (sflag && keys & F_CKSUM)
 		mtree_err("%s checksum: %u", fullpath, crc_total);

Modified: head/contrib/mtree/verify.c
==============================================================================
--- head/contrib/mtree/verify.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/contrib/mtree/verify.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -90,7 +90,7 @@ vwalk(void)
 		mtree_err("fts_open: %s", strerror(errno));
 	level = root;
 	specdepth = rval = 0;
-	while ((p = fts_read(t)) != NULL) {
+	while (errno = 0, (p = fts_read(t)) != NULL) {
 		if (check_excludes(p->fts_name, p->fts_path)) {
 			fts_set(t, p, FTS_SKIP);
 			continue;
@@ -160,6 +160,8 @@ vwalk(void)
 		}
 		fts_set(t, p, FTS_SKIP);
 	}
+	if (errno != 0)
+		mtree_err("fts_read: %s", strerror(errno));
 	fts_close(t);
 	if (sflag)
 		warnx("%s checksum: %u", fullpath, crc_total);

Modified: head/usr.bin/du/du.c
==============================================================================
--- head/usr.bin/du/du.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.bin/du/du.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -268,7 +268,7 @@ main(int argc, char *argv[])
 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
 		err(1, "fts_open");
 
-	while ((p = fts_read(fts)) != NULL) {
+	while (errno = 0, (p = fts_read(fts)) != NULL) {
 		switch (p->fts_info) {
 		case FTS_D:			/* Ignore. */
 			if (ignorep(p))

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.bin/grep/util.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -154,7 +154,7 @@ grep_tree(char **argv)
 	    __DECONST(char * const *, wd) : argv, fts_flags, NULL);
 	if (fts == NULL)
 		err(2, "fts_open");
-	while ((p = fts_read(fts)) != NULL) {
+	while (errno = 0, (p = fts_read(fts)) != NULL) {
 		switch (p->fts_info) {
 		case FTS_DNR:
 			/* FALLTHROUGH */
@@ -187,6 +187,8 @@ grep_tree(char **argv)
 			break;
 		}
 	}
+	if (errno != 0)
+		err(2, "fts_read");
 
 	fts_close(fts);
 	return (matched);

Modified: head/usr.bin/gzip/gzip.c
==============================================================================
--- head/usr.bin/gzip/gzip.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.bin/gzip/gzip.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -2075,7 +2075,7 @@ handle_dir(char *dir)
 		return;
 	}
 
-	while ((entry = fts_read(fts))) {
+	while (errno = 0, (entry = fts_read(fts))) {
 		switch(entry->fts_info) {
 		case FTS_D:
 		case FTS_DP:
@@ -2090,6 +2090,8 @@ handle_dir(char *dir)
 			handle_file(entry->fts_path, entry->fts_statp);
 		}
 	}
+	if (errno != 0)
+		warn("error with fts_read %s", dir);
 	(void)fts_close(fts);
 }
 #endif

Modified: head/usr.sbin/chown/chown.c
==============================================================================
--- head/usr.sbin/chown/chown.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.sbin/chown/chown.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -177,7 +177,7 @@ main(int argc, char **argv)
 	if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
 		err(1, NULL);
 
-	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
+	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
 		int atflag;
 
 		if ((fts_options & FTS_LOGICAL) ||

Modified: head/usr.sbin/ckdist/ckdist.c
==============================================================================
--- head/usr.sbin/ckdist/ckdist.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.sbin/ckdist/ckdist.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -151,7 +151,7 @@ main(int argc, char *argv[])
 	    arg[0] = *argv;
 	    if ((ftsp = fts_open(arg, FTS_LOGICAL, NULL)) == NULL)
 		err(2, "fts_open");
-	    while ((f = fts_read(ftsp)) != NULL)
+	    while (errno = 0, (f = fts_read(ftsp)) != NULL)
 		switch (f->fts_info) {
 		case FTS_DC:
 		    rval = fail(f->fts_path, "Directory causes a cycle");

Modified: head/usr.sbin/fmtree/create.c
==============================================================================
--- head/usr.sbin/fmtree/create.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.sbin/fmtree/create.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -102,7 +102,7 @@ cwalk(void)
 	argv[1] = NULL;
 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
 		err(1, "fts_open()");
-	while ((p = fts_read(t))) {
+	while (errno = 0, (p = fts_read(t))) {
 		if (iflag)
 			indent = p->fts_level * 4;
 		if (check_excludes(p->fts_name, p->fts_path)) {
@@ -137,6 +137,8 @@ cwalk(void)
 
 		}
 	}
+	if (errno != 0)
+		err(1, "fts_read()");
 	(void)fts_close(t);
 	if (sflag && keys & F_CKSUM)
 		warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);

Modified: head/usr.sbin/fmtree/verify.c
==============================================================================
--- head/usr.sbin/fmtree/verify.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.sbin/fmtree/verify.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -86,7 +86,7 @@ vwalk(void)
 		err(1, "line %d: fts_open", lineno);
 	level = root;
 	specdepth = rval = 0;
-	while ((p = fts_read(t))) {
+	while (errno = 0, (p = fts_read(t))) {
 		if (check_excludes(p->fts_name, p->fts_path)) {
 			fts_set(t, p, FTS_SKIP);
 			continue;
@@ -149,6 +149,8 @@ extra:
 		}
 		(void)fts_set(t, p, FTS_SKIP);
 	}
+	if (errno != 0)
+		err(1, "fts_read()");
 	(void)fts_close(t);
 	if (sflag)
 		warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);

Modified: head/usr.sbin/setfmac/setfmac.c
==============================================================================
--- head/usr.sbin/setfmac/setfmac.c	Tue Dec  8 22:37:30 2020	(r368466)
+++ head/usr.sbin/setfmac/setfmac.c	Tue Dec  8 23:38:26 2020	(r368467)
@@ -142,7 +142,7 @@ main(int argc, char **argv)
 	fts = fts_open(argv, hflag | xflag, NULL);
 	if (fts == NULL)
 		err(1, "cannot traverse filesystem%s", argc ? "s" : "");
-	while ((ftsent = fts_read(fts)) != NULL) {
+	while (errno = 0, (ftsent = fts_read(fts)) != NULL) {
 		switch (ftsent->fts_info) {
 		case FTS_DP:		/* skip post-order */
 			break;
@@ -176,6 +176,8 @@ main(int argc, char **argv)
 			    ftsent->fts_info, ftsent->fts_path);
 		}
 	}
+	if (errno != 0)
+		err(1, "fts_read");
 	fts_close(fts);
 	exit(0);
 }


More information about the svn-src-head mailing list