From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932733AbcFOD14 (ORCPT ); Tue, 14 Jun 2016 23:27:56 -0400 Received: from mail.kernel.org ([198.145.29.136]:58138 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932225AbcFOD1y (ORCPT ); Tue, 14 Jun 2016 23:27:54 -0400 From: Masami Hiramatsu To: Arnaldo Carvalho de Melo Cc: Masami Hiramatsu , linux-kernel@vger.kernel.org, Namhyung Kim , Peter Zijlstra , Ingo Molnar , Hemant Kumar , Ananth N Mavinakayanahalli , Brendan Gregg Subject: [PATCH perf/core v11 01/20] perf: util: Fix rm_rf() to handle non-regular files correctly Date: Wed, 15 Jun 2016 12:27:44 +0900 Message-Id: <20160615032743.31330.20172.stgit@devbox> X-Mailer: git-send-email 2.1.0 In-Reply-To: <20160615032728.31330.45150.stgit@devbox> References: <20160615032728.31330.45150.stgit@devbox> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix rm_rf() to handle non-regular files correctly. This fix includes two changes; - Fix to use lstat(3) instead of stat(3) since if the target file is a symbolic link, rm_rf() should unlink the symbolic link itself, not the file which pointed by the symlink. - Fix to unlink non-regular files (except for directory), including symlink. Even though the first one fixes to stat symlink itself, without second fix, it still failed because the symlink is not a regular file. Signed-off-by: Masami Hiramatsu --- tools/perf/util/util.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 23504ad..e08b9a0 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -97,20 +97,17 @@ int rm_rf(char *path) scnprintf(namebuf, sizeof(namebuf), "%s/%s", path, d->d_name); - ret = stat(namebuf, &statbuf); + /* We have to check symbolic link itself */ + ret = lstat(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)) + if (S_ISDIR(statbuf.st_mode)) ret = rm_rf(namebuf); - else { - pr_debug("unknown file: %s\n", namebuf); - ret = -1; - } + else + ret = unlink(namebuf); } closedir(dir);