Skip to content

Commit

Permalink
fts_read: Handle error from a NULL return better.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bdrewery committed Dec 8, 2020
1 parent f1b18a6 commit 2dfa4b6
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bin/chflags/chflags.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
2 changes: 1 addition & 1 deletion bin/chmod/chmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
3 changes: 2 additions & 1 deletion bin/cp/cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion bin/ls/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bin/rm/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion bin/setfacl/setfacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion contrib/mtree/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion contrib/mtree/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion usr.bin/du/du.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion usr.bin/grep/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -187,6 +187,8 @@ grep_tree(char **argv)
break;
}
}
if (errno != 0)
err(2, "fts_read");

fts_close(fts);
return (matched);
Expand Down
4 changes: 3 additions & 1 deletion usr.bin/gzip/gzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion usr.sbin/chown/chown.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
2 changes: 1 addition & 1 deletion usr.sbin/ckdist/ckdist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion usr.sbin/fmtree/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion usr.sbin/fmtree/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,6 +149,8 @@ vwalk(void)
}
(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);
Expand Down
4 changes: 3 additions & 1 deletion usr.sbin/setfmac/setfmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 2dfa4b6

Please sign in to comment.