diff options
author | Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> | 2016-04-26 05:02:42 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-04-26 12:14:55 -0400 |
commit | e1ce726e1db2522b4848b3acffb7ece12439517c (patch) | |
tree | 4aa431756eb86d941aeffd06d955a681c3a3eb8d /tools | |
parent | 062d6c2aec0e087be956494a73221c04eca115fe (diff) |
perf tools: Add lsdir() helper to read a directory
As a utility function, add lsdir() which reads given directory and store
entry name into a strlist. lsdir accepts a filter function so that user
can filter out unneeded entries.
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Hemant Kumar <hemant@linux.vnet.ibm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160426090242.11891.79014.stgit@devbox
[ Do not use the 'dirname' it is used in some distros ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/perf/util/util.c | 34 | ||||
-rw-r--r-- | tools/perf/util/util.h | 3 |
2 files changed, 37 insertions, 0 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index b7766c577b01..9473d46c00bb 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -117,6 +117,40 @@ int rm_rf(char *path) | |||
117 | return rmdir(path); | 117 | return rmdir(path); |
118 | } | 118 | } |
119 | 119 | ||
120 | /* A filter which removes dot files */ | ||
121 | bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d) | ||
122 | { | ||
123 | return d->d_name[0] != '.'; | ||
124 | } | ||
125 | |||
126 | /* lsdir reads a directory and store it in strlist */ | ||
127 | struct strlist *lsdir(const char *name, | ||
128 | bool (*filter)(const char *, struct dirent *)) | ||
129 | { | ||
130 | struct strlist *list = NULL; | ||
131 | DIR *dir; | ||
132 | struct dirent *d; | ||
133 | |||
134 | dir = opendir(name); | ||
135 | if (!dir) | ||
136 | return NULL; | ||
137 | |||
138 | list = strlist__new(NULL, NULL); | ||
139 | if (!list) { | ||
140 | errno = -ENOMEM; | ||
141 | goto out; | ||
142 | } | ||
143 | |||
144 | while ((d = readdir(dir)) != NULL) { | ||
145 | if (!filter || filter(name, d)) | ||
146 | strlist__add(list, d->d_name); | ||
147 | } | ||
148 | |||
149 | out: | ||
150 | closedir(dir); | ||
151 | return list; | ||
152 | } | ||
153 | |||
120 | static int slow_copyfile(const char *from, const char *to) | 154 | static int slow_copyfile(const char *from, const char *to) |
121 | { | 155 | { |
122 | int err = -1; | 156 | int err = -1; |
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 3bf3de86d429..26a924651e7b 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
@@ -79,6 +79,7 @@ | |||
79 | #include <termios.h> | 79 | #include <termios.h> |
80 | #include <linux/bitops.h> | 80 | #include <linux/bitops.h> |
81 | #include <termios.h> | 81 | #include <termios.h> |
82 | #include "strlist.h" | ||
82 | 83 | ||
83 | extern const char *graph_line; | 84 | extern const char *graph_line; |
84 | extern const char *graph_dotted_line; | 85 | extern const char *graph_dotted_line; |
@@ -222,6 +223,8 @@ static inline int sane_case(int x, int high) | |||
222 | 223 | ||
223 | int mkdir_p(char *path, mode_t mode); | 224 | int mkdir_p(char *path, mode_t mode); |
224 | int rm_rf(char *path); | 225 | int rm_rf(char *path); |
226 | struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *)); | ||
227 | bool lsdir_no_dot_filter(const char *name, struct dirent *d); | ||
225 | int copyfile(const char *from, const char *to); | 228 | int copyfile(const char *from, const char *to); |
226 | int copyfile_mode(const char *from, const char *to, mode_t mode); | 229 | int copyfile_mode(const char *from, const char *to, mode_t mode); |
227 | int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size); | 230 | int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size); |