Some system can return DT_UNKNOWN in readdir's struct dirent::d_type
and we must handle it properly. In this case we can directly check
if the entity we found is directory and skip it.

Making is_directory function global.

Reported-by: Michael Petlan <mpet...@redhat.com>
Link: http://lkml.kernel.org/n/tip-hgwpktyrx657m1i255da4...@git.kernel.org
Signed-off-by: Jiri Olsa <jo...@kernel.org>
---
 tools/perf/builtin-script.c     | 14 +-------------
 tools/perf/tests/builtin-test.c | 10 +++++-----
 tools/perf/util/path.c          | 13 +++++++++++++
 tools/perf/util/path.h          |  4 ++++
 4 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 39d8b55f0db3..a5c1361ccf26 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -26,6 +26,7 @@
 #include "util/string2.h"
 #include "util/thread-stack.h"
 #include "util/time-utils.h"
+#include "util/path.h"
 #include "print_binary.h"
 #include <linux/bitmap.h>
 #include <linux/kernel.h>
@@ -2399,19 +2400,6 @@ static int parse_output_fields(const struct option *opt 
__maybe_unused,
        return rc;
 }
 
-/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
-static int is_directory(const char *base_path, const struct dirent *dent)
-{
-       char path[PATH_MAX];
-       struct stat st;
-
-       sprintf(path, "%s/%s", base_path, dent->d_name);
-       if (stat(path, &st))
-               return 0;
-
-       return S_ISDIR(st.st_mode);
-}
-
 #define for_each_lang(scripts_path, scripts_dir, lang_dirent)          \
        while ((lang_dirent = readdir(scripts_dir)) != NULL)            \
                if ((lang_dirent->d_type == DT_DIR ||                   \
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 766573e236e4..fafa014240cd 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -411,9 +411,9 @@ static const char *shell_test__description(char 
*description, size_t size,
        return description ? trim(description + 1) : NULL;
 }
 
-#define for_each_shell_test(dir, ent)          \
+#define for_each_shell_test(dir, base, ent)    \
        while ((ent = readdir(dir)) != NULL)    \
-               if (ent->d_type == DT_REG && ent->d_name[0] != '.')
+               if (!is_directory(base, ent))
 
 static const char *shell_tests__dir(char *path, size_t size)
 {
@@ -452,7 +452,7 @@ static int shell_tests__max_desc_width(void)
        if (!dir)
                return -1;
 
-       for_each_shell_test(dir, ent) {
+       for_each_shell_test(dir, path, ent) {
                char bf[256];
                const char *desc = shell_test__description(bf, sizeof(bf), 
path, ent->d_name);
 
@@ -504,7 +504,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(dir, st.dir, ent) {
                int curr = i++;
                char desc[256];
                struct test test = {
@@ -614,7 +614,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(dir, path, ent) {
                int curr = i++;
                char bf[256];
                struct test t = {
diff --git a/tools/perf/util/path.c b/tools/perf/util/path.c
index 933f5c6bffb4..ab1be320f3ef 100644
--- a/tools/perf/util/path.c
+++ b/tools/perf/util/path.c
@@ -77,3 +77,16 @@ bool is_regular_file(const char *file)
 
        return S_ISREG(st.st_mode);
 }
+
+/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
+bool is_directory(const char *base_path, const struct dirent *dent)
+{
+       char path[PATH_MAX];
+       struct stat st;
+
+       sprintf(path, "%s/%s", base_path, dent->d_name);
+       if (stat(path, &st))
+               return false;
+
+       return S_ISDIR(st.st_mode);
+}
diff --git a/tools/perf/util/path.h b/tools/perf/util/path.h
index 14a254ada7eb..f0f3a6bedc57 100644
--- a/tools/perf/util/path.h
+++ b/tools/perf/util/path.h
@@ -2,9 +2,13 @@
 #ifndef _PERF_PATH_H
 #define _PERF_PATH_H
 
+#include <sys/types.h>
+#include <dirent.h>
+
 int path__join(char *bf, size_t size, const char *path1, const char *path2);
 int path__join3(char *bf, size_t size, const char *path1, const char *path2, 
const char *path3);
 
 bool is_regular_file(const char *file);
+bool is_directory(const char *base_path, const struct dirent *dent);
 
 #endif /* _PERF_PATH_H */
-- 
2.13.6

Reply via email to