Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753451AbcD0Pfw (ORCPT ); Wed, 27 Apr 2016 11:35:52 -0400 Received: from terminus.zytor.com ([198.137.202.10]:51118 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751550AbcD0Pfu (ORCPT ); Wed, 27 Apr 2016 11:35:50 -0400 Date: Wed, 27 Apr 2016 08:35:10 -0700 From: tip-bot for Masami Hiramatsu Message-ID: Cc: linux-kernel@vger.kernel.org, mingo@kernel.org, ananth@linux.vnet.ibm.com, hpa@zytor.com, hemant@linux.vnet.ibm.com, mhiramat@kernel.org, tglx@linutronix.de, masami.hiramatsu.pt@hitachi.com, acme@redhat.com, peterz@infradead.org, namhyung@kernel.org Reply-To: hemant@linux.vnet.ibm.com, hpa@zytor.com, acme@redhat.com, namhyung@kernel.org, peterz@infradead.org, tglx@linutronix.de, masami.hiramatsu.pt@hitachi.com, mhiramat@kernel.org, linux-kernel@vger.kernel.org, ananth@linux.vnet.ibm.com, mingo@kernel.org In-Reply-To: <20160426090242.11891.79014.stgit@devbox> References: <20160426090242.11891.79014.stgit@devbox> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Add lsdir() helper to read a directory Git-Commit-ID: e1ce726e1db2522b4848b3acffb7ece12439517c X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2988 Lines: 93 Commit-ID: e1ce726e1db2522b4848b3acffb7ece12439517c Gitweb: http://git.kernel.org/tip/e1ce726e1db2522b4848b3acffb7ece12439517c Author: Masami Hiramatsu AuthorDate: Tue, 26 Apr 2016 18:02:42 +0900 Committer: Arnaldo Carvalho de Melo CommitDate: Tue, 26 Apr 2016 13:14:55 -0300 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 Signed-off-by: Masami Hiramatsu Cc: Ananth N Mavinakayanahalli Cc: Hemant Kumar Cc: Namhyung Kim Cc: Peter Zijlstra 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 --- tools/perf/util/util.c | 34 ++++++++++++++++++++++++++++++++++ tools/perf/util/util.h | 3 +++ 2 files changed, 37 insertions(+) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index b7766c5..9473d46 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -117,6 +117,40 @@ int rm_rf(char *path) return rmdir(path); } +/* A filter which removes dot files */ +bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d) +{ + return d->d_name[0] != '.'; +} + +/* lsdir reads a directory and store it in strlist */ +struct strlist *lsdir(const char *name, + bool (*filter)(const char *, struct dirent *)) +{ + struct strlist *list = NULL; + DIR *dir; + struct dirent *d; + + dir = opendir(name); + if (!dir) + return NULL; + + list = strlist__new(NULL, NULL); + if (!list) { + errno = -ENOMEM; + goto out; + } + + while ((d = readdir(dir)) != NULL) { + if (!filter || filter(name, d)) + strlist__add(list, d->d_name); + } + +out: + closedir(dir); + return list; +} + static int slow_copyfile(const char *from, const char *to) { int err = -1; diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 3bf3de8..26a9246 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -79,6 +79,7 @@ #include #include #include +#include "strlist.h" extern const char *graph_line; extern const char *graph_dotted_line; @@ -222,6 +223,8 @@ static inline int sane_case(int x, int high) int mkdir_p(char *path, mode_t mode); int rm_rf(char *path); +struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *)); +bool lsdir_no_dot_filter(const char *name, struct dirent *d); int copyfile(const char *from, const char *to); int copyfile_mode(const char *from, const char *to, mode_t mode); int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);