Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
cdb6b023 05a48659

+33 -3
+33 -3
tools/perf/util/util.c
··· 21 21 #include <linux/time64.h> 22 22 #include <unistd.h> 23 23 #include "strlist.h" 24 + #include "string2.h" 24 25 25 26 /* 26 27 * XXX We need to find a better place for these things... ··· 118 117 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; 119 118 } 120 119 120 + static bool match_pat(char *file, const char **pat) 121 + { 122 + int i = 0; 123 + 124 + if (!pat) 125 + return true; 126 + 127 + while (pat[i]) { 128 + if (strglobmatch(file, pat[i])) 129 + return true; 130 + 131 + i++; 132 + } 133 + 134 + return false; 135 + } 136 + 121 137 /* 122 138 * The depth specify how deep the removal will go. 123 139 * 0 - will remove only files under the 'path' directory 124 140 * 1 .. x - will dive in x-level deep under the 'path' directory 141 + * 142 + * If specified the pat is array of string patterns ended with NULL, 143 + * which are checked upon every file/directory found. Only matching 144 + * ones are removed. 145 + * 146 + * The function returns: 147 + * 0 on success 148 + * -1 on removal failure with errno set 149 + * -2 on pattern failure 125 150 */ 126 - static int rm_rf_depth(const char *path, int depth) 151 + static int rm_rf_depth_pat(const char *path, int depth, const char **pat) 127 152 { 128 153 DIR *dir; 129 154 int ret; ··· 176 149 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) 177 150 continue; 178 151 152 + if (!match_pat(d->d_name, pat)) 153 + return -2; 154 + 179 155 scnprintf(namebuf, sizeof(namebuf), "%s/%s", 180 156 path, d->d_name); 181 157 ··· 190 160 } 191 161 192 162 if (S_ISDIR(statbuf.st_mode)) 193 - ret = depth ? rm_rf_depth(namebuf, depth - 1) : 0; 163 + ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0; 194 164 else 195 165 ret = unlink(namebuf); 196 166 } ··· 204 174 205 175 int rm_rf(const char *path) 206 176 { 207 - return rm_rf_depth(path, INT_MAX); 177 + return rm_rf_depth_pat(path, INT_MAX, NULL); 208 178 } 209 179 210 180 /* A filter which removes dot files */