[PATCH 04/17] perf data: Fail check_backup in case of error

From: Jiri Olsa
Date: Thu Feb 21 2019 - 04:42:04 EST


And display the error message from removing
the old data file:

$ perf record ls
Can't remove old data: Permission denied (perf.data.old)
Perf session creation failed.

Not sure how to make fail the rename (after we successfully
remove the destination file/dir) to show the message,
anyway let's have it there.

Link: http://lkml.kernel.org/n/tip-6k0tikvlyugms7xxqs794jxz@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/util/data.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index 0a3051cc0ea0..18e5d94b0e97 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -41,12 +41,24 @@ static int check_backup(struct perf_data *data)
return 0;

if (!stat(data->path, &st) && st.st_size) {
- /* TODO check errors properly */
char oldname[PATH_MAX];
+
snprintf(oldname, sizeof(oldname), "%s.old",
data->path);
- rm_rf(oldname);
- rename(data->path, oldname);
+
+ if (rm_rf(oldname)) {
+ pr_err("Can't remove old data: %s (%s)\n",
+ strerror(errno), oldname);
+
+ return -1;
+ }
+
+ if (rename(data->path, oldname)) {
+ pr_err("Can't move data: %s (%s to %s)\n",
+ strerror(errno),
+ data->path, oldname);
+ return -1;
+ }
}

return 0;
--
2.17.2