1 #include <linux/compiler.h>
2 #include <linux/kernel.h>
13 static bool check_pipe(struct perf_data_file *file)
17 int fd = perf_data_file__is_read(file) ?
18 STDIN_FILENO : STDOUT_FILENO;
21 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
24 if (!strcmp(file->path, "-"))
31 return file->is_pipe = is_pipe;
34 static int check_backup(struct perf_data_file *file)
38 if (!stat(file->path, &st) && st.st_size) {
39 /* TODO check errors properly */
40 char oldname[PATH_MAX];
41 snprintf(oldname, sizeof(oldname), "%s.old",
44 rename(file->path, oldname);
50 static int open_file_read(struct perf_data_file *file)
54 char sbuf[STRERR_BUFSIZE];
56 fd = open(file->path, O_RDONLY);
60 pr_err("failed to open %s: %s", file->path,
61 str_error_r(err, sbuf, sizeof(sbuf)));
62 if (err == ENOENT && !strcmp(file->path, "perf.data"))
63 pr_err(" (try 'perf record' first)");
68 if (fstat(fd, &st) < 0)
71 if (!file->force && st.st_uid && (st.st_uid != geteuid())) {
72 pr_err("File %s not owned by current user or root (use -f to override)\n",
78 pr_info("zero-sized file (%s), nothing to do!\n",
83 file->size = st.st_size;
91 static int open_file_write(struct perf_data_file *file)
94 char sbuf[STRERR_BUFSIZE];
96 if (check_backup(file))
99 fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
102 pr_err("failed to open %s : %s\n", file->path,
103 str_error_r(errno, sbuf, sizeof(sbuf)));
108 static int open_file(struct perf_data_file *file)
112 fd = perf_data_file__is_read(file) ?
113 open_file_read(file) : open_file_write(file);
116 return fd < 0 ? -1 : 0;
119 int perf_data_file__open(struct perf_data_file *file)
121 if (check_pipe(file))
125 file->path = "perf.data";
127 return open_file(file);
130 void perf_data_file__close(struct perf_data_file *file)
135 ssize_t perf_data_file__write(struct perf_data_file *file,
136 void *buf, size_t size)
138 return writen(file->fd, buf, size);
141 int perf_data_file__switch(struct perf_data_file *file,
143 size_t pos, bool at_exit)
148 if (check_pipe(file))
150 if (perf_data_file__is_read(file))
153 if (asprintf(&new_filepath, "%s.%s", file->path, postfix) < 0)
157 * Only fire a warning, don't return error, continue fill
160 if (rename(file->path, new_filepath))
161 pr_warning("Failed to rename %s to %s\n", file->path, new_filepath);
165 ret = perf_data_file__open(file);
169 if (lseek(file->fd, pos, SEEK_SET) == (off_t)-1) {
171 pr_debug("Failed to lseek to %zu: %s",
172 pos, strerror(errno));