1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
4 #include <linux/zalloc.h>
12 #include <sys/types.h>
20 static void close_dir(struct perf_data_file *files, int nr)
24 zfree(&files[nr].path);
29 void perf_data__close_dir(struct perf_data *data)
31 close_dir(data->dir.files, data->dir.nr);
34 int perf_data__create_dir(struct perf_data *data, int nr)
36 struct perf_data_file *files = NULL;
39 if (WARN_ON(!data->is_dir))
42 files = zalloc(nr * sizeof(*files));
46 data->dir.version = PERF_DIR_VERSION;
47 data->dir.files = files;
50 for (i = 0; i < nr; i++) {
51 struct perf_data_file *file = &files[i];
53 if (asprintf(&file->path, "%s/data.%d", data->path, i) < 0)
56 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
70 int perf_data__open_dir(struct perf_data *data)
72 struct perf_data_file *files = NULL;
78 if (WARN_ON(!data->is_dir))
81 /* The version is provided by DIR_FORMAT feature. */
82 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
85 dir = opendir(data->path);
89 while ((dent = readdir(dir)) != NULL) {
90 struct perf_data_file *file;
94 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
98 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data", 4))
103 file = realloc(files, (nr + 1) * sizeof(*files));
110 file->path = strdup(path);
114 ret = open(file->path, O_RDONLY);
119 file->size = st.st_size;
125 data->dir.files = files;
130 close_dir(files, nr);
134 int perf_data__update_dir(struct perf_data *data)
138 if (WARN_ON(!data->is_dir))
141 for (i = 0; i < data->dir.nr; i++) {
142 struct perf_data_file *file = &data->dir.files[i];
145 if (fstat(file->fd, &st))
148 file->size = st.st_size;
154 static bool check_pipe(struct perf_data *data)
157 bool is_pipe = false;
158 int fd = perf_data__is_read(data) ?
159 STDIN_FILENO : STDOUT_FILENO;
162 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
165 if (!strcmp(data->path, "-"))
172 return data->is_pipe = is_pipe;
175 static int check_backup(struct perf_data *data)
179 if (perf_data__is_read(data))
182 if (!stat(data->path, &st) && st.st_size) {
183 char oldname[PATH_MAX];
186 snprintf(oldname, sizeof(oldname), "%s.old",
189 ret = rm_rf_perf_data(oldname);
191 pr_err("Can't remove old data: %s (%s)\n",
193 "Unknown file found" : strerror(errno),
198 if (rename(data->path, oldname)) {
199 pr_err("Can't move data: %s (%s to %s)\n",
201 data->path, oldname);
209 static bool is_dir(struct perf_data *data)
213 if (stat(data->path, &st))
216 return (st.st_mode & S_IFMT) == S_IFDIR;
219 static int open_file_read(struct perf_data *data)
223 char sbuf[STRERR_BUFSIZE];
225 fd = open(data->file.path, O_RDONLY);
229 pr_err("failed to open %s: %s", data->file.path,
230 str_error_r(err, sbuf, sizeof(sbuf)));
231 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
232 pr_err(" (try 'perf record' first)");
237 if (fstat(fd, &st) < 0)
240 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
241 pr_err("File %s not owned by current user or root (use -f to override)\n",
247 pr_info("zero-sized data (%s), nothing to do!\n",
252 data->file.size = st.st_size;
260 static int open_file_write(struct perf_data *data)
263 char sbuf[STRERR_BUFSIZE];
265 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
269 pr_err("failed to open %s : %s\n", data->file.path,
270 str_error_r(errno, sbuf, sizeof(sbuf)));
275 static int open_file(struct perf_data *data)
279 fd = perf_data__is_read(data) ?
280 open_file_read(data) : open_file_write(data);
283 zfree(&data->file.path);
291 static int open_file_dup(struct perf_data *data)
293 data->file.path = strdup(data->path);
294 if (!data->file.path)
297 return open_file(data);
300 static int open_dir(struct perf_data *data)
305 * So far we open only the header, so we can read the data version and
308 if (asprintf(&data->file.path, "%s/header", data->path) < 0)
311 if (perf_data__is_write(data) &&
312 mkdir(data->path, S_IRWXU) < 0)
315 ret = open_file(data);
317 /* Cleanup whatever we managed to create so far. */
318 if (ret && perf_data__is_write(data))
319 rm_rf_perf_data(data->path);
324 int perf_data__open(struct perf_data *data)
326 if (check_pipe(data))
330 data->path = "perf.data";
332 if (check_backup(data))
335 if (perf_data__is_read(data))
336 data->is_dir = is_dir(data);
338 return perf_data__is_dir(data) ?
339 open_dir(data) : open_file_dup(data);
342 void perf_data__close(struct perf_data *data)
344 if (perf_data__is_dir(data))
345 perf_data__close_dir(data);
347 zfree(&data->file.path);
348 close(data->file.fd);
351 ssize_t perf_data_file__write(struct perf_data_file *file,
352 void *buf, size_t size)
354 return writen(file->fd, buf, size);
357 ssize_t perf_data__write(struct perf_data *data,
358 void *buf, size_t size)
360 return perf_data_file__write(&data->file, buf, size);
363 int perf_data__switch(struct perf_data *data,
365 size_t pos, bool at_exit,
370 if (check_pipe(data))
372 if (perf_data__is_read(data))
375 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
379 * Only fire a warning, don't return error, continue fill
382 if (rename(data->path, *new_filepath))
383 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
386 close(data->file.fd);
387 ret = perf_data__open(data);
391 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
393 pr_debug("Failed to lseek to %zu: %s",
394 pos, strerror(errno));
403 unsigned long perf_data__size(struct perf_data *data)
405 u64 size = data->file.size;
411 for (i = 0; i < data->dir.nr; i++) {
412 struct perf_data_file *file = &data->dir.files[i];