1 #include <linux/compiler.h>
2 #include <linux/kernel.h>
12 static bool check_pipe(struct perf_data_file *file)
16 int fd = perf_data_file__is_read(file) ?
17 STDIN_FILENO : STDOUT_FILENO;
20 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
23 if (!strcmp(file->path, "-"))
30 return file->is_pipe = is_pipe;
33 static int check_backup(struct perf_data_file *file)
37 if (!stat(file->path, &st) && st.st_size) {
38 /* TODO check errors properly */
39 char oldname[PATH_MAX];
40 snprintf(oldname, sizeof(oldname), "%s.old",
43 rename(file->path, oldname);
49 static int open_file_read(struct perf_data_file *file)
54 fd = open(file->path, O_RDONLY);
58 pr_err("failed to open %s: %s", file->path, strerror(err));
59 if (err == ENOENT && !strcmp(file->path, "perf.data"))
60 pr_err(" (try 'perf record' first)");
65 if (fstat(fd, &st) < 0)
68 if (!file->force && st.st_uid && (st.st_uid != geteuid())) {
69 pr_err("File %s not owned by current user or root (use -f to override)\n",
75 pr_info("zero-sized file (%s), nothing to do!\n",
80 file->size = st.st_size;
88 static int open_file_write(struct perf_data_file *file)
92 if (check_backup(file))
95 fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
98 pr_err("failed to open %s : %s\n", file->path, strerror(errno));
103 static int open_file(struct perf_data_file *file)
107 fd = perf_data_file__is_read(file) ?
108 open_file_read(file) : open_file_write(file);
111 return fd < 0 ? -1 : 0;
114 int perf_data_file__open(struct perf_data_file *file)
116 if (check_pipe(file))
120 file->path = "perf.data";
122 return open_file(file);
125 void perf_data_file__close(struct perf_data_file *file)
130 ssize_t perf_data_file__write(struct perf_data_file *file,
131 void *buf, size_t size)
133 return writen(file->fd, buf, size);