b71c441cafbb21011f5f8fcb14a97328161b0a2e
[linux-2.6-microblaze.git] / tools / perf / util / data.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <asm/bug.h>
11 #include <sys/types.h>
12 #include <dirent.h>
13
14 #include "data.h"
15 #include "util.h"
16 #include "debug.h"
17
18 static void close_dir(struct perf_data_file *files, int nr)
19 {
20         while (--nr >= 1) {
21                 close(files[nr].fd);
22                 free(files[nr].path);
23         }
24         free(files);
25 }
26
27 void perf_data__close_dir(struct perf_data *data)
28 {
29         close_dir(data->dir.files, data->dir.nr);
30 }
31
32 int perf_data__create_dir(struct perf_data *data, int nr)
33 {
34         struct perf_data_file *files = NULL;
35         int i, ret = -1;
36
37         if (WARN_ON(!data->is_dir))
38                 return -EINVAL;
39
40         files = zalloc(nr * sizeof(*files));
41         if (!files)
42                 return -ENOMEM;
43
44         data->dir.files = files;
45         data->dir.nr    = nr;
46
47         for (i = 0; i < nr; i++) {
48                 struct perf_data_file *file = &files[i];
49
50                 if (asprintf(&file->path, "%s/data.%d", data->path, i) < 0)
51                         goto out_err;
52
53                 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
54                 if (ret < 0)
55                         goto out_err;
56
57                 file->fd = ret;
58         }
59
60         return 0;
61
62 out_err:
63         close_dir(files, i);
64         return ret;
65 }
66
67 int perf_data__open_dir(struct perf_data *data)
68 {
69         struct perf_data_file *files = NULL;
70         struct dirent *dent;
71         int ret = -1;
72         DIR *dir;
73         int nr = 0;
74
75         if (WARN_ON(!data->is_dir))
76                 return -EINVAL;
77
78         dir = opendir(data->path);
79         if (!dir)
80                 return -EINVAL;
81
82         while ((dent = readdir(dir)) != NULL) {
83                 struct perf_data_file *file;
84                 char path[PATH_MAX];
85                 struct stat st;
86
87                 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
88                 if (stat(path, &st))
89                         continue;
90
91                 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data", 4))
92                         continue;
93
94                 ret = -ENOMEM;
95
96                 file = realloc(files, (nr + 1) * sizeof(*files));
97                 if (!file)
98                         goto out_err;
99
100                 files = file;
101                 file = &files[nr++];
102
103                 file->path = strdup(path);
104                 if (!file->path)
105                         goto out_err;
106
107                 ret = open(file->path, O_RDONLY);
108                 if (ret < 0)
109                         goto out_err;
110
111                 file->fd = ret;
112                 file->size = st.st_size;
113         }
114
115         if (!files)
116                 return -EINVAL;
117
118         data->dir.files = files;
119         data->dir.nr    = nr;
120         return 0;
121
122 out_err:
123         close_dir(files, nr);
124         return ret;
125 }
126
127 int perf_data__update_dir(struct perf_data *data)
128 {
129         int i;
130
131         if (WARN_ON(!data->is_dir))
132                 return -EINVAL;
133
134         for (i = 0; i < data->dir.nr; i++) {
135                 struct perf_data_file *file = &data->dir.files[i];
136                 struct stat st;
137
138                 if (fstat(file->fd, &st))
139                         return -1;
140
141                 file->size = st.st_size;
142         }
143
144         return 0;
145 }
146
147 static bool check_pipe(struct perf_data *data)
148 {
149         struct stat st;
150         bool is_pipe = false;
151         int fd = perf_data__is_read(data) ?
152                  STDIN_FILENO : STDOUT_FILENO;
153
154         if (!data->path) {
155                 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
156                         is_pipe = true;
157         } else {
158                 if (!strcmp(data->path, "-"))
159                         is_pipe = true;
160         }
161
162         if (is_pipe)
163                 data->file.fd = fd;
164
165         return data->is_pipe = is_pipe;
166 }
167
168 static int check_backup(struct perf_data *data)
169 {
170         struct stat st;
171
172         if (perf_data__is_read(data))
173                 return 0;
174
175         if (!stat(data->path, &st) && st.st_size) {
176                 char oldname[PATH_MAX];
177                 int ret;
178
179                 snprintf(oldname, sizeof(oldname), "%s.old",
180                          data->path);
181
182                 ret = rm_rf_perf_data(oldname);
183                 if (ret) {
184                         pr_err("Can't remove old data: %s (%s)\n",
185                                ret == -2 ?
186                                "Unknown file found" : strerror(errno),
187                                oldname);
188                         return -1;
189                 }
190
191                 if (rename(data->path, oldname)) {
192                         pr_err("Can't move data: %s (%s to %s)\n",
193                                strerror(errno),
194                                data->path, oldname);
195                         return -1;
196                 }
197         }
198
199         return 0;
200 }
201
202 static bool is_dir(struct perf_data *data)
203 {
204         struct stat st;
205
206         if (stat(data->path, &st))
207                 return false;
208
209         return (st.st_mode & S_IFMT) == S_IFDIR;
210 }
211
212 static int open_file_read(struct perf_data *data)
213 {
214         struct stat st;
215         int fd;
216         char sbuf[STRERR_BUFSIZE];
217
218         fd = open(data->file.path, O_RDONLY);
219         if (fd < 0) {
220                 int err = errno;
221
222                 pr_err("failed to open %s: %s", data->file.path,
223                         str_error_r(err, sbuf, sizeof(sbuf)));
224                 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
225                         pr_err("  (try 'perf record' first)");
226                 pr_err("\n");
227                 return -err;
228         }
229
230         if (fstat(fd, &st) < 0)
231                 goto out_close;
232
233         if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
234                 pr_err("File %s not owned by current user or root (use -f to override)\n",
235                        data->file.path);
236                 goto out_close;
237         }
238
239         if (!st.st_size) {
240                 pr_info("zero-sized data (%s), nothing to do!\n",
241                         data->file.path);
242                 goto out_close;
243         }
244
245         data->file.size = st.st_size;
246         return fd;
247
248  out_close:
249         close(fd);
250         return -1;
251 }
252
253 static int open_file_write(struct perf_data *data)
254 {
255         int fd;
256         char sbuf[STRERR_BUFSIZE];
257
258         fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
259                   S_IRUSR|S_IWUSR);
260
261         if (fd < 0)
262                 pr_err("failed to open %s : %s\n", data->file.path,
263                         str_error_r(errno, sbuf, sizeof(sbuf)));
264
265         return fd;
266 }
267
268 static int open_file(struct perf_data *data)
269 {
270         int fd;
271
272         fd = perf_data__is_read(data) ?
273              open_file_read(data) : open_file_write(data);
274
275         if (fd < 0) {
276                 zfree(&data->file.path);
277                 return -1;
278         }
279
280         data->file.fd = fd;
281         return 0;
282 }
283
284 static int open_file_dup(struct perf_data *data)
285 {
286         data->file.path = strdup(data->path);
287         if (!data->file.path)
288                 return -ENOMEM;
289
290         return open_file(data);
291 }
292
293 static int open_dir(struct perf_data *data)
294 {
295         int ret;
296
297         /*
298          * So far we open only the header, so we can read the data version and
299          * layout.
300          */
301         if (asprintf(&data->file.path, "%s/header", data->path) < 0)
302                 return -1;
303
304         if (perf_data__is_write(data) &&
305             mkdir(data->path, S_IRWXU) < 0)
306                 return -1;
307
308         ret = open_file(data);
309
310         /* Cleanup whatever we managed to create so far. */
311         if (ret && perf_data__is_write(data))
312                 rm_rf_perf_data(data->path);
313
314         return ret;
315 }
316
317 int perf_data__open(struct perf_data *data)
318 {
319         if (check_pipe(data))
320                 return 0;
321
322         if (!data->path)
323                 data->path = "perf.data";
324
325         if (check_backup(data))
326                 return -1;
327
328         if (perf_data__is_read(data))
329                 data->is_dir = is_dir(data);
330
331         return perf_data__is_dir(data) ?
332                open_dir(data) : open_file_dup(data);
333 }
334
335 void perf_data__close(struct perf_data *data)
336 {
337         if (perf_data__is_dir(data))
338                 perf_data__close_dir(data);
339
340         zfree(&data->file.path);
341         close(data->file.fd);
342 }
343
344 ssize_t perf_data_file__write(struct perf_data_file *file,
345                               void *buf, size_t size)
346 {
347         return writen(file->fd, buf, size);
348 }
349
350 ssize_t perf_data__write(struct perf_data *data,
351                               void *buf, size_t size)
352 {
353         return perf_data_file__write(&data->file, buf, size);
354 }
355
356 int perf_data__switch(struct perf_data *data,
357                            const char *postfix,
358                            size_t pos, bool at_exit)
359 {
360         char *new_filepath;
361         int ret;
362
363         if (check_pipe(data))
364                 return -EINVAL;
365         if (perf_data__is_read(data))
366                 return -EINVAL;
367
368         if (asprintf(&new_filepath, "%s.%s", data->path, postfix) < 0)
369                 return -ENOMEM;
370
371         /*
372          * Only fire a warning, don't return error, continue fill
373          * original file.
374          */
375         if (rename(data->path, new_filepath))
376                 pr_warning("Failed to rename %s to %s\n", data->path, new_filepath);
377
378         if (!at_exit) {
379                 close(data->file.fd);
380                 ret = perf_data__open(data);
381                 if (ret < 0)
382                         goto out;
383
384                 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
385                         ret = -errno;
386                         pr_debug("Failed to lseek to %zu: %s",
387                                  pos, strerror(errno));
388                         goto out;
389                 }
390         }
391         ret = data->file.fd;
392 out:
393         free(new_filepath);
394         return ret;
395 }
396
397 unsigned long perf_data__size(struct perf_data *data)
398 {
399         u64 size = data->file.size;
400         int i;
401
402         if (!data->is_dir)
403                 return size;
404
405         for (i = 0; i < data->dir.nr; i++) {
406                 struct perf_data_file *file = &data->dir.files[i];
407
408                 size += file->size;
409         }
410
411         return size;
412 }