summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2019-06-23 10:07:49 -0400
committerTavian Barnes <tavianator@tavianator.com>2019-06-25 01:18:47 -0400
commitc8f7eca0eb6327e8b8ea066af55183a135818fe1 (patch)
treecffd779c202fe52cfe0b603372a33b77721214c9
parent8d393a4e1a37a78bccb3928379693529f78ff20e (diff)
downloadbfs-c8f7eca0eb6327e8b8ea066af55183a135818fe1.tar.xz
util: Filter out . and .. in xreaddir()
-rw-r--r--bftw.c3
-rw-r--r--eval.c33
-rw-r--r--util.c20
3 files changed, 22 insertions, 34 deletions
diff --git a/bftw.c b/bftw.c
index 6772408..4c0ac63 100644
--- a/bftw.c
+++ b/bftw.c
@@ -1259,9 +1259,6 @@ start:
while (bftw_reader_read(reader) > 0) {
const char *name = reader->de->d_name;
- if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) {
- continue;
- }
switch (bftw_visit(&state, reader->dir, name, BFTW_PRE)) {
case BFTW_CONTINUE:
diff --git a/eval.c b/eval.c
index a455a58..d2eea03 100644
--- a/eval.c
+++ b/eval.c
@@ -423,25 +423,13 @@ bool eval_empty(const struct expr *expr, struct eval_state *state) {
goto done;
}
- ret = true;
-
- while (true) {
- struct dirent *de;
- if (xreaddir(dir, &de) != 0) {
- eval_report_error(state);
- goto done_dir;
- }
- if (!de) {
- break;
- }
-
- if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) {
- ret = false;
- break;
- }
+ struct dirent *de;
+ if (xreaddir(dir, &de) == 0) {
+ ret = !de;
+ } else {
+ eval_report_error(state);
}
- done_dir:
closedir(dir);
} else {
const struct bfs_stat *statbuf = eval_stat(state);
@@ -1282,14 +1270,9 @@ static int infer_fdlimit(const struct cmdline *cmdline) {
// Account for 'dir' itself
nopen = -1;
- while (true) {
- struct dirent *de;
- if (xreaddir(dir, &de) != 0 || !de) {
- break;
- }
- if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) {
- ++nopen;
- }
+ struct dirent *de;
+ while (xreaddir(dir, &de) == 0 && de) {
+ ++nopen;
}
closedir(dir);
diff --git a/util.c b/util.c
index 2a01af1..01fb1ae 100644
--- a/util.c
+++ b/util.c
@@ -41,12 +41,20 @@
#endif
int xreaddir(DIR *dir, struct dirent **de) {
- errno = 0;
- *de = readdir(dir);
- if (!*de && errno != 0) {
- return -1;
- } else {
- return 0;
+ while (true) {
+ errno = 0;
+ *de = readdir(dir);
+
+ if (*de) {
+ const char *name = (*de)->d_name;
+ if (name[0] != '.' || (name[1] != '\0' && (name[1] != '.' || name[2] != '\0'))) {
+ return 0;
+ }
+ } else if (errno != 0) {
+ return -1;
+ } else {
+ return 0;
+ }
}
}