1 #include <linux/compiler.h>
2 #include <linux/kernel.h>
11 static bool check_pipe(struct perf_data_file *file)
15 int fd = perf_data_file__is_read(file) ?
16 STDIN_FILENO : STDOUT_FILENO;
19 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
22 if (!strcmp(file->path, "-"))
29 return file->is_pipe = is_pipe;
32 static int check_backup(struct perf_data_file *file)
36 if (!stat(file->path, &st) && st.st_size) {
37 /* TODO check errors properly */
38 char oldname[PATH_MAX];
39 snprintf(oldname, sizeof(oldname), "%s.old",
42 rename(file->path, oldname);
48 static int open_file_read(struct perf_data_file *file)
53 fd = open(file->path, O_RDONLY);
57 pr_err("failed to open %s: %s", file->path, strerror(err));
58 if (err == ENOENT && !strcmp(file->path, "perf.data"))
59 pr_err(" (try 'perf record' first)");
64 if (fstat(fd, &st) < 0)
67 if (!file->force && st.st_uid && (st.st_uid != geteuid())) {
68 pr_err("file %s not owned by current user or root\n",
74 pr_info("zero-sized file (%s), nothing to do!\n",
79 file->size = st.st_size;
87 static int open_file_write(struct perf_data_file *file)
91 if (check_backup(file))
94 fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
97 pr_err("failed to open %s : %s\n", file->path, strerror(errno));
102 static int open_file(struct perf_data_file *file)
106 fd = perf_data_file__is_read(file) ?
107 open_file_read(file) : open_file_write(file);
110 return fd < 0 ? -1 : 0;
113 int perf_data_file__open(struct perf_data_file *file)
115 if (check_pipe(file))
119 file->path = "perf.data";
121 return open_file(file);
124 void perf_data_file__close(struct perf_data_file *file)
129 ssize_t perf_data_file__write(struct perf_data_file *file,
130 void *buf, size_t size)
132 return writen(file->fd, buf, size);