Skip to content

Commit b4409ae

Browse files
olsajiriacmel
authored andcommitted
perf tools: Make rm_rf() remove single file
Let rm_rf() remove a file if it's provided by path, not just directories. Signed-off-by: Jiri Olsa <jolsa@kernel.org> 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> Link: http://lkml.kernel.org/r/20190220122800.864-7-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent deb83da commit b4409ae

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tools/perf/util/util.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,26 @@ int mkdir_p(char *path, mode_t mode)
120120
int rm_rf(const char *path)
121121
{
122122
DIR *dir;
123-
int ret = 0;
123+
int ret;
124124
struct dirent *d;
125125
char namebuf[PATH_MAX];
126+
struct stat statbuf;
126127

128+
/* Do not fail if there's no file. */
129+
ret = lstat(path, &statbuf);
130+
if (ret)
131+
return 0;
132+
133+
/* Try to remove any file we get. */
134+
if (!(statbuf.st_mode & S_IFDIR))
135+
return unlink(path);
136+
137+
/* We have directory in path. */
127138
dir = opendir(path);
128139
if (dir == NULL)
129-
return 0;
140+
return -1;
130141

131142
while ((d = readdir(dir)) != NULL && !ret) {
132-
struct stat statbuf;
133143

134144
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
135145
continue;

0 commit comments

Comments
 (0)