]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/commitdiff
perf tools: Add lsdir() helper to read a directory
authorMasami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Tue, 26 Apr 2016 09:02:42 +0000 (18:02 +0900)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 26 Apr 2016 16:14:55 +0000 (13:14 -0300)
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>
tools/perf/util/util.c
tools/perf/util/util.h

index b7766c577b015d978fd3e9960c451692f81daa6e..9473d46c00bba0985c28eafa0cf687f589c63dfa 100644 (file)
@@ -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;
index 3bf3de86d4297b5f21e893f2e6b315bc8bbe3abd..26a924651e7be18e1683b5e36d741d3231f3e567 100644 (file)
@@ -79,6 +79,7 @@
 #include <termios.h>
 #include <linux/bitops.h>
 #include <termios.h>
+#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);