[PATCH] perf test: use stat() to check type of file instead of readdir

From: Li Zhijian
Date: Thu Oct 19 2017 - 03:34:01 EST


according to man-page: http://man7.org/linux/man-pages/man3/readdir.3.html
d_type is not supported by all filesystems
-------------
Currently, only some filesystems (among them: Btrfs, ext2,
ext3, and ext4) have full support for returning the file type
in d_type. All applications must properly handle a return of
DT_UNKNOWN.
-------------
so if we run the perf test under xfs, all cases under tests/shell/ will not be
covered previously.

Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxxxxx>
---
tools/perf/tests/builtin-test.c | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 377bea0..0bf1c27 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -409,9 +409,16 @@ static const char *shell_test__description(char *description, size_t size,
return description ? trim(description + 1) : NULL;
}

-#define for_each_shell_test(dir, ent) \
- while ((ent = readdir(dir)) != NULL) \
- if (ent->d_type == DT_REG && ent->d_name[0] != '.')
+#define for_each_shell_test(path, dir, ent) \
+ while ((ent = readdir(dir)) != NULL) { \
+ int ret; \
+ struct stat sb; \
+ char pathname[4096]; \
+ snprintf(pathname, 4096, "%s/%s", path, ent->d_name); \
+ ret = stat(pathname, &sb); \
+ if (ret == 0 && S_ISREG(sb.st_mode) && ent->d_name[0] != '.')
+
+#define for_each_shell_test_end() }

static const char *shell_tests__dir(char *path, size_t size)
{
@@ -450,7 +457,7 @@ static int shell_tests__max_desc_width(void)
if (!dir)
return -1;

- for_each_shell_test(dir, ent) {
+ for_each_shell_test(path, dir, ent) {
char bf[256];
const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);

@@ -461,6 +468,7 @@ static int shell_tests__max_desc_width(void)
width = len;
}
}
+ for_each_shell_test_end()

closedir(dir);
return width;
@@ -502,7 +510,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
if (!dir)
return -1;

- for_each_shell_test(dir, ent) {
+ for_each_shell_test(st.dir, dir, ent) {
int curr = i++;
char desc[256];
struct test test = {
@@ -518,6 +526,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
pr_info("%2d: %-*s:", i, width, test.desc);
test_and_print(&test, false, -1);
}
+ for_each_shell_test_end()

closedir(dir);
return 0;
@@ -612,7 +621,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
if (!dir)
return -1;

- for_each_shell_test(dir, ent) {
+ for_each_shell_test(path, dir, ent) {
int curr = i++;
char bf[256];
struct test t = {
@@ -624,6 +633,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i)

pr_info("%2d: %s\n", i, t.desc);
}
+ for_each_shell_test_end()

closedir(dir);
return 0;
--
2.7.4