perf tests: Handle properly readdir DT_UNKNOWN
Jiri Olsa
reviewerPATCH 1/1 perf tests: Handle properly readdir DT_UNKNOWN
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.
--- 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(-)
tools/perf/builtin-script.c +1 -13
@@ -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 || \
tools/perf/tests/builtin-test.c +5 -5
@@ -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 = {
tools/perf/util/path.c +13 -0
@@ -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); +}
tools/perf/util/path.h +4 -0
@@ -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 */
Signature · 1 line
2.13.6
Arnaldo Carvalho de Melo
maintainerEm Wed, Dec 06, 2017 at 06:45:35PM +0100, Jiri Olsa escreveu:
Quoted 5 lines
Some system can return
DT_UNKNOWNin readdir's structdirent::d_typeand we must handle it properly. In this case we can directly check if the entity we found is directory and skip it.Making
is_directoryfunction global.
Split this part into a prep patch and left with the previous paragraph the 'perf test' bits.
[acme@jouet perf]$ git log --oneline -2de7cee586145 (HEAD -> perf/core, acme.korg/perf/core) perf test: Handle properly readdir DT_UNKNOWN ef87e1133606 perf utils: Move is_directory() to path.h
[acme@jouet perf]$
Thanks,
- Arnaldo
Quoted 126 lines
Reported-by Michael Petlan <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- 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(-)tools/perf/builtin-script.c +1
-13@@ -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 || \tools/perf/tests/builtin-test.c +5
-5@@ -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 = {tools/perf/util/path.c +13
-0@@ -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); +}tools/perf/util/path.h +4
-0@@ -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 */ Signature · 1 line
2.13.6
Quoted 7 lines · Arnaldo Carvalho de Melo
Em Wed, Dec 06, 2017 at 06:45:35PM +0100, Jiri Olsa escreveu:
Quoted 5 lines
Some system can return
DT_UNKNOWNin readdir's structdirent::d_typeand we must handle it properly. In this case we can directly check if the entity we found is directory and skip it.Making
is_directoryfunction global.
Quoted 7 lines
Split this part into a prep patch and left with the previous paragraph the 'perf test' bits.
[acme@jouet perf]$ git log --oneline -2
de7cee586145(HEAD -> perf/core, acme.korg/perf/core) perf test: Handle properly readdirDT_UNKNOWNef87e1133606perf utils: Moveis_directory()topath.h
[acme@jouet perf]$
thanks,
jirka
tip-bot for Jiri Olsa
merge bot[tip:perf/core] perf utils: Move is_directory() to path.h
In reply to Jiri Olsaperf utils: Move is_directory() to path.h
Commit-ID: 06c3f2aa9fc68e7f3fe3d83e7569d2a2801d9f99 Gitweb: https://git.kernel.org/tip/06c3f2aa9fc68e7f3fe3d83e7569d2a2801d9f99 Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 6 Dec 2017 18:45:35 +0100 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Dec 2017 12:15:48 -0300
perf utils: Move is_directory() to path.h
So that it can be used more widely, like in the next patch, when it will be used to fix a bug in 'perf test' handling of dirent.d_type == DT_UNKNOWN.
--- tools/perf/builtin-script.c | 14 +------------- tools/perf/util/path.c | 14 ++++++++++++++ tools/perf/util/path.h | 3 +++ 3 files changed, 18 insertions(+), 13 deletions(-)
tools/perf/builtin-script.c +1 -13
@@ -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>
@@ -2401,19 +2402,6 @@ out: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 || \
tools/perf/util/path.c +14 -0
@@ -18,6 +18,7 @@#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> +#include <dirent.h> #include <unistd.h> static char bad_path[] ="/bad-path/" ;
@@ -77,3 +78,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); +}
tools/perf/util/path.h +3 -0
@@ -2,9 +2,12 @@#ifndef _PERF_PATH_H#define _PERF_PATH_H +struct dirent ; +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 */
tip-bot for Jiri Olsa
merge bot[tip:perf/core] perf test: Handle properly readdir DT_UNKNOWN
In reply to Jiri Olsaperf test: Handle properly readdir DT_UNKNOWN
Commit-ID: 378811ac303df13efbe49f3ad1795b63d334ac5d Gitweb: https://git.kernel.org/tip/378811ac303df13efbe49f3ad1795b63d334ac5d Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 6 Dec 2017 18:45:35 +0100 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Dec 2017 12:15:48 -0300
perf test: Handle properly readdir DT_UNKNOWN
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.
--- tools/perf/tests/builtin-test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/perf/tests/builtin-test.c +5 -5
@@ -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 = {