From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753632AbbETMZG (ORCPT ); Wed, 20 May 2015 08:25:06 -0400 Received: from terminus.zytor.com ([198.137.202.10]:56782 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753605AbbETMY7 (ORCPT ); Wed, 20 May 2015 08:24:59 -0400 Date: Wed, 20 May 2015 05:24:25 -0700 From: tip-bot for Namhyung Kim Message-ID: Cc: adrian.hunter@intel.com, andi@firstfloor.org, mingo@kernel.org, namhyung@kernel.org, acme@redhat.com, dsahern@gmail.com, hpa@zytor.com, eranian@google.com, tglx@linutronix.de, linux-kernel@vger.kernel.org, fweisbec@gmail.com, a.p.zijlstra@chello.nl, jolsa@kernel.org Reply-To: adrian.hunter@intel.com, andi@firstfloor.org, mingo@kernel.org, namhyung@kernel.org, acme@redhat.com, dsahern@gmail.com, hpa@zytor.com, eranian@google.com, tglx@linutronix.de, fweisbec@gmail.com, linux-kernel@vger.kernel.org, a.p.zijlstra@chello.nl, jolsa@kernel.org In-Reply-To: <20150130150256.GF6188@krava.brq.redhat.com> References: <1431909055-21442-3-git-send-email-namhyung@kernel.org> <20150130150256.GF6188@krava.brq.redhat.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Add rm_rf() utility function Git-Commit-ID: 0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8 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 Commit-ID: 0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8 Gitweb: http://git.kernel.org/tip/0b1de0be1eac7b23e89cb43c17b02d38ead6b6c8 Author: Namhyung Kim AuthorDate: Mon, 18 May 2015 09:30:17 +0900 Committer: Arnaldo Carvalho de Melo CommitDate: Mon, 18 May 2015 10:17:34 -0300 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 Acked-by: Jiri Olsa Cc: Adrian Hunter Cc: Andi Kleen Cc: David Ahern Cc: Frederic Weisbecker Cc: Peter Zijlstra Cc: Stephane Eranian 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 --- tools/perf/util/util.c | 43 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/util.h | 1 + 2 files changed, 44 insertions(+) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 4ee6d0d..6104afb 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; diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 3601ffd..c4fe38a 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -249,6 +249,7 @@ static inline int sane_case(int x, int high) } int mkdir_p(char *path, mode_t mode); +int rm_rf(char *path); int copyfile(const char *from, const char *to); int copyfile_mode(const char *from, const char *to, mode_t mode);