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