diff options
author | Namhyung Kim <namhyung@kernel.org> | 2015-05-18 02:30:17 +0200 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-05-18 15:17:34 +0200 |
commit | 0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8 (patch) | |
tree | 447d4d7618d6d8911cdd5779918b1f44f63fe599 /tools/perf/util/util.c | |
parent | perf tools: Elliminate alignment holes (diff) | |
download | linux-0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8.tar.xz linux-0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8.zip |
perf tools: Add rm_rf() utility function
The rm_rf() function does same as the shell command 'rm -rf' which
removes all directory entries recursively.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1431909055-21442-3-git-send-email-namhyung@kernel.org
Link: http://lkml.kernel.org/r/20150130150256.GF6188@krava.brq.redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/util.c')
-rw-r--r-- | tools/perf/util/util.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 4ee6d0d4c993..6104afb7e1ef 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -72,6 +72,49 @@ int mkdir_p(char *path, mode_t mode) return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; } +int rm_rf(char *path) +{ + DIR *dir; + int ret = 0; + struct dirent *d; + char namebuf[PATH_MAX]; + + dir = opendir(path); + if (dir == NULL) + return 0; + + while ((d = readdir(dir)) != NULL && !ret) { + struct stat statbuf; + + if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + continue; + + scnprintf(namebuf, sizeof(namebuf), "%s/%s", + path, d->d_name); + + ret = stat(namebuf, &statbuf); + if (ret < 0) { + pr_debug("stat failed: %s\n", namebuf); + break; + } + + if (S_ISREG(statbuf.st_mode)) + ret = unlink(namebuf); + else if (S_ISDIR(statbuf.st_mode)) + ret = rm_rf(namebuf); + else { + pr_debug("unknown file: %s\n", namebuf); + ret = -1; + } + } + closedir(dir); + + if (ret < 0) + return ret; + + return rmdir(path); +} + static int slow_copyfile(const char *from, const char *to, mode_t mode) { int err = -1; |