perf tools: Make rm_rf() remove single file
authorJiri Olsa <jolsa@kernel.org>
Wed, 20 Feb 2019 12:28:00 +0000 (13:28 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Feb 2019 20:09:28 +0000 (17:09 -0300)
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>
tools/perf/util/util.c

index 320b0fef249afe433064c9281178ff71c1884554..3ee410fc047a7ac00e0fe523eca921776e51e207 100644 (file)
@@ -120,16 +120,26 @@ int mkdir_p(char *path, mode_t mode)
 int rm_rf(const char *path)
 {
        DIR *dir;
-       int ret = 0;
+       int ret;
        struct dirent *d;
        char namebuf[PATH_MAX];
+       struct stat statbuf;
 
+       /* Do not fail if there's no file. */
+       ret = lstat(path, &statbuf);
+       if (ret)
+               return 0;
+
+       /* Try to remove any file we get. */
+       if (!(statbuf.st_mode & S_IFDIR))
+               return unlink(path);
+
+       /* We have directory in path. */
        dir = opendir(path);
        if (dir == NULL)
-               return 0;
+               return -1;
 
        while ((d = readdir(dir)) != NULL && !ret) {
-               struct stat statbuf;
 
                if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                        continue;