All of lore.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Jiri Olsa <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: adrian.hunter@intel.com, alexander.shishkin@linux.intel.com,
	alexey.budankov@linux.intel.com, eranian@google.com,
	linux-kernel@vger.kernel.org, ak@linux.intel.com,
	namhyung@kernel.org, peterz@infradead.org, mingo@kernel.org,
	tglx@linutronix.de, jolsa@kernel.org, hpa@zytor.com,
	acme@redhat.com
Subject: [tip:perf/core] perf tools: Add pattern name checking to rm_rf
Date: Thu, 28 Feb 2019 00:00:50 -0800	[thread overview]
Message-ID: <tip-cdb6b0235f170a5ffcd74731178efc064bd4d24a@git.kernel.org> (raw)
In-Reply-To: <20190224190656.30163-3-jolsa@kernel.org>

Commit-ID:  cdb6b0235f170a5ffcd74731178efc064bd4d24a
Gitweb:     https://git.kernel.org/tip/cdb6b0235f170a5ffcd74731178efc064bd4d24a
Author:     Jiri Olsa <jolsa@kernel.org>
AuthorDate: Sun, 24 Feb 2019 20:06:38 +0100
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Mon, 25 Feb 2019 10:33:04 -0300

perf tools: Add pattern name checking to rm_rf

Add pattern argument to rm_rf_depth() (and rename it to rm_rf_depth_pat())
to specify the name pattern files need to match inside the directory.

The function fails if we find different file to remove.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20190224190656.30163-3-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/util.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index bcf436892155..02b7a38f98ce 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -21,6 +21,7 @@
 #include <linux/time64.h>
 #include <unistd.h>
 #include "strlist.h"
+#include "string2.h"
 
 /*
  * XXX We need to find a better place for these things...
@@ -117,12 +118,38 @@ int mkdir_p(char *path, mode_t mode)
 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
 }
 
+static bool match_pat(char *file, const char **pat)
+{
+	int i = 0;
+
+	if (!pat)
+		return true;
+
+	while (pat[i]) {
+		if (strglobmatch(file, pat[i]))
+			return true;
+
+		i++;
+	}
+
+	return false;
+}
+
 /*
  * The depth specify how deep the removal will go.
  * 0       - will remove only files under the 'path' directory
  * 1 .. x  - will dive in x-level deep under the 'path' directory
+ *
+ * If specified the pat is array of string patterns ended with NULL,
+ * which are checked upon every file/directory found. Only matching
+ * ones are removed.
+ *
+ * The function returns:
+ *    0 on success
+ *   -1 on removal failure with errno set
+ *   -2 on pattern failure
  */
-static int rm_rf_depth(const char *path, int depth)
+static int rm_rf_depth_pat(const char *path, int depth, const char **pat)
 {
 	DIR *dir;
 	int ret;
@@ -149,6 +176,9 @@ static int rm_rf_depth(const char *path, int depth)
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
 
+		if (!match_pat(d->d_name, pat))
+			return -2;
+
 		scnprintf(namebuf, sizeof(namebuf), "%s/%s",
 			  path, d->d_name);
 
@@ -160,7 +190,7 @@ static int rm_rf_depth(const char *path, int depth)
 		}
 
 		if (S_ISDIR(statbuf.st_mode))
-			ret = depth ? rm_rf_depth(namebuf, depth - 1) : 0;
+			ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0;
 		else
 			ret = unlink(namebuf);
 	}
@@ -174,7 +204,7 @@ static int rm_rf_depth(const char *path, int depth)
 
 int rm_rf(const char *path)
 {
-	return rm_rf_depth(path, INT_MAX);
+	return rm_rf_depth_pat(path, INT_MAX, NULL);
 }
 
 /* A filter which removes dot files */

  reply	other threads:[~2019-02-28  8:01 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-24 19:06 [PATCHv3 00/20] perf record: Add support to store data in directory Jiri Olsa
2019-02-24 19:06 ` [PATCH 01/20] perf tools: Add depth checking to rm_rf Jiri Olsa
2019-02-28  8:00   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 02/20] perf tools: Add pattern name " Jiri Olsa
2019-02-28  8:00   ` tip-bot for Jiri Olsa [this message]
2019-02-24 19:06 ` [PATCH 03/20] perf tools: Add rm_rf_perf_data function Jiri Olsa
2019-02-28  8:01   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 04/20] perf data: Move size to struct perf_data_file Jiri Olsa
2019-02-24 19:06 ` [PATCH 05/20] perf data: Add global path holder Jiri Olsa
2019-02-24 19:06 ` [PATCH 06/20] perf data: Make check_backup work over directories Jiri Olsa
2019-02-28  8:02   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 07/20] perf data: Fail check_backup in case of error Jiri Olsa
2019-02-28  8:02   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 08/20] perf data: Add perf_data__(create_dir|close_dir) functions Jiri Olsa
2019-02-28  8:03   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 09/20] perf data: Add perf_data__open_dir_data function Jiri Olsa
2019-02-28  8:04   ` [tip:perf/core] " tip-bot for Jiri Olsa
2019-02-24 19:06 ` [PATCH 10/20] perf data: Add directory support Jiri Olsa
2019-02-25 13:45   ` Arnaldo Carvalho de Melo
2019-02-25 13:56     ` Jiri Olsa
2019-02-25 15:07       ` Arnaldo Carvalho de Melo
2019-02-25 15:36         ` Jiri Olsa
2019-02-24 19:06 ` [PATCH 11/20] perf data: Don't store auxtrace index for directory data file Jiri Olsa
2019-02-24 19:06 ` [PATCH 12/20] perf data: Add perf_data__update_dir function Jiri Olsa
2019-02-24 19:06 ` [PATCH 13/20] perf data: Make perf_data__size to work over directory Jiri Olsa
2019-02-24 19:06 ` [PATCH 14/20] perf header: Add DIR_FORMAT feature to describe directory data Jiri Olsa
2019-02-24 19:06 ` [PATCH 15/20] perf session: Add process callback to reader object Jiri Olsa
2019-02-24 19:06 ` [PATCH 16/20] perf session: Add __perf_session__process_dir_events function Jiri Olsa
2019-02-24 19:06 ` [PATCH 17/20] perf session: Add path to reader object Jiri Olsa
2019-02-24 19:06 ` [PATCH 18/20] perf record: Add --dir option to store data in directory Jiri Olsa
2019-02-24 19:06 ` [PATCH 19/20] perf record: Add --output-dir " Jiri Olsa
2019-02-24 19:06 ` [PATCH 20/20] perf record: Describe perf.data directory format Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-cdb6b0235f170a5ffcd74731178efc064bd4d24a@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alexey.budankov@linux.intel.com \
    --cc=eranian@google.com \
    --cc=hpa@zytor.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --subject='Re: [tip:perf/core] perf tools: Add pattern name checking to rm_rf' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.