1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
11 #include <sys/types.h>
18 static void close_dir(struct perf_data_file *files, int nr)
27 void perf_data__close_dir(struct perf_data *data)
29 close_dir(data->dir.files, data->dir.nr);
32 int perf_data__create_dir(struct perf_data *data, int nr)
34 struct perf_data_file *files = NULL;
37 if (WARN_ON(!data->is_dir))
40 files = zalloc(nr * sizeof(*files));
44 data->dir.files = files;
47 for (i = 0; i < nr; i++) {
48 struct perf_data_file *file = &files[i];
50 if (asprintf(&file->path, "%s/data.%d", data->path, i) < 0)
53 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
67 int perf_data__open_dir(struct perf_data *data)
69 struct perf_data_file *files = NULL;
75 if (WARN_ON(!data->is_dir))
78 dir = opendir(data->path);
82 while ((dent = readdir(dir)) != NULL) {
83 struct perf_data_file *file;
87 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
91 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data", 4))
96 file = realloc(files, (nr + 1) * sizeof(*files));
103 file->path = strdup(path);
107 ret = open(file->path, O_RDONLY);
112 file->size = st.st_size;
118 data->dir.files = files;
123 close_dir(files, nr);
127 int perf_data__update_dir(struct perf_data *data)
131 if (WARN_ON(!data->is_dir))
134 for (i = 0; i < data->dir.nr; i++) {
135 struct perf_data_file *file = &data->dir.files[i];
138 if (fstat(file->fd, &st))
141 file->size = st.st_size;
147 static bool check_pipe(struct perf_data *data)
150 bool is_pipe = false;
151 int fd = perf_data__is_read(data) ?
152 STDIN_FILENO : STDOUT_FILENO;
155 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
158 if (!strcmp(data->path, "-"))
165 return data->is_pipe = is_pipe;
168 static int check_backup(struct perf_data *data)
172 if (perf_data__is_read(data))
175 if (!stat(data->path, &st) && st.st_size) {
176 char oldname[PATH_MAX];
179 snprintf(oldname, sizeof(oldname), "%s.old",
182 ret = rm_rf_perf_data(oldname);
184 pr_err("Can't remove old data: %s (%s)\n",
186 "Unknown file found" : strerror(errno),
191 if (rename(data->path, oldname)) {
192 pr_err("Can't move data: %s (%s to %s)\n",
194 data->path, oldname);
202 static bool is_dir(struct perf_data *data)
206 if (stat(data->path, &st))
209 return (st.st_mode & S_IFMT) == S_IFDIR;
212 static int open_file_read(struct perf_data *data)
216 char sbuf[STRERR_BUFSIZE];
218 fd = open(data->file.path, O_RDONLY);
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)");
230 if (fstat(fd, &st) < 0)
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",
240 pr_info("zero-sized data (%s), nothing to do!\n",
245 data->file.size = st.st_size;
253 static int open_file_write(struct perf_data *data)
256 char sbuf[STRERR_BUFSIZE];
258 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
262 pr_err("failed to open %s : %s\n", data->file.path,
263 str_error_r(errno, sbuf, sizeof(sbuf)));
268 static int open_file(struct perf_data *data)
272 fd = perf_data__is_read(data) ?
273 open_file_read(data) : open_file_write(data);
276 zfree(&data->file.path);
284 static int open_file_dup(struct perf_data *data)
286 data->file.path = strdup(data->path);
287 if (!data->file.path)
290 return open_file(data);
293 static int open_dir(struct perf_data *data)
298 * So far we open only the header, so we can read the data version and
301 if (asprintf(&data->file.path, "%s/header", data->path) < 0)
304 if (perf_data__is_write(data) &&
305 mkdir(data->path, S_IRWXU) < 0)
308 ret = open_file(data);
310 /* Cleanup whatever we managed to create so far. */
311 if (ret && perf_data__is_write(data))
312 rm_rf_perf_data(data->path);
317 int perf_data__open(struct perf_data *data)
319 if (check_pipe(data))
323 data->path = "perf.data";
325 if (check_backup(data))
328 if (perf_data__is_read(data))
329 data->is_dir = is_dir(data);
331 return perf_data__is_dir(data) ?
332 open_dir(data) : open_file_dup(data);
335 void perf_data__close(struct perf_data *data)
337 if (perf_data__is_dir(data))
338 perf_data__close_dir(data);
340 zfree(&data->file.path);
341 close(data->file.fd);
344 ssize_t perf_data_file__write(struct perf_data_file *file,
345 void *buf, size_t size)
347 return writen(file->fd, buf, size);
350 ssize_t perf_data__write(struct perf_data *data,
351 void *buf, size_t size)
353 return perf_data_file__write(&data->file, buf, size);
356 int perf_data__switch(struct perf_data *data,
358 size_t pos, bool at_exit)
363 if (check_pipe(data))
365 if (perf_data__is_read(data))
368 if (asprintf(&new_filepath, "%s.%s", data->path, postfix) < 0)
372 * Only fire a warning, don't return error, continue fill
375 if (rename(data->path, new_filepath))
376 pr_warning("Failed to rename %s to %s\n", data->path, new_filepath);
379 close(data->file.fd);
380 ret = perf_data__open(data);
384 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
386 pr_debug("Failed to lseek to %zu: %s",
387 pos, strerror(errno));
397 unsigned long perf_data__size(struct perf_data *data)
399 u64 size = data->file.size;
405 for (i = 0; i < data->dir.nr; i++) {
406 struct perf_data_file *file = &data->dir.files[i];